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.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 mapping, ActionForm form, PortletConfig portletConfig,
055                            ActionRequest actionRequest, ActionResponse actionResponse)
056                    throws Exception {
057    
058                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059    
060                    try {
061                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
062                                    updateFolder(actionRequest);
063                            }
064                            else if (cmd.equals(Constants.DELETE)) {
065                                    deleteFolders(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.MOVE)) {
068                                    moveFolders(actionRequest);
069                            }
070                            else if (cmd.equals("updateWorkflowDefinitions")) {
071                                    updateWorkflowDefinitions(actionRequest);
072                            }
073    
074                            sendRedirect(actionRequest, actionResponse);
075                    }
076                    catch (Exception e) {
077                            if (e instanceof NoSuchFolderException ||
078                                    e instanceof PrincipalException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass().getName());
081    
082                                    setForward(actionRequest, "portlet.document_library.error");
083                            }
084                            else if (e instanceof DuplicateFileException ||
085                                             e instanceof DuplicateFolderNameException ||
086                                             e instanceof FolderNameException) {
087    
088                                    SessionErrors.add(actionRequest, e.getClass().getName());
089                            }
090                            else {
091                                    throw e;
092                            }
093                    }
094            }
095    
096            @Override
097            public ActionForward render(
098                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
099                            RenderRequest renderRequest, RenderResponse renderResponse)
100                    throws Exception {
101    
102                    try {
103                            ActionUtil.getFolder(renderRequest);
104                    }
105                    catch (Exception e) {
106                            if (e instanceof NoSuchFolderException ||
107                                    e instanceof PrincipalException) {
108    
109                                    SessionErrors.add(renderRequest, e.getClass().getName());
110    
111                                    return mapping.findForward("portlet.document_library.error");
112                            }
113                            else {
114                                    throw e;
115                            }
116                    }
117    
118                    return mapping.findForward(
119                            getForward(renderRequest, "portlet.document_library.edit_folder"));
120            }
121    
122            protected void deleteFolders(ActionRequest actionRequest) throws Exception {
123                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
124    
125                    if (folderId > 0) {
126                            DLAppServiceUtil.deleteFolder(folderId);
127    
128                            AssetPublisherUtil.removeRecentFolderId(
129                                    actionRequest, DLFileEntry.class.getName(), folderId);
130                    }
131                    else {
132                            long[] folderIds = StringUtil.split(
133                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
134    
135                            for (long curFolderId : folderIds) {
136                                    DLAppServiceUtil.deleteFolder(curFolderId);
137    
138                                    AssetPublisherUtil.removeRecentFolderId(
139                                            actionRequest, DLFileEntry.class.getName(), curFolderId);
140                            }
141                    }
142            }
143    
144            protected void moveFolders(ActionRequest actionRequest) throws Exception {
145                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
146    
147                    long parentFolderId = ParamUtil.getLong(
148                            actionRequest, "parentFolderId");
149    
150                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
151                            DLFileEntry.class.getName(), actionRequest);
152    
153                    if (folderId > 0) {
154                            DLAppServiceUtil.moveFolder(
155                                    folderId, parentFolderId, serviceContext);
156                    }
157                    else {
158                            long[] folderIds = StringUtil.split(
159                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
160    
161                            for (long curFolderId : folderIds) {
162                                    DLAppServiceUtil.moveFolder(
163                                            curFolderId, parentFolderId, serviceContext);
164                            }
165                    }
166            }
167    
168            protected void updateFolder(ActionRequest actionRequest) throws Exception {
169                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
170    
171                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
172                    long parentFolderId = ParamUtil.getLong(
173                            actionRequest, "parentFolderId");
174                    String name = ParamUtil.getString(actionRequest, "name");
175                    String description = ParamUtil.getString(actionRequest, "description");
176    
177                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
178                            DLFolder.class.getName(), actionRequest);
179    
180                    if (folderId <= 0) {
181    
182                            // Add folder
183    
184                            DLAppServiceUtil.addFolder(
185                                    repositoryId, parentFolderId, name, description,
186                                    serviceContext);
187                    }
188                    else {
189    
190                            // Update folder
191    
192                            DLAppServiceUtil.updateFolder(
193                                    folderId, name, description, serviceContext);
194                    }
195            }
196    
197            protected void updateWorkflowDefinitions(ActionRequest actionRequest)
198                    throws Exception {
199    
200                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
201                            DLFileEntry.class.getName(), actionRequest);
202    
203                    DLAppServiceUtil.updateFolder(
204                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, null, null,
205                            serviceContext);
206            }
207    
208    }