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