1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  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         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
127 
128         long folderId = ParamUtil.getLong(actionRequest, "folderId");
129 
130         long parentFolderId = ParamUtil.getLong(
131             actionRequest, "parentFolderId");
132         String name = ParamUtil.getString(actionRequest, "name");
133         String description = ParamUtil.getString(actionRequest, "description");
134 
135         String[] communityPermissions = PortalUtil.getCommunityPermissions(
136             actionRequest);
137         String[] guestPermissions = PortalUtil.getGuestPermissions(
138             actionRequest);
139 
140         if (folderId <= 0) {
141 
142             // Add folder
143 
144             DLFolderServiceUtil.addFolder(
145                 layout.getPlid(), parentFolderId, name, description,
146                 communityPermissions, guestPermissions);
147         }
148         else {
149 
150             // Update folder
151 
152             DLFolderServiceUtil.updateFolder(
153                 folderId, parentFolderId, name, description);
154         }
155     }
156 
157 }