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                            if (Validator.isNotNull(redirect)) {
111                                    actionResponse.sendRedirect(redirect);
112                            }
113                            else {
114                                    sendRedirect(actionRequest, actionResponse);
115                            }
116                    }
117                    catch (Exception e) {
118                            if (e instanceof RestoreEntryException) {
119                                    RestoreEntryException ree = (RestoreEntryException)e;
120    
121                                    SessionErrors.add(actionRequest, ree.getClass(), ree);
122                            }
123                            else if (e instanceof TrashPermissionException) {
124                                    TrashPermissionException tpe = (TrashPermissionException)e;
125    
126                                    SessionErrors.add(actionRequest, tpe.getClass(), tpe);
127                            }
128                            else {
129                                    SessionErrors.add(actionRequest, e.getClass());
130                            }
131    
132                            WindowState windowState = actionRequest.getWindowState();
133    
134                            if (windowState.equals(LiferayWindowState.EXCLUSIVE) ||
135                                    windowState.equals(LiferayWindowState.POP_UP)) {
136    
137                                    sendRedirect(actionRequest, actionResponse);
138                            }
139                    }
140            }
141    
142            @Override
143            public ActionForward render(
144                            ActionMapping actionMapping, ActionForm actionForm,
145                            PortletConfig portletConfig, RenderRequest renderRequest,
146                            RenderResponse renderResponse)
147                    throws Exception {
148    
149                    return actionMapping.findForward(
150                            getForward(renderRequest, "portlet.trash.view"));
151            }
152    
153            protected void addRestoreData(
154                            ActionRequest actionRequest,
155                            List<ObjectValuePair<String, Long>> entryOVPs)
156                    throws Exception {
157    
158                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
159                            WebKeys.THEME_DISPLAY);
160    
161                    if ((entryOVPs == null) || (entryOVPs.size() <= 0)) {
162                            return;
163                    }
164    
165                    List<String> restoreClassNames = new ArrayList<String>();
166                    List<String> restoreEntryLinks = new ArrayList<String>();
167                    List<String> restoreEntryMessages = new ArrayList<String>();
168                    List<String> restoreLinks = new ArrayList<String>();
169                    List<String> restoreMessages = new ArrayList<String>();
170    
171                    for (int i = 0; i < entryOVPs.size(); i++) {
172                            ObjectValuePair<String, Long> entryOVP = entryOVPs.get(i);
173    
174                            TrashHandler trashHandler =
175                                    TrashHandlerRegistryUtil.getTrashHandler(entryOVP.getKey());
176    
177                            String restoreEntryLink = trashHandler.getRestoreContainedModelLink(
178                                    actionRequest, entryOVP.getValue());
179                            String restoreLink = trashHandler.getRestoreContainerModelLink(
180                                    actionRequest, entryOVP.getValue());
181                            String restoreMessage = trashHandler.getRestoreMessage(
182                                    actionRequest, entryOVP.getValue());
183    
184                            if (Validator.isNull(restoreLink) ||
185                                    Validator.isNull(restoreMessage)) {
186    
187                                    continue;
188                            }
189    
190                            restoreClassNames.add(trashHandler.getClassName());
191                            restoreEntryLinks.add(restoreEntryLink);
192    
193                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
194                                    entryOVP.getValue());
195    
196                            String restoreEntryTitle = trashRenderer.getTitle(
197                                    themeDisplay.getLocale());
198    
199                            restoreEntryMessages.add(restoreEntryTitle);
200    
201                            restoreLinks.add(restoreLink);
202                            restoreMessages.add(restoreMessage);
203                    }
204    
205                    Map<String, List<String>> data = new HashMap<String, List<String>>();
206    
207                    data.put("restoreClassNames", restoreClassNames);
208                    data.put("restoreEntryLinks", restoreEntryLinks);
209                    data.put("restoreEntryMessages", restoreEntryMessages);
210                    data.put("restoreLinks", restoreLinks);
211                    data.put("restoreMessages", restoreMessages);
212    
213                    SessionMessages.add(
214                            actionRequest,
215                            PortalUtil.getPortletId(actionRequest) +
216                                    SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
217    
218                    hideDefaultSuccessMessage(actionRequest);
219            }
220    
221            protected void deleteEntries(ActionRequest actionRequest) throws Exception {
222                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
223    
224                    if (trashEntryId > 0) {
225                            TrashEntryServiceUtil.deleteEntry(trashEntryId);
226    
227                            return;
228                    }
229    
230                    long[] deleteEntryIds = StringUtil.split(
231                            ParamUtil.getString(actionRequest, "deleteThrashEntryIds"), 0L);
232    
233                    if (deleteEntryIds.length > 0) {
234                            for (int i = 0; i < deleteEntryIds.length; i++) {
235                                    TrashEntryServiceUtil.deleteEntry(deleteEntryIds[i]);
236                            }
237    
238                            return;
239                    }
240    
241                    String className = ParamUtil.getString(actionRequest, "className");
242                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
243    
244                    if (Validator.isNotNull(className) && (classPK > 0)) {
245                            TrashEntryServiceUtil.deleteEntry(className, classPK);
246                    }
247            }
248    
249            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
250                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
251                            WebKeys.THEME_DISPLAY);
252    
253                    long groupId = ParamUtil.getLong(
254                            actionRequest, "groupId", themeDisplay.getScopeGroupId());
255    
256                    TrashEntryServiceUtil.deleteEntries(groupId);
257            }
258    
259            protected List<ObjectValuePair<String, Long>> getEntryOVPs(
260                    String className, long classPK) {
261    
262                    List<ObjectValuePair<String, Long>> entryOVPs =
263                            new ArrayList<ObjectValuePair<String, Long>>();
264    
265                    ObjectValuePair<String, Long> entryOVP =
266                            new ObjectValuePair<String, Long>(className, classPK);
267    
268                    entryOVPs.add(entryOVP);
269    
270                    return entryOVPs;
271            }
272    
273            protected List<ObjectValuePair<String, Long>> moveEntry(
274                            ActionRequest actionRequest)
275                    throws Exception {
276    
277                    long containerModelId = ParamUtil.getLong(
278                            actionRequest, "containerModelId");
279                    String className = ParamUtil.getString(actionRequest, "className");
280                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
281    
282                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
283                            className, actionRequest);
284    
285                    TrashEntryServiceUtil.moveEntry(
286                            className, classPK, containerModelId, serviceContext);
287    
288                    return getEntryOVPs(className, classPK);
289            }
290    
291            protected List<ObjectValuePair<String, Long>> restoreEntries(
292                            ActionRequest actionRequest)
293                    throws Exception {
294    
295                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
296    
297                    if (trashEntryId > 0) {
298                            TrashEntry entry = TrashEntryServiceUtil.restoreEntry(trashEntryId);
299    
300                            return getEntryOVPs(entry.getClassName(), entry.getClassPK());
301                    }
302    
303                    long[] restoreEntryIds = StringUtil.split(
304                            ParamUtil.getString(actionRequest, "restoreTrashEntryIds"), 0L);
305    
306                    List<ObjectValuePair<String, Long>> entryOVPs =
307                            new ArrayList<ObjectValuePair<String, Long>>();
308    
309                    for (long restoreEntryId : restoreEntryIds) {
310                            TrashEntry entry = TrashEntryServiceUtil.restoreEntry(
311                                    restoreEntryId);
312    
313                            entryOVPs.addAll(
314                                    getEntryOVPs(entry.getClassName(), entry.getClassPK()));
315                    }
316    
317                    return entryOVPs;
318            }
319    
320            protected List<ObjectValuePair<String, Long>> restoreOverride(
321                            ActionRequest actionRequest)
322                    throws Exception {
323    
324                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
325    
326                    long duplicateEntryId = ParamUtil.getLong(
327                            actionRequest, "duplicateEntryId");
328    
329                    TrashEntry entry = TrashEntryServiceUtil.restoreEntry(
330                            trashEntryId, duplicateEntryId, null);
331    
332                    return getEntryOVPs(entry.getClassName(), entry.getClassPK());
333            }
334    
335            protected List<ObjectValuePair<String, Long>> restoreRename(
336                            ActionRequest actionRequest)
337                    throws Exception {
338    
339                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
340                            WebKeys.THEME_DISPLAY);
341    
342                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
343    
344                    String newName = ParamUtil.getString(actionRequest, "newName");
345    
346                    if (Validator.isNull(newName)) {
347                            String oldName = ParamUtil.getString(actionRequest, "oldName");
348    
349                            newName = TrashUtil.getNewName(themeDisplay, null, 0, oldName);
350                    }
351    
352                    TrashEntry entry = TrashEntryServiceUtil.restoreEntry(
353                            trashEntryId, 0, newName);
354    
355                    return getEntryOVPs(entry.getClassName(), entry.getClassPK());
356            }
357    
358    }