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