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