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