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.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.WebKeys;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portlet.journal.NoSuchArticleException;
023    import com.liferay.portlet.journal.model.JournalArticle;
024    import com.liferay.portlet.journal.model.JournalArticleConstants;
025    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
026    import com.liferay.portlet.journal.util.JournalUtil;
027    
028    import javax.portlet.PortletConfig;
029    import javax.portlet.PortletRequest;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Roberto D??az
039     */
040    public class PreviewArticleContentAction extends PortletAction {
041    
042            @Override
043            public ActionForward render(
044                            ActionMapping actionMapping, ActionForm actionForm,
045                            PortletConfig portletConfig, RenderRequest renderRequest,
046                            RenderResponse renderResponse)
047                    throws Exception {
048    
049                    try {
050                            getArticle(renderRequest);
051                    }
052                    catch (Exception e) {
053                            if (e instanceof NoSuchArticleException ||
054                                    e instanceof PrincipalException) {
055    
056                                    SessionErrors.add(renderRequest, e.getClass());
057    
058                                    return actionMapping.findForward("portlet.journal.error");
059                            }
060                            else {
061                                    throw e;
062                            }
063                    }
064    
065                    return actionMapping.findForward(
066                            getForward(
067                                    renderRequest, "portlet.journal.preview_article_content"));
068            }
069    
070            protected void getArticle(PortletRequest portletRequest) throws Exception {
071                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
072                    String articleId = ParamUtil.getString(portletRequest, "articleId");
073                    double version = ParamUtil.getDouble(
074                            portletRequest, "version", JournalArticleConstants.VERSION_DEFAULT);
075    
076                    JournalArticle article = JournalArticleServiceUtil.getArticle(
077                            groupId, articleId, version);
078    
079                    portletRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
080    
081                    JournalUtil.addRecentArticle(portletRequest, article);
082            }
083    
084    }