001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.model.JournalArticle;
026    import com.liferay.portlet.journal.model.JournalArticleDisplay;
027    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
028    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
029    import com.liferay.util.portlet.PortletRequestUtil;
030    
031    import javax.portlet.PortletConfig;
032    import javax.portlet.PortletPreferences;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Raymond Augé
043     */
044    public class ViewAction extends WebContentAction {
045    
046            @Override
047            public ActionForward render(
048                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049                            RenderRequest renderRequest, RenderResponse renderResponse)
050                    throws Exception {
051    
052                    PortletPreferences preferences = renderRequest.getPreferences();
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    long groupId = ParamUtil.getLong(renderRequest, "groupId");
058    
059                    if (groupId <= 0) {
060                            groupId = GetterUtil.getLong(
061                                    preferences.getValue("groupId", StringPool.BLANK));
062                    }
063    
064                    String articleId = ParamUtil.getString(renderRequest, "articleId");
065                    String templateId = ParamUtil.getString(renderRequest, "templateId");
066    
067                    if (Validator.isNull(articleId)) {
068                            articleId = GetterUtil.getString(
069                                    preferences.getValue("articleId", StringPool.BLANK));
070                            templateId = GetterUtil.getString(
071                                    preferences.getValue("templateId", StringPool.BLANK));
072                    }
073    
074                    String viewMode = ParamUtil.getString(renderRequest, "viewMode");
075                    String languageId = LanguageUtil.getLanguageId(renderRequest);
076                    int page = ParamUtil.getInteger(renderRequest, "page", 1);
077                    String xmlRequest = PortletRequestUtil.toXML(
078                            renderRequest, renderResponse);
079    
080                    JournalArticle article = null;
081                    JournalArticleDisplay articleDisplay = null;
082    
083                    if ((groupId > 0) && Validator.isNotNull(articleId)) {
084                            try {
085                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
086                                            groupId, articleId, WorkflowConstants.STATUS_ANY);
087    
088                                    double version = article.getVersion();
089    
090                                    articleDisplay = JournalContentUtil.getDisplay(
091                                            groupId, articleId, version, templateId, viewMode,
092                                            languageId, themeDisplay, page, xmlRequest);
093                            }
094                            catch (Exception e) {
095                                    renderRequest.removeAttribute(WebKeys.JOURNAL_ARTICLE);
096    
097                                    articleDisplay = JournalContentUtil.getDisplay(
098                                            groupId, articleId, templateId, viewMode, languageId,
099                                            themeDisplay, page, xmlRequest);
100                            }
101                    }
102    
103                    if (article != null) {
104                            renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
105                    }
106    
107                    if (articleDisplay != null) {
108                            renderRequest.setAttribute(
109                                    WebKeys.JOURNAL_ARTICLE_DISPLAY, articleDisplay);
110                    }
111                    else {
112                            renderRequest.removeAttribute(WebKeys.JOURNAL_ARTICLE_DISPLAY);
113                    }
114    
115                    return mapping.findForward("portlet.journal_content.view");
116            }
117    
118    }