001    /**
002     * Copyright (c) 2000-present 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.model.TrashedModel;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextFactory;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.messageboards.CategoryNameException;
033    import com.liferay.portlet.messageboards.MailingListEmailAddressException;
034    import com.liferay.portlet.messageboards.MailingListInServerNameException;
035    import com.liferay.portlet.messageboards.MailingListInUserNameException;
036    import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
037    import com.liferay.portlet.messageboards.MailingListOutServerNameException;
038    import com.liferay.portlet.messageboards.MailingListOutUserNameException;
039    import com.liferay.portlet.messageboards.NoSuchCategoryException;
040    import com.liferay.portlet.messageboards.model.MBCategory;
041    import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
042    import com.liferay.portlet.trash.util.TrashUtil;
043    
044    import java.util.ArrayList;
045    import java.util.List;
046    
047    import javax.portlet.ActionRequest;
048    import javax.portlet.ActionResponse;
049    import javax.portlet.PortletConfig;
050    import javax.portlet.RenderRequest;
051    import javax.portlet.RenderResponse;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionForward;
055    import org.apache.struts.action.ActionMapping;
056    
057    /**
058     * @author Brian Wing Shun Chan
059     * @author Daniel Sanz
060     */
061    public class EditCategoryAction extends PortletAction {
062    
063            @Override
064            public void processAction(
065                            ActionMapping actionMapping, ActionForm actionForm,
066                            PortletConfig portletConfig, ActionRequest actionRequest,
067                            ActionResponse actionResponse)
068                    throws Exception {
069    
070                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
071    
072                    try {
073                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
074                                    updateCategory(actionRequest);
075                            }
076                            else if (cmd.equals(Constants.DELETE)) {
077                                    deleteCategories(actionRequest, false);
078                            }
079                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
080                                    deleteCategories(actionRequest, true);
081                            }
082                            else if (cmd.equals(Constants.SUBSCRIBE)) {
083                                    subscribeCategory(actionRequest);
084                            }
085                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
086                                    unsubscribeCategory(actionRequest);
087                            }
088    
089                            sendRedirect(actionRequest, actionResponse);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchCategoryException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(actionRequest, e.getClass());
096    
097                                    setForward(actionRequest, "portlet.message_boards.error");
098                            }
099                            else if (e instanceof CaptchaMaxChallengesException ||
100                                             e instanceof CaptchaTextException ||
101                                             e instanceof CategoryNameException ||
102                                             e instanceof MailingListEmailAddressException ||
103                                             e instanceof MailingListInServerNameException ||
104                                             e instanceof MailingListInUserNameException ||
105                                             e instanceof MailingListOutEmailAddressException ||
106                                             e instanceof MailingListOutServerNameException ||
107                                             e instanceof MailingListOutUserNameException) {
108    
109                                    SessionErrors.add(actionRequest, e.getClass());
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115            }
116    
117            @Override
118            public ActionForward render(
119                            ActionMapping actionMapping, ActionForm actionForm,
120                            PortletConfig portletConfig, RenderRequest renderRequest,
121                            RenderResponse renderResponse)
122                    throws Exception {
123    
124                    try {
125                            ActionUtil.getCategory(renderRequest);
126                    }
127                    catch (Exception e) {
128                            if (e instanceof NoSuchCategoryException ||
129                                    e instanceof PrincipalException) {
130    
131                                    SessionErrors.add(renderRequest, e.getClass());
132    
133                                    return actionMapping.findForward(
134                                            "portlet.message_boards.error");
135                            }
136                            else {
137                                    throw e;
138                            }
139                    }
140    
141                    return actionMapping.findForward(
142                            getForward(renderRequest, "portlet.message_boards.edit_category"));
143            }
144    
145            protected void deleteCategories(
146                            ActionRequest actionRequest, boolean moveToTrash)
147                    throws Exception {
148    
149                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
150                            WebKeys.THEME_DISPLAY);
151    
152                    long[] deleteCategoryIds = null;
153    
154                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
155    
156                    if (categoryId > 0) {
157                            deleteCategoryIds = new long[] {categoryId};
158                    }
159                    else {
160                            deleteCategoryIds = StringUtil.split(
161                                    ParamUtil.getString(actionRequest, "deleteCategoryIds"), 0L);
162                    }
163    
164                    List<TrashedModel> trashedModels = new ArrayList<TrashedModel>();
165    
166                    for (long deleteCategoryId : deleteCategoryIds) {
167                            if (moveToTrash) {
168                                    MBCategory category = MBCategoryServiceUtil.moveCategoryToTrash(
169                                            deleteCategoryId);
170    
171                                    trashedModels.add(category);
172                            }
173                            else {
174                                    MBCategoryServiceUtil.deleteCategory(
175                                            themeDisplay.getScopeGroupId(), deleteCategoryId);
176                            }
177                    }
178    
179                    if (moveToTrash && !trashedModels.isEmpty()) {
180                            TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);
181    
182                            hideDefaultSuccessMessage(actionRequest);
183                    }
184            }
185    
186            protected void subscribeCategory(ActionRequest actionRequest)
187                    throws Exception {
188    
189                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
190                            WebKeys.THEME_DISPLAY);
191    
192                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
193    
194                    MBCategoryServiceUtil.subscribeCategory(
195                            themeDisplay.getScopeGroupId(), categoryId);
196            }
197    
198            protected void unsubscribeCategory(ActionRequest actionRequest)
199                    throws Exception {
200    
201                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
202                            WebKeys.THEME_DISPLAY);
203    
204                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
205    
206                    MBCategoryServiceUtil.unsubscribeCategory(
207                            themeDisplay.getScopeGroupId(), categoryId);
208            }
209    
210            protected void updateCategory(ActionRequest actionRequest)
211                    throws Exception {
212    
213                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
214    
215                    long parentCategoryId = ParamUtil.getLong(
216                            actionRequest, "parentCategoryId");
217                    String name = ParamUtil.getString(actionRequest, "name");
218                    String description = ParamUtil.getString(actionRequest, "description");
219                    String displayStyle = ParamUtil.getString(
220                            actionRequest, "displayStyle");
221    
222                    String emailAddress = ParamUtil.getString(
223                            actionRequest, "emailAddress");
224                    String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
225                    String inServerName = ParamUtil.getString(
226                            actionRequest, "inServerName");
227                    int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
228                    boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
229                    String inUserName = ParamUtil.getString(actionRequest, "inUserName");
230                    String inPassword = ParamUtil.getString(actionRequest, "inPassword");
231                    int inReadInterval = ParamUtil.getInteger(
232                            actionRequest, "inReadInterval");
233                    String outEmailAddress = ParamUtil.getString(
234                            actionRequest, "outEmailAddress");
235                    boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
236                    String outServerName = ParamUtil.getString(
237                            actionRequest, "outServerName");
238                    int outServerPort = ParamUtil.getInteger(
239                            actionRequest, "outServerPort");
240                    boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
241                    String outUserName = ParamUtil.getString(actionRequest, "outUserName");
242                    String outPassword = ParamUtil.getString(actionRequest, "outPassword");
243                    boolean allowAnonymous = ParamUtil.getBoolean(
244                            actionRequest, "allowAnonymous");
245                    boolean mailingListActive = ParamUtil.getBoolean(
246                            actionRequest, "mailingListActive");
247    
248                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
249                            actionRequest, "mergeWithParentCategory");
250    
251                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
252                            MBCategory.class.getName(), actionRequest);
253    
254                    if (categoryId <= 0) {
255                            if (PropsValues.
256                                            CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
257    
258                                    CaptchaUtil.check(actionRequest);
259                            }
260    
261                            // Add category
262    
263                            MBCategoryServiceUtil.addCategory(
264                                    parentCategoryId, name, description, displayStyle, emailAddress,
265                                    inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
266                                    inPassword, inReadInterval, outEmailAddress, outCustom,
267                                    outServerName, outServerPort, outUseSSL, outUserName,
268                                    outPassword, allowAnonymous, mailingListActive, serviceContext);
269                    }
270                    else {
271    
272                            // Update category
273    
274                            MBCategoryServiceUtil.updateCategory(
275                                    categoryId, parentCategoryId, name, description, displayStyle,
276                                    emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
277                                    inUserName, inPassword, inReadInterval, outEmailAddress,
278                                    outCustom, outServerName, outServerPort, outUseSSL, outUserName,
279                                    outPassword, allowAnonymous, mailingListActive,
280                                    mergeWithParentCategory, serviceContext);
281                    }
282            }
283    
284    }