1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.imagegallery.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.imagegallery.DuplicateFolderNameException;
26  import com.liferay.portlet.imagegallery.FolderNameException;
27  import com.liferay.portlet.imagegallery.NoSuchFolderException;
28  import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class EditFolderAction extends PortletAction {
46  
47      public void processAction(
48              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
49              ActionRequest actionRequest, ActionResponse actionResponse)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
53  
54          try {
55              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
56                  updateFolder(actionRequest);
57              }
58              else if (cmd.equals(Constants.DELETE)) {
59                  deleteFolder(actionRequest);
60              }
61  
62              sendRedirect(actionRequest, actionResponse);
63          }
64          catch (Exception e) {
65              if (e instanceof NoSuchFolderException ||
66                  e instanceof PrincipalException) {
67  
68                  SessionErrors.add(actionRequest, e.getClass().getName());
69  
70                  setForward(actionRequest, "portlet.image_gallery.error");
71              }
72              else if (e instanceof DuplicateFolderNameException ||
73                       e instanceof FolderNameException) {
74  
75                  SessionErrors.add(actionRequest, e.getClass().getName());
76              }
77              else {
78                  throw e;
79              }
80          }
81      }
82  
83      public ActionForward render(
84              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
85              RenderRequest renderRequest, RenderResponse renderResponse)
86          throws Exception {
87  
88          try {
89              ActionUtil.getFolder(renderRequest);
90          }
91          catch (Exception e) {
92              if (e instanceof NoSuchFolderException ||
93                  e instanceof PrincipalException) {
94  
95                  SessionErrors.add(renderRequest, e.getClass().getName());
96  
97                  return mapping.findForward("portlet.image_gallery.error");
98              }
99              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 
114     protected void updateFolder(ActionRequest actionRequest) throws Exception {
115         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
116 
117         long folderId = ParamUtil.getLong(actionRequest, "folderId");
118 
119         long parentFolderId = ParamUtil.getLong(
120             actionRequest, "parentFolderId");
121         String name = ParamUtil.getString(actionRequest, "name");
122         String description = ParamUtil.getString(actionRequest, "description");
123 
124         boolean mergeWithParentFolder = ParamUtil.getBoolean(
125             actionRequest, "mergeWithParentFolder");
126 
127         String[] communityPermissions = PortalUtil.getCommunityPermissions(
128             actionRequest);
129         String[] guestPermissions = PortalUtil.getGuestPermissions(
130             actionRequest);
131 
132         if (folderId <= 0) {
133 
134             // Add folder
135 
136             IGFolderServiceUtil.addFolder(
137                 layout.getPlid(), parentFolderId, name, description,
138                 communityPermissions, guestPermissions);
139         }
140         else {
141 
142             // Update folder
143 
144             IGFolderServiceUtil.updateFolder(
145                 folderId, parentFolderId, name, description,
146                 mergeWithParentFolder);
147         }
148     }
149 
150 }