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