1   /**
2    * Copyright (c) 2000-2009 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.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.service.ServiceContext;
33  import com.liferay.portal.service.ServiceContextFactory;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.util.PropsValues;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.messageboards.CategoryNameException;
38  import com.liferay.portlet.messageboards.MailingListEmailAddressException;
39  import com.liferay.portlet.messageboards.MailingListInServerNameException;
40  import com.liferay.portlet.messageboards.MailingListInUserNameException;
41  import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
42  import com.liferay.portlet.messageboards.MailingListOutServerNameException;
43  import com.liferay.portlet.messageboards.MailingListOutUserNameException;
44  import com.liferay.portlet.messageboards.NoSuchCategoryException;
45  import com.liferay.portlet.messageboards.model.MBCategory;
46  import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.RenderResponse;
53  
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="EditCategoryAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
63   */
64  public class EditCategoryAction extends PortletAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
68              ActionRequest actionRequest, ActionResponse actionResponse)
69          throws Exception {
70  
71          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
72  
73          try {
74              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
75                  updateCategory(actionRequest);
76              }
77              else if (cmd.equals(Constants.DELETE)) {
78                  deleteCategory(actionRequest);
79              }
80              else if (cmd.equals(Constants.SUBSCRIBE)) {
81                  subscribeCategory(actionRequest);
82              }
83              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
84                  unsubscribeCategory(actionRequest);
85              }
86  
87              sendRedirect(actionRequest, actionResponse);
88          }
89          catch (Exception e) {
90              if (e instanceof NoSuchCategoryException ||
91                  e instanceof PrincipalException) {
92  
93                  SessionErrors.add(actionRequest, e.getClass().getName());
94  
95                  setForward(actionRequest, "portlet.message_boards.error");
96              }
97              else if (e instanceof CaptchaTextException ||
98                       e instanceof CategoryNameException ||
99                       e instanceof MailingListEmailAddressException ||
100                      e instanceof MailingListInServerNameException ||
101                      e instanceof MailingListInUserNameException ||
102                      e instanceof MailingListOutEmailAddressException ||
103                      e instanceof MailingListOutServerNameException ||
104                      e instanceof MailingListOutUserNameException) {
105 
106                 SessionErrors.add(actionRequest, e.getClass().getName());
107             }
108             else {
109                 throw e;
110             }
111         }
112     }
113 
114     public ActionForward render(
115             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
116             RenderRequest renderRequest, RenderResponse renderResponse)
117         throws Exception {
118 
119         try {
120             ActionUtil.getCategory(renderRequest);
121         }
122         catch (Exception e) {
123             if (e instanceof NoSuchCategoryException ||
124                 e instanceof PrincipalException) {
125 
126                 SessionErrors.add(renderRequest, e.getClass().getName());
127 
128                 return mapping.findForward("portlet.message_boards.error");
129             }
130             else {
131                 throw e;
132             }
133         }
134 
135         return mapping.findForward(
136             getForward(renderRequest, "portlet.message_boards.edit_category"));
137     }
138 
139     protected void deleteCategory(ActionRequest actionRequest)
140         throws Exception {
141 
142         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
143 
144         MBCategoryServiceUtil.deleteCategory(categoryId);
145     }
146 
147     protected void subscribeCategory(ActionRequest actionRequest)
148         throws Exception {
149 
150         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
151 
152         MBCategoryServiceUtil.subscribeCategory(categoryId);
153     }
154 
155     protected void unsubscribeCategory(ActionRequest actionRequest)
156         throws Exception {
157 
158         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
159 
160         MBCategoryServiceUtil.unsubscribeCategory(categoryId);
161     }
162 
163     protected void updateCategory(ActionRequest actionRequest)
164         throws Exception {
165 
166         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
167 
168         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
169 
170         long parentCategoryId = ParamUtil.getLong(
171             actionRequest, "parentCategoryId");
172         String name = ParamUtil.getString(actionRequest, "name");
173         String description = ParamUtil.getString(actionRequest, "description");
174 
175         String emailAddress = ParamUtil.getString(
176             actionRequest, "emailAddress");
177         String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
178         String inServerName = ParamUtil.getString(
179             actionRequest, "inServerName");
180         int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
181         boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
182         String inUserName = ParamUtil.getString(actionRequest, "inUserName");
183         String inPassword = ParamUtil.getString(actionRequest, "inPassword");
184         int inReadInterval = ParamUtil.getInteger(
185             actionRequest, "inReadInterval");
186         String outEmailAddress = ParamUtil.getString(
187             actionRequest, "outEmailAddress");
188         boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
189         String outServerName = ParamUtil.getString(
190             actionRequest, "outServerName");
191         int outServerPort = ParamUtil.getInteger(
192             actionRequest, "outServerPort");
193         boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
194         String outUserName = ParamUtil.getString(actionRequest, "outUserName");
195         String outPassword = ParamUtil.getString(actionRequest, "outPassword");
196         boolean mailingListActive = ParamUtil.getBoolean(
197             actionRequest, "mailingListActive");
198 
199         boolean mergeWithParentCategory = ParamUtil.getBoolean(
200             actionRequest, "mergeWithParentCategory");
201 
202         ServiceContext serviceContext = ServiceContextFactory.getInstance(
203             MBCategory.class.getName(), actionRequest);
204 
205         if (categoryId <= 0) {
206             if (PropsValues.
207                     CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
208 
209                 CaptchaUtil.check(actionRequest);
210             }
211 
212             // Add category
213 
214             MBCategoryServiceUtil.addCategory(
215                 parentCategoryId, name, description, emailAddress, inProtocol,
216                 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
217                 inReadInterval, outEmailAddress, outCustom, outServerName,
218                 outServerPort, outUseSSL, outUserName, outPassword,
219                 mailingListActive, serviceContext);
220         }
221         else {
222 
223             // Update category
224 
225             MBCategoryServiceUtil.updateCategory(
226                 categoryId, parentCategoryId, name, description, emailAddress,
227                 inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
228                 inPassword, inReadInterval, outEmailAddress, outCustom,
229                 outServerName, outServerPort, outUseSSL, outUserName,
230                 outPassword, mailingListActive, mergeWithParentCategory);
231         }
232     }
233 
234 }