1   /**
2    * Copyright (c) 2000-2008 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.struts.PortletAction;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.bookmarks.FolderNameException;
33  import com.liferay.portlet.bookmarks.NoSuchFolderException;
34  import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class EditFolderAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60  
61          try {
62              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63                  updateFolder(actionRequest);
64              }
65              else if (cmd.equals(Constants.DELETE)) {
66                  deleteFolder(actionRequest);
67              }
68  
69              sendRedirect(actionRequest, actionResponse);
70          }
71          catch (Exception e) {
72              if (e instanceof NoSuchFolderException ||
73                  e instanceof PrincipalException) {
74  
75                  SessionErrors.add(actionRequest, e.getClass().getName());
76  
77                  setForward(actionRequest, "portlet.bookmarks.error");
78              }
79              else if (e instanceof FolderNameException) {
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.bookmarks.error");
103             }
104             else {
105                 throw e;
106             }
107         }
108 
109         return mapping.findForward(
110             getForward(renderRequest, "portlet.bookmarks.edit_folder"));
111     }
112 
113     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
114         long folderId = ParamUtil.getLong(actionRequest, "folderId");
115 
116         BookmarksFolderServiceUtil.deleteFolder(folderId);
117     }
118 
119     protected void updateFolder(ActionRequest actionRequest) throws Exception {
120         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
121 
122         long folderId = ParamUtil.getLong(actionRequest, "folderId");
123 
124         long parentFolderId = ParamUtil.getLong(
125             actionRequest, "parentFolderId");
126         String name = ParamUtil.getString(actionRequest, "name");
127         String description = ParamUtil.getString(actionRequest, "description");
128 
129         boolean mergeWithParentFolder = ParamUtil.getBoolean(
130             actionRequest, "mergeWithParentFolder");
131 
132         String[] communityPermissions = actionRequest.getParameterValues(
133             "communityPermissions");
134         String[] guestPermissions = actionRequest.getParameterValues(
135             "guestPermissions");
136 
137         if (folderId <= 0) {
138 
139             // Add folder
140 
141             BookmarksFolderServiceUtil.addFolder(
142                 layout.getPlid(), parentFolderId, name, description,
143                 communityPermissions, guestPermissions);
144         }
145         else {
146 
147             // Update folder
148 
149             BookmarksFolderServiceUtil.updateFolder(
150                 folderId, parentFolderId, name, description,
151                 mergeWithParentFolder);
152         }
153     }
154 
155 }