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