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