1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.bookmarks.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.bookmarks.FolderNameException;
35  import com.liferay.portlet.bookmarks.NoSuchFolderException;
36  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
37  import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
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   *
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.bookmarks.error");
81              }
82              else if (e instanceof FolderNameException) {
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws Exception {
95  
96          try {
97              ActionUtil.getFolder(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchFolderException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(renderRequest, e.getClass().getName());
104 
105                 return mapping.findForward("portlet.bookmarks.error");
106             }
107             else {
108                 throw e;
109             }
110         }
111 
112         return mapping.findForward(
113             getForward(renderRequest, "portlet.bookmarks.edit_folder"));
114     }
115 
116     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
117         long folderId = ParamUtil.getLong(actionRequest, "folderId");
118 
119         BookmarksFolderServiceUtil.deleteFolder(folderId);
120     }
121 
122     protected void updateFolder(ActionRequest actionRequest) throws Exception {
123         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
124 
125         long folderId = ParamUtil.getLong(actionRequest, "folderId");
126 
127         long parentFolderId = ParamUtil.getLong(
128             actionRequest, "parentFolderId");
129         String name = ParamUtil.getString(actionRequest, "name");
130         String description = ParamUtil.getString(actionRequest, "description");
131 
132         boolean mergeWithParentFolder = ParamUtil.getBoolean(
133             actionRequest, "mergeWithParentFolder");
134 
135         ServiceContext serviceContext = ServiceContextFactory.getInstance(
136             BookmarksFolder.class.getName(), actionRequest);
137 
138         if (folderId <= 0) {
139 
140             // Add folder
141 
142             BookmarksFolderServiceUtil.addFolder(
143                 parentFolderId, name, description, serviceContext);
144         }
145         else {
146 
147             // Update folder
148 
149             BookmarksFolderServiceUtil.updateFolder(
150                 folderId, parentFolderId, name, description,
151                 mergeWithParentFolder);
152         }
153     }
154 
155 }