001    /**
002     * Copyright (c) 2000-2010 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.imagegallery.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.documentlibrary.model.DLFolder;
026    import com.liferay.portlet.imagegallery.DuplicateFolderNameException;
027    import com.liferay.portlet.imagegallery.FolderNameException;
028    import com.liferay.portlet.imagegallery.NoSuchFolderException;
029    import com.liferay.portlet.imagegallery.model.IGImage;
030    import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
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 Brian Wing Shun Chan
044     */
045    public class EditFolderAction extends PortletAction {
046    
047            public void processAction(
048                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049                            ActionRequest actionRequest, ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    try {
055                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
056                                    updateFolder(actionRequest);
057                            }
058                            else if (cmd.equals(Constants.DELETE)) {
059                                    deleteFolder(actionRequest);
060                            }
061    
062                            sendRedirect(actionRequest, actionResponse);
063                    }
064                    catch (Exception e) {
065                            if (e instanceof NoSuchFolderException ||
066                                    e instanceof PrincipalException) {
067    
068                                    SessionErrors.add(actionRequest, e.getClass().getName());
069    
070                                    setForward(actionRequest, "portlet.image_gallery.error");
071                            }
072                            else if (e instanceof DuplicateFolderNameException ||
073                                             e instanceof FolderNameException) {
074    
075                                    SessionErrors.add(actionRequest, e.getClass().getName());
076                            }
077                            else {
078                                    throw e;
079                            }
080                    }
081            }
082    
083            public ActionForward render(
084                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085                            RenderRequest renderRequest, RenderResponse renderResponse)
086                    throws Exception {
087    
088                    try {
089                            ActionUtil.getFolder(renderRequest);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchFolderException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(renderRequest, e.getClass().getName());
096    
097                                    return mapping.findForward("portlet.image_gallery.error");
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103    
104                    return mapping.findForward(
105                            getForward(renderRequest, "portlet.image_gallery.edit_folder"));
106            }
107    
108            protected void deleteFolder(ActionRequest actionRequest) throws Exception {
109                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
110    
111                    IGFolderServiceUtil.deleteFolder(folderId);
112    
113                    AssetPublisherUtil.removeRecentFolderId(
114                            actionRequest, IGImage.class.getName(), folderId);
115            }
116    
117            protected void updateFolder(ActionRequest actionRequest) throws Exception {
118                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
119    
120                    long parentFolderId = ParamUtil.getLong(
121                            actionRequest, "parentFolderId");
122                    String name = ParamUtil.getString(actionRequest, "name");
123                    String description = ParamUtil.getString(actionRequest, "description");
124    
125                    boolean mergeWithParentFolder = ParamUtil.getBoolean(
126                            actionRequest, "mergeWithParentFolder");
127    
128                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
129                            DLFolder.class.getName(), actionRequest);
130    
131                    if (folderId <= 0) {
132    
133                            // Add folder
134    
135                            IGFolderServiceUtil.addFolder(
136                                    parentFolderId, name, description, serviceContext);
137                    }
138                    else {
139    
140                            // Update folder
141    
142                            IGFolderServiceUtil.updateFolder(
143                                    folderId, parentFolderId, name, description,
144                                    mergeWithParentFolder, serviceContext);
145                    }
146            }
147    
148    }