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