001    /**
002     * Copyright (c) 2000-2010 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.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.AssetRenderer;
027    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
028    import com.liferay.portlet.journal.NoSuchArticleException;
029    import com.liferay.portlet.journal.model.JournalArticle;
030    import com.liferay.portlet.journal.model.JournalArticleResource;
031    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
032    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
033    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
034    import com.liferay.portlet.journal.service.permission.JournalPermission;
035    
036    import javax.portlet.PortletURL;
037    
038    /**
039     * @author Julio Camarero
040     * @author Juan Fernández
041     */
042    public class JournalArticleAssetRendererFactory
043            extends BaseAssetRendererFactory {
044    
045            public static final String CLASS_NAME = JournalArticle.class.getName();
046    
047            public static final String TYPE = "content";
048    
049            public AssetRenderer getAssetRenderer(long classPK, int type)
050                    throws PortalException, SystemException {
051    
052                    JournalArticle article = null;
053    
054                    try {
055                            article = JournalArticleLocalServiceUtil.getArticle(classPK);
056                    }
057                    catch (NoSuchArticleException nsae) {
058                            JournalArticleResource articleResource =
059                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
060                                            classPK);
061    
062                            int status = WorkflowConstants.STATUS_ANY;
063    
064                            if (type == TYPE_LATEST_APPROVED) {
065                                    status = WorkflowConstants.STATUS_APPROVED;
066                            }
067    
068                            article =
069                                    JournalArticleLocalServiceUtil.getLatestArticle(
070                                            articleResource.getGroupId(),
071                                            articleResource.getArticleId(), status);
072                    }
073    
074                    return new JournalArticleAssetRenderer(article);
075            }
076    
077            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
078                    throws PortalException, SystemException {
079    
080                    JournalArticle article = JournalArticleServiceUtil.getArticleByUrlTitle(
081                            groupId, urlTitle);
082    
083                    return new JournalArticleAssetRenderer(article);
084            }
085    
086            public String getClassName() {
087                    return CLASS_NAME;
088            }
089    
090            public String getType() {
091                    return TYPE;
092            }
093    
094            public PortletURL getURLAdd(
095                    LiferayPortletRequest liferayPortletRequest,
096                    LiferayPortletResponse liferayPortletResponse) {
097    
098                    ThemeDisplay themeDisplay =
099                            (ThemeDisplay)liferayPortletRequest.getAttribute(
100                                    WebKeys.THEME_DISPLAY);
101    
102                    PortletURL addAssetURL = null;
103    
104                    if (JournalPermission.contains(
105                                    themeDisplay.getPermissionChecker(),
106                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
107    
108                            addAssetURL = liferayPortletResponse.createRenderURL(
109                                    PortletKeys.JOURNAL);
110    
111                            addAssetURL.setParameter("struts_action", "/journal/edit_article");
112                            addAssetURL.setParameter(
113                                    "groupId", String.valueOf(themeDisplay.getScopeGroupId()));
114                    }
115    
116                    return addAssetURL;
117            }
118    
119            protected String getIconPath(ThemeDisplay themeDisplay) {
120                    return themeDisplay.getPathThemeImages() + "/common/history.png";
121            }
122    
123    }