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