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.journal.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.servlet.SessionMessages;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
031    import com.liferay.portlet.journal.DuplicateFolderNameException;
032    import com.liferay.portlet.journal.FolderNameException;
033    import com.liferay.portlet.journal.NoSuchFolderException;
034    import com.liferay.portlet.journal.model.JournalArticle;
035    import com.liferay.portlet.journal.model.JournalFolder;
036    import com.liferay.portlet.journal.service.JournalFolderServiceUtil;
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 Sergio González
053     */
054    public class EditFolderAction extends PortletAction {
055    
056            @Override
057            public void processAction(
058                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
059                            ActionRequest actionRequest, ActionResponse actionResponse)
060                    throws Exception {
061    
062                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
063    
064                    try {
065                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
066                                    updateFolder(actionRequest);
067                            }
068                            else if (cmd.equals(Constants.DELETE)) {
069                                    deleteFolders(
070                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
071                            }
072                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
073                                    deleteFolders(
074                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
075                            }
076                            else if (cmd.equals(Constants.RESTORE)) {
077                                    restoreFolderFromTrash(actionRequest);
078                            }
079                            else if (cmd.equals(Constants.MOVE)) {
080                                    moveFolder(actionRequest);
081                            }
082    
083                            sendRedirect(actionRequest, actionResponse);
084                    }
085                    catch (Exception e) {
086                            if (e instanceof NoSuchFolderException ||
087                                    e instanceof PrincipalException) {
088    
089                                    SessionErrors.add(actionRequest, e.getClass());
090    
091                                    setForward(actionRequest, "portlet.journal.error");
092                            }
093                            else if (e instanceof DuplicateFolderNameException ||
094                                             e instanceof FolderNameException) {
095    
096                                    SessionErrors.add(actionRequest, e.getClass());
097                            }
098                            else {
099                                    throw e;
100                            }
101                    }
102            }
103    
104            @Override
105            public ActionForward render(
106                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
107                            RenderRequest renderRequest, RenderResponse renderResponse)
108                    throws Exception {
109    
110                    try {
111                            ActionUtil.getFolder(renderRequest);
112                    }
113                    catch (Exception e) {
114                            if (e instanceof NoSuchFolderException ||
115                                    e instanceof PrincipalException) {
116    
117                                    SessionErrors.add(renderRequest, e.getClass());
118    
119                                    return mapping.findForward("portlet.journal.error");
120                            }
121                            else {
122                                    throw e;
123                            }
124                    }
125    
126                    return mapping.findForward(
127                            getForward(renderRequest, "portlet.journal.edit_folder"));
128            }
129    
130            protected void deleteFolders(
131                            LiferayPortletConfig liferayPortletConfig,
132                            ActionRequest actionRequest, boolean moveToTrash)
133                    throws Exception {
134    
135                    long[] deleteFolderIds = null;
136    
137                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
138    
139                    if (folderId > 0) {
140                            deleteFolderIds = new long[] {folderId};
141                    }
142                    else {
143                            deleteFolderIds = StringUtil.split(
144                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
145                    }
146    
147                    for (long deleteFolderId : deleteFolderIds) {
148                            if (moveToTrash) {
149                                    JournalFolderServiceUtil.moveFolderToTrash(deleteFolderId);
150                            }
151                            else {
152                                    JournalFolderServiceUtil.deleteFolder(deleteFolderId);
153                            }
154    
155                            AssetPublisherUtil.removeRecentFolderId(
156                                    actionRequest, JournalArticle.class.getName(), deleteFolderId);
157                    }
158    
159                    if (moveToTrash && (deleteFolderIds.length > 0)) {
160                            Map<String, String[]> data = new HashMap<String, String[]>();
161    
162                            data.put(
163                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
164    
165                            SessionMessages.add(
166                                    actionRequest,
167                                    liferayPortletConfig.getPortletId() +
168                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
169    
170                            SessionMessages.add(
171                                    actionRequest,
172                                    liferayPortletConfig.getPortletId() +
173                                            SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
174                    }
175            }
176    
177            protected void moveFolder(ActionRequest actionRequest) throws Exception {
178                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
179    
180                    long parentFolderId = ParamUtil.getLong(
181                            actionRequest, "parentFolderId");
182    
183                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
184                            JournalFolder.class.getName(), actionRequest);
185    
186                    JournalFolderServiceUtil.moveFolder(
187                            folderId, parentFolderId, serviceContext);
188            }
189    
190            protected void restoreFolderFromTrash(ActionRequest actionRequest)
191                    throws PortalException, SystemException {
192    
193                    long[] restoreEntryIds = StringUtil.split(
194                            ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
195    
196                    for (long restoreEntryId : restoreEntryIds) {
197                            JournalFolderServiceUtil.restoreFolderFromTrash(restoreEntryId);
198                    }
199            }
200    
201            protected void updateFolder(ActionRequest actionRequest) throws Exception {
202                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
203    
204                    long parentFolderId = ParamUtil.getLong(
205                            actionRequest, "parentFolderId");
206                    String name = ParamUtil.getString(actionRequest, "name");
207                    String description = ParamUtil.getString(actionRequest, "description");
208    
209                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
210                            JournalFolder.class.getName(), actionRequest);
211    
212                    if (folderId <= 0) {
213    
214                            // Add folder
215    
216                            JournalFolderServiceUtil.addFolder(
217                                    serviceContext.getScopeGroupId(), parentFolderId, name,
218                                    description, serviceContext);
219                    }
220                    else {
221    
222                            // Update folder
223    
224                            JournalFolderServiceUtil.updateFolder(
225                                    folderId, parentFolderId, name, description, false,
226                                    serviceContext);
227                    }
228            }
229    
230    }