001    /**
002     * Copyright (c) 2000-2013 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.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    }