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.bookmarks.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.security.auth.PrincipalException;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portal.service.ServiceContextFactory;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portlet.bookmarks.FolderNameException;
25  import com.liferay.portlet.bookmarks.NoSuchFolderException;
26  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
27  import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class EditFolderAction extends PortletAction {
45  
46      public void processAction(
47              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
48              ActionRequest actionRequest, ActionResponse actionResponse)
49          throws Exception {
50  
51          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
52  
53          try {
54              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
55                  updateFolder(actionRequest);
56              }
57              else if (cmd.equals(Constants.DELETE)) {
58                  deleteFolder(actionRequest);
59              }
60  
61              sendRedirect(actionRequest, actionResponse);
62          }
63          catch (Exception e) {
64              if (e instanceof NoSuchFolderException ||
65                  e instanceof PrincipalException) {
66  
67                  SessionErrors.add(actionRequest, e.getClass().getName());
68  
69                  setForward(actionRequest, "portlet.bookmarks.error");
70              }
71              else if (e instanceof FolderNameException) {
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73              }
74              else {
75                  throw e;
76              }
77          }
78      }
79  
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
82              RenderRequest renderRequest, RenderResponse renderResponse)
83          throws Exception {
84  
85          try {
86              ActionUtil.getFolder(renderRequest);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchFolderException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(renderRequest, e.getClass().getName());
93  
94                  return mapping.findForward("portlet.bookmarks.error");
95              }
96              else {
97                  throw e;
98              }
99          }
100 
101         return mapping.findForward(
102             getForward(renderRequest, "portlet.bookmarks.edit_folder"));
103     }
104 
105     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
106         long folderId = ParamUtil.getLong(actionRequest, "folderId");
107 
108         BookmarksFolderServiceUtil.deleteFolder(folderId);
109     }
110 
111     protected void updateFolder(ActionRequest actionRequest) throws Exception {
112         long folderId = ParamUtil.getLong(actionRequest, "folderId");
113 
114         long parentFolderId = ParamUtil.getLong(
115             actionRequest, "parentFolderId");
116         String name = ParamUtil.getString(actionRequest, "name");
117         String description = ParamUtil.getString(actionRequest, "description");
118 
119         boolean mergeWithParentFolder = ParamUtil.getBoolean(
120             actionRequest, "mergeWithParentFolder");
121 
122         ServiceContext serviceContext = ServiceContextFactory.getInstance(
123             BookmarksFolder.class.getName(), actionRequest);
124 
125         if (folderId <= 0) {
126 
127             // Add folder
128 
129             BookmarksFolderServiceUtil.addFolder(
130                 parentFolderId, name, description, serviceContext);
131         }
132         else {
133 
134             // Update folder
135 
136             BookmarksFolderServiceUtil.updateFolder(
137                 folderId, parentFolderId, name, description,
138                 mergeWithParentFolder, serviceContext);
139         }
140     }
141 
142 }