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.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.repository.model.Folder;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextFactory;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
033    import com.liferay.portlet.documentlibrary.DuplicateFileException;
034    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
035    import com.liferay.portlet.documentlibrary.FolderNameException;
036    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
037    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
038    import com.liferay.portlet.documentlibrary.model.DLFolder;
039    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
040    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
041    import com.liferay.portlet.trash.util.TrashUtil;
042    
043    import java.util.HashMap;
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    
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 Brian Wing Shun Chan
058     * @author Alexander Chow
059     * @author Sergio Gonz??lez
060     * @author Levente Hud??k
061     */
062    public class EditFolderAction extends PortletAction {
063    
064            @Override
065            public void processAction(
066                            ActionMapping actionMapping, ActionForm actionForm,
067                            PortletConfig portletConfig, ActionRequest actionRequest,
068                            ActionResponse actionResponse)
069                    throws Exception {
070    
071                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
072    
073                    try {
074                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
075                                    updateFolder(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.DELETE)) {
078                                    deleteFolders(actionRequest, false);
079                            }
080                            else if (cmd.equals(Constants.MOVE)) {
081                                    moveFolders(actionRequest, false);
082                            }
083                            else if (cmd.equals(Constants.MOVE_FROM_TRASH)) {
084                                    moveFolders(actionRequest, true);
085                            }
086                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
087                                    deleteFolders(actionRequest, true);
088                            }
089                            else if (cmd.equals(Constants.SUBSCRIBE)) {
090                                    subscribeFolder(actionRequest);
091                            }
092                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
093                                    unsubscribeFolder(actionRequest);
094                            }
095                            else if (cmd.equals("updateWorkflowDefinitions")) {
096                                    updateWorkflowDefinitions(actionRequest);
097                            }
098    
099                            sendRedirect(actionRequest, actionResponse);
100                    }
101                    catch (Exception e) {
102                            if (e instanceof NoSuchFolderException ||
103                                    e instanceof PrincipalException) {
104    
105                                    SessionErrors.add(actionRequest, e.getClass());
106    
107                                    setForward(actionRequest, "portlet.document_library.error");
108                            }
109                            else if (e instanceof DuplicateFileException ||
110                                             e instanceof DuplicateFolderNameException ||
111                                             e instanceof FolderNameException) {
112    
113                                    SessionErrors.add(actionRequest, e.getClass());
114                            }
115                            else {
116                                    throw e;
117                            }
118                    }
119            }
120    
121            @Override
122            public ActionForward render(
123                            ActionMapping actionMapping, ActionForm actionForm,
124                            PortletConfig portletConfig, RenderRequest renderRequest,
125                            RenderResponse renderResponse)
126                    throws Exception {
127    
128                    try {
129                            ActionUtil.getFolder(renderRequest);
130                    }
131                    catch (Exception e) {
132                            if (e instanceof NoSuchFolderException ||
133                                    e instanceof PrincipalException) {
134    
135                                    SessionErrors.add(renderRequest, e.getClass());
136    
137                                    return actionMapping.findForward(
138                                            "portlet.document_library.error");
139                            }
140                            else {
141                                    throw e;
142                            }
143                    }
144    
145                    return actionMapping.findForward(
146                            getForward(renderRequest, "portlet.document_library.edit_folder"));
147            }
148    
149            protected void deleteFolders(
150                            ActionRequest actionRequest, boolean moveToTrash)
151                    throws Exception {
152    
153                    String deleteEntryTitle = null;
154    
155                    long[] deleteFolderIds = null;
156    
157                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
158    
159                    if (folderId > 0) {
160                            deleteFolderIds = new long[] {folderId};
161                    }
162                    else {
163                            deleteFolderIds = StringUtil.split(
164                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
165                    }
166    
167                    for (int i = 0; i < deleteFolderIds.length; i++) {
168                            long deleteFolderId = deleteFolderIds[i];
169    
170                            if (moveToTrash) {
171                                    Folder folder = DLAppServiceUtil.moveFolderToTrash(
172                                            deleteFolderId);
173    
174                                    if (i == 0) {
175                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
176                                                    folder.getName());
177                                    }
178                            }
179                            else {
180                                    DLAppServiceUtil.deleteFolder(deleteFolderId);
181                            }
182    
183                            AssetPublisherUtil.removeRecentFolderId(
184                                    actionRequest, DLFileEntry.class.getName(), deleteFolderId);
185                    }
186    
187                    if (moveToTrash && (deleteFolderIds.length > 0)) {
188                            Map<String, String[]> data = new HashMap<String, String[]>();
189    
190                            data.put(
191                                    "deleteEntryClassName",
192                                    new String[] {DLFolder.class.getName()});
193    
194                            if (Validator.isNotNull(deleteEntryTitle)) {
195                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
196                            }
197    
198                            data.put(
199                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
200    
201                            SessionMessages.add(
202                                    actionRequest,
203                                    PortalUtil.getPortletId(actionRequest) +
204                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
205    
206                            hideDefaultSuccessMessage(actionRequest);
207                    }
208            }
209    
210            protected void moveFolders(
211                            ActionRequest actionRequest, boolean moveFromTrash)
212                    throws Exception {
213    
214                    long[] folderIds = null;
215    
216                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
217    
218                    if (folderId > 0) {
219                            folderIds = new long[] {folderId};
220                    }
221                    else {
222                            folderIds = StringUtil.split(
223                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
224                    }
225    
226                    long parentFolderId = ParamUtil.getLong(
227                            actionRequest, "parentFolderId");
228    
229                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
230                            DLFileEntry.class.getName(), actionRequest);
231    
232                    for (long moveFolderId : folderIds) {
233                            if (moveFromTrash) {
234                                    DLAppServiceUtil.moveFolderFromTrash(
235                                            moveFolderId, parentFolderId, serviceContext);
236                            }
237                            else {
238                                    DLAppServiceUtil.moveFolder(
239                                            moveFolderId, parentFolderId, serviceContext);
240                            }
241                    }
242            }
243    
244            protected void subscribeFolder(ActionRequest actionRequest)
245                    throws Exception {
246    
247                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
248                            WebKeys.THEME_DISPLAY);
249    
250                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
251    
252                    DLAppServiceUtil.subscribeFolder(
253                            themeDisplay.getScopeGroupId(), folderId);
254            }
255    
256            protected void unsubscribeFolder(ActionRequest actionRequest)
257                    throws Exception {
258    
259                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
260                            WebKeys.THEME_DISPLAY);
261    
262                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
263    
264                    DLAppServiceUtil.unsubscribeFolder(
265                            themeDisplay.getScopeGroupId(), folderId);
266            }
267    
268            protected void updateFolder(ActionRequest actionRequest) throws Exception {
269                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
270    
271                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
272                    long parentFolderId = ParamUtil.getLong(
273                            actionRequest, "parentFolderId");
274                    String name = ParamUtil.getString(actionRequest, "name");
275                    String description = ParamUtil.getString(actionRequest, "description");
276    
277                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
278                            DLFolder.class.getName(), actionRequest);
279    
280                    if (folderId <= 0) {
281    
282                            // Add folder
283    
284                            DLAppServiceUtil.addFolder(
285                                    repositoryId, parentFolderId, name, description,
286                                    serviceContext);
287                    }
288                    else {
289    
290                            // Update folder
291    
292                            DLAppServiceUtil.updateFolder(
293                                    folderId, name, description, serviceContext);
294                    }
295            }
296    
297            protected void updateWorkflowDefinitions(ActionRequest actionRequest)
298                    throws Exception {
299    
300                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
301                            DLFileEntry.class.getName(), actionRequest);
302    
303                    DLAppServiceUtil.updateFolder(
304                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, null, null,
305                            serviceContext);
306            }
307    
308    }