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.servlet.SessionMessages;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
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.PortalUtil;
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(actionRequest, false);
080                            }
081                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
082                                    deleteCategories(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 actionMapping, ActionForm actionForm,
122                            PortletConfig portletConfig, RenderRequest renderRequest,
123                            RenderResponse renderResponse)
124                    throws Exception {
125    
126                    try {
127                            ActionUtil.getCategory(renderRequest);
128                    }
129                    catch (Exception e) {
130                            if (e instanceof NoSuchCategoryException ||
131                                    e instanceof PrincipalException) {
132    
133                                    SessionErrors.add(renderRequest, e.getClass());
134    
135                                    return actionMapping.findForward(
136                                            "portlet.message_boards.error");
137                            }
138                            else {
139                                    throw e;
140                            }
141                    }
142    
143                    return actionMapping.findForward(
144                            getForward(renderRequest, "portlet.message_boards.edit_category"));
145            }
146    
147            protected void deleteCategories(
148                            ActionRequest actionRequest, boolean moveToTrash)
149                    throws Exception {
150    
151                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
152                            WebKeys.THEME_DISPLAY);
153    
154                    String deleteEntryTitle = null;
155    
156                    long[] deleteCategoryIds = null;
157    
158                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
159    
160                    if (categoryId > 0) {
161                            deleteCategoryIds = new long[] {categoryId};
162                    }
163                    else {
164                            deleteCategoryIds = StringUtil.split(
165                                    ParamUtil.getString(actionRequest, "deleteCategoryIds"), 0L);
166                    }
167    
168                    for (int i = 0; i < deleteCategoryIds.length; i++) {
169                            long deleteCategoryId = deleteCategoryIds[i];
170    
171                            if (moveToTrash) {
172                                    MBCategory category = MBCategoryServiceUtil.moveCategoryToTrash(
173                                            deleteCategoryId);
174    
175                                    if (i == 0) {
176                                            deleteEntryTitle = category.getName();
177                                    }
178                            }
179                            else {
180                                    MBCategoryServiceUtil.deleteCategory(
181                                            themeDisplay.getScopeGroupId(), deleteCategoryId);
182                            }
183                    }
184    
185                    if (moveToTrash && (deleteCategoryIds.length > 0)) {
186                            Map<String, String[]> data = new HashMap<String, String[]>();
187    
188                            data.put(
189                                    "deleteEntryClassName",
190                                    new String[] {MBCategory.class.getName()});
191    
192                            if (Validator.isNotNull(deleteEntryTitle)) {
193                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
194                            }
195    
196                            data.put(
197                                    "restoreCategoryIds",
198                                    ArrayUtil.toStringArray(deleteCategoryIds));
199    
200                            SessionMessages.add(
201                                    actionRequest,
202                                    PortalUtil.getPortletId(actionRequest) +
203                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
204    
205                            hideDefaultSuccessMessage(actionRequest);
206                    }
207            }
208    
209            protected void subscribeCategory(ActionRequest actionRequest)
210                    throws Exception {
211    
212                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
213                            WebKeys.THEME_DISPLAY);
214    
215                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
216    
217                    MBCategoryServiceUtil.subscribeCategory(
218                            themeDisplay.getScopeGroupId(), categoryId);
219            }
220    
221            protected void unsubscribeCategory(ActionRequest actionRequest)
222                    throws Exception {
223    
224                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
225                            WebKeys.THEME_DISPLAY);
226    
227                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
228    
229                    MBCategoryServiceUtil.unsubscribeCategory(
230                            themeDisplay.getScopeGroupId(), categoryId);
231            }
232    
233            protected void updateCategory(ActionRequest actionRequest)
234                    throws Exception {
235    
236                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
237    
238                    long parentCategoryId = ParamUtil.getLong(
239                            actionRequest, "parentCategoryId");
240                    String name = ParamUtil.getString(actionRequest, "name");
241                    String description = ParamUtil.getString(actionRequest, "description");
242                    String displayStyle = ParamUtil.getString(
243                            actionRequest, "displayStyle");
244    
245                    String emailAddress = ParamUtil.getString(
246                            actionRequest, "emailAddress");
247                    String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
248                    String inServerName = ParamUtil.getString(
249                            actionRequest, "inServerName");
250                    int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
251                    boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
252                    String inUserName = ParamUtil.getString(actionRequest, "inUserName");
253                    String inPassword = ParamUtil.getString(actionRequest, "inPassword");
254                    int inReadInterval = ParamUtil.getInteger(
255                            actionRequest, "inReadInterval");
256                    String outEmailAddress = ParamUtil.getString(
257                            actionRequest, "outEmailAddress");
258                    boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
259                    String outServerName = ParamUtil.getString(
260                            actionRequest, "outServerName");
261                    int outServerPort = ParamUtil.getInteger(
262                            actionRequest, "outServerPort");
263                    boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
264                    String outUserName = ParamUtil.getString(actionRequest, "outUserName");
265                    String outPassword = ParamUtil.getString(actionRequest, "outPassword");
266                    boolean allowAnonymous = ParamUtil.getBoolean(
267                            actionRequest, "allowAnonymous");
268                    boolean mailingListActive = ParamUtil.getBoolean(
269                            actionRequest, "mailingListActive");
270    
271                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
272                            actionRequest, "mergeWithParentCategory");
273    
274                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
275                            MBCategory.class.getName(), actionRequest);
276    
277                    if (categoryId <= 0) {
278                            if (PropsValues.
279                                            CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
280    
281                                    CaptchaUtil.check(actionRequest);
282                            }
283    
284                            // Add category
285    
286                            MBCategoryServiceUtil.addCategory(
287                                    parentCategoryId, name, description, displayStyle, emailAddress,
288                                    inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
289                                    inPassword, inReadInterval, outEmailAddress, outCustom,
290                                    outServerName, outServerPort, outUseSSL, outUserName,
291                                    outPassword, allowAnonymous, mailingListActive, serviceContext);
292                    }
293                    else {
294    
295                            // Update category
296    
297                            MBCategoryServiceUtil.updateCategory(
298                                    categoryId, parentCategoryId, name, description, displayStyle,
299                                    emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
300                                    inUserName, inPassword, inReadInterval, outEmailAddress,
301                                    outCustom, outServerName, outServerPort, outUseSSL, outUserName,
302                                    outPassword, allowAnonymous, mailingListActive,
303                                    mergeWithParentCategory, serviceContext);
304                    }
305            }
306    
307    }