001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.trash.action;
016    
017    import com.liferay.portal.TrashPermissionException;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
020    import com.liferay.portal.kernel.portlet.LiferayWindowState;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.servlet.SessionMessages;
023    import com.liferay.portal.kernel.trash.TrashHandler;
024    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
025    import com.liferay.portal.kernel.trash.TrashRenderer;
026    import com.liferay.portal.kernel.util.Constants;
027    import com.liferay.portal.kernel.util.ObjectValuePair;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.service.ServiceContextFactory;
033    import com.liferay.portal.struts.PortletAction;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.trash.model.TrashEntry;
037    import com.liferay.portlet.trash.service.TrashEntryServiceUtil;
038    import com.liferay.portlet.trash.util.TrashUtil;
039    
040    import java.util.ArrayList;
041    import java.util.HashMap;
042    import java.util.List;
043    import java.util.Map;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.RenderRequest;
049    import javax.portlet.RenderResponse;
050    import javax.portlet.WindowState;
051    
052    import org.apache.struts.action.ActionForm;
053    import org.apache.struts.action.ActionForward;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Manuel de la Pe??a
058     * @author Zsolt Berentey
059     */
060    public class EditEntryAction extends PortletAction {
061    
062            @Override
063            public void processAction(
064                            ActionMapping actionMapping, ActionForm actionForm,
065                            PortletConfig portletConfig, ActionRequest actionRequest,
066                            ActionResponse actionResponse)
067                    throws Exception {
068    
069                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070    
071                    try {
072                            List<ObjectValuePair<String, Long>> entryOVPs = null;
073    
074                            if (cmd.equals(Constants.CHECK)) {
075                                    JSONObject jsonObject = ActionUtil.checkEntry(actionRequest);
076    
077                                    writeJSON(actionRequest, actionResponse, jsonObject);
078    
079                                    return;
080                            }
081                            else if (cmd.equals(Constants.DELETE)) {
082                                    deleteEntries(actionRequest);
083                            }
084                            else if (cmd.equals(Constants.EMPTY_TRASH)) {
085                                    emptyTrash(actionRequest);
086                            }
087                            else if (cmd.equals(Constants.MOVE)) {
088                                    entryOVPs = moveEntry(actionRequest);
089                            }
090                            else if (cmd.equals(Constants.RENAME)) {
091                                    entryOVPs = restoreRename(actionRequest);
092                            }
093                            else if (cmd.equals(Constants.RESTORE)) {
094                                    entryOVPs = restoreEntries(actionRequest);
095                            }
096                            else if (cmd.equals(Constants.OVERRIDE)) {
097                                    entryOVPs = restoreOverride(actionRequest);
098                            }
099    
100                            if (cmd.equals(Constants.RENAME) || cmd.equals(Constants.RESTORE) ||
101                                    cmd.equals(Constants.OVERRIDE) || cmd.equals(Constants.MOVE)) {
102    
103                                    addRestoreData(
104                                            (LiferayPortletConfig)portletConfig, actionRequest,
105                                            entryOVPs);
106                            }
107    
108                            sendRedirect(actionRequest, actionResponse);
109                    }
110                    catch (Exception e) {
111                            if (e instanceof TrashPermissionException) {
112                                    TrashPermissionException tpe = (TrashPermissionException)e;
113    
114                                    SessionErrors.add(actionRequest, tpe.getClass(), tpe);
115                            }
116                            else {
117                                    SessionErrors.add(actionRequest, e.getClass());
118                            }
119    
120                            WindowState windowState = actionRequest.getWindowState();
121    
122                            if (windowState.equals(LiferayWindowState.EXCLUSIVE) ||
123                                    windowState.equals(LiferayWindowState.POP_UP)) {
124    
125                                    sendRedirect(actionRequest, actionResponse);
126                            }
127                    }
128            }
129    
130            @Override
131            public ActionForward render(
132                            ActionMapping actionMapping, ActionForm actionForm,
133                            PortletConfig portletConfig, RenderRequest renderRequest,
134                            RenderResponse renderResponse)
135                    throws Exception {
136    
137                    return actionMapping.findForward(
138                            getForward(renderRequest, "portlet.trash.view"));
139            }
140    
141            protected void addRestoreData(
142                            LiferayPortletConfig liferayPortletConfig,
143                            ActionRequest actionRequest,
144                            List<ObjectValuePair<String, Long>> entryOVPs)
145                    throws Exception {
146    
147                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148                            WebKeys.THEME_DISPLAY);
149    
150                    if ((entryOVPs == null) || (entryOVPs.size() <= 0)) {
151                            return;
152                    }
153    
154                    List<String> restoreClassNames = new ArrayList<String>();
155                    List<String> restoreEntryLinks = new ArrayList<String>();
156                    List<String> restoreEntryMessages = new ArrayList<String>();
157                    List<String> restoreLinks = new ArrayList<String>();
158                    List<String> restoreMessages = new ArrayList<String>();
159    
160                    for (int i = 0; i < entryOVPs.size(); i++) {
161                            ObjectValuePair<String, Long> entryOVP = entryOVPs.get(i);
162    
163                            TrashHandler trashHandler =
164                                    TrashHandlerRegistryUtil.getTrashHandler(entryOVP.getKey());
165    
166                            String restoreEntryLink = trashHandler.getRestoreContainedModelLink(
167                                    actionRequest, entryOVP.getValue());
168                            String restoreLink = trashHandler.getRestoreContainerModelLink(
169                                    actionRequest, entryOVP.getValue());
170                            String restoreMessage = trashHandler.getRestoreMessage(
171                                    actionRequest, entryOVP.getValue());
172    
173                            if (Validator.isNull(restoreLink) ||
174                                    Validator.isNull(restoreMessage)) {
175    
176                                    continue;
177                            }
178    
179                            restoreClassNames.add(trashHandler.getClassName());
180                            restoreEntryLinks.add(restoreEntryLink);
181    
182                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
183                                    entryOVP.getValue());
184    
185                            String restoreEntryTitle = trashRenderer.getTitle(
186                                    themeDisplay.getLocale());
187    
188                            restoreEntryMessages.add(restoreEntryTitle);
189    
190                            restoreLinks.add(restoreLink);
191                            restoreMessages.add(restoreMessage);
192                    }
193    
194                    Map<String, List<String>> data = new HashMap<String, List<String>>();
195    
196                    data.put("restoreClassNames", restoreClassNames);
197                    data.put("restoreEntryLinks", restoreEntryLinks);
198                    data.put("restoreEntryMessages", restoreEntryMessages);
199                    data.put("restoreLinks", restoreLinks);
200                    data.put("restoreMessages", restoreMessages);
201    
202                    SessionMessages.add(
203                            actionRequest,
204                            liferayPortletConfig.getPortletId() +
205                                    SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
206    
207                    hideDefaultSuccessMessage(liferayPortletConfig, actionRequest);
208            }
209    
210            protected void deleteEntries(ActionRequest actionRequest) throws Exception {
211                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
212    
213                    if (trashEntryId > 0) {
214                            TrashEntryServiceUtil.deleteEntry(trashEntryId);
215    
216                            return;
217                    }
218    
219                    long[] deleteEntryIds = StringUtil.split(
220                            ParamUtil.getString(actionRequest, "deleteThrashEntryIds"), 0L);
221    
222                    if (deleteEntryIds.length > 0) {
223                            for (int i = 0; i < deleteEntryIds.length; i++) {
224                                    TrashEntryServiceUtil.deleteEntry(deleteEntryIds[i]);
225                            }
226    
227                            return;
228                    }
229    
230                    String className = ParamUtil.getString(actionRequest, "className");
231                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
232    
233                    if (Validator.isNotNull(className) && (classPK > 0)) {
234                            TrashEntryServiceUtil.deleteEntry(className, classPK);
235                    }
236            }
237    
238            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
239                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
240                            WebKeys.THEME_DISPLAY);
241    
242                    long groupId = ParamUtil.getLong(
243                            actionRequest, "groupId", themeDisplay.getScopeGroupId());
244    
245                    TrashEntryServiceUtil.deleteEntries(groupId);
246            }
247    
248            protected List<ObjectValuePair<String, Long>> getEntryOVPs(
249                    String className, long classPK) {
250    
251                    List<ObjectValuePair<String, Long>> entryOVPs =
252                            new ArrayList<ObjectValuePair<String, Long>>();
253    
254                    ObjectValuePair<String, Long> entryOVP =
255                            new ObjectValuePair<String, Long>(className, classPK);
256    
257                    entryOVPs.add(entryOVP);
258    
259                    return entryOVPs;
260            }
261    
262            protected List<ObjectValuePair<String, Long>> moveEntry(
263                            ActionRequest actionRequest)
264                    throws Exception {
265    
266                    long containerModelId = ParamUtil.getLong(
267                            actionRequest, "containerModelId");
268                    String className = ParamUtil.getString(actionRequest, "className");
269                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
270    
271                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
272                            className, actionRequest);
273    
274                    TrashEntryServiceUtil.moveEntry(
275                            className, classPK, containerModelId, serviceContext);
276    
277                    return getEntryOVPs(className, classPK);
278            }
279    
280            protected List<ObjectValuePair<String, Long>> restoreEntries(
281                            ActionRequest actionRequest)
282                    throws Exception {
283    
284                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
285    
286                    if (trashEntryId > 0) {
287                            TrashEntry entry = TrashEntryServiceUtil.restoreEntry(trashEntryId);
288    
289                            return getEntryOVPs(entry.getClassName(), entry.getClassPK());
290                    }
291    
292                    long[] restoreEntryIds = StringUtil.split(
293                            ParamUtil.getString(actionRequest, "restoreTrashEntryIds"), 0L);
294    
295                    List<ObjectValuePair<String, Long>> entryOVPs =
296                            new ArrayList<ObjectValuePair<String, Long>>();
297    
298                    for (int i = 0; i < restoreEntryIds.length; i++) {
299                            TrashEntry entry = TrashEntryServiceUtil.restoreEntry(trashEntryId);
300    
301                            entryOVPs.addAll(
302                                    getEntryOVPs(entry.getClassName(), entry.getClassPK()));
303                    }
304    
305                    return entryOVPs;
306            }
307    
308            protected List<ObjectValuePair<String, Long>> restoreOverride(
309                            ActionRequest actionRequest)
310                    throws Exception {
311    
312                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
313    
314                    long duplicateEntryId = ParamUtil.getLong(
315                            actionRequest, "duplicateEntryId");
316    
317                    TrashEntry entry = TrashEntryServiceUtil.restoreEntry(
318                            trashEntryId, duplicateEntryId, null);
319    
320                    return getEntryOVPs(entry.getClassName(), entry.getClassPK());
321            }
322    
323            protected List<ObjectValuePair<String, Long>> restoreRename(
324                            ActionRequest actionRequest)
325                    throws Exception {
326    
327                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
328                            WebKeys.THEME_DISPLAY);
329    
330                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
331    
332                    String newName = ParamUtil.getString(actionRequest, "newName");
333    
334                    if (Validator.isNull(newName)) {
335                            String oldName = ParamUtil.getString(actionRequest, "oldName");
336    
337                            newName = TrashUtil.getNewName(themeDisplay, null, 0, oldName);
338                    }
339    
340                    TrashEntry entry = TrashEntryServiceUtil.restoreEntry(
341                            trashEntryId, 0, newName);
342    
343                    return getEntryOVPs(entry.getClassName(), entry.getClassPK());
344            }
345    
346    }