001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.portal.kernel.captcha.CaptchaMaxChallengesException;
018    import com.liferay.portal.kernel.captcha.CaptchaTextException;
019    import com.liferay.portal.kernel.captcha.CaptchaUtil;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.messageboards.CategoryNameException;
032    import com.liferay.portlet.messageboards.MailingListEmailAddressException;
033    import com.liferay.portlet.messageboards.MailingListInServerNameException;
034    import com.liferay.portlet.messageboards.MailingListInUserNameException;
035    import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
036    import com.liferay.portlet.messageboards.MailingListOutServerNameException;
037    import com.liferay.portlet.messageboards.MailingListOutUserNameException;
038    import com.liferay.portlet.messageboards.NoSuchCategoryException;
039    import com.liferay.portlet.messageboards.model.MBCategory;
040    import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Brian Wing Shun Chan
054     * @author Daniel Sanz
055     */
056    public class EditCategoryAction extends PortletAction {
057    
058            @Override
059            public void processAction(
060                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
061                            ActionRequest actionRequest, ActionResponse actionResponse)
062                    throws Exception {
063    
064                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
065    
066                    try {
067                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
068                                    updateCategory(actionRequest);
069                            }
070                            else if (cmd.equals(Constants.DELETE)) {
071                                    deleteCategories(actionRequest);
072                            }
073                            else if (cmd.equals(Constants.SUBSCRIBE)) {
074                                    subscribeCategory(actionRequest);
075                            }
076                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
077                                    unsubscribeCategory(actionRequest);
078                            }
079    
080                            sendRedirect(actionRequest, actionResponse);
081                    }
082                    catch (Exception e) {
083                            if (e instanceof NoSuchCategoryException ||
084                                    e instanceof PrincipalException) {
085    
086                                    SessionErrors.add(actionRequest, e.getClass().getName());
087    
088                                    setForward(actionRequest, "portlet.message_boards.error");
089                            }
090                            else if (e instanceof CaptchaMaxChallengesException ||
091                                             e instanceof CaptchaTextException ||
092                                             e instanceof CategoryNameException ||
093                                             e instanceof MailingListEmailAddressException ||
094                                             e instanceof MailingListInServerNameException ||
095                                             e instanceof MailingListInUserNameException ||
096                                             e instanceof MailingListOutEmailAddressException ||
097                                             e instanceof MailingListOutServerNameException ||
098                                             e instanceof MailingListOutUserNameException) {
099    
100                                    SessionErrors.add(actionRequest, e.getClass().getName());
101                            }
102                            else {
103                                    throw e;
104                            }
105                    }
106            }
107    
108            @Override
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 deleteCategories(ActionRequest actionRequest)
135                    throws Exception {
136    
137                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
138                            WebKeys.THEME_DISPLAY);
139    
140                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
141    
142                    if (categoryId > 0) {
143                            MBCategoryServiceUtil.deleteCategory(
144                                    themeDisplay.getScopeGroupId(), categoryId);
145                    }
146                    else {
147                            long[] deleteCategoryIds = StringUtil.split(
148                                    ParamUtil.getString(actionRequest, "deleteCategoryIds"), 0L);
149    
150                            for (int i = 0; i < deleteCategoryIds.length; i++) {
151                                    MBCategoryServiceUtil.deleteCategory(
152                                            themeDisplay.getScopeGroupId(), deleteCategoryIds[i]);
153                            }
154                    }
155            }
156    
157            protected void subscribeCategory(ActionRequest actionRequest)
158                    throws Exception {
159    
160                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
161                            WebKeys.THEME_DISPLAY);
162    
163                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
164    
165                    MBCategoryServiceUtil.subscribeCategory(
166                            themeDisplay.getScopeGroupId(), categoryId);
167            }
168    
169            protected void unsubscribeCategory(ActionRequest actionRequest)
170                    throws Exception {
171    
172                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
173                            WebKeys.THEME_DISPLAY);
174    
175                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
176    
177                    MBCategoryServiceUtil.unsubscribeCategory(
178                            themeDisplay.getScopeGroupId(), categoryId);
179            }
180    
181            protected void updateCategory(ActionRequest actionRequest)
182                    throws Exception {
183    
184                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
185    
186                    long parentCategoryId = ParamUtil.getLong(
187                            actionRequest, "parentCategoryId");
188                    String name = ParamUtil.getString(actionRequest, "name");
189                    String description = ParamUtil.getString(actionRequest, "description");
190                    String displayStyle = ParamUtil.getString(
191                            actionRequest, "displayStyle");
192    
193                    String emailAddress = ParamUtil.getString(
194                            actionRequest, "emailAddress");
195                    String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
196                    String inServerName = ParamUtil.getString(
197                            actionRequest, "inServerName");
198                    int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
199                    boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
200                    String inUserName = ParamUtil.getString(actionRequest, "inUserName");
201                    String inPassword = ParamUtil.getString(actionRequest, "inPassword");
202                    int inReadInterval = ParamUtil.getInteger(
203                            actionRequest, "inReadInterval");
204                    String outEmailAddress = ParamUtil.getString(
205                            actionRequest, "outEmailAddress");
206                    boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
207                    String outServerName = ParamUtil.getString(
208                            actionRequest, "outServerName");
209                    int outServerPort = ParamUtil.getInteger(
210                            actionRequest, "outServerPort");
211                    boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
212                    String outUserName = ParamUtil.getString(actionRequest, "outUserName");
213                    String outPassword = ParamUtil.getString(actionRequest, "outPassword");
214                    boolean allowAnonymous = ParamUtil.getBoolean(
215                            actionRequest, "allowAnonymous");
216                    boolean mailingListActive = ParamUtil.getBoolean(
217                            actionRequest, "mailingListActive");
218    
219                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
220                            actionRequest, "mergeWithParentCategory");
221    
222                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
223                            MBCategory.class.getName(), actionRequest);
224    
225                    if (categoryId <= 0) {
226                            if (PropsValues.
227                                            CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
228    
229                                    CaptchaUtil.check(actionRequest);
230                            }
231    
232                            // Add category
233    
234                            MBCategoryServiceUtil.addCategory(
235                                    parentCategoryId, name, description, displayStyle, emailAddress,
236                                    inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
237                                    inPassword, inReadInterval, outEmailAddress, outCustom,
238                                    outServerName, outServerPort, outUseSSL, outUserName,
239                                    outPassword, allowAnonymous, mailingListActive, serviceContext);
240                    }
241                    else {
242    
243                            // Update category
244    
245                            MBCategoryServiceUtil.updateCategory(
246                                    categoryId, parentCategoryId, name, description, displayStyle,
247                                    emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
248                                    inUserName, inPassword, inReadInterval, outEmailAddress,
249                                    outCustom, outServerName, outServerPort, outUseSSL, outUserName,
250                                    outPassword, allowAnonymous, mailingListActive,
251                                    mergeWithParentCategory, serviceContext);
252                    }
253            }
254    
255    }