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.util.ParamUtil;
018    import com.liferay.portal.security.auth.PrincipalException;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.messageboards.model.MBCategory;
024    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
025    import com.liferay.portlet.messageboards.model.MBMessage;
026    import com.liferay.portlet.messageboards.model.MBThread;
027    import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
028    import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
029    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
030    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
031    import com.liferay.portlet.messageboards.service.permission.MBPermission;
032    
033    import javax.portlet.PortletRequest;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class ActionUtil {
041    
042            public static void getCategory(HttpServletRequest request)
043                    throws Exception {
044    
045                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
046                            WebKeys.THEME_DISPLAY);
047    
048                    String topLink = ParamUtil.getString(request, "topLink");
049    
050                    if (topLink.equals("banned-users") &&
051                            !MBPermission.contains(
052                                    themeDisplay.getPermissionChecker(),
053                                    themeDisplay.getScopeGroupId(), ActionKeys.BAN_USER)) {
054    
055                            throw new PrincipalException();
056                    }
057    
058                    MBBanLocalServiceUtil.checkBan(
059                            themeDisplay.getScopeGroupId(), themeDisplay.getUserId());
060    
061                    long categoryId = ParamUtil.getLong(request, "mbCategoryId");
062    
063                    MBCategory category = null;
064    
065                    if ((categoryId > 0) &&
066                            (categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
067    
068                            category = MBCategoryServiceUtil.getCategory(categoryId);
069                    }
070                    else {
071                            MBPermission.check(
072                                    themeDisplay.getPermissionChecker(),
073                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
074                    }
075    
076                    request.setAttribute(WebKeys.MESSAGE_BOARDS_CATEGORY, category);
077            }
078    
079            public static void getCategory(PortletRequest portletRequest)
080                    throws Exception {
081    
082                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
083                            portletRequest);
084    
085                    getCategory(request);
086            }
087    
088            public static void getMessage(HttpServletRequest request) throws Exception {
089                    long messageId = ParamUtil.getLong(request, "messageId");
090    
091                    MBMessage message = null;
092    
093                    if (messageId > 0) {
094                            message = MBMessageServiceUtil.getMessage(messageId);
095                    }
096    
097                    request.setAttribute(WebKeys.MESSAGE_BOARDS_MESSAGE, message);
098            }
099    
100            public static void getMessage(PortletRequest portletRequest)
101                    throws Exception {
102    
103                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
104                            portletRequest);
105    
106                    getMessage(request);
107            }
108    
109            public static void getThreadMessage(HttpServletRequest request)
110                    throws Exception {
111    
112                    long threadId = ParamUtil.getLong(request, "threadId");
113    
114                    MBMessage message = null;
115    
116                    if (threadId > 0) {
117                            MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
118    
119                            message = MBMessageServiceUtil.getMessage(
120                                    thread.getRootMessageId());
121                    }
122    
123                    request.setAttribute(WebKeys.MESSAGE_BOARDS_MESSAGE, message);
124            }
125    
126            public static void getThreadMessage(PortletRequest portletRequest)
127                    throws Exception {
128    
129                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
130                            portletRequest);
131    
132                    getThreadMessage(request);
133            }
134    
135    }