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.journalcontent.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.journal.NoSuchArticleException;
026    import com.liferay.portlet.journal.model.JournalArticle;
027    import com.liferay.portlet.journal.model.JournalArticleDisplay;
028    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
029    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
030    import com.liferay.util.portlet.PortletRequestUtil;
031    
032    import javax.portlet.PortletConfig;
033    import javax.portlet.PortletPreferences;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Raymond Aug??
044     */
045    public class ViewAction extends WebContentAction {
046    
047            @Override
048            public ActionForward render(
049                            ActionMapping actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, RenderRequest renderRequest,
051                            RenderResponse renderResponse)
052                    throws Exception {
053    
054                    PortletPreferences preferences = renderRequest.getPreferences();
055    
056                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
057                            WebKeys.THEME_DISPLAY);
058    
059                    long groupId = ParamUtil.getLong(renderRequest, "groupId");
060    
061                    if (groupId <= 0) {
062                            groupId = GetterUtil.getLong(
063                                    preferences.getValue("groupId", StringPool.BLANK));
064                    }
065    
066                    String articleId = ParamUtil.getString(renderRequest, "articleId");
067                    String templateId = ParamUtil.getString(renderRequest, "templateId");
068    
069                    if (Validator.isNull(articleId)) {
070                            articleId = GetterUtil.getString(
071                                    preferences.getValue("articleId", StringPool.BLANK));
072                            templateId = GetterUtil.getString(
073                                    preferences.getValue("templateId", StringPool.BLANK));
074                    }
075    
076                    String viewMode = ParamUtil.getString(renderRequest, "viewMode");
077                    String languageId = LanguageUtil.getLanguageId(renderRequest);
078                    int page = ParamUtil.getInteger(renderRequest, "page", 1);
079                    String xmlRequest = PortletRequestUtil.toXML(
080                            renderRequest, renderResponse);
081    
082                    JournalArticle article = null;
083                    JournalArticleDisplay articleDisplay = null;
084    
085                    if ((groupId > 0) && Validator.isNotNull(articleId)) {
086                            try {
087                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
088                                            groupId, articleId, WorkflowConstants.STATUS_APPROVED);
089                            }
090                            catch (NoSuchArticleException nsae) {
091                            }
092    
093                            try {
094                                    if (article == null) {
095                                            article = JournalArticleLocalServiceUtil.getLatestArticle(
096                                                    groupId, articleId, WorkflowConstants.STATUS_ANY);
097                                    }
098    
099                                    double version = article.getVersion();
100    
101                                    articleDisplay = JournalContentUtil.getDisplay(
102                                            groupId, articleId, version, templateId, viewMode,
103                                            languageId, themeDisplay, page, xmlRequest);
104                            }
105                            catch (Exception e) {
106                                    renderRequest.removeAttribute(WebKeys.JOURNAL_ARTICLE);
107    
108                                    articleDisplay = JournalContentUtil.getDisplay(
109                                            groupId, articleId, templateId, viewMode, languageId,
110                                            themeDisplay, page, xmlRequest);
111                            }
112                    }
113    
114                    if (article != null) {
115                            renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
116                    }
117    
118                    if (articleDisplay != null) {
119                            renderRequest.setAttribute(
120                                    WebKeys.JOURNAL_ARTICLE_DISPLAY, articleDisplay);
121                    }
122                    else {
123                            renderRequest.removeAttribute(WebKeys.JOURNAL_ARTICLE_DISPLAY);
124                    }
125    
126                    return actionMapping.findForward("portlet.journal_content.view");
127            }
128    
129    }