1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditCategoryAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63  
64          try {
65              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66                  updateCategory(actionRequest);
67              }
68              else if (cmd.equals(Constants.DELETE)) {
69                  deleteCategory(actionRequest);
70              }
71              else if (cmd.equals(Constants.SUBSCRIBE)) {
72                  subscribeCategory(actionRequest);
73              }
74              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
75                  unsubscribeCategory(actionRequest);
76              }
77  
78              sendRedirect(actionRequest, actionResponse);
79          }
80          catch (Exception e) {
81              if (e instanceof NoSuchCategoryException ||
82                  e instanceof PrincipalException) {
83  
84                  SessionErrors.add(actionRequest, e.getClass().getName());
85  
86                  setForward(actionRequest, "portlet.message_boards.error");
87              }
88              else if (e instanceof CaptchaTextException ||
89                       e instanceof CategoryNameException) {
90  
91                  SessionErrors.add(actionRequest, e.getClass().getName());
92              }
93              else {
94                  throw e;
95              }
96          }
97      }
98  
99      public ActionForward render(
100             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101             RenderRequest renderRequest, RenderResponse renderResponse)
102         throws Exception {
103 
104         try {
105             ActionUtil.getCategory(renderRequest);
106         }
107         catch (Exception e) {
108             if (e instanceof NoSuchCategoryException ||
109                 e instanceof PrincipalException) {
110 
111                 SessionErrors.add(renderRequest, e.getClass().getName());
112 
113                 return mapping.findForward("portlet.message_boards.error");
114             }
115             else {
116                 throw e;
117             }
118         }
119 
120         return mapping.findForward(
121             getForward(renderRequest, "portlet.message_boards.edit_category"));
122     }
123 
124     protected void deleteCategory(ActionRequest actionRequest)
125         throws Exception {
126 
127         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
128 
129         MBCategoryServiceUtil.deleteCategory(categoryId);
130     }
131 
132     protected void subscribeCategory(ActionRequest actionRequest)
133         throws Exception {
134 
135         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
136 
137         MBCategoryServiceUtil.subscribeCategory(categoryId);
138     }
139 
140     protected void unsubscribeCategory(ActionRequest actionRequest)
141         throws Exception {
142 
143         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
144 
145         MBCategoryServiceUtil.unsubscribeCategory(categoryId);
146     }
147 
148     protected void updateCategory(ActionRequest actionRequest)
149         throws Exception {
150 
151         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
152 
153         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
154 
155         long parentCategoryId = ParamUtil.getLong(
156             actionRequest, "parentCategoryId");
157         String name = ParamUtil.getString(actionRequest, "name");
158         String description = ParamUtil.getString(actionRequest, "description");
159 
160         boolean mergeWithParentCategory = ParamUtil.getBoolean(
161             actionRequest, "mergeWithParentCategory");
162 
163         String[] communityPermissions = PortalUtil.getCommunityPermissions(
164             actionRequest);
165         String[] guestPermissions = PortalUtil.getGuestPermissions(
166             actionRequest);
167 
168         if (categoryId <= 0) {
169             if (PropsValues.
170                     CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
171 
172                 CaptchaUtil.check(actionRequest);
173             }
174 
175             // Add category
176 
177             MBCategoryServiceUtil.addCategory(
178                 layout.getPlid(), parentCategoryId, name, description,
179                 communityPermissions, guestPermissions);
180         }
181         else {
182 
183             // Update category
184 
185             MBCategoryServiceUtil.updateCategory(
186                 categoryId, parentCategoryId, name, description,
187                 mergeWithParentCategory);
188         }
189     }
190 
191 }