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