1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.security.auth.PrincipalException;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portal.service.ServiceContextFactory;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PropsValues;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.messageboards.CategoryNameException;
30  import com.liferay.portlet.messageboards.MailingListEmailAddressException;
31  import com.liferay.portlet.messageboards.MailingListInServerNameException;
32  import com.liferay.portlet.messageboards.MailingListInUserNameException;
33  import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
34  import com.liferay.portlet.messageboards.MailingListOutServerNameException;
35  import com.liferay.portlet.messageboards.MailingListOutUserNameException;
36  import com.liferay.portlet.messageboards.NoSuchCategoryException;
37  import com.liferay.portlet.messageboards.model.MBCategory;
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                       e instanceof MailingListEmailAddressException ||
91                       e instanceof MailingListInServerNameException ||
92                       e instanceof MailingListInUserNameException ||
93                       e instanceof MailingListOutEmailAddressException ||
94                       e instanceof MailingListOutServerNameException ||
95                       e instanceof MailingListOutUserNameException) {
96  
97                  SessionErrors.add(actionRequest, e.getClass().getName());
98              }
99              else {
100                 throw e;
101             }
102         }
103     }
104 
105     public ActionForward render(
106             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
107             RenderRequest renderRequest, RenderResponse renderResponse)
108         throws Exception {
109 
110         try {
111             ActionUtil.getCategory(renderRequest);
112         }
113         catch (Exception e) {
114             if (e instanceof NoSuchCategoryException ||
115                 e instanceof PrincipalException) {
116 
117                 SessionErrors.add(renderRequest, e.getClass().getName());
118 
119                 return mapping.findForward("portlet.message_boards.error");
120             }
121             else {
122                 throw e;
123             }
124         }
125 
126         return mapping.findForward(
127             getForward(renderRequest, "portlet.message_boards.edit_category"));
128     }
129 
130     protected void deleteCategory(ActionRequest actionRequest)
131         throws Exception {
132 
133         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
134             WebKeys.THEME_DISPLAY);
135 
136         long groupId = themeDisplay.getScopeGroupId();
137         long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
138 
139         MBCategoryServiceUtil.deleteCategory(groupId, categoryId);
140     }
141 
142     protected void subscribeCategory(ActionRequest actionRequest)
143         throws Exception {
144 
145         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
146             WebKeys.THEME_DISPLAY);
147 
148         long groupId = themeDisplay.getScopeGroupId();
149         long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
150 
151         MBCategoryServiceUtil.subscribeCategory(groupId, categoryId);
152     }
153 
154     protected void unsubscribeCategory(ActionRequest actionRequest)
155         throws Exception {
156 
157         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
158             WebKeys.THEME_DISPLAY);
159 
160         long groupId = themeDisplay.getScopeGroupId();
161         long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
162 
163         MBCategoryServiceUtil.unsubscribeCategory(groupId, categoryId);
164     }
165 
166     protected void updateCategory(ActionRequest actionRequest)
167         throws Exception {
168 
169         long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
170 
171         long parentCategoryId = ParamUtil.getLong(
172             actionRequest, "parentCategoryId");
173         String name = ParamUtil.getString(actionRequest, "name");
174         String description = ParamUtil.getString(actionRequest, "description");
175 
176         String emailAddress = ParamUtil.getString(
177             actionRequest, "emailAddress");
178         String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
179         String inServerName = ParamUtil.getString(
180             actionRequest, "inServerName");
181         int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
182         boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
183         String inUserName = ParamUtil.getString(actionRequest, "inUserName");
184         String inPassword = ParamUtil.getString(actionRequest, "inPassword");
185         int inReadInterval = ParamUtil.getInteger(
186             actionRequest, "inReadInterval");
187         String outEmailAddress = ParamUtil.getString(
188             actionRequest, "outEmailAddress");
189         boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
190         String outServerName = ParamUtil.getString(
191             actionRequest, "outServerName");
192         int outServerPort = ParamUtil.getInteger(
193             actionRequest, "outServerPort");
194         boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
195         String outUserName = ParamUtil.getString(actionRequest, "outUserName");
196         String outPassword = ParamUtil.getString(actionRequest, "outPassword");
197         boolean mailingListActive = ParamUtil.getBoolean(
198             actionRequest, "mailingListActive");
199 
200         boolean mergeWithParentCategory = ParamUtil.getBoolean(
201             actionRequest, "mergeWithParentCategory");
202 
203         ServiceContext serviceContext = ServiceContextFactory.getInstance(
204             MBCategory.class.getName(), actionRequest);
205 
206         if (categoryId <= 0) {
207             if (PropsValues.
208                     CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
209 
210                 CaptchaUtil.check(actionRequest);
211             }
212 
213             // Add category
214 
215             MBCategoryServiceUtil.addCategory(
216                 parentCategoryId, name, description, emailAddress, inProtocol,
217                 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
218                 inReadInterval, outEmailAddress, outCustom, outServerName,
219                 outServerPort, outUseSSL, outUserName, outPassword,
220                 mailingListActive, serviceContext);
221         }
222         else {
223 
224             // Update category
225 
226             MBCategoryServiceUtil.updateCategory(
227                 categoryId, parentCategoryId, name, description, emailAddress,
228                 inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
229                 inPassword, inReadInterval, outEmailAddress, outCustom,
230                 outServerName, outServerPort, outUseSSL, outUserName,
231                 outPassword, mailingListActive, mergeWithParentCategory,
232                 serviceContext);
233         }
234     }
235 
236 }