1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.util.PropsValues;
27  import com.liferay.portlet.messageboards.CategoryNameException;
28  import com.liferay.portlet.messageboards.MailingListEmailAddressException;
29  import com.liferay.portlet.messageboards.MailingListInServerNameException;
30  import com.liferay.portlet.messageboards.MailingListInUserNameException;
31  import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
32  import com.liferay.portlet.messageboards.MailingListOutServerNameException;
33  import com.liferay.portlet.messageboards.MailingListOutUserNameException;
34  import com.liferay.portlet.messageboards.NoSuchCategoryException;
35  import com.liferay.portlet.messageboards.model.MBCategory;
36  import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
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  public class EditCategoryAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateCategory(actionRequest);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteCategory(actionRequest);
68              }
69              else if (cmd.equals(Constants.SUBSCRIBE)) {
70                  subscribeCategory(actionRequest);
71              }
72              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
73                  unsubscribeCategory(actionRequest);
74              }
75  
76              sendRedirect(actionRequest, actionResponse);
77          }
78          catch (Exception e) {
79              if (e instanceof NoSuchCategoryException ||
80                  e instanceof PrincipalException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83  
84                  setForward(actionRequest, "portlet.message_boards.error");
85              }
86              else if (e instanceof CaptchaTextException ||
87                       e instanceof CategoryNameException ||
88                       e instanceof MailingListEmailAddressException ||
89                       e instanceof MailingListInServerNameException ||
90                       e instanceof MailingListInUserNameException ||
91                       e instanceof MailingListOutEmailAddressException ||
92                       e instanceof MailingListOutServerNameException ||
93                       e instanceof MailingListOutUserNameException) {
94  
95                  SessionErrors.add(actionRequest, e.getClass().getName());
96              }
97              else {
98                  throw e;
99              }
100         }
101     }
102 
103     public ActionForward render(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws Exception {
107 
108         try {
109             ActionUtil.getCategory(renderRequest);
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchCategoryException ||
113                 e instanceof PrincipalException) {
114 
115                 SessionErrors.add(renderRequest, e.getClass().getName());
116 
117                 return mapping.findForward("portlet.message_boards.error");
118             }
119             else {
120                 throw e;
121             }
122         }
123 
124         return mapping.findForward(
125             getForward(renderRequest, "portlet.message_boards.edit_category"));
126     }
127 
128     protected void deleteCategory(ActionRequest actionRequest)
129         throws Exception {
130 
131         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
132 
133         MBCategoryServiceUtil.deleteCategory(categoryId);
134     }
135 
136     protected void subscribeCategory(ActionRequest actionRequest)
137         throws Exception {
138 
139         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
140 
141         MBCategoryServiceUtil.subscribeCategory(categoryId);
142     }
143 
144     protected void unsubscribeCategory(ActionRequest actionRequest)
145         throws Exception {
146 
147         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
148 
149         MBCategoryServiceUtil.unsubscribeCategory(categoryId);
150     }
151 
152     protected void updateCategory(ActionRequest actionRequest)
153         throws Exception {
154 
155         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
156 
157         long parentCategoryId = ParamUtil.getLong(
158             actionRequest, "parentCategoryId");
159         String name = ParamUtil.getString(actionRequest, "name");
160         String description = ParamUtil.getString(actionRequest, "description");
161 
162         String emailAddress = ParamUtil.getString(
163             actionRequest, "emailAddress");
164         String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
165         String inServerName = ParamUtil.getString(
166             actionRequest, "inServerName");
167         int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
168         boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
169         String inUserName = ParamUtil.getString(actionRequest, "inUserName");
170         String inPassword = ParamUtil.getString(actionRequest, "inPassword");
171         int inReadInterval = ParamUtil.getInteger(
172             actionRequest, "inReadInterval");
173         String outEmailAddress = ParamUtil.getString(
174             actionRequest, "outEmailAddress");
175         boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
176         String outServerName = ParamUtil.getString(
177             actionRequest, "outServerName");
178         int outServerPort = ParamUtil.getInteger(
179             actionRequest, "outServerPort");
180         boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
181         String outUserName = ParamUtil.getString(actionRequest, "outUserName");
182         String outPassword = ParamUtil.getString(actionRequest, "outPassword");
183         boolean mailingListActive = ParamUtil.getBoolean(
184             actionRequest, "mailingListActive");
185 
186         boolean mergeWithParentCategory = ParamUtil.getBoolean(
187             actionRequest, "mergeWithParentCategory");
188 
189         ServiceContext serviceContext = ServiceContextFactory.getInstance(
190             MBCategory.class.getName(), actionRequest);
191 
192         if (categoryId <= 0) {
193             if (PropsValues.
194                     CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
195 
196                 CaptchaUtil.check(actionRequest);
197             }
198 
199             // Add category
200 
201             MBCategoryServiceUtil.addCategory(
202                 parentCategoryId, name, description, emailAddress, inProtocol,
203                 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
204                 inReadInterval, outEmailAddress, outCustom, outServerName,
205                 outServerPort, outUseSSL, outUserName, outPassword,
206                 mailingListActive, serviceContext);
207         }
208         else {
209 
210             // Update category
211 
212             MBCategoryServiceUtil.updateCategory(
213                 categoryId, parentCategoryId, name, description, emailAddress,
214                 inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
215                 inPassword, inReadInterval, outEmailAddress, outCustom,
216                 outServerName, outServerPort, outUseSSL, outUserName,
217                 outPassword, mailingListActive, mergeWithParentCategory);
218         }
219     }
220 
221 }