001    /**
002     * Copyright (c) 2000-2010 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.journal.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.kernel.workflow.WorkflowConstants;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.journal.model.JournalArticle;
023    import com.liferay.portlet.journal.model.JournalFeed;
024    import com.liferay.portlet.journal.model.JournalStructure;
025    import com.liferay.portlet.journal.model.JournalTemplate;
026    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
027    import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
028    import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
029    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
030    import com.liferay.portlet.journal.util.JournalUtil;
031    
032    import javax.portlet.PortletRequest;
033    
034    import javax.servlet.http.HttpServletRequest;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class ActionUtil {
040    
041            public static void getArticle(HttpServletRequest request) throws Exception {
042                    long groupId = ParamUtil.getLong(request, "groupId");
043                    String articleId = ParamUtil.getString(request, "articleId");
044    
045                    JournalArticle article = null;
046    
047                    if (Validator.isNotNull(articleId)) {
048                            article = JournalArticleServiceUtil.getLatestArticle(
049                                    groupId, articleId, WorkflowConstants.STATUS_ANY);
050                    }
051    
052                    request.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
053            }
054    
055            public static void getArticle(PortletRequest portletRequest)
056                    throws Exception {
057    
058                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
059                            portletRequest);
060    
061                    getArticle(request);
062    
063                    JournalArticle article = (JournalArticle)portletRequest.getAttribute(
064                            WebKeys.JOURNAL_ARTICLE);
065    
066                    JournalUtil.addRecentArticle(portletRequest, article);
067            }
068    
069            public static void getFeed(HttpServletRequest request) throws Exception {
070                    long groupId = ParamUtil.getLong(request, "groupId");
071                    String feedId = ParamUtil.getString(request, "feedId");
072    
073                    JournalFeed feed = null;
074    
075                    if (Validator.isNotNull(feedId)) {
076                            feed = JournalFeedServiceUtil.getFeed(groupId, feedId);
077                    }
078    
079                    request.setAttribute(WebKeys.JOURNAL_FEED, feed);
080            }
081    
082            public static void getFeed(PortletRequest portletRequest) throws Exception {
083                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
084                            portletRequest);
085    
086                    getFeed(request);
087            }
088    
089            public static void getStructure(HttpServletRequest request)
090                    throws Exception {
091    
092                    long groupId = ParamUtil.getLong(request, "groupId");
093                    String structureId = ParamUtil.getString(request, "structureId");
094    
095                    JournalStructure structure = null;
096    
097                    if (Validator.isNotNull(structureId)) {
098                            structure = JournalStructureServiceUtil.getStructure(
099                                    groupId, structureId);
100                    }
101    
102                    request.setAttribute(WebKeys.JOURNAL_STRUCTURE, structure);
103            }
104    
105            public static void getStructure(PortletRequest portletRequest)
106                    throws Exception {
107    
108                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
109                            portletRequest);
110    
111                    getStructure(request);
112    
113                    JournalStructure structure =
114                            (JournalStructure)portletRequest.getAttribute(
115                                    WebKeys.JOURNAL_STRUCTURE);
116    
117                    JournalUtil.addRecentStructure(portletRequest, structure);
118            }
119    
120            public static void getTemplate(HttpServletRequest request)
121                    throws Exception {
122    
123                    long groupId = ParamUtil.getLong(request, "groupId");
124                    String templateId = ParamUtil.getString(request, "templateId");
125    
126                    JournalTemplate template = null;
127    
128                    if (Validator.isNotNull(templateId)) {
129                            template = JournalTemplateServiceUtil.getTemplate(
130                                    groupId, templateId);
131                    }
132    
133                    request.setAttribute(WebKeys.JOURNAL_TEMPLATE, template);
134            }
135    
136            public static void getTemplate(PortletRequest portletRequest)
137                    throws Exception {
138    
139                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
140                            portletRequest);
141    
142                    getTemplate(request);
143    
144                    JournalTemplate template = (JournalTemplate)portletRequest.getAttribute(
145                            WebKeys.JOURNAL_TEMPLATE);
146    
147                    JournalUtil.addRecentTemplate(portletRequest, template);
148            }
149    
150    }