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.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.security.auth.PrincipalException;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
025    import com.liferay.portlet.journal.DuplicateFolderNameException;
026    import com.liferay.portlet.journal.FolderNameException;
027    import com.liferay.portlet.journal.NoSuchFolderException;
028    import com.liferay.portlet.journal.model.JournalArticle;
029    import com.liferay.portlet.journal.model.JournalFolder;
030    import com.liferay.portlet.journal.service.JournalFolderServiceUtil;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Sergio González
044     */
045    public class EditFolderAction extends PortletAction {
046    
047            @Override
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
057                                    updateFolder(actionRequest);
058                            }
059                            else if (cmd.equals(Constants.DELETE)) {
060                                    deleteFolder(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.MOVE)) {
063                                    moveFolder(actionRequest);
064                            }
065    
066                            sendRedirect(actionRequest, actionResponse);
067                    }
068                    catch (Exception e) {
069                            if (e instanceof NoSuchFolderException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass());
073    
074                                    setForward(actionRequest, "portlet.journal.error");
075                            }
076                            else if (e instanceof DuplicateFolderNameException ||
077                                             e instanceof FolderNameException) {
078    
079                                    SessionErrors.add(actionRequest, e.getClass());
080                            }
081                            else {
082                                    throw e;
083                            }
084                    }
085            }
086    
087            @Override
088            public ActionForward render(
089                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
090                            RenderRequest renderRequest, RenderResponse renderResponse)
091                    throws Exception {
092    
093                    try {
094                            ActionUtil.getFolder(renderRequest);
095                    }
096                    catch (Exception e) {
097                            if (e instanceof NoSuchFolderException ||
098                                    e instanceof PrincipalException) {
099    
100                                    SessionErrors.add(renderRequest, e.getClass());
101    
102                                    return mapping.findForward("portlet.journal.error");
103                            }
104                            else {
105                                    throw e;
106                            }
107                    }
108    
109                    return mapping.findForward(
110                            getForward(renderRequest, "portlet.journal.edit_folder"));
111            }
112    
113            protected void deleteFolder(ActionRequest actionRequest) throws Exception {
114                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
115    
116                    JournalFolderServiceUtil.deleteFolder(folderId);
117    
118                    AssetPublisherUtil.removeRecentFolderId(
119                            actionRequest, JournalArticle.class.getName(), folderId);
120            }
121    
122            protected void moveFolder(ActionRequest actionRequest) throws Exception {
123                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
124    
125                    long parentFolderId = ParamUtil.getLong(
126                            actionRequest, "parentFolderId");
127    
128                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
129                            JournalFolder.class.getName(), actionRequest);
130    
131                    JournalFolderServiceUtil.moveFolder(
132                            folderId, parentFolderId, serviceContext);
133            }
134    
135            protected void updateFolder(ActionRequest actionRequest) throws Exception {
136                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
137    
138                    long parentFolderId = ParamUtil.getLong(
139                            actionRequest, "parentFolderId");
140                    String name = ParamUtil.getString(actionRequest, "name");
141                    String description = ParamUtil.getString(actionRequest, "description");
142    
143                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
144                            JournalFolder.class.getName(), actionRequest);
145    
146                    if (folderId <= 0) {
147    
148                            // Add folder
149    
150                            JournalFolderServiceUtil.addFolder(
151                                    serviceContext.getScopeGroupId(), parentFolderId, name,
152                                    description, serviceContext);
153                    }
154                    else {
155    
156                            // Update folder
157    
158                            JournalFolderServiceUtil.updateFolder(
159                                    folderId, parentFolderId, name, description, false,
160                                    serviceContext);
161                    }
162            }
163    
164    }