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