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.security.auth.PrincipalException;
27  import com.liferay.portal.service.ServiceContext;
28  import com.liferay.portal.service.ServiceContextFactory;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
33  import com.liferay.portlet.documentlibrary.FolderNameException;
34  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
35  import com.liferay.portlet.documentlibrary.model.DLFolder;
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         ServiceContext serviceContext = ServiceContextFactory.getInstance(
137             DLFolder.class.getName(), actionRequest);
138 
139         if (folderId <= 0) {
140 
141             // Add folder
142 
143             DLFolderServiceUtil.addFolder(
144                 themeDisplay.getScopeGroupId(), parentFolderId, name,
145                 description, serviceContext);
146         }
147         else {
148 
149             // Update folder
150 
151             DLFolderServiceUtil.updateFolder(
152                 folderId, parentFolderId, name, description, serviceContext);
153         }
154     }
155 
156 }