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