001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
061                            PortletConfig portletConfig, ActionRequest actionRequest,
062                            ActionResponse actionResponse)
063                    throws Exception {
064    
065                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066    
067                    try {
068                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069                                    updateCategory(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.DELETE)) {
072                                    deleteCategories(actionRequest);
073                            }
074                            else if (cmd.equals(Constants.SUBSCRIBE)) {
075                                    subscribeCategory(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
078                                    unsubscribeCategory(actionRequest);
079                            }
080    
081                            sendRedirect(actionRequest, actionResponse);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof NoSuchCategoryException ||
085                                    e instanceof PrincipalException) {
086    
087                                    SessionErrors.add(actionRequest, e.getClass());
088    
089                                    setForward(actionRequest, "portlet.message_boards.error");
090                            }
091                            else if (e instanceof CaptchaMaxChallengesException ||
092                                             e instanceof CaptchaTextException ||
093                                             e instanceof CategoryNameException ||
094                                             e instanceof MailingListEmailAddressException ||
095                                             e instanceof MailingListInServerNameException ||
096                                             e instanceof MailingListInUserNameException ||
097                                             e instanceof MailingListOutEmailAddressException ||
098                                             e instanceof MailingListOutServerNameException ||
099                                             e instanceof MailingListOutUserNameException) {
100    
101                                    SessionErrors.add(actionRequest, e.getClass());
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107            }
108    
109            @Override
110            public ActionForward render(
111                            ActionMapping actionMapping, ActionForm actionForm,
112                            PortletConfig portletConfig, RenderRequest renderRequest,
113                            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());
124    
125                                    return actionMapping.findForward(
126                                            "portlet.message_boards.error");
127                            }
128                            else {
129                                    throw e;
130                            }
131                    }
132    
133                    return actionMapping.findForward(
134                            getForward(renderRequest, "portlet.message_boards.edit_category"));
135            }
136    
137            protected void deleteCategories(ActionRequest actionRequest)
138                    throws Exception {
139    
140                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
141                            WebKeys.THEME_DISPLAY);
142    
143                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
144    
145                    if (categoryId > 0) {
146                            MBCategoryServiceUtil.deleteCategory(
147                                    themeDisplay.getScopeGroupId(), categoryId);
148                    }
149                    else {
150                            long[] deleteCategoryIds = StringUtil.split(
151                                    ParamUtil.getString(actionRequest, "deleteCategoryIds"), 0L);
152    
153                            for (int i = 0; i < deleteCategoryIds.length; i++) {
154                                    MBCategoryServiceUtil.deleteCategory(
155                                            themeDisplay.getScopeGroupId(), deleteCategoryIds[i]);
156                            }
157                    }
158            }
159    
160            protected void subscribeCategory(ActionRequest actionRequest)
161                    throws Exception {
162    
163                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
164                            WebKeys.THEME_DISPLAY);
165    
166                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
167    
168                    MBCategoryServiceUtil.subscribeCategory(
169                            themeDisplay.getScopeGroupId(), categoryId);
170            }
171    
172            protected void unsubscribeCategory(ActionRequest actionRequest)
173                    throws Exception {
174    
175                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
176                            WebKeys.THEME_DISPLAY);
177    
178                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
179    
180                    MBCategoryServiceUtil.unsubscribeCategory(
181                            themeDisplay.getScopeGroupId(), categoryId);
182            }
183    
184            protected void updateCategory(ActionRequest actionRequest)
185                    throws Exception {
186    
187                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
188    
189                    long parentCategoryId = ParamUtil.getLong(
190                            actionRequest, "parentCategoryId");
191                    String name = ParamUtil.getString(actionRequest, "name");
192                    String description = ParamUtil.getString(actionRequest, "description");
193                    String displayStyle = ParamUtil.getString(
194                            actionRequest, "displayStyle");
195    
196                    String emailAddress = ParamUtil.getString(
197                            actionRequest, "emailAddress");
198                    String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
199                    String inServerName = ParamUtil.getString(
200                            actionRequest, "inServerName");
201                    int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
202                    boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
203                    String inUserName = ParamUtil.getString(actionRequest, "inUserName");
204                    String inPassword = ParamUtil.getString(actionRequest, "inPassword");
205                    int inReadInterval = ParamUtil.getInteger(
206                            actionRequest, "inReadInterval");
207                    String outEmailAddress = ParamUtil.getString(
208                            actionRequest, "outEmailAddress");
209                    boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
210                    String outServerName = ParamUtil.getString(
211                            actionRequest, "outServerName");
212                    int outServerPort = ParamUtil.getInteger(
213                            actionRequest, "outServerPort");
214                    boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
215                    String outUserName = ParamUtil.getString(actionRequest, "outUserName");
216                    String outPassword = ParamUtil.getString(actionRequest, "outPassword");
217                    boolean allowAnonymous = ParamUtil.getBoolean(
218                            actionRequest, "allowAnonymous");
219                    boolean mailingListActive = ParamUtil.getBoolean(
220                            actionRequest, "mailingListActive");
221    
222                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
223                            actionRequest, "mergeWithParentCategory");
224    
225                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
226                            MBCategory.class.getName(), actionRequest);
227    
228                    if (categoryId <= 0) {
229                            if (PropsValues.
230                                            CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
231    
232                                    CaptchaUtil.check(actionRequest);
233                            }
234    
235                            // Add category
236    
237                            MBCategoryServiceUtil.addCategory(
238                                    parentCategoryId, name, description, displayStyle, emailAddress,
239                                    inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
240                                    inPassword, inReadInterval, outEmailAddress, outCustom,
241                                    outServerName, outServerPort, outUseSSL, outUserName,
242                                    outPassword, allowAnonymous, mailingListActive, serviceContext);
243                    }
244                    else {
245    
246                            // Update category
247    
248                            MBCategoryServiceUtil.updateCategory(
249                                    categoryId, parentCategoryId, name, description, displayStyle,
250                                    emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
251                                    inUserName, inPassword, inReadInterval, outEmailAddress,
252                                    outCustom, outServerName, outServerPort, outUseSSL, outUserName,
253                                    outPassword, allowAnonymous, mailingListActive,
254                                    mergeWithParentCategory, serviceContext);
255                    }
256            }
257    
258    }