1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.security.auth.PrincipalException;
22  import com.liferay.portal.service.ServiceContext;
23  import com.liferay.portal.service.ServiceContextFactory;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
28  import com.liferay.portlet.documentlibrary.FolderNameException;
29  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
30  import com.liferay.portlet.documentlibrary.model.DLFolder;
31  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Alexander Chow
48   */
49  public class EditFolderAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57  
58          try {
59              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
60                  updateFolder(actionRequest);
61              }
62              else if (cmd.equals(Constants.DELETE)) {
63                  deleteFolder(actionRequest);
64              }
65  
66              sendRedirect(actionRequest, actionResponse);
67          }
68          catch (Exception e) {
69              if (e instanceof NoSuchFolderException ||
70                  e instanceof PrincipalException) {
71  
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73  
74                  setForward(actionRequest, "portlet.document_library.error");
75              }
76              else if (e instanceof DuplicateFileException ||
77                       e instanceof DuplicateFolderNameException ||
78                       e instanceof FolderNameException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81              }
82              else {
83                  throw e;
84              }
85          }
86      }
87  
88      public ActionForward render(
89              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90              RenderRequest renderRequest, RenderResponse renderResponse)
91          throws Exception {
92  
93          try {
94              ActionUtil.getFolder(renderRequest);
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchFolderException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(renderRequest, e.getClass().getName());
101 
102                 return mapping.findForward("portlet.document_library.error");
103             }
104             else {
105                 throw e;
106             }
107         }
108 
109         return mapping.findForward(
110             getForward(renderRequest, "portlet.document_library.edit_folder"));
111     }
112 
113     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
114         long folderId = ParamUtil.getLong(actionRequest, "folderId");
115 
116         DLFolderServiceUtil.deleteFolder(folderId);
117     }
118 
119     protected void updateFolder(ActionRequest actionRequest) throws Exception {
120         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
121             WebKeys.THEME_DISPLAY);
122 
123         long folderId = ParamUtil.getLong(actionRequest, "folderId");
124 
125         long parentFolderId = ParamUtil.getLong(
126             actionRequest, "parentFolderId");
127         String name = ParamUtil.getString(actionRequest, "name");
128         String description = ParamUtil.getString(actionRequest, "description");
129 
130         ServiceContext serviceContext = ServiceContextFactory.getInstance(
131             DLFolder.class.getName(), actionRequest);
132 
133         if (folderId <= 0) {
134 
135             // Add folder
136 
137             DLFolderServiceUtil.addFolder(
138                 themeDisplay.getScopeGroupId(), parentFolderId, name,
139                 description, serviceContext);
140         }
141         else {
142 
143             // Update folder
144 
145             DLFolderServiceUtil.updateFolder(
146                 folderId, parentFolderId, name, description, serviceContext);
147         }
148     }
149 
150 }