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