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.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 mapping, ActionForm form, PortletConfig portletConfig,
050                            RenderRequest renderRequest, RenderResponse renderResponse)
051                    throws Exception {
052    
053                    PortletPreferences preferences = renderRequest.getPreferences();
054    
055                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
056                            WebKeys.THEME_DISPLAY);
057    
058                    long groupId = ParamUtil.getLong(renderRequest, "groupId");
059    
060                    if (groupId <= 0) {
061                            groupId = GetterUtil.getLong(
062                                    preferences.getValue("groupId", StringPool.BLANK));
063                    }
064    
065                    String articleId = ParamUtil.getString(renderRequest, "articleId");
066                    String templateId = ParamUtil.getString(renderRequest, "templateId");
067    
068                    if (Validator.isNull(articleId)) {
069                            articleId = GetterUtil.getString(
070                                    preferences.getValue("articleId", StringPool.BLANK));
071                            templateId = GetterUtil.getString(
072                                    preferences.getValue("templateId", StringPool.BLANK));
073                    }
074    
075                    String viewMode = ParamUtil.getString(renderRequest, "viewMode");
076                    String languageId = LanguageUtil.getLanguageId(renderRequest);
077                    int page = ParamUtil.getInteger(renderRequest, "page", 1);
078                    String xmlRequest = PortletRequestUtil.toXML(
079                            renderRequest, renderResponse);
080    
081                    JournalArticle article = null;
082                    JournalArticleDisplay articleDisplay = null;
083    
084                    if ((groupId > 0) && Validator.isNotNull(articleId)) {
085                            try {
086                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
087                                            groupId, articleId, WorkflowConstants.STATUS_APPROVED);
088                            }
089                            catch (NoSuchArticleException nsae) {
090                            }
091    
092                            try {
093                                    if (article == null) {
094                                            article = JournalArticleLocalServiceUtil.getLatestArticle(
095                                                    groupId, articleId, WorkflowConstants.STATUS_ANY);
096                                    }
097    
098                                    double version = article.getVersion();
099    
100                                    articleDisplay = JournalContentUtil.getDisplay(
101                                            groupId, articleId, version, templateId, viewMode,
102                                            languageId, themeDisplay, page, xmlRequest);
103                            }
104                            catch (Exception e) {
105                                    renderRequest.removeAttribute(WebKeys.JOURNAL_ARTICLE);
106    
107                                    articleDisplay = JournalContentUtil.getDisplay(
108                                            groupId, articleId, templateId, viewMode, languageId,
109                                            themeDisplay, page, xmlRequest);
110                            }
111                    }
112    
113                    if (article != null) {
114                            renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
115                    }
116    
117                    if (articleDisplay != null) {
118                            renderRequest.setAttribute(
119                                    WebKeys.JOURNAL_ARTICLE_DISPLAY, articleDisplay);
120                    }
121                    else {
122                            renderRequest.removeAttribute(WebKeys.JOURNAL_ARTICLE_DISPLAY);
123                    }
124    
125                    return mapping.findForward("portlet.journal_content.view");
126            }
127    
128    }