001    /**
002     * Copyright (c) 2000-2012 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) &&
099                            (message.isInTrash() || message.isInTrashThread())) {
100    
101                            throw new NoSuchMessageException();
102                    }
103    
104                    request.setAttribute(WebKeys.MESSAGE_BOARDS_MESSAGE, message);
105            }
106    
107            public static void getMessage(PortletRequest portletRequest)
108                    throws Exception {
109    
110                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
111                            portletRequest);
112    
113                    getMessage(request);
114            }
115    
116            public static void getThreadMessage(HttpServletRequest request)
117                    throws Exception {
118    
119                    long threadId = ParamUtil.getLong(request, "threadId");
120    
121                    MBMessage message = null;
122    
123                    if (threadId > 0) {
124                            MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
125    
126                            message = MBMessageServiceUtil.getMessage(
127                                    thread.getRootMessageId());
128                    }
129    
130                    if ((message != null) &&
131                                    (message.isInTrash() || message.isInTrashThread())) {
132    
133                            throw new NoSuchMessageException();
134                    }
135    
136                    request.setAttribute(WebKeys.MESSAGE_BOARDS_MESSAGE, message);
137            }
138    
139            public static void getThreadMessage(PortletRequest portletRequest)
140                    throws Exception {
141    
142                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
143                            portletRequest);
144    
145                    getThreadMessage(request);
146            }
147    
148    }