001    /**
002     * Copyright (c) 2000-present 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.portlet.LiferayPortletRequest;
019    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
020    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
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.portlet.asset.model.AssetRenderer;
027    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
028    import com.liferay.portlet.asset.model.BaseDDMStructureClassTypeReader;
029    import com.liferay.portlet.asset.model.ClassTypeReader;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
031    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
032    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
033    import com.liferay.portlet.journal.NoSuchArticleException;
034    import com.liferay.portlet.journal.model.JournalArticle;
035    import com.liferay.portlet.journal.model.JournalArticleResource;
036    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
038    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
039    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
040    import com.liferay.portlet.journal.service.permission.JournalPermission;
041    
042    import java.util.Locale;
043    
044    import javax.portlet.PortletRequest;
045    import javax.portlet.PortletURL;
046    import javax.portlet.WindowState;
047    import javax.portlet.WindowStateException;
048    
049    /**
050     * @author Julio Camarero
051     * @author Juan Fern??ndez
052     * @author Raymond Aug??
053     * @author Sergio Gonz??lez
054     */
055    public class JournalArticleAssetRendererFactory
056            extends BaseAssetRendererFactory {
057    
058            public static final String TYPE = "content";
059    
060            public JournalArticleAssetRendererFactory() {
061                    setLinkable(true);
062                    setSupportsClassTypes(true);
063            }
064    
065            @Override
066            public AssetRenderer getAssetRenderer(long classPK, int type)
067                    throws PortalException {
068    
069                    JournalArticle article = null;
070    
071                    try {
072                            article = JournalArticleLocalServiceUtil.getArticle(classPK);
073                    }
074                    catch (NoSuchArticleException nsae1) {
075                            JournalArticleResource articleResource =
076                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
077                                            classPK);
078    
079                            boolean approvedArticleAvailable = true;
080    
081                            if (type == TYPE_LATEST_APPROVED) {
082                                    try {
083                                            article = JournalArticleLocalServiceUtil.getDisplayArticle(
084                                                    articleResource.getGroupId(),
085                                                    articleResource.getArticleId());
086                                    }
087                                    catch (NoSuchArticleException nsae2) {
088                                            approvedArticleAvailable = false;
089                                    }
090                            }
091    
092                            if ((type != TYPE_LATEST_APPROVED) || !approvedArticleAvailable) {
093                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
094                                            articleResource.getGroupId(),
095                                            articleResource.getArticleId(),
096                                            WorkflowConstants.STATUS_ANY);
097                            }
098                    }
099    
100                    JournalArticleAssetRenderer journalArticleAssetRenderer =
101                            new JournalArticleAssetRenderer(article);
102    
103                    journalArticleAssetRenderer.setAssetRendererType(type);
104    
105                    return journalArticleAssetRenderer;
106            }
107    
108            @Override
109            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
110                    throws PortalException {
111    
112                    JournalArticle article =
113                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
114                                    groupId, urlTitle);
115    
116                    return new JournalArticleAssetRenderer(article);
117            }
118    
119            @Override
120            public String getClassName() {
121                    return JournalArticle.class.getName();
122            }
123    
124            @Override
125            public ClassTypeReader getClassTypeReader() {
126                    return new BaseDDMStructureClassTypeReader(getClassName());
127            }
128    
129            @Override
130            public String getIconCssClass() {
131                    return "icon-file-2";
132            }
133    
134            @Override
135            public String getType() {
136                    return TYPE;
137            }
138    
139            @Override
140            public String getTypeName(Locale locale, long subtypeId) {
141                    try {
142                            DDMStructure ddmStructure =
143                                    DDMStructureLocalServiceUtil.getStructure(subtypeId);
144    
145                            return ddmStructure.getName(locale);
146                    }
147                    catch (Exception e) {
148                            return super.getTypeName(locale, subtypeId);
149                    }
150            }
151    
152            @Override
153            public PortletURL getURLAdd(
154                    LiferayPortletRequest liferayPortletRequest,
155                    LiferayPortletResponse liferayPortletResponse) {
156    
157                    PortletURL portletURL = liferayPortletResponse.createRenderURL(
158                            PortletKeys.JOURNAL);
159    
160                    portletURL.setParameter("struts_action", "/journal/edit_article");
161    
162                    return portletURL;
163            }
164    
165            @Override
166            public PortletURL getURLView(
167                    LiferayPortletResponse liferayPortletResponse,
168                    WindowState windowState) {
169    
170                    LiferayPortletURL liferayPortletURL =
171                            liferayPortletResponse.createLiferayPortletURL(
172                                    PortletKeys.JOURNAL, PortletRequest.RENDER_PHASE);
173    
174                    try {
175                            liferayPortletURL.setWindowState(windowState);
176                    }
177                    catch (WindowStateException wse) {
178                    }
179    
180                    return liferayPortletURL;
181            }
182    
183            @Override
184            public boolean hasAddPermission(
185                            PermissionChecker permissionChecker, long groupId, long classTypeId)
186                    throws Exception {
187    
188                    if (classTypeId == 0) {
189                            return false;
190                    }
191    
192                    if (!DDMStructurePermission.contains(
193                                    permissionChecker, classTypeId, ActionKeys.VIEW)) {
194    
195                            return false;
196                    }
197    
198                    return JournalPermission.contains(
199                            permissionChecker, groupId, ActionKeys.ADD_ARTICLE);
200            }
201    
202            @Override
203            public boolean hasPermission(
204                            PermissionChecker permissionChecker, long classPK, String actionId)
205                    throws Exception {
206    
207                    return JournalArticlePermission.contains(
208                            permissionChecker, classPK, actionId);
209            }
210    
211            @Override
212            public boolean isListable(long classPK) {
213                    JournalArticle article =
214                            JournalArticleLocalServiceUtil.fetchLatestArticle(
215                                    classPK, WorkflowConstants.STATUS_APPROVED, true);
216    
217                    if ((article != null) && article.isIndexable()) {
218                            return true;
219                    }
220    
221                    return false;
222            }
223    
224            @Override
225            protected String getIconPath(ThemeDisplay themeDisplay) {
226                    return themeDisplay.getPathThemeImages() + "/common/history.png";
227            }
228    
229    }