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