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