001    /**
002     * Copyright (c) 2000-2012 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.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.trash.TrashHandler;
022    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.trash.TrashEntryConstants;
032    import com.liferay.portlet.trash.model.TrashEntry;
033    import com.liferay.portlet.trash.service.TrashEntryLocalServiceUtil;
034    import com.liferay.portlet.trash.service.TrashEntryServiceUtil;
035    import com.liferay.portlet.trash.util.TrashUtil;
036    
037    import java.util.ArrayList;
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Manuel de la Peña
054     * @author Zsolt Berentey
055     */
056    public class EditEntryAction extends PortletAction {
057    
058            @Override
059            public void processAction(
060                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
061                            ActionRequest actionRequest, ActionResponse actionResponse)
062                    throws Exception {
063    
064                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
065    
066                    try {
067                            TrashEntry[] entries = null;
068    
069                            if (cmd.equals(Constants.CHECK)) {
070                                    JSONObject jsonObject = ActionUtil.checkEntry(actionRequest);
071    
072                                    writeJSON(actionRequest, actionResponse, jsonObject);
073    
074                                    return;
075                            }
076                            else if (cmd.equals(Constants.DELETE)) {
077                                    deleteEntries(actionRequest);
078                            }
079                            else if (cmd.equals(Constants.EMPTY_TRASH)) {
080                                    emptyTrash(actionRequest);
081                            }
082                            else if (cmd.equals(Constants.RENAME)) {
083                                    entries = restoreRename(actionRequest);
084                            }
085                            else if (cmd.equals(Constants.RESTORE)) {
086                                    entries = restoreEntries(actionRequest);
087                            }
088                            else if (cmd.equals(Constants.OVERRIDE)) {
089                                    entries = restoreOverride(actionRequest);
090                            }
091    
092                            if (cmd.equals(Constants.RENAME) || cmd.equals(Constants.RESTORE) ||
093                                    cmd.equals(Constants.OVERRIDE)) {
094    
095                                    addRestoreData(
096                                            (LiferayPortletConfig)portletConfig, actionRequest,
097                                            entries);
098                            }
099    
100                            sendRedirect(actionRequest, actionResponse);
101                    }
102                    catch (Exception e) {
103                            SessionErrors.add(actionRequest, e.getClass());
104                    }
105            }
106    
107            @Override
108            public ActionForward render(
109                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
110                            RenderRequest renderRequest, RenderResponse renderResponse)
111                    throws Exception {
112    
113                    return mapping.findForward(
114                            getForward(renderRequest, "portlet.trash.view"));
115            }
116    
117            protected void addRestoreData(
118                            LiferayPortletConfig liferayPortletConfig,
119                            ActionRequest actionRequest, TrashEntry[] entries)
120                    throws Exception {
121    
122                    if ((entries == null) || (entries.length <= 0)) {
123                            return;
124                    }
125    
126                    List<String> restoreLinks = new ArrayList<String>();
127                    List<String> restoreMessages = new ArrayList<String>();
128    
129                    for (TrashEntry entry : entries) {
130                            TrashHandler trashHandler =
131                                    TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());
132    
133                            String restoreLink = trashHandler.getRestoreLink(
134                                    actionRequest, entry.getClassPK());
135    
136                            String restoreMessage = trashHandler.getRestoreMessage(
137                                    actionRequest, entry.getClassPK());
138    
139                            if (Validator.isNull(restoreLink) ||
140                                    Validator.isNull(restoreMessage)) {
141    
142                                    continue;
143                            }
144    
145                            restoreLinks.add(restoreLink);
146                            restoreMessages.add(restoreMessage);
147                    }
148    
149                    Map<String, List<String>> data = new HashMap<String, List<String>>();
150    
151                    data.put("restoreLinks", restoreLinks);
152                    data.put("restoreMessages", restoreMessages);
153    
154                    SessionMessages.add(
155                            actionRequest,
156                            liferayPortletConfig.getPortletId() +
157                                    SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
158    
159                    SessionMessages.add(
160                            actionRequest,
161                            liferayPortletConfig.getPortletId() +
162                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
163            }
164    
165            protected void deleteEntries(ActionRequest actionRequest) throws Exception {
166                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
167    
168                    if (entryId > 0) {
169                            deleteEntry(entryId);
170                    }
171                    else {
172                            long[] deleteEntryIds = StringUtil.split(
173                                    ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
174    
175                            for (int i = 0; i < deleteEntryIds.length; i++) {
176                                    deleteEntry(deleteEntryIds[i]);
177                            }
178                    }
179            }
180    
181            protected void deleteEntry(long entryId) throws Exception {
182                    TrashEntry entry = TrashEntryLocalServiceUtil.getTrashEntry(entryId);
183    
184                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
185                            entry.getClassName());
186    
187                    trashHandler.deleteTrashEntry(entry.getClassPK());
188            }
189    
190            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
191                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
192                            WebKeys.THEME_DISPLAY);
193    
194                    TrashEntryServiceUtil.deleteEntries(themeDisplay.getScopeGroupId());
195            }
196    
197            protected TrashEntry[] restoreEntries(ActionRequest actionRequest)
198                    throws Exception {
199    
200                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
201    
202                    TrashEntry[] entry = null;
203    
204                    if (entryId > 0) {
205                            entry = restoreEntry(entryId);
206                    }
207                    else {
208                            long[] restoreEntryIds = StringUtil.split(
209                                    ParamUtil.getString(actionRequest, "restoreEntryIds"), 0L);
210    
211                            entry = new TrashEntry[restoreEntryIds.length];
212    
213                            for (int i = 0; i < restoreEntryIds.length; i++) {
214                                    entry[i] = restoreEntry(restoreEntryIds[i])[0];
215                            }
216                    }
217    
218                    return entry;
219            }
220    
221            protected TrashEntry[] restoreEntry(long entryId) throws Exception {
222                    TrashEntry entry = TrashEntryLocalServiceUtil.getTrashEntry(entryId);
223    
224                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
225                            entry.getClassName());
226    
227                    trashHandler.checkDuplicateTrashEntry(
228                            entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, StringPool.BLANK);
229    
230                    trashHandler.restoreTrashEntry(entry.getClassPK());
231    
232                    return new TrashEntry[] {entry};
233            }
234    
235            protected TrashEntry[] restoreOverride(ActionRequest actionRequest)
236                    throws Exception {
237    
238                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
239    
240                    TrashEntry entry = TrashEntryLocalServiceUtil.getTrashEntry(
241                            trashEntryId);
242    
243                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
244                            entry.getClassName());
245    
246                    long duplicateEntryId = ParamUtil.getLong(
247                            actionRequest, "duplicateEntryId");
248    
249                    trashHandler.deleteTrashEntries(new long[] {duplicateEntryId});
250    
251                    trashHandler.restoreTrashEntry(entry.getClassPK());
252    
253                    return new TrashEntry[] {entry};
254            }
255    
256            protected TrashEntry[] restoreRename(ActionRequest actionRequest)
257                    throws Exception {
258    
259                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
260                            WebKeys.THEME_DISPLAY);
261    
262                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
263    
264                    TrashEntry entry = TrashEntryLocalServiceUtil.getTrashEntry(
265                            trashEntryId);
266    
267                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
268                            entry.getClassName());
269    
270                    String newName = ParamUtil.getString(actionRequest, "newName");
271    
272                    if (Validator.isNull(newName)) {
273                            String oldName = ParamUtil.getString(actionRequest, "oldName");
274    
275                            newName = TrashUtil.getNewName(themeDisplay, oldName);
276                    }
277    
278                    trashHandler.updateTitle(entry.getClassPK(), newName);
279    
280                    trashHandler.restoreTrashEntry(entry.getClassPK());
281    
282                    return new TrashEntry[] {entry};
283            }
284    
285    }