1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portal.service.ServiceContextFactory;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portlet.documentlibrary.model.DLFolder;
33  import com.liferay.portlet.imagegallery.DuplicateFolderNameException;
34  import com.liferay.portlet.imagegallery.FolderNameException;
35  import com.liferay.portlet.imagegallery.NoSuchFolderException;
36  import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.PortletConfig;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class EditFolderAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
62  
63          try {
64              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
65                  updateFolder(actionRequest);
66              }
67              else if (cmd.equals(Constants.DELETE)) {
68                  deleteFolder(actionRequest);
69              }
70  
71              sendRedirect(actionRequest, actionResponse);
72          }
73          catch (Exception e) {
74              if (e instanceof NoSuchFolderException ||
75                  e instanceof PrincipalException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78  
79                  setForward(actionRequest, "portlet.image_gallery.error");
80              }
81              else if (e instanceof DuplicateFolderNameException ||
82                       e instanceof FolderNameException) {
83  
84                  SessionErrors.add(actionRequest, e.getClass().getName());
85              }
86              else {
87                  throw e;
88              }
89          }
90      }
91  
92      public ActionForward render(
93              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
94              RenderRequest renderRequest, RenderResponse renderResponse)
95          throws Exception {
96  
97          try {
98              ActionUtil.getFolder(renderRequest);
99          }
100         catch (Exception e) {
101             if (e instanceof NoSuchFolderException ||
102                 e instanceof PrincipalException) {
103 
104                 SessionErrors.add(renderRequest, e.getClass().getName());
105 
106                 return mapping.findForward("portlet.image_gallery.error");
107             }
108             else {
109                 throw e;
110             }
111         }
112 
113         return mapping.findForward(
114             getForward(renderRequest, "portlet.image_gallery.edit_folder"));
115     }
116 
117     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
118         long folderId = ParamUtil.getLong(actionRequest, "folderId");
119 
120         IGFolderServiceUtil.deleteFolder(folderId);
121     }
122 
123     protected void updateFolder(ActionRequest actionRequest) throws Exception {
124         long folderId = ParamUtil.getLong(actionRequest, "folderId");
125 
126         long parentFolderId = ParamUtil.getLong(
127             actionRequest, "parentFolderId");
128         String name = ParamUtil.getString(actionRequest, "name");
129         String description = ParamUtil.getString(actionRequest, "description");
130 
131         boolean mergeWithParentFolder = ParamUtil.getBoolean(
132             actionRequest, "mergeWithParentFolder");
133 
134         ServiceContext serviceContext = ServiceContextFactory.getInstance(
135             DLFolder.class.getName(), actionRequest);
136 
137         if (folderId <= 0) {
138 
139             // Add folder
140 
141             IGFolderServiceUtil.addFolder(
142                 parentFolderId, name, description, serviceContext);
143         }
144         else {
145 
146             // Update folder
147 
148             IGFolderServiceUtil.updateFolder(
149                 folderId, parentFolderId, name, description,
150                 mergeWithParentFolder);
151         }
152     }
153 
154 }