1   /**
2    * Copyright (c) 2000-2009 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.messageboards.action;
24  
25  import com.liferay.portal.kernel.captcha.CaptchaTextException;
26  import com.liferay.portal.kernel.captcha.CaptchaUtil;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PropsValues;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.messageboards.CategoryNameException;
37  import com.liferay.portlet.messageboards.NoSuchCategoryException;
38  import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditCategoryAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class EditCategoryAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
64  
65          try {
66              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
67                  updateCategory(actionRequest);
68              }
69              else if (cmd.equals(Constants.DELETE)) {
70                  deleteCategory(actionRequest);
71              }
72              else if (cmd.equals(Constants.SUBSCRIBE)) {
73                  subscribeCategory(actionRequest);
74              }
75              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
76                  unsubscribeCategory(actionRequest);
77              }
78  
79              sendRedirect(actionRequest, actionResponse);
80          }
81          catch (Exception e) {
82              if (e instanceof NoSuchCategoryException ||
83                  e instanceof PrincipalException) {
84  
85                  SessionErrors.add(actionRequest, e.getClass().getName());
86  
87                  setForward(actionRequest, "portlet.message_boards.error");
88              }
89              else if (e instanceof CaptchaTextException ||
90                       e instanceof CategoryNameException) {
91  
92                  SessionErrors.add(actionRequest, e.getClass().getName());
93              }
94              else {
95                  throw e;
96              }
97          }
98      }
99  
100     public ActionForward render(
101             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102             RenderRequest renderRequest, RenderResponse renderResponse)
103         throws Exception {
104 
105         try {
106             ActionUtil.getCategory(renderRequest);
107         }
108         catch (Exception e) {
109             if (e instanceof NoSuchCategoryException ||
110                 e instanceof PrincipalException) {
111 
112                 SessionErrors.add(renderRequest, e.getClass().getName());
113 
114                 return mapping.findForward("portlet.message_boards.error");
115             }
116             else {
117                 throw e;
118             }
119         }
120 
121         return mapping.findForward(
122             getForward(renderRequest, "portlet.message_boards.edit_category"));
123     }
124 
125     protected void deleteCategory(ActionRequest actionRequest)
126         throws Exception {
127 
128         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
129 
130         MBCategoryServiceUtil.deleteCategory(categoryId);
131     }
132 
133     protected void subscribeCategory(ActionRequest actionRequest)
134         throws Exception {
135 
136         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
137 
138         MBCategoryServiceUtil.subscribeCategory(categoryId);
139     }
140 
141     protected void unsubscribeCategory(ActionRequest actionRequest)
142         throws Exception {
143 
144         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
145 
146         MBCategoryServiceUtil.unsubscribeCategory(categoryId);
147     }
148 
149     protected void updateCategory(ActionRequest actionRequest)
150         throws Exception {
151 
152         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
153 
154         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
155 
156         long parentCategoryId = ParamUtil.getLong(
157             actionRequest, "parentCategoryId");
158         String name = ParamUtil.getString(actionRequest, "name");
159         String description = ParamUtil.getString(actionRequest, "description");
160 
161         boolean mergeWithParentCategory = ParamUtil.getBoolean(
162             actionRequest, "mergeWithParentCategory");
163 
164         String[] communityPermissions = PortalUtil.getCommunityPermissions(
165             actionRequest);
166         String[] guestPermissions = PortalUtil.getGuestPermissions(
167             actionRequest);
168 
169         if (categoryId <= 0) {
170             if (PropsValues.
171                     CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
172 
173                 CaptchaUtil.check(actionRequest);
174             }
175 
176             // Add category
177 
178             MBCategoryServiceUtil.addCategory(
179                 layout.getPlid(), parentCategoryId, name, description,
180                 communityPermissions, guestPermissions);
181         }
182         else {
183 
184             // Update category
185 
186             MBCategoryServiceUtil.updateCategory(
187                 categoryId, parentCategoryId, name, description,
188                 mergeWithParentCategory);
189         }
190     }
191 
192 }