001    /**
002     * Copyright (c) 2000-2012 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.language.LanguageUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
021    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.PortletURLFactoryUtil;
031    import com.liferay.portlet.asset.model.AssetRenderer;
032    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
035    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
036    import com.liferay.portlet.journal.NoSuchArticleException;
037    import com.liferay.portlet.journal.model.JournalArticle;
038    import com.liferay.portlet.journal.model.JournalArticleResource;
039    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
040    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
041    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
042    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
043    import com.liferay.portlet.journal.service.permission.JournalPermission;
044    
045    import java.util.HashMap;
046    import java.util.List;
047    import java.util.Locale;
048    import java.util.Map;
049    
050    import javax.portlet.PortletRequest;
051    import javax.portlet.PortletURL;
052    
053    /**
054     * @author Julio Camarero
055     * @author Juan Fernández
056     * @author Raymond Augé
057     * @author Sergio González
058     */
059    public class JournalArticleAssetRendererFactory
060            extends BaseAssetRendererFactory {
061    
062            public static final String CLASS_NAME = JournalArticle.class.getName();
063    
064            public static final String TYPE = "content";
065    
066            public AssetRenderer getAssetRenderer(long classPK, int type)
067                    throws PortalException, SystemException {
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                    return new JournalArticleAssetRenderer(article);
101            }
102    
103            @Override
104            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
105                    throws PortalException, SystemException {
106    
107                    JournalArticle article =
108                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
109                                    groupId, urlTitle);
110    
111                    return new JournalArticleAssetRenderer(article);
112            }
113    
114            public String getClassName() {
115                    return CLASS_NAME;
116            }
117    
118            @Override
119            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
120                    throws Exception {
121    
122                    Map<Long, String> classTypes = new HashMap<Long, String>();
123    
124                    for (long groupId : groupIds) {
125                            List<DDMStructure> ddmStructures =
126                                    DDMStructureLocalServiceUtil.getStructures(
127                                            groupId,
128                                            PortalUtil.getClassNameId(JournalArticle.class.getName()));
129    
130                            for (DDMStructure ddmStructure : ddmStructures) {
131                                    classTypes.put(
132                                            ddmStructure.getStructureId(),
133                                            ddmStructure.getName(locale));
134                            }
135                    }
136    
137                    return classTypes;
138            }
139    
140            public String getType() {
141                    return TYPE;
142            }
143    
144            @Override
145            public String getTypeName(Locale locale, boolean hasSubtypes) {
146                    if (hasSubtypes) {
147                            return LanguageUtil.get(locale, "basic-web-content");
148                    }
149    
150                    return super.getTypeName(locale, hasSubtypes);
151            }
152    
153            @Override
154            public PortletURL getURLAdd(
155                            LiferayPortletRequest liferayPortletRequest,
156                            LiferayPortletResponse liferayPortletResponse)
157                    throws PortalException, SystemException {
158    
159                    ThemeDisplay themeDisplay =
160                            (ThemeDisplay)liferayPortletRequest.getAttribute(
161                                    WebKeys.THEME_DISPLAY);
162    
163                    if (!JournalPermission.contains(
164                                    themeDisplay.getPermissionChecker(),
165                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
166    
167                            return null;
168                    }
169    
170                    long classTypeId = GetterUtil.getLong(
171                            liferayPortletRequest.getAttribute(
172                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
173    
174                    if ((classTypeId > 0) &&
175                            !DDMStructurePermission.contains(
176                                    themeDisplay.getPermissionChecker(), classTypeId,
177                                    ActionKeys.VIEW)) {
178    
179                            return null;
180                    }
181    
182                    PortletURL portletURL = PortletURLFactoryUtil.create(
183                            liferayPortletRequest, PortletKeys.JOURNAL,
184                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
185    
186                    portletURL.setParameter("struts_action", "/journal/edit_article");
187    
188                    return portletURL;
189            }
190    
191            @Override
192            public boolean hasPermission(
193                            PermissionChecker permissionChecker, long classPK, String actionId)
194                    throws Exception {
195    
196                    return JournalArticlePermission.contains(
197                            permissionChecker, classPK, actionId);
198            }
199    
200            @Override
201            public boolean isLinkable() {
202                    return _LINKABLE;
203            }
204    
205            @Override
206            protected String getIconPath(ThemeDisplay themeDisplay) {
207                    return themeDisplay.getPathThemeImages() + "/common/history.png";
208            }
209    
210            private static final boolean _LINKABLE = true;
211    
212    }