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.journal.action;
016    
017    import com.liferay.portal.kernel.util.Constants;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.journal.NoSuchStructureException;
025    import com.liferay.portlet.journal.model.JournalArticle;
026    import com.liferay.portlet.journal.model.JournalFeed;
027    import com.liferay.portlet.journal.model.JournalStructure;
028    import com.liferay.portlet.journal.model.JournalTemplate;
029    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
030    import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
031    import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
032    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
033    import com.liferay.portlet.journal.util.JournalUtil;
034    
035    import javax.portlet.PortletRequest;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class ActionUtil {
043    
044            public static void getArticle(HttpServletRequest request) throws Exception {
045                    String cmd = ParamUtil.getString(request, Constants.CMD);
046    
047                    long groupId = ParamUtil.getLong(request, "groupId");
048                    long classNameId = ParamUtil.getLong(request, "classNameId");
049                    long classPK = ParamUtil.getLong(request, "classPK");
050                    String articleId = ParamUtil.getString(request, "articleId");
051                    String structureId = ParamUtil.getString(request, "structureId");
052    
053                    JournalArticle article = null;
054    
055                    if (!cmd.equals(Constants.ADD) && Validator.isNotNull(articleId)) {
056                            article = JournalArticleServiceUtil.getLatestArticle(
057                                    groupId, articleId, WorkflowConstants.STATUS_ANY);
058                    }
059                    else if ((classNameId > 0) && (classPK > 0)) {
060                            String className = PortalUtil.getClassName(classNameId);
061    
062                            article = JournalArticleServiceUtil.getLatestArticle(
063                                    groupId, className, classPK);
064                    }
065                    else if (Validator.isNotNull(structureId)) {
066                            JournalStructure structure = null;
067    
068                            try {
069                                    structure = JournalStructureServiceUtil.getStructure(
070                                            groupId, structureId);
071                            }
072                            catch (NoSuchStructureException nsse1) {
073                                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
074                                            WebKeys.THEME_DISPLAY);
075    
076                                    if (groupId == themeDisplay.getCompanyGroupId()) {
077                                            return;
078                                    }
079    
080                                    try {
081                                            structure = JournalStructureServiceUtil.getStructure(
082                                                    themeDisplay.getCompanyGroupId(), structureId);
083                                    }
084                                    catch (NoSuchStructureException nsse2) {
085                                            return;
086                                    }
087                            }
088    
089                            article = JournalArticleServiceUtil.getArticle(
090                                    structure.getGroupId(), JournalStructure.class.getName(),
091                                    structure.getId());
092    
093                            article.setNew(true);
094    
095                            article.setId(0);
096                            article.setClassNameId(0);
097                            article.setClassPK(0);
098                            article.setArticleId(null);
099                            article.setVersion(0);
100                    }
101    
102                    request.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
103            }
104    
105            public static void getArticle(PortletRequest portletRequest)
106                    throws Exception {
107    
108                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
109                            portletRequest);
110    
111                    getArticle(request);
112    
113                    JournalArticle article = (JournalArticle)portletRequest.getAttribute(
114                            WebKeys.JOURNAL_ARTICLE);
115    
116                    JournalUtil.addRecentArticle(portletRequest, article);
117            }
118    
119            public static void getFeed(HttpServletRequest request) throws Exception {
120                    long groupId = ParamUtil.getLong(request, "groupId");
121                    String feedId = ParamUtil.getString(request, "feedId");
122    
123                    JournalFeed feed = null;
124    
125                    if (Validator.isNotNull(feedId)) {
126                            feed = JournalFeedServiceUtil.getFeed(groupId, feedId);
127                    }
128    
129                    request.setAttribute(WebKeys.JOURNAL_FEED, feed);
130            }
131    
132            public static void getFeed(PortletRequest portletRequest) throws Exception {
133                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
134                            portletRequest);
135    
136                    getFeed(request);
137            }
138    
139            public static void getStructure(HttpServletRequest request)
140                    throws Exception {
141    
142                    long groupId = ParamUtil.getLong(request, "groupId");
143                    String structureId = ParamUtil.getString(request, "structureId");
144    
145                    JournalStructure structure = null;
146    
147                    if (Validator.isNotNull(structureId)) {
148                            structure = JournalStructureServiceUtil.getStructure(
149                                    groupId, structureId);
150                    }
151    
152                    request.setAttribute(WebKeys.JOURNAL_STRUCTURE, structure);
153            }
154    
155            public static void getStructure(PortletRequest portletRequest)
156                    throws Exception {
157    
158                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
159                            portletRequest);
160    
161                    getStructure(request);
162    
163                    JournalStructure structure =
164                            (JournalStructure)portletRequest.getAttribute(
165                                    WebKeys.JOURNAL_STRUCTURE);
166    
167                    JournalUtil.addRecentStructure(portletRequest, structure);
168            }
169    
170            public static void getTemplate(HttpServletRequest request)
171                    throws Exception {
172    
173                    long groupId = ParamUtil.getLong(request, "groupId");
174                    String templateId = ParamUtil.getString(request, "templateId");
175    
176                    JournalTemplate template = null;
177    
178                    if (Validator.isNotNull(templateId)) {
179                            template = JournalTemplateServiceUtil.getTemplate(
180                                    groupId, templateId);
181                    }
182    
183                    request.setAttribute(WebKeys.JOURNAL_TEMPLATE, template);
184            }
185    
186            public static void getTemplate(PortletRequest portletRequest)
187                    throws Exception {
188    
189                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
190                            portletRequest);
191    
192                    getTemplate(request);
193    
194                    JournalTemplate template = (JournalTemplate)portletRequest.getAttribute(
195                            WebKeys.JOURNAL_TEMPLATE);
196    
197                    JournalUtil.addRecentTemplate(portletRequest, template);
198            }
199    
200    }