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