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.portlet.LiferayPortletURL;
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.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.DDMStructureServiceUtil;
036    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
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    import javax.portlet.WindowState;
053    import javax.portlet.WindowStateException;
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 =
071                            JournalArticleLocalServiceUtil.fetchJournalArticle(classPK);
072    
073                    if (article == null) {
074                            JournalArticleResource articleResource =
075                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
076                                            classPK);
077    
078                            if (type == TYPE_LATEST_APPROVED) {
079                                    article = JournalArticleLocalServiceUtil.fetchDisplayArticle(
080                                            articleResource.getGroupId(),
081                                            articleResource.getArticleId());
082                            }
083    
084                            if (article == null) {
085                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
086                                            articleResource.getGroupId(),
087                                            articleResource.getArticleId(),
088                                            WorkflowConstants.STATUS_ANY);
089                            }
090                    }
091    
092                    JournalArticleAssetRenderer journalArticleAssetRenderer =
093                            new JournalArticleAssetRenderer(article);
094    
095                    journalArticleAssetRenderer.setAssetRendererType(type);
096    
097                    return journalArticleAssetRenderer;
098            }
099    
100            @Override
101            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
102                    throws PortalException, SystemException {
103    
104                    JournalArticle article =
105                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
106                                    groupId, urlTitle);
107    
108                    return new JournalArticleAssetRenderer(article);
109            }
110    
111            @Override
112            public String getClassName() {
113                    return JournalArticle.class.getName();
114            }
115    
116            @Override
117            public List<Tuple> getClassTypeFieldNames(
118                            long classTypeId, Locale locale, int start, int end)
119                    throws Exception {
120    
121                    DDMStructure ddmStructure =
122                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
123    
124                    List<Tuple> fieldNames = getDDMStructureFieldNames(
125                            ddmStructure, locale);
126    
127                    return ListUtil.subList(fieldNames, start, end);
128            }
129    
130            @Override
131            public int getClassTypeFieldNamesCount(long classTypeId, Locale locale)
132                    throws Exception {
133    
134                    DDMStructure ddmStructure =
135                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
136    
137                    List<Tuple> fieldNames = getDDMStructureFieldNames(
138                            ddmStructure, locale);
139    
140                    return fieldNames.size();
141            }
142    
143            @Override
144            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
145                    throws Exception {
146    
147                    Map<Long, String> classTypes = new HashMap<Long, String>();
148    
149                    List<DDMStructure> ddmStructures =
150                            DDMStructureServiceUtil.getStructures(
151                                    groupIds,
152                                    PortalUtil.getClassNameId(JournalArticle.class.getName()));
153    
154                    for (DDMStructure ddmStructure : ddmStructures) {
155                            classTypes.put(
156                                    ddmStructure.getStructureId(), ddmStructure.getName(locale));
157                    }
158    
159                    return classTypes;
160            }
161    
162            @Override
163            public String getType() {
164                    return TYPE;
165            }
166    
167            @Override
168            public String getTypeName(Locale locale, boolean hasSubtypes) {
169                    if (hasSubtypes) {
170                            return LanguageUtil.get(locale, "basic-web-content");
171                    }
172    
173                    return super.getTypeName(locale, hasSubtypes);
174            }
175    
176            @Override
177            public PortletURL getURLAdd(
178                    LiferayPortletRequest liferayPortletRequest,
179                    LiferayPortletResponse liferayPortletResponse) {
180    
181                    PortletURL portletURL = liferayPortletResponse.createRenderURL(
182                            PortletKeys.JOURNAL);
183    
184                    portletURL.setParameter("struts_action", "/journal/edit_article");
185    
186                    return portletURL;
187            }
188    
189            @Override
190            public PortletURL getURLView(
191                    LiferayPortletResponse liferayPortletResponse,
192                    WindowState windowState) {
193    
194                    LiferayPortletURL liferayPortletURL =
195                            liferayPortletResponse.createLiferayPortletURL(
196                                    PortletKeys.JOURNAL, PortletRequest.RENDER_PHASE);
197    
198                    try {
199                            liferayPortletURL.setWindowState(windowState);
200                    }
201                    catch (WindowStateException wse) {
202                    }
203    
204                    return liferayPortletURL;
205            }
206    
207            @Override
208            public boolean hasAddPermission(
209                            PermissionChecker permissionChecker, long groupId, long classTypeId)
210                    throws Exception {
211    
212                    if ((classTypeId > 0) &&
213                            !DDMStructurePermission.contains(
214                                    permissionChecker, classTypeId, ActionKeys.VIEW)) {
215    
216                            return false;
217                    }
218    
219                    return JournalPermission.contains(
220                            permissionChecker, groupId, ActionKeys.ADD_ARTICLE);
221            }
222    
223            @Override
224            public boolean hasPermission(
225                            PermissionChecker permissionChecker, long classPK, String actionId)
226                    throws Exception {
227    
228                    return JournalArticlePermission.contains(
229                            permissionChecker, classPK, actionId);
230            }
231    
232            @Override
233            public boolean isLinkable() {
234                    return _LINKABLE;
235            }
236    
237            @Override
238            public boolean isListable(long classPK) throws SystemException {
239                    JournalArticle article =
240                            JournalArticleLocalServiceUtil.fetchLatestArticle(
241                                    classPK, WorkflowConstants.STATUS_APPROVED, true);
242    
243                    if ((article != null) && article.isIndexable()) {
244                            return true;
245                    }
246    
247                    return false;
248            }
249    
250            @Override
251            protected String getIconPath(ThemeDisplay themeDisplay) {
252                    return themeDisplay.getPathThemeImages() + "/common/history.png";
253            }
254    
255            private static final boolean _LINKABLE = true;
256    
257    }