1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.security.auth.PrincipalException;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portal.service.ServiceContextFactory;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portlet.documentlibrary.model.DLFolder;
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         long folderId = ParamUtil.getLong(actionRequest, "folderId");
116 
117         long parentFolderId = ParamUtil.getLong(
118             actionRequest, "parentFolderId");
119         String name = ParamUtil.getString(actionRequest, "name");
120         String description = ParamUtil.getString(actionRequest, "description");
121 
122         boolean mergeWithParentFolder = ParamUtil.getBoolean(
123             actionRequest, "mergeWithParentFolder");
124 
125         ServiceContext serviceContext = ServiceContextFactory.getInstance(
126             DLFolder.class.getName(), actionRequest);
127 
128         if (folderId <= 0) {
129 
130             // Add folder
131 
132             IGFolderServiceUtil.addFolder(
133                 parentFolderId, name, description, serviceContext);
134         }
135         else {
136 
137             // Update folder
138 
139             IGFolderServiceUtil.updateFolder(
140                 folderId, parentFolderId, name, description,
141                 mergeWithParentFolder, serviceContext);
142         }
143     }
144 
145 }