1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.documentlibrary.action;
24  
25  import com.liferay.documentlibrary.DuplicateFileException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
36  import com.liferay.portlet.documentlibrary.FolderNameException;
37  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
38  import com.liferay.portlet.documentlibrary.model.DLFolder;
39  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Alexander Chow
56   *
57   */
58  public class EditFolderAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
69                  updateFolder(actionRequest);
70              }
71              else if (cmd.equals(Constants.DELETE)) {
72                  deleteFolder(actionRequest);
73              }
74  
75              sendRedirect(actionRequest, actionResponse);
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchFolderException ||
79                  e instanceof PrincipalException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82  
83                  setForward(actionRequest, "portlet.document_library.error");
84              }
85              else if (e instanceof DuplicateFileException ||
86                       e instanceof DuplicateFolderNameException ||
87                       e instanceof FolderNameException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName());
90              }
91              else {
92                  throw e;
93              }
94          }
95      }
96  
97      public ActionForward render(
98              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
99              RenderRequest renderRequest, RenderResponse renderResponse)
100         throws Exception {
101 
102         try {
103             ActionUtil.getFolder(renderRequest);
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchFolderException ||
107                 e instanceof PrincipalException) {
108 
109                 SessionErrors.add(renderRequest, e.getClass().getName());
110 
111                 return mapping.findForward("portlet.document_library.error");
112             }
113             else {
114                 throw e;
115             }
116         }
117 
118         return mapping.findForward(
119             getForward(renderRequest, "portlet.document_library.edit_folder"));
120     }
121 
122     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
123         long folderId = ParamUtil.getLong(actionRequest, "folderId");
124 
125         DLFolderServiceUtil.deleteFolder(folderId);
126     }
127 
128     protected void updateFolder(ActionRequest actionRequest) throws Exception {
129         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
130             WebKeys.THEME_DISPLAY);
131 
132         long folderId = ParamUtil.getLong(actionRequest, "folderId");
133 
134         long parentFolderId = ParamUtil.getLong(
135             actionRequest, "parentFolderId");
136         String name = ParamUtil.getString(actionRequest, "name");
137         String description = ParamUtil.getString(actionRequest, "description");
138 
139         ServiceContext serviceContext = ServiceContextFactory.getInstance(
140             DLFolder.class.getName(), actionRequest);
141 
142         if (folderId <= 0) {
143 
144             // Add folder
145 
146             DLFolderServiceUtil.addFolder(
147                 themeDisplay.getScopeGroupId(), parentFolderId, name,
148                 description, serviceContext);
149         }
150         else {
151 
152             // Update folder
153 
154             DLFolderServiceUtil.updateFolder(
155                 folderId, parentFolderId, name, description, serviceContext);
156         }
157     }
158 
159 }