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