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.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.assetpublisher.util.AssetPublisherUtil;
25  import com.liferay.portlet.documentlibrary.model.DLFolder;
26  import com.liferay.portlet.imagegallery.DuplicateFolderNameException;
27  import com.liferay.portlet.imagegallery.FolderNameException;
28  import com.liferay.portlet.imagegallery.NoSuchFolderException;
29  import com.liferay.portlet.imagegallery.model.IGImage;
30  import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class EditFolderAction extends PortletAction {
48  
49      public void processAction(
50              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws Exception {
53  
54          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
55  
56          try {
57              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
58                  updateFolder(actionRequest);
59              }
60              else if (cmd.equals(Constants.DELETE)) {
61                  deleteFolder(actionRequest);
62              }
63  
64              sendRedirect(actionRequest, actionResponse);
65          }
66          catch (Exception e) {
67              if (e instanceof NoSuchFolderException ||
68                  e instanceof PrincipalException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71  
72                  setForward(actionRequest, "portlet.image_gallery.error");
73              }
74              else if (e instanceof DuplicateFolderNameException ||
75                       e instanceof FolderNameException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78              }
79              else {
80                  throw e;
81              }
82          }
83      }
84  
85      public ActionForward render(
86              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
87              RenderRequest renderRequest, RenderResponse renderResponse)
88          throws Exception {
89  
90          try {
91              ActionUtil.getFolder(renderRequest);
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchFolderException ||
95                  e instanceof PrincipalException) {
96  
97                  SessionErrors.add(renderRequest, e.getClass().getName());
98  
99                  return mapping.findForward("portlet.image_gallery.error");
100             }
101             else {
102                 throw e;
103             }
104         }
105 
106         return mapping.findForward(
107             getForward(renderRequest, "portlet.image_gallery.edit_folder"));
108     }
109 
110     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
111         long folderId = ParamUtil.getLong(actionRequest, "folderId");
112 
113         IGFolderServiceUtil.deleteFolder(folderId);
114 
115         AssetPublisherUtil.removeRecentFolderId(
116             actionRequest, IGImage.class.getName(), folderId);
117     }
118 
119     protected void updateFolder(ActionRequest actionRequest) throws Exception {
120         long folderId = ParamUtil.getLong(actionRequest, "folderId");
121 
122         long parentFolderId = ParamUtil.getLong(
123             actionRequest, "parentFolderId");
124         String name = ParamUtil.getString(actionRequest, "name");
125         String description = ParamUtil.getString(actionRequest, "description");
126 
127         boolean mergeWithParentFolder = ParamUtil.getBoolean(
128             actionRequest, "mergeWithParentFolder");
129 
130         ServiceContext serviceContext = ServiceContextFactory.getInstance(
131             DLFolder.class.getName(), actionRequest);
132 
133         if (folderId <= 0) {
134 
135             // Add folder
136 
137             IGFolderServiceUtil.addFolder(
138                 parentFolderId, name, description, serviceContext);
139         }
140         else {
141 
142             // Update folder
143 
144             IGFolderServiceUtil.updateFolder(
145                 folderId, parentFolderId, name, description,
146                 mergeWithParentFolder, serviceContext);
147         }
148     }
149 
150 }