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