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