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