1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.model.Layout;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
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  public class EditFolderAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateFolder(actionRequest);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteFolder(actionRequest);
68              }
69  
70              sendRedirect(actionRequest, actionResponse);
71          }
72          catch (Exception e) {
73              if (e instanceof NoSuchFolderException ||
74                  e instanceof PrincipalException) {
75  
76                  SessionErrors.add(actionRequest, e.getClass().getName());
77  
78                  setForward(actionRequest, "portlet.image_gallery.error");
79              }
80              else if (e instanceof DuplicateFolderNameException ||
81                       e instanceof FolderNameException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws Exception {
95  
96          try {
97              ActionUtil.getFolder(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchFolderException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(renderRequest, e.getClass().getName());
104 
105                 return mapping.findForward("portlet.image_gallery.error");
106             }
107             else {
108                 throw e;
109             }
110         }
111 
112         return mapping.findForward(
113             getForward(renderRequest, "portlet.image_gallery.edit_folder"));
114     }
115 
116     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
117         long folderId = ParamUtil.getLong(actionRequest, "folderId");
118 
119         IGFolderServiceUtil.deleteFolder(folderId);
120     }
121 
122     protected void updateFolder(ActionRequest actionRequest) throws Exception {
123         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
124 
125         long folderId = ParamUtil.getLong(actionRequest, "folderId");
126 
127         long parentFolderId = ParamUtil.getLong(
128             actionRequest, "parentFolderId");
129         String name = ParamUtil.getString(actionRequest, "name");
130         String description = ParamUtil.getString(actionRequest, "description");
131 
132         boolean mergeWithParentFolder = ParamUtil.getBoolean(
133             actionRequest, "mergeWithParentFolder");
134 
135         String[] communityPermissions = PortalUtil.getCommunityPermissions(
136             actionRequest);
137         String[] guestPermissions = PortalUtil.getGuestPermissions(
138             actionRequest);
139 
140         if (folderId <= 0) {
141 
142             // Add folder
143 
144             IGFolderServiceUtil.addFolder(
145                 layout.getPlid(), parentFolderId, name, description,
146                 communityPermissions, guestPermissions);
147         }
148         else {
149 
150             // Update folder
151 
152             IGFolderServiceUtil.updateFolder(
153                 folderId, parentFolderId, name, description,
154                 mergeWithParentFolder);
155         }
156     }
157 
158 }