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