001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Map;
046    
047    import javax.portlet.PortletRequest;
048    import javax.portlet.PortletURL;
049    
050    import javax.servlet.http.HttpServletRequest;
051    
052    /**
053     * @author Julio Camarero
054     * @author Juan Fernández
055     * @author Raymond Augé
056     * @author Sergio González
057     */
058    public class JournalArticleAssetRendererFactory
059            extends BaseAssetRendererFactory {
060    
061            public static final String CLASS_NAME = JournalArticle.class.getName();
062    
063            public static final String TYPE = "content";
064    
065            public AssetRenderer getAssetRenderer(long classPK, int type)
066                    throws PortalException, SystemException {
067    
068                    JournalArticle article = null;
069    
070                    try {
071                            article = JournalArticleLocalServiceUtil.getArticle(classPK);
072                    }
073                    catch (NoSuchArticleException nsae) {
074                            JournalArticleResource articleResource =
075                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
076                                            classPK);
077    
078                            if (type == TYPE_LATEST_APPROVED) {
079                                    article = JournalArticleLocalServiceUtil.getDisplayArticle(
080                                            articleResource.getGroupId(),
081                                            articleResource.getArticleId());
082                            }
083                            else {
084                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
085                                            articleResource.getGroupId(),
086                                            articleResource.getArticleId(),
087                                            WorkflowConstants.STATUS_ANY);
088                            }
089                    }
090    
091                    return new JournalArticleAssetRenderer(article);
092            }
093    
094            @Override
095            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
096                    throws PortalException, SystemException {
097    
098                    JournalArticle article = JournalArticleServiceUtil.getArticleByUrlTitle(
099                            groupId, urlTitle);
100    
101                    return new JournalArticleAssetRenderer(article);
102            }
103    
104            public String getClassName() {
105                    return CLASS_NAME;
106            }
107    
108            @Override
109            public Map<Long, String> getClassTypes(long[] groupIds) throws Exception {
110                    Map<Long, String> classTypes = new HashMap<Long, String>();
111    
112                    for (long groupId : groupIds) {
113                            List<JournalStructure> structures =
114                                    JournalStructureLocalServiceUtil.getStructures(groupId);
115    
116                            for (JournalStructure structure : structures) {
117                                    classTypes.put(structure.getId(), structure.getName());
118                            }
119                    }
120    
121                    return classTypes;
122            }
123    
124            public String getType() {
125                    return TYPE;
126            }
127    
128            @Override
129            public PortletURL getURLAdd(
130                            LiferayPortletRequest liferayPortletRequest,
131                            LiferayPortletResponse liferayPortletResponse)
132                    throws PortalException, SystemException {
133    
134                    HttpServletRequest request =
135                            liferayPortletRequest.getHttpServletRequest();
136    
137                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
138                            WebKeys.THEME_DISPLAY);
139    
140                    if (!JournalPermission.contains(
141                                    themeDisplay.getPermissionChecker(),
142                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
143    
144                            return null;
145                    }
146    
147                    long classTypeId = GetterUtil.getLong(
148                            liferayPortletRequest.getAttribute(
149                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
150    
151                    if ((classTypeId > 0) &&
152                            !JournalStructurePermission.contains(
153                                    themeDisplay.getPermissionChecker(), classTypeId,
154                                    ActionKeys.VIEW)) {
155    
156                            return null;
157                    }
158    
159                    PortletURL portletURL = PortletURLFactoryUtil.create(
160                            request, PortletKeys.JOURNAL, getControlPanelPlid(themeDisplay),
161                            PortletRequest.RENDER_PHASE);
162    
163                    portletURL.setParameter("struts_action", "/journal/edit_article");
164    
165                    return portletURL;
166            }
167    
168            @Override
169            public boolean hasPermission(
170                            PermissionChecker permissionChecker, long classPK, String actionId)
171                    throws Exception {
172    
173                    return JournalArticlePermission.contains(
174                            permissionChecker, classPK, actionId);
175            }
176    
177            @Override
178            public boolean isLinkable() {
179                    return _LINKABLE;
180            }
181    
182            @Override
183            protected String getIconPath(ThemeDisplay themeDisplay) {
184                    return themeDisplay.getPathThemeImages() + "/common/history.png";
185            }
186    
187            private static final boolean _LINKABLE = true;
188    
189    }