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.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.repository.model.Folder;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
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(
079                                            (LiferayPortletConfig)portletConfig, 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(
089                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
090                            }
091                            else if (cmd.equals(Constants.SUBSCRIBE)) {
092                                    subscribeFolder(actionRequest);
093                            }
094                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
095                                    unsubscribeFolder(actionRequest);
096                            }
097                            else if (cmd.equals("updateWorkflowDefinitions")) {
098                                    updateWorkflowDefinitions(actionRequest);
099                            }
100    
101                            sendRedirect(actionRequest, actionResponse);
102                    }
103                    catch (Exception e) {
104                            if (e instanceof NoSuchFolderException ||
105                                    e instanceof PrincipalException) {
106    
107                                    SessionErrors.add(actionRequest, e.getClass());
108    
109                                    setForward(actionRequest, "portlet.document_library.error");
110                            }
111                            else if (e instanceof DuplicateFileException ||
112                                             e instanceof DuplicateFolderNameException ||
113                                             e instanceof FolderNameException) {
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                            LiferayPortletConfig liferayPortletConfig,
153                            ActionRequest actionRequest, boolean moveToTrash)
154                    throws Exception {
155    
156                    String deleteEntryTitle = null;
157    
158                    long[] deleteFolderIds = null;
159    
160                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
161    
162                    if (folderId > 0) {
163                            deleteFolderIds = new long[] {folderId};
164                    }
165                    else {
166                            deleteFolderIds = StringUtil.split(
167                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
168                    }
169    
170                    for (int i = 0; i < deleteFolderIds.length; i++) {
171                            long deleteFolderId = deleteFolderIds[i];
172    
173                            if (moveToTrash) {
174                                    Folder folder = DLAppServiceUtil.moveFolderToTrash(
175                                            deleteFolderId);
176    
177                                    if (i == 0) {
178                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
179                                                    folder.getName());
180                                    }
181                            }
182                            else {
183                                    DLAppServiceUtil.deleteFolder(deleteFolderId);
184                            }
185    
186                            AssetPublisherUtil.removeRecentFolderId(
187                                    actionRequest, DLFileEntry.class.getName(), deleteFolderId);
188                    }
189    
190                    if (moveToTrash && (deleteFolderIds.length > 0)) {
191                            Map<String, String[]> data = new HashMap<String, String[]>();
192    
193                            data.put(
194                                    "deleteEntryClassName",
195                                    new String[] {DLFolder.class.getName()});
196    
197                            if (Validator.isNotNull(deleteEntryTitle)) {
198                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
199                            }
200    
201                            data.put(
202                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
203    
204                            SessionMessages.add(
205                                    actionRequest,
206                                    liferayPortletConfig.getPortletId() +
207                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
208    
209                            hideDefaultSuccessMessage(liferayPortletConfig, actionRequest);
210                    }
211            }
212    
213            protected void moveFolders(
214                            ActionRequest actionRequest, boolean moveFromTrash)
215                    throws Exception {
216    
217                    long[] folderIds = null;
218    
219                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
220    
221                    if (folderId > 0) {
222                            folderIds = new long[] {folderId};
223                    }
224                    else {
225                            folderIds = StringUtil.split(
226                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
227                    }
228    
229                    long parentFolderId = ParamUtil.getLong(
230                            actionRequest, "parentFolderId");
231    
232                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
233                            DLFileEntry.class.getName(), actionRequest);
234    
235                    for (long moveFolderId : folderIds) {
236                            if (moveFromTrash) {
237                                    DLAppServiceUtil.moveFolderFromTrash(
238                                            moveFolderId, parentFolderId, serviceContext);
239                            }
240                            else {
241                                    DLAppServiceUtil.moveFolder(
242                                            moveFolderId, parentFolderId, serviceContext);
243                            }
244                    }
245            }
246    
247            protected void subscribeFolder(ActionRequest actionRequest)
248                    throws Exception {
249    
250                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
251                            WebKeys.THEME_DISPLAY);
252    
253                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
254    
255                    DLAppServiceUtil.subscribeFolder(
256                            themeDisplay.getScopeGroupId(), folderId);
257            }
258    
259            protected void unsubscribeFolder(ActionRequest actionRequest)
260                    throws Exception {
261    
262                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
263                            WebKeys.THEME_DISPLAY);
264    
265                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
266    
267                    DLAppServiceUtil.unsubscribeFolder(
268                            themeDisplay.getScopeGroupId(), folderId);
269            }
270    
271            protected void updateFolder(ActionRequest actionRequest) throws Exception {
272                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
273    
274                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
275                    long parentFolderId = ParamUtil.getLong(
276                            actionRequest, "parentFolderId");
277                    String name = ParamUtil.getString(actionRequest, "name");
278                    String description = ParamUtil.getString(actionRequest, "description");
279    
280                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
281                            DLFolder.class.getName(), actionRequest);
282    
283                    if (folderId <= 0) {
284    
285                            // Add folder
286    
287                            DLAppServiceUtil.addFolder(
288                                    repositoryId, parentFolderId, name, description,
289                                    serviceContext);
290                    }
291                    else {
292    
293                            // Update folder
294    
295                            DLAppServiceUtil.updateFolder(
296                                    folderId, name, description, serviceContext);
297                    }
298            }
299    
300            protected void updateWorkflowDefinitions(ActionRequest actionRequest)
301                    throws Exception {
302    
303                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
304                            DLFileEntry.class.getName(), actionRequest);
305    
306                    DLAppServiceUtil.updateFolder(
307                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, null, null,
308                            serviceContext);
309            }
310    
311    }