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