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