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