1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
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.model.Layout;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.bookmarks.FolderNameException;
26  import com.liferay.portlet.bookmarks.NoSuchFolderException;
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         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
113 
114         long folderId = ParamUtil.getLong(actionRequest, "folderId");
115 
116         long parentFolderId = ParamUtil.getLong(
117             actionRequest, "parentFolderId");
118         String name = ParamUtil.getString(actionRequest, "name");
119         String description = ParamUtil.getString(actionRequest, "description");
120 
121         boolean mergeWithParentFolder = ParamUtil.getBoolean(
122             actionRequest, "mergeWithParentFolder");
123 
124         String[] communityPermissions = PortalUtil.getCommunityPermissions(
125             actionRequest);
126         String[] guestPermissions = PortalUtil.getGuestPermissions(
127             actionRequest);
128 
129         if (folderId <= 0) {
130 
131             // Add folder
132 
133             BookmarksFolderServiceUtil.addFolder(
134                 layout.getPlid(), parentFolderId, name, description,
135                 communityPermissions, guestPermissions);
136         }
137         else {
138 
139             // Update folder
140 
141             BookmarksFolderServiceUtil.updateFolder(
142                 folderId, parentFolderId, name, description,
143                 mergeWithParentFolder);
144         }
145     }
146 
147 }