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