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.DDMStructureServiceUtil;
038    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
039    import com.liferay.portlet.journal.NoSuchArticleException;
040    import com.liferay.portlet.journal.model.JournalArticle;
041    import com.liferay.portlet.journal.model.JournalArticleResource;
042    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
043    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
044    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
045    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
046    import com.liferay.portlet.journal.service.permission.JournalPermission;
047    
048    import java.util.HashMap;
049    import java.util.List;
050    import java.util.Locale;
051    import java.util.Map;
052    
053    import javax.portlet.PortletRequest;
054    import javax.portlet.PortletURL;
055    import javax.portlet.WindowState;
056    import javax.portlet.WindowStateException;
057    
058    /**
059     * @author Julio Camarero
060     * @author Juan Fern??ndez
061     * @author Raymond Aug??
062     * @author Sergio Gonz??lez
063     */
064    public class JournalArticleAssetRendererFactory
065            extends BaseAssetRendererFactory {
066    
067            public static final String TYPE = "content";
068    
069            @Override
070            public AssetRenderer getAssetRenderer(long classPK, int type)
071                    throws PortalException, SystemException {
072    
073                    JournalArticle article = null;
074    
075                    try {
076                            article = JournalArticleLocalServiceUtil.getArticle(classPK);
077                    }
078                    catch (NoSuchArticleException nsae1) {
079                            JournalArticleResource articleResource =
080                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
081                                            classPK);
082    
083                            boolean approvedArticleAvailable = true;
084    
085                            if (type == TYPE_LATEST_APPROVED) {
086                                    try {
087                                            article = JournalArticleLocalServiceUtil.getDisplayArticle(
088                                                    articleResource.getGroupId(),
089                                                    articleResource.getArticleId());
090                                    }
091                                    catch (NoSuchArticleException nsae2) {
092                                            approvedArticleAvailable = false;
093                                    }
094                            }
095    
096                            if ((type != TYPE_LATEST_APPROVED) || !approvedArticleAvailable) {
097                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
098                                            articleResource.getGroupId(),
099                                            articleResource.getArticleId(),
100                                            WorkflowConstants.STATUS_ANY);
101                            }
102                    }
103    
104                    JournalArticleAssetRenderer journalArticleAssetRenderer =
105                            new JournalArticleAssetRenderer(article);
106    
107                    journalArticleAssetRenderer.setAssetRendererType(type);
108    
109                    return journalArticleAssetRenderer;
110            }
111    
112            @Override
113            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
114                    throws PortalException, SystemException {
115    
116                    JournalArticle article =
117                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
118                                    groupId, urlTitle);
119    
120                    return new JournalArticleAssetRenderer(article);
121            }
122    
123            @Override
124            public String getClassName() {
125                    return JournalArticle.class.getName();
126            }
127    
128            @Override
129            public List<Tuple> getClassTypeFieldNames(
130                            long classTypeId, Locale locale, int start, int end)
131                    throws Exception {
132    
133                    DDMStructure ddmStructure =
134                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
135    
136                    List<Tuple> fieldNames = getDDMStructureFieldNames(
137                            ddmStructure, locale);
138    
139                    return ListUtil.subList(fieldNames, start, end);
140            }
141    
142            @Override
143            public int getClassTypeFieldNamesCount(long classTypeId, Locale locale)
144                    throws Exception {
145    
146                    DDMStructure ddmStructure =
147                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
148    
149                    List<Tuple> fieldNames = getDDMStructureFieldNames(
150                            ddmStructure, locale);
151    
152                    return fieldNames.size();
153            }
154    
155            @Override
156            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
157                    throws Exception {
158    
159                    Map<Long, String> classTypes = new HashMap<Long, String>();
160    
161                    List<DDMStructure> ddmStructures =
162                            DDMStructureServiceUtil.getStructures(
163                                    groupIds,
164                                    PortalUtil.getClassNameId(JournalArticle.class.getName()));
165    
166                    for (DDMStructure ddmStructure : ddmStructures) {
167                            classTypes.put(
168                                    ddmStructure.getStructureId(), ddmStructure.getName(locale));
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 = liferayPortletResponse.createRenderURL(
218                            PortletKeys.JOURNAL);
219    
220                    portletURL.setParameter("struts_action", "/journal/edit_article");
221    
222                    return portletURL;
223            }
224    
225            @Override
226            public PortletURL getURLView(
227                    LiferayPortletResponse liferayPortletResponse,
228                    WindowState windowState) {
229    
230                    LiferayPortletURL liferayPortletURL =
231                            liferayPortletResponse.createLiferayPortletURL(
232                                    PortletKeys.JOURNAL, PortletRequest.RENDER_PHASE);
233    
234                    try {
235                            liferayPortletURL.setWindowState(windowState);
236                    }
237                    catch (WindowStateException wse) {
238                    }
239    
240                    return liferayPortletURL;
241            }
242    
243            @Override
244            public boolean hasPermission(
245                            PermissionChecker permissionChecker, long classPK, String actionId)
246                    throws Exception {
247    
248                    return JournalArticlePermission.contains(
249                            permissionChecker, classPK, actionId);
250            }
251    
252            @Override
253            public boolean isLinkable() {
254                    return _LINKABLE;
255            }
256    
257            @Override
258            protected String getIconPath(ThemeDisplay themeDisplay) {
259                    return themeDisplay.getPathThemeImages() + "/common/history.png";
260            }
261    
262            private static final boolean _LINKABLE = true;
263    
264    }