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