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.portlet.LiferayPortletRequest;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.PortletURLFactoryUtil;
029    import com.liferay.portlet.asset.model.AssetRenderer;
030    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
031    import com.liferay.portlet.journal.NoSuchArticleException;
032    import com.liferay.portlet.journal.model.JournalArticle;
033    import com.liferay.portlet.journal.model.JournalArticleResource;
034    import com.liferay.portlet.journal.model.JournalStructure;
035    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
036    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
037    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
038    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
039    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
040    import com.liferay.portlet.journal.service.permission.JournalPermission;
041    import com.liferay.portlet.journal.service.permission.JournalStructurePermission;
042    
043    import java.util.HashMap;
044    import java.util.List;
045    import java.util.Locale;
046    import java.util.Map;
047    
048    import javax.portlet.PortletRequest;
049    import javax.portlet.PortletURL;
050    
051    import javax.servlet.http.HttpServletRequest;
052    
053    /**
054     * @author Julio Camarero
055     * @author Juan Fern??ndez
056     * @author Raymond Aug??
057     * @author Sergio Gonz??lez
058     */
059    public class JournalArticleAssetRendererFactory
060            extends BaseAssetRendererFactory {
061    
062            public static final String CLASS_NAME = JournalArticle.class.getName();
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                    return new JournalArticleAssetRenderer(article);
102            }
103    
104            @Override
105            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
106                    throws PortalException, SystemException {
107    
108                    JournalArticle article =
109                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
110                                    groupId, urlTitle);
111    
112                    return new JournalArticleAssetRenderer(article);
113            }
114    
115            @Override
116            public String getClassName() {
117                    return CLASS_NAME;
118            }
119    
120            @Override
121            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
122                    throws Exception {
123    
124                    Map<Long, String> classTypes = new HashMap<Long, String>();
125    
126                    for (long groupId : groupIds) {
127                            List<JournalStructure> structures =
128                                    JournalStructureLocalServiceUtil.getStructures(groupId);
129    
130                            for (JournalStructure structure : structures) {
131                                    classTypes.put(structure.getId(), structure.getName(locale));
132                            }
133                    }
134    
135                    return classTypes;
136            }
137    
138            @Override
139            public String getType() {
140                    return TYPE;
141            }
142    
143            @Override
144            public PortletURL getURLAdd(
145                            LiferayPortletRequest liferayPortletRequest,
146                            LiferayPortletResponse liferayPortletResponse)
147                    throws PortalException, SystemException {
148    
149                    HttpServletRequest request =
150                            liferayPortletRequest.getHttpServletRequest();
151    
152                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
153                            WebKeys.THEME_DISPLAY);
154    
155                    if (!JournalPermission.contains(
156                                    themeDisplay.getPermissionChecker(),
157                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
158    
159                            return null;
160                    }
161    
162                    long classTypeId = GetterUtil.getLong(
163                            liferayPortletRequest.getAttribute(
164                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
165    
166                    if ((classTypeId > 0) &&
167                            !JournalStructurePermission.contains(
168                                    themeDisplay.getPermissionChecker(), classTypeId,
169                                    ActionKeys.VIEW)) {
170    
171                            return null;
172                    }
173    
174                    PortletURL portletURL = PortletURLFactoryUtil.create(
175                            request, PortletKeys.JOURNAL, getControlPanelPlid(themeDisplay),
176                            PortletRequest.RENDER_PHASE);
177    
178                    portletURL.setParameter("struts_action", "/journal/edit_article");
179    
180                    return portletURL;
181            }
182    
183            @Override
184            public boolean hasPermission(
185                            PermissionChecker permissionChecker, long classPK, String actionId)
186                    throws Exception {
187    
188                    return JournalArticlePermission.contains(
189                            permissionChecker, classPK, actionId);
190            }
191    
192            @Override
193            public boolean isLinkable() {
194                    return _LINKABLE;
195            }
196    
197            @Override
198            protected String getIconPath(ThemeDisplay themeDisplay) {
199                    return themeDisplay.getPathThemeImages() + "/common/history.png";
200            }
201    
202            private static final boolean _LINKABLE = true;
203    
204    }