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.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                            category = MBCategoryServiceUtil.getCategory(categoryId);
067                    }
068                    else {
069                            MBPermission.check(
070                                    themeDisplay.getPermissionChecker(),
071                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
072                    }
073    
074                    request.setAttribute(WebKeys.MESSAGE_BOARDS_CATEGORY, category);
075            }
076    
077            public static void getCategory(PortletRequest portletRequest)
078                    throws Exception {
079    
080                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
081                            portletRequest);
082    
083                    getCategory(request);
084            }
085    
086            public static void getMessage(HttpServletRequest request) throws Exception {
087                    long messageId = ParamUtil.getLong(request, "messageId");
088    
089                    MBMessage message = null;
090    
091                    if (messageId > 0) {
092                            message = MBMessageServiceUtil.getMessage(messageId);
093                    }
094    
095                    if ((message != null) && message.isInTrash()) {
096                            throw new NoSuchMessageException("{messageId=" + messageId + "}");
097                    }
098    
099                    request.setAttribute(WebKeys.MESSAGE_BOARDS_MESSAGE, message);
100            }
101    
102            public static void getMessage(PortletRequest portletRequest)
103                    throws Exception {
104    
105                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
106                            portletRequest);
107    
108                    getMessage(request);
109            }
110    
111            public static void getThreadMessage(HttpServletRequest request)
112                    throws Exception {
113    
114                    long threadId = ParamUtil.getLong(request, "threadId");
115    
116                    MBMessage message = null;
117    
118                    if (threadId > 0) {
119                            MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
120    
121                            message = MBMessageServiceUtil.getMessage(
122                                    thread.getRootMessageId());
123                    }
124    
125                    if ((message != null) && message.isInTrash()) {
126                            throw new NoSuchMessageException("{threadId=" + threadId + "}");
127                    }
128    
129                    request.setAttribute(WebKeys.MESSAGE_BOARDS_MESSAGE, message);
130            }
131    
132            public static void getThreadMessage(PortletRequest portletRequest)
133                    throws Exception {
134    
135                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
136                            portletRequest);
137    
138                    getThreadMessage(request);
139            }
140    
141    }