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