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.portlet.LiferayPortletConfig;
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.security.auth.PrincipalException;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
029    import com.liferay.portlet.documentlibrary.DuplicateFileException;
030    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
031    import com.liferay.portlet.documentlibrary.FolderNameException;
032    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034    import com.liferay.portlet.documentlibrary.model.DLFolder;
035    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
036    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
037    
038    import java.util.HashMap;
039    import java.util.Map;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.RenderRequest;
045    import javax.portlet.RenderResponse;
046    
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionForward;
049    import org.apache.struts.action.ActionMapping;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     * @author Alexander Chow
054     * @author Sergio González
055     * @author Levente Hudák
056     */
057    public class EditFolderAction extends PortletAction {
058    
059            @Override
060            public void processAction(
061                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062                            ActionRequest actionRequest, ActionResponse actionResponse)
063                    throws Exception {
064    
065                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066    
067                    try {
068                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069                                    updateFolder(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.DELETE)) {
072                                    deleteFolders(
073                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
074                            }
075                            else if (cmd.equals(Constants.MOVE)) {
076                                    moveFolders(actionRequest, false);
077                            }
078                            else if (cmd.equals(Constants.MOVE_FROM_TRASH)) {
079                                    moveFolders(actionRequest, true);
080                            }
081                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
082                                    deleteFolders(
083                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
084                            }
085                            else if (cmd.equals("updateWorkflowDefinitions")) {
086                                    updateWorkflowDefinitions(actionRequest);
087                            }
088    
089                            sendRedirect(actionRequest, actionResponse);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchFolderException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(actionRequest, e.getClass());
096    
097                                    setForward(actionRequest, "portlet.document_library.error");
098                            }
099                            else if (e instanceof DuplicateFileException ||
100                                             e instanceof DuplicateFolderNameException ||
101                                             e instanceof FolderNameException) {
102    
103                                    SessionErrors.add(actionRequest, e.getClass());
104                            }
105                            else {
106                                    throw e;
107                            }
108                    }
109            }
110    
111            @Override
112            public ActionForward render(
113                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
114                            RenderRequest renderRequest, RenderResponse renderResponse)
115                    throws Exception {
116    
117                    try {
118                            ActionUtil.getFolder(renderRequest);
119                    }
120                    catch (Exception e) {
121                            if (e instanceof NoSuchFolderException ||
122                                    e instanceof PrincipalException) {
123    
124                                    SessionErrors.add(renderRequest, e.getClass());
125    
126                                    return mapping.findForward("portlet.document_library.error");
127                            }
128                            else {
129                                    throw e;
130                            }
131                    }
132    
133                    return mapping.findForward(
134                            getForward(renderRequest, "portlet.document_library.edit_folder"));
135            }
136    
137            protected void deleteFolders(
138                            LiferayPortletConfig liferayPortletConfig,
139                            ActionRequest actionRequest, boolean moveToTrash)
140                    throws Exception {
141    
142                    long[] deleteFolderIds = null;
143    
144                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
145    
146                    if (folderId > 0) {
147                            deleteFolderIds = new long[] {folderId};
148                    }
149                    else {
150                            deleteFolderIds = StringUtil.split(
151                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
152                    }
153    
154                    for (long deleteFolderId : deleteFolderIds) {
155                            if (moveToTrash) {
156                                    DLAppServiceUtil.moveFolderToTrash(deleteFolderId);
157                            }
158                            else {
159                                    DLAppServiceUtil.deleteFolder(deleteFolderId);
160                            }
161    
162                            AssetPublisherUtil.removeRecentFolderId(
163                                    actionRequest, DLFileEntry.class.getName(), deleteFolderId);
164                    }
165    
166                    if (moveToTrash && (deleteFolderIds.length > 0)) {
167                            Map<String, String[]> data = new HashMap<String, String[]>();
168    
169                            data.put(
170                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
171    
172                            SessionMessages.add(
173                                    actionRequest,
174                                    liferayPortletConfig.getPortletId() +
175                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
176    
177                            SessionMessages.add(
178                                    actionRequest,
179                                    liferayPortletConfig.getPortletId() +
180                                            SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
181                    }
182            }
183    
184            protected void moveFolders(
185                            ActionRequest actionRequest, boolean moveFromTrash)
186                    throws Exception {
187    
188                    long[] folderIds = null;
189    
190                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
191    
192                    if (folderId > 0) {
193                            folderIds = new long[] {folderId};
194                    }
195                    else {
196                            folderIds = StringUtil.split(
197                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
198                    }
199    
200                    long parentFolderId = ParamUtil.getLong(
201                            actionRequest, "parentFolderId");
202    
203                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
204                            DLFileEntry.class.getName(), actionRequest);
205    
206                    for (long moveFolderId : folderIds) {
207                            if (moveFromTrash) {
208                                    DLAppServiceUtil.moveFolderFromTrash(
209                                            moveFolderId, parentFolderId, serviceContext);
210                            }
211                            else {
212                                    DLAppServiceUtil.moveFolder(
213                                            moveFolderId, parentFolderId, serviceContext);
214                            }
215                    }
216            }
217    
218            protected void updateFolder(ActionRequest actionRequest) throws Exception {
219                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
220    
221                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
222                    long parentFolderId = ParamUtil.getLong(
223                            actionRequest, "parentFolderId");
224                    String name = ParamUtil.getString(actionRequest, "name");
225                    String description = ParamUtil.getString(actionRequest, "description");
226    
227                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
228                            DLFolder.class.getName(), actionRequest);
229    
230                    if (folderId <= 0) {
231    
232                            // Add folder
233    
234                            DLAppServiceUtil.addFolder(
235                                    repositoryId, parentFolderId, name, description,
236                                    serviceContext);
237                    }
238                    else {
239    
240                            // Update folder
241    
242                            DLAppServiceUtil.updateFolder(
243                                    folderId, name, description, serviceContext);
244                    }
245            }
246    
247            protected void updateWorkflowDefinitions(ActionRequest actionRequest)
248                    throws Exception {
249    
250                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
251                            DLFileEntry.class.getName(), actionRequest);
252    
253                    DLAppServiceUtil.updateFolder(
254                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, null, null,
255                            serviceContext);
256            }
257    
258    }