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