1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.asset;
16  
17  import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
18  import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
19  import com.liferay.portal.security.permission.ActionKeys;
20  import com.liferay.portal.theme.ThemeDisplay;
21  import com.liferay.portal.util.PortletKeys;
22  import com.liferay.portal.util.WebKeys;
23  import com.liferay.portlet.asset.model.AssetRenderer;
24  import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
25  import com.liferay.portlet.journal.model.JournalArticle;
26  import com.liferay.portlet.journal.model.JournalArticleResource;
27  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
28  import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
29  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
30  import com.liferay.portlet.journal.service.permission.JournalPermission;
31  
32  import javax.portlet.PortletURL;
33  
34  /**
35   * <a href="JournalArticleAssetRendererFactory.java.html"><b><i>View Source</i>
36   * </b></a>
37   *
38   * @author Julio Camarero
39   */
40  public class JournalArticleAssetRendererFactory
41      extends BaseAssetRendererFactory {
42  
43      public static final String CLASS_NAME = JournalArticle.class.getName();
44  
45      public static final String TYPE = "content";
46  
47      public AssetRenderer getAssetRenderer(long classPK) throws Exception {
48          JournalArticleResource articleResource =
49              JournalArticleResourceLocalServiceUtil.getArticleResource(classPK);
50  
51          JournalArticle article =
52              JournalArticleLocalServiceUtil.getArticle(
53                  articleResource.getGroupId(), articleResource.getArticleId());
54  
55          return new JournalArticleAssetRenderer(article);
56      }
57  
58      public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
59          throws Exception {
60  
61          JournalArticle article = JournalArticleServiceUtil.getArticleByUrlTitle(
62              groupId, urlTitle);
63  
64          return new JournalArticleAssetRenderer(article);
65      }
66  
67      public String getClassName() {
68          return CLASS_NAME;
69      }
70  
71      public String getType() {
72          return TYPE;
73      }
74  
75      public PortletURL getURLAdd(
76          LiferayPortletRequest liferayPortletRequest,
77          LiferayPortletResponse liferayPortletResponse) {
78  
79          ThemeDisplay themeDisplay =
80              (ThemeDisplay)liferayPortletRequest.getAttribute(
81                  WebKeys.THEME_DISPLAY);
82  
83          PortletURL addAssetURL = null;
84  
85          if (JournalPermission.contains(
86                  themeDisplay.getPermissionChecker(),
87                  themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
88  
89              addAssetURL = liferayPortletResponse.createRenderURL(
90                  PortletKeys.JOURNAL);
91  
92              addAssetURL.setParameter("struts_action", "/journal/edit_article");
93              addAssetURL.setParameter(
94                  "groupId", String.valueOf(themeDisplay.getScopeGroupId()));
95          }
96  
97          return addAssetURL;
98      }
99  
100 }