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.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
026    import com.liferay.portlet.documentlibrary.DuplicateFileException;
027    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
028    import com.liferay.portlet.documentlibrary.FolderNameException;
029    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
033    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Alexander Chow
048     * @author Sergio Gonz??lez
049     */
050    public class EditFolderAction extends PortletAction {
051    
052            @Override
053            public void processAction(
054                            ActionMapping actionMapping, ActionForm actionForm,
055                            PortletConfig portletConfig, ActionRequest actionRequest,
056                            ActionResponse actionResponse)
057                    throws Exception {
058    
059                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
060    
061                    try {
062                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
063                                    updateFolder(actionRequest);
064                            }
065                            else if (cmd.equals(Constants.DELETE)) {
066                                    deleteFolders(actionRequest);
067                            }
068                            else if (cmd.equals(Constants.MOVE)) {
069                                    moveFolders(actionRequest);
070                            }
071                            else if (cmd.equals("updateWorkflowDefinitions")) {
072                                    updateWorkflowDefinitions(actionRequest);
073                            }
074    
075                            sendRedirect(actionRequest, actionResponse);
076                    }
077                    catch (Exception e) {
078                            if (e instanceof NoSuchFolderException ||
079                                    e instanceof PrincipalException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass());
082    
083                                    setForward(actionRequest, "portlet.document_library.error");
084                            }
085                            else if (e instanceof DuplicateFileException ||
086                                             e instanceof DuplicateFolderNameException ||
087                                             e instanceof FolderNameException) {
088    
089                                    SessionErrors.add(actionRequest, e.getClass());
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095            }
096    
097            @Override
098            public ActionForward render(
099                            ActionMapping actionMapping, ActionForm actionForm,
100                            PortletConfig portletConfig, RenderRequest renderRequest,
101                            RenderResponse renderResponse)
102                    throws Exception {
103    
104                    try {
105                            ActionUtil.getFolder(renderRequest);
106                    }
107                    catch (Exception e) {
108                            if (e instanceof NoSuchFolderException ||
109                                    e instanceof PrincipalException) {
110    
111                                    SessionErrors.add(renderRequest, e.getClass());
112    
113                                    return actionMapping.findForward(
114                                            "portlet.document_library.error");
115                            }
116                            else {
117                                    throw e;
118                            }
119                    }
120    
121                    return actionMapping.findForward(
122                            getForward(renderRequest, "portlet.document_library.edit_folder"));
123            }
124    
125            protected void deleteFolders(ActionRequest actionRequest) throws Exception {
126                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
127    
128                    if (folderId > 0) {
129                            DLAppServiceUtil.deleteFolder(folderId);
130    
131                            AssetPublisherUtil.removeRecentFolderId(
132                                    actionRequest, DLFileEntry.class.getName(), folderId);
133                    }
134                    else {
135                            long[] folderIds = StringUtil.split(
136                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
137    
138                            for (long curFolderId : folderIds) {
139                                    DLAppServiceUtil.deleteFolder(curFolderId);
140    
141                                    AssetPublisherUtil.removeRecentFolderId(
142                                            actionRequest, DLFileEntry.class.getName(), curFolderId);
143                            }
144                    }
145            }
146    
147            protected void moveFolders(ActionRequest actionRequest) throws Exception {
148                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
149    
150                    long parentFolderId = ParamUtil.getLong(
151                            actionRequest, "parentFolderId");
152    
153                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
154                            DLFileEntry.class.getName(), actionRequest);
155    
156                    if (folderId > 0) {
157                            DLAppServiceUtil.moveFolder(
158                                    folderId, parentFolderId, serviceContext);
159                    }
160                    else {
161                            long[] folderIds = StringUtil.split(
162                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
163    
164                            for (long curFolderId : folderIds) {
165                                    DLAppServiceUtil.moveFolder(
166                                            curFolderId, parentFolderId, serviceContext);
167                            }
168                    }
169            }
170    
171            protected void updateFolder(ActionRequest actionRequest) throws Exception {
172                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
173    
174                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
175                    long parentFolderId = ParamUtil.getLong(
176                            actionRequest, "parentFolderId");
177                    String name = ParamUtil.getString(actionRequest, "name");
178                    String description = ParamUtil.getString(actionRequest, "description");
179    
180                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
181                            DLFolder.class.getName(), actionRequest);
182    
183                    if (folderId <= 0) {
184    
185                            // Add folder
186    
187                            DLAppServiceUtil.addFolder(
188                                    repositoryId, parentFolderId, name, description,
189                                    serviceContext);
190                    }
191                    else {
192    
193                            // Update folder
194    
195                            DLAppServiceUtil.updateFolder(
196                                    folderId, name, description, serviceContext);
197                    }
198            }
199    
200            protected void updateWorkflowDefinitions(ActionRequest actionRequest)
201                    throws Exception {
202    
203                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
204                            DLFileEntry.class.getName(), actionRequest);
205    
206                    DLAppServiceUtil.updateFolder(
207                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, null, null,
208                            serviceContext);
209            }
210    
211    }