001    /**
002     * Copyright (c) 2000-2012 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            public AssetRenderer getAssetRenderer(long classPK, int type)
067                    throws PortalException, SystemException {
068    
069                    JournalArticle article = null;
070    
071                    try {
072                            article = JournalArticleLocalServiceUtil.getArticle(classPK);
073                    }
074                    catch (NoSuchArticleException nsae) {
075                            JournalArticleResource articleResource =
076                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
077                                            classPK);
078    
079                            boolean approvedArticleAvailable = true;
080    
081                            if (type == TYPE_LATEST_APPROVED) {
082                                    try {
083                                            article = JournalArticleLocalServiceUtil.getDisplayArticle(
084                                                    articleResource.getGroupId(),
085                                                    articleResource.getArticleId());
086                                    }
087                                    catch (NoSuchArticleException nsae1) {
088                                            approvedArticleAvailable = false;
089                                    }
090                            }
091    
092                            if ((type != TYPE_LATEST_APPROVED) || !approvedArticleAvailable) {
093                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
094                                            articleResource.getGroupId(),
095                                            articleResource.getArticleId(),
096                                            WorkflowConstants.STATUS_ANY);
097                            }
098                    }
099    
100                    return new JournalArticleAssetRenderer(article);
101            }
102    
103            @Override
104            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
105                    throws PortalException, SystemException {
106    
107                    JournalArticle article =
108                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
109                                    groupId, urlTitle);
110    
111                    return new JournalArticleAssetRenderer(article);
112            }
113    
114            public String getClassName() {
115                    return CLASS_NAME;
116            }
117    
118            @Override
119            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
120                    throws Exception {
121    
122                    Map<Long, String> classTypes = new HashMap<Long, String>();
123    
124                    for (long groupId : groupIds) {
125                            List<JournalStructure> structures =
126                                    JournalStructureLocalServiceUtil.getStructures(groupId);
127    
128                            for (JournalStructure structure : structures) {
129                                    classTypes.put(structure.getId(), structure.getName(locale));
130                            }
131                    }
132    
133                    return classTypes;
134            }
135    
136            public String getType() {
137                    return TYPE;
138            }
139    
140            @Override
141            public PortletURL getURLAdd(
142                            LiferayPortletRequest liferayPortletRequest,
143                            LiferayPortletResponse liferayPortletResponse)
144                    throws PortalException, SystemException {
145    
146                    HttpServletRequest request =
147                            liferayPortletRequest.getHttpServletRequest();
148    
149                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
150                            WebKeys.THEME_DISPLAY);
151    
152                    if (!JournalPermission.contains(
153                                    themeDisplay.getPermissionChecker(),
154                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
155    
156                            return null;
157                    }
158    
159                    long classTypeId = GetterUtil.getLong(
160                            liferayPortletRequest.getAttribute(
161                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
162    
163                    if ((classTypeId > 0) &&
164                            !JournalStructurePermission.contains(
165                                    themeDisplay.getPermissionChecker(), classTypeId,
166                                    ActionKeys.VIEW)) {
167    
168                            return null;
169                    }
170    
171                    PortletURL portletURL = PortletURLFactoryUtil.create(
172                            request, PortletKeys.JOURNAL, getControlPanelPlid(themeDisplay),
173                            PortletRequest.RENDER_PHASE);
174    
175                    portletURL.setParameter("struts_action", "/journal/edit_article");
176    
177                    return portletURL;
178            }
179    
180            @Override
181            public boolean hasPermission(
182                            PermissionChecker permissionChecker, long classPK, String actionId)
183                    throws Exception {
184    
185                    return JournalArticlePermission.contains(
186                            permissionChecker, classPK, actionId);
187            }
188    
189            @Override
190            public boolean isLinkable() {
191                    return _LINKABLE;
192            }
193    
194            @Override
195            protected String getIconPath(ThemeDisplay themeDisplay) {
196                    return themeDisplay.getPathThemeImages() + "/common/history.png";
197            }
198    
199            private static final boolean _LINKABLE = true;
200    
201    }