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.documentlibrary.action;
16  
17  import com.liferay.documentlibrary.DuplicateFileException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
27  import com.liferay.portlet.documentlibrary.FolderNameException;
28  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
29  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Alexander Chow
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.document_library.error");
73              }
74              else if (e instanceof DuplicateFileException ||
75                       e instanceof DuplicateFolderNameException ||
76                       e instanceof FolderNameException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          try {
92              ActionUtil.getFolder(renderRequest);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchFolderException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.document_library.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(
108             getForward(renderRequest, "portlet.document_library.edit_folder"));
109     }
110 
111     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
112         long folderId = ParamUtil.getLong(actionRequest, "folderId");
113 
114         DLFolderServiceUtil.deleteFolder(folderId);
115     }
116 
117     protected void updateFolder(ActionRequest actionRequest) throws Exception {
118         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
119 
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         String[] communityPermissions = PortalUtil.getCommunityPermissions(
128             actionRequest);
129         String[] guestPermissions = PortalUtil.getGuestPermissions(
130             actionRequest);
131 
132         if (folderId <= 0) {
133 
134             // Add folder
135 
136             DLFolderServiceUtil.addFolder(
137                 layout.getPlid(), parentFolderId, name, description,
138                 communityPermissions, guestPermissions);
139         }
140         else {
141 
142             // Update folder
143 
144             DLFolderServiceUtil.updateFolder(
145                 folderId, parentFolderId, name, description);
146         }
147     }
148 
149 }