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