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.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
20  import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
21  import com.liferay.portal.security.permission.ActionKeys;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.PortletKeys;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.asset.model.AssetRenderer;
26  import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
27  import com.liferay.portlet.journal.model.JournalArticle;
28  import com.liferay.portlet.journal.model.JournalArticleResource;
29  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
30  import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
31  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
32  import com.liferay.portlet.journal.service.permission.JournalPermission;
33  
34  import javax.portlet.PortletURL;
35  
36  /**
37   * <a href="JournalArticleAssetRendererFactory.java.html"><b><i>View Source</i>
38   * </b></a>
39   *
40   * @author Julio Camarero
41   */
42  public class JournalArticleAssetRendererFactory
43      extends BaseAssetRendererFactory {
44  
45      public static final String CLASS_NAME = JournalArticle.class.getName();
46  
47      public static final String TYPE = "content";
48  
49      public AssetRenderer getAssetRenderer(long classPK)
50          throws PortalException, SystemException {
51  
52          JournalArticleResource articleResource =
53              JournalArticleResourceLocalServiceUtil.getArticleResource(classPK);
54  
55          JournalArticle article =
56              JournalArticleLocalServiceUtil.getArticle(
57                  articleResource.getGroupId(), articleResource.getArticleId());
58  
59          return new JournalArticleAssetRenderer(article);
60      }
61  
62      public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
63          throws PortalException, SystemException {
64  
65          JournalArticle article = JournalArticleServiceUtil.getArticleByUrlTitle(
66              groupId, urlTitle);
67  
68          return new JournalArticleAssetRenderer(article);
69      }
70  
71      public String getClassName() {
72          return CLASS_NAME;
73      }
74  
75      public String getType() {
76          return TYPE;
77      }
78  
79      public PortletURL getURLAdd(
80          LiferayPortletRequest liferayPortletRequest,
81          LiferayPortletResponse liferayPortletResponse) {
82  
83          ThemeDisplay themeDisplay =
84              (ThemeDisplay)liferayPortletRequest.getAttribute(
85                  WebKeys.THEME_DISPLAY);
86  
87          PortletURL addAssetURL = null;
88  
89          if (JournalPermission.contains(
90                  themeDisplay.getPermissionChecker(),
91                  themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
92  
93              addAssetURL = liferayPortletResponse.createRenderURL(
94                  PortletKeys.JOURNAL);
95  
96              addAssetURL.setParameter("struts_action", "/journal/edit_article");
97              addAssetURL.setParameter(
98                  "groupId", String.valueOf(themeDisplay.getScopeGroupId()));
99          }
100 
101         return addAssetURL;
102     }
103 
104 }