001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.portal.kernel.captcha.CaptchaTextException;
018    import com.liferay.portal.kernel.captcha.CaptchaUtil;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.messageboards.CategoryNameException;
030    import com.liferay.portlet.messageboards.MailingListEmailAddressException;
031    import com.liferay.portlet.messageboards.MailingListInServerNameException;
032    import com.liferay.portlet.messageboards.MailingListInUserNameException;
033    import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
034    import com.liferay.portlet.messageboards.MailingListOutServerNameException;
035    import com.liferay.portlet.messageboards.MailingListOutUserNameException;
036    import com.liferay.portlet.messageboards.NoSuchCategoryException;
037    import com.liferay.portlet.messageboards.model.MBCategory;
038    import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    import javax.portlet.RenderRequest;
044    import javax.portlet.RenderResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     */
053    public class EditCategoryAction extends PortletAction {
054    
055            public void processAction(
056                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
057                            ActionRequest actionRequest, ActionResponse actionResponse)
058                    throws Exception {
059    
060                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                    try {
063                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064                                    updateCategory(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.DELETE)) {
067                                    deleteCategory(actionRequest);
068                            }
069                            else if (cmd.equals(Constants.SUBSCRIBE)) {
070                                    subscribeCategory(actionRequest);
071                            }
072                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
073                                    unsubscribeCategory(actionRequest);
074                            }
075    
076                            sendRedirect(actionRequest, actionResponse);
077                    }
078                    catch (Exception e) {
079                            if (e instanceof NoSuchCategoryException ||
080                                    e instanceof PrincipalException) {
081    
082                                    SessionErrors.add(actionRequest, e.getClass().getName());
083    
084                                    setForward(actionRequest, "portlet.message_boards.error");
085                            }
086                            else if (e instanceof CaptchaTextException ||
087                                             e instanceof CategoryNameException ||
088                                             e instanceof MailingListEmailAddressException ||
089                                             e instanceof MailingListInServerNameException ||
090                                             e instanceof MailingListInUserNameException ||
091                                             e instanceof MailingListOutEmailAddressException ||
092                                             e instanceof MailingListOutServerNameException ||
093                                             e instanceof MailingListOutUserNameException) {
094    
095                                    SessionErrors.add(actionRequest, e.getClass().getName());
096                            }
097                            else {
098                                    throw e;
099                            }
100                    }
101            }
102    
103            public ActionForward render(
104                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105                            RenderRequest renderRequest, RenderResponse renderResponse)
106                    throws Exception {
107    
108                    try {
109                            ActionUtil.getCategory(renderRequest);
110                    }
111                    catch (Exception e) {
112                            if (e instanceof NoSuchCategoryException ||
113                                    e instanceof PrincipalException) {
114    
115                                    SessionErrors.add(renderRequest, e.getClass().getName());
116    
117                                    return mapping.findForward("portlet.message_boards.error");
118                            }
119                            else {
120                                    throw e;
121                            }
122                    }
123    
124                    return mapping.findForward(
125                            getForward(renderRequest, "portlet.message_boards.edit_category"));
126            }
127    
128            protected void deleteCategory(ActionRequest actionRequest)
129                    throws Exception {
130    
131                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
132                            WebKeys.THEME_DISPLAY);
133    
134                    long groupId = themeDisplay.getScopeGroupId();
135                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
136    
137                    MBCategoryServiceUtil.deleteCategory(groupId, categoryId);
138            }
139    
140            protected void subscribeCategory(ActionRequest actionRequest)
141                    throws Exception {
142    
143                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
144                            WebKeys.THEME_DISPLAY);
145    
146                    long groupId = themeDisplay.getScopeGroupId();
147                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
148    
149                    MBCategoryServiceUtil.subscribeCategory(groupId, categoryId);
150            }
151    
152            protected void unsubscribeCategory(ActionRequest actionRequest)
153                    throws Exception {
154    
155                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
156                            WebKeys.THEME_DISPLAY);
157    
158                    long groupId = themeDisplay.getScopeGroupId();
159                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
160    
161                    MBCategoryServiceUtil.unsubscribeCategory(groupId, categoryId);
162            }
163    
164            protected void updateCategory(ActionRequest actionRequest)
165                    throws Exception {
166    
167                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
168    
169                    long parentCategoryId = ParamUtil.getLong(
170                            actionRequest, "parentCategoryId");
171                    String name = ParamUtil.getString(actionRequest, "name");
172                    String description = ParamUtil.getString(actionRequest, "description");
173    
174                    String emailAddress = ParamUtil.getString(
175                            actionRequest, "emailAddress");
176                    String inProtocol = ParamUtil.getString(actionRequest, "inProtocol");
177                    String inServerName = ParamUtil.getString(
178                            actionRequest, "inServerName");
179                    int inServerPort = ParamUtil.getInteger(actionRequest, "inServerPort");
180                    boolean inUseSSL = ParamUtil.getBoolean(actionRequest, "inUseSSL");
181                    String inUserName = ParamUtil.getString(actionRequest, "inUserName");
182                    String inPassword = ParamUtil.getString(actionRequest, "inPassword");
183                    int inReadInterval = ParamUtil.getInteger(
184                            actionRequest, "inReadInterval");
185                    String outEmailAddress = ParamUtil.getString(
186                            actionRequest, "outEmailAddress");
187                    boolean outCustom = ParamUtil.getBoolean(actionRequest, "outCustom");
188                    String outServerName = ParamUtil.getString(
189                            actionRequest, "outServerName");
190                    int outServerPort = ParamUtil.getInteger(
191                            actionRequest, "outServerPort");
192                    boolean outUseSSL = ParamUtil.getBoolean(actionRequest, "outUseSSL");
193                    String outUserName = ParamUtil.getString(actionRequest, "outUserName");
194                    String outPassword = ParamUtil.getString(actionRequest, "outPassword");
195                    boolean mailingListActive = ParamUtil.getBoolean(
196                            actionRequest, "mailingListActive");
197    
198                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
199                            actionRequest, "mergeWithParentCategory");
200    
201                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
202                            MBCategory.class.getName(), actionRequest);
203    
204                    if (categoryId <= 0) {
205                            if (PropsValues.
206                                            CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
207    
208                                    CaptchaUtil.check(actionRequest);
209                            }
210    
211                            // Add category
212    
213                            MBCategoryServiceUtil.addCategory(
214                                    parentCategoryId, name, description, emailAddress, inProtocol,
215                                    inServerName, inServerPort, inUseSSL, inUserName, inPassword,
216                                    inReadInterval, outEmailAddress, outCustom, outServerName,
217                                    outServerPort, outUseSSL, outUserName, outPassword,
218                                    mailingListActive, serviceContext);
219                    }
220                    else {
221    
222                            // Update category
223    
224                            MBCategoryServiceUtil.updateCategory(
225                                    categoryId, parentCategoryId, name, description, emailAddress,
226                                    inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
227                                    inPassword, inReadInterval, outEmailAddress, outCustom,
228                                    outServerName, outServerPort, outUseSSL, outUserName,
229                                    outPassword, mailingListActive, mergeWithParentCategory,
230                                    serviceContext);
231                    }
232            }
233    
234    }