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.asset.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.HtmlUtil;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.model.Company;
24  import com.liferay.portal.security.permission.ActionKeys;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
27  import com.liferay.portlet.asset.model.AssetEntry;
28  import com.liferay.portlet.asset.model.AssetEntryDisplay;
29  import com.liferay.portlet.asset.model.AssetRendererFactory;
30  import com.liferay.portlet.asset.service.base.AssetEntryServiceBaseImpl;
31  import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
32  import com.liferay.portlet.asset.service.permission.AssetTagPermission;
33  import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
34  import com.liferay.util.RSSUtil;
35  
36  import com.sun.syndication.feed.synd.SyndContent;
37  import com.sun.syndication.feed.synd.SyndContentImpl;
38  import com.sun.syndication.feed.synd.SyndEntry;
39  import com.sun.syndication.feed.synd.SyndEntryImpl;
40  import com.sun.syndication.feed.synd.SyndFeed;
41  import com.sun.syndication.feed.synd.SyndFeedImpl;
42  import com.sun.syndication.io.FeedException;
43  
44  import java.util.ArrayList;
45  import java.util.Date;
46  import java.util.List;
47  
48  /**
49   * <a href="AssetEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Jorge Ferrer
53   * @author Bruno Farache
54   */
55  public class AssetEntryServiceImpl extends AssetEntryServiceBaseImpl {
56  
57      public void deleteEntry(long entryId)
58          throws PortalException, SystemException {
59  
60          assetEntryLocalService.deleteEntry(entryId);
61      }
62  
63      public List<AssetEntry> getCompanyEntries(
64              long companyId, int start, int end)
65          throws SystemException {
66  
67          return assetEntryLocalService.getCompanyEntries(companyId, start, end);
68      }
69  
70      public int getCompanyEntriesCount(long companyId) throws SystemException {
71          return assetEntryLocalService.getCompanyEntriesCount(companyId);
72      }
73  
74      public String getCompanyEntriesRSS(
75              long companyId, int max, String type, double version,
76              String displayStyle, String feedURL, String tagURL)
77          throws PortalException, SystemException {
78  
79          Company company = companyPersistence.findByPrimaryKey(companyId);
80  
81          String name = company.getName();
82  
83          List<AssetEntry> entries = getCompanyEntries(companyId, 0, max);
84  
85          return exportToRSS(
86              name, null, type, version, displayStyle, feedURL, tagURL, entries);
87      }
88  
89      public AssetEntryDisplay[] getCompanyEntryDisplays(
90              long companyId, int start, int end, String languageId)
91          throws SystemException {
92  
93          return assetEntryLocalService.getCompanyEntryDisplays(
94              companyId, start, end, languageId);
95      }
96  
97      public List<AssetEntry> getEntries(AssetEntryQuery entryQuery)
98          throws PortalException, SystemException {
99  
100         filterQuery(entryQuery);
101 
102         return assetEntryLocalService.getEntries(entryQuery);
103     }
104 
105     public int getEntriesCount(AssetEntryQuery entryQuery)
106         throws PortalException, SystemException {
107 
108         filterQuery(entryQuery);
109 
110         return assetEntryLocalService.getEntriesCount(entryQuery);
111     }
112 
113     public String getEntriesRSS(
114             AssetEntryQuery entryQuery, String name, String type,
115             double version, String displayStyle, String feedURL, String tagURL)
116         throws PortalException, SystemException {
117 
118         filterQuery(entryQuery);
119 
120         List<AssetEntry> entries = assetEntryLocalService.getEntries(
121             entryQuery);
122 
123         return exportToRSS(
124             name, null, type, version, displayStyle, feedURL, tagURL, entries);
125     }
126 
127     public AssetEntry getEntry(long entryId)
128         throws PortalException, SystemException {
129 
130         return assetEntryLocalService.getEntry(entryId);
131     }
132 
133     public void incrementViewCounter(String className, long classPK)
134         throws PortalException, SystemException {
135 
136         assetEntryLocalService.incrementViewCounter(
137             getGuestOrUserId(), className, classPK);
138     }
139 
140     public AssetEntryDisplay[] searchEntryDisplays(
141             long companyId, String portletId, String keywords,
142             String languageId, int start, int end)
143         throws SystemException {
144 
145         return assetEntryLocalService.searchEntryDisplays(
146             companyId, portletId, keywords, languageId, start, end);
147     }
148 
149     public int searchEntryDisplaysCount(
150             long companyId, String portletId, String keywords,
151             String languageId)
152         throws SystemException {
153 
154         return assetEntryLocalService.searchEntryDisplaysCount(
155             companyId, portletId, keywords, languageId);
156     }
157 
158     public AssetEntry updateEntry(
159             long groupId, String className, long classPK, long[] categoryIds,
160             String[] tagNames, boolean visible, Date startDate, Date endDate,
161             Date publishDate, Date expirationDate, String mimeType,
162             String title, String description, String summary, String url,
163             int height, int width, Integer priority, boolean sync)
164         throws PortalException, SystemException {
165 
166         return assetEntryLocalService.updateEntry(
167             getUserId(), groupId, className, classPK, categoryIds, tagNames,
168             visible, startDate, endDate, publishDate, expirationDate, mimeType,
169             title, description, summary, url, height, width, priority, sync);
170     }
171 
172     protected String exportToRSS(
173             String name, String description, String type, double version,
174             String displayStyle, String feedURL, String tagURL,
175             List<AssetEntry> assetEntries)
176         throws SystemException {
177 
178         SyndFeed syndFeed = new SyndFeedImpl();
179 
180         syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
181         syndFeed.setTitle(name);
182         syndFeed.setLink(feedURL);
183         syndFeed.setDescription(GetterUtil.getString(description, name));
184 
185         List<SyndEntry> entries = new ArrayList<SyndEntry>();
186 
187         syndFeed.setEntries(entries);
188 
189         for (AssetEntry entry : assetEntries) {
190             AssetRendererFactory assetRendererFactory =
191                 AssetRendererFactoryRegistryUtil.
192                     getAssetRendererFactoryByClassName(entry.getClassName());
193 
194             String author = HtmlUtil.escape(
195                 PortalUtil.getUserName(entry.getUserId(), entry.getUserName()));
196 
197             StringBundler sb = new StringBundler(4);
198 
199             sb.append(tagURL);
200             sb.append(assetRendererFactory.getType());
201             sb.append("/id/");
202             sb.append(entry.getEntryId());
203 
204             String link = sb.toString();
205 
206             String value = entry.getSummary();
207 
208             SyndEntry syndEntry = new SyndEntryImpl();
209 
210             syndEntry.setAuthor(author);
211             syndEntry.setTitle(entry.getTitle());
212             syndEntry.setLink(link);
213             syndEntry.setUri(syndEntry.getLink());
214             syndEntry.setPublishedDate(entry.getCreateDate());
215             syndEntry.setUpdatedDate(entry.getModifiedDate());
216 
217             SyndContent syndContent = new SyndContentImpl();
218 
219             syndContent.setType(RSSUtil.DEFAULT_ENTRY_TYPE);
220             syndContent.setValue(value);
221 
222             syndEntry.setDescription(syndContent);
223 
224             entries.add(syndEntry);
225         }
226 
227         try {
228             return RSSUtil.export(syndFeed);
229         }
230         catch (FeedException fe) {
231             throw new SystemException(fe);
232         }
233     }
234 
235     protected long[] filterCategoryIds(long[] categoryIds)
236         throws PortalException, SystemException {
237 
238         List<Long> viewableCategoryIds = new ArrayList<Long>();
239 
240         for (long categoryId : categoryIds) {
241             if (AssetCategoryPermission.contains(
242                     getPermissionChecker(), categoryId, ActionKeys.VIEW)) {
243 
244                 viewableCategoryIds.add(categoryId);
245             }
246         }
247 
248         return ArrayUtil.toArray(
249             viewableCategoryIds.toArray(new Long[viewableCategoryIds.size()]));
250     }
251 
252     protected void filterQuery(AssetEntryQuery entryQuery)
253         throws PortalException, SystemException {
254 
255         entryQuery.setAllCategoryIds(filterCategoryIds(
256             entryQuery.getAllCategoryIds()));
257         entryQuery.setAnyCategoryIds(filterCategoryIds(
258             entryQuery.getAnyCategoryIds()));
259 
260         entryQuery.setAllTagIds(filterTagIds(entryQuery.getAllTagIds()));
261         entryQuery.setAnyTagIds(filterTagIds(entryQuery.getAnyTagIds()));
262     }
263 
264     protected long[] filterTagIds(long[] tagIds)
265         throws PortalException, SystemException {
266 
267         List<Long> viewableTagIds = new ArrayList<Long>();
268 
269         for (long tagId : tagIds) {
270             if (AssetTagPermission.contains(
271                     getPermissionChecker(), tagId, ActionKeys.VIEW)) {
272 
273                 viewableTagIds.add(tagId);
274             }
275         }
276 
277         return ArrayUtil.toArray(
278             viewableTagIds.toArray(new Long[viewableTagIds.size()]));
279     }
280 
281 }