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