1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.tags.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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.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.tags.model.TagsAsset;
27  import com.liferay.portlet.tags.model.TagsAssetDisplay;
28  import com.liferay.portlet.tags.model.TagsAssetType;
29  import com.liferay.portlet.tags.service.base.TagsAssetServiceBaseImpl;
30  import com.liferay.portlet.tags.service.permission.TagsEntryPermission;
31  import com.liferay.util.RSSUtil;
32  
33  import com.sun.syndication.feed.synd.SyndContent;
34  import com.sun.syndication.feed.synd.SyndContentImpl;
35  import com.sun.syndication.feed.synd.SyndEntry;
36  import com.sun.syndication.feed.synd.SyndEntryImpl;
37  import com.sun.syndication.feed.synd.SyndFeed;
38  import com.sun.syndication.feed.synd.SyndFeedImpl;
39  import com.sun.syndication.io.FeedException;
40  
41  import java.util.ArrayList;
42  import java.util.Date;
43  import java.util.List;
44  
45  /**
46   * <a href="TagsAssetServiceImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Jorge Ferrer
50   * @author Bruno Farache
51   */
52  public class TagsAssetServiceImpl extends TagsAssetServiceBaseImpl {
53  
54      public void deleteAsset(long assetId)
55          throws PortalException, SystemException {
56  
57          tagsAssetLocalService.deleteAsset(assetId);
58      }
59  
60      public TagsAsset getAsset(long assetId)
61          throws PortalException, SystemException {
62  
63          return tagsAssetLocalService.getAsset(assetId);
64      }
65  
66      public List<TagsAsset> getAssets(
67              long groupId, long[] classNameIds, long[] entryIds,
68              long[] notEntryIds, boolean andOperator, String orderByCol1,
69              String orderByCol2, String orderByType1, String orderByType2,
70              boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
71              int start, int end)
72          throws PortalException, SystemException {
73  
74          long[][] viewableEntryIds = getViewableEntryIds(entryIds, notEntryIds);
75  
76          entryIds = viewableEntryIds[0];
77          notEntryIds = viewableEntryIds[1];
78  
79          return tagsAssetLocalService.getAssets(
80              groupId, classNameIds, entryIds, notEntryIds, andOperator,
81              orderByCol1, orderByCol2, orderByType1, orderByType2,
82              excludeZeroViewCount, publishDate, expirationDate, start, end);
83      }
84  
85      public int getAssetsCount(
86              long groupId, long[] classNameIds, long[] entryIds,
87              long[] notEntryIds, boolean andOperator,
88              boolean excludeZeroViewCount, Date publishDate, Date expirationDate)
89          throws PortalException, SystemException {
90  
91          long[][] viewableEntryIds = getViewableEntryIds(entryIds, notEntryIds);
92  
93          entryIds = viewableEntryIds[0];
94          notEntryIds = viewableEntryIds[1];
95  
96          return tagsAssetLocalService.getAssetsCount(
97              groupId, classNameIds, entryIds, notEntryIds, andOperator,
98              excludeZeroViewCount, publishDate, expirationDate);
99      }
100 
101     public String getAssetsRSS(
102             long groupId, long[] classNameIds, long[] entryIds,
103             long[] notEntryIds, boolean andOperator, String orderByCol1,
104             String orderByCol2, String orderByType1, String orderByType2,
105             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
106             int max, String type, double version, String displayStyle,
107             String feedURL, String entryURL)
108         throws PortalException, SystemException {
109 
110         Group group = groupPersistence.findByPrimaryKey(groupId);
111 
112         String name = group.getName();
113 
114         List<TagsAsset> assets = tagsAssetLocalService.getAssets(
115             groupId, classNameIds, entryIds, notEntryIds, andOperator,
116             orderByCol1, orderByCol2, orderByType1, orderByType2,
117             excludeZeroViewCount, publishDate, expirationDate, 0, max);
118 
119         return exportToRSS(
120             name, null, type, version, displayStyle, feedURL, entryURL, assets);
121     }
122 
123     public TagsAssetType[] getAssetTypes(String languageId) {
124         return tagsAssetLocalService.getAssetTypes(languageId);
125     }
126 
127     public TagsAssetDisplay[] getCompanyAssetDisplays(
128             long companyId, int start, int end, String languageId)
129         throws SystemException {
130 
131         return tagsAssetLocalService.getCompanyAssetDisplays(
132             companyId, start, end, languageId);
133     }
134 
135     public List<TagsAsset> getCompanyAssets(long companyId, int start, int end)
136         throws SystemException {
137 
138         return tagsAssetLocalService.getCompanyAssets(companyId, start, end);
139     }
140 
141     public int getCompanyAssetsCount(long companyId) throws SystemException {
142         return tagsAssetLocalService.getCompanyAssetsCount(companyId);
143     }
144 
145     public String getCompanyAssetsRSS(
146             long companyId, int max, String type, double version,
147             String displayStyle, String feedURL, String entryURL)
148         throws PortalException, SystemException {
149 
150         Company company = companyPersistence.findByPrimaryKey(companyId);
151 
152         String name = company.getName();
153 
154         List<TagsAsset> assets = getCompanyAssets(companyId, 0, max);
155 
156         return exportToRSS(
157             name, null, type, version, displayStyle, feedURL, entryURL, assets);
158     }
159 
160     public TagsAsset incrementViewCounter(String className, long classPK)
161         throws SystemException {
162 
163         return tagsAssetLocalService.incrementViewCounter(className, classPK);
164     }
165 
166     public TagsAssetDisplay[] searchAssetDisplays(
167             long companyId, String portletId, String keywords,
168             String languageId, int start, int end)
169         throws SystemException {
170 
171         return tagsAssetLocalService.searchAssetDisplays(
172             companyId, portletId, keywords, languageId, start, end);
173     }
174 
175     public int searchAssetDisplaysCount(
176             long companyId, String portletId, String keywords,
177             String languageId)
178         throws SystemException {
179 
180         return tagsAssetLocalService.searchAssetDisplaysCount(
181             companyId, portletId, keywords, languageId);
182     }
183 
184     public TagsAsset updateAsset(
185             long groupId, String className, long classPK,
186             String[] categoryNames, String[] entryNames, boolean visible,
187             Date startDate, Date endDate, Date publishDate, Date expirationDate,
188             String mimeType, String title, String description, String summary,
189             String url, int height, int width, Integer priority)
190         throws PortalException, SystemException {
191 
192         return tagsAssetLocalService.updateAsset(
193             getUserId(), groupId, className, classPK, categoryNames, entryNames,
194             visible, startDate, endDate, publishDate, expirationDate, mimeType,
195             title, description, summary, url, height, width, priority);
196     }
197 
198     protected String exportToRSS(
199             String name, String description, String type, double version,
200             String displayStyle, String feedURL, String entryURL,
201             List<TagsAsset> assets)
202         throws SystemException {
203 
204         SyndFeed syndFeed = new SyndFeedImpl();
205 
206         syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
207         syndFeed.setTitle(name);
208         syndFeed.setLink(feedURL);
209         syndFeed.setDescription(GetterUtil.getString(description, name));
210 
211         List<SyndEntry> entries = new ArrayList<SyndEntry>();
212 
213         syndFeed.setEntries(entries);
214 
215         for (TagsAsset asset : assets) {
216             String author = HtmlUtil.escape(
217                 PortalUtil.getUserName(asset.getUserId(), asset.getUserName()));
218 
219             String link = entryURL + "assetId=" + asset.getAssetId();
220 
221             String value = asset.getSummary();
222 
223             SyndEntry syndEntry = new SyndEntryImpl();
224 
225             syndEntry.setAuthor(author);
226             syndEntry.setTitle(asset.getTitle());
227             syndEntry.setLink(link);
228             syndEntry.setUri(syndEntry.getLink());
229             syndEntry.setPublishedDate(asset.getCreateDate());
230             syndEntry.setUpdatedDate(asset.getModifiedDate());
231 
232             SyndContent syndContent = new SyndContentImpl();
233 
234             syndContent.setType(RSSUtil.DEFAULT_ENTRY_TYPE);
235             syndContent.setValue(value);
236 
237             syndEntry.setDescription(syndContent);
238 
239             entries.add(syndEntry);
240         }
241 
242         try {
243             return RSSUtil.export(syndFeed);
244         }
245         catch (FeedException fe) {
246             throw new SystemException(fe);
247         }
248     }
249 
250     protected long[][] getViewableEntryIds(long[] entryIds, long[] notEntryIds)
251         throws PortalException, SystemException {
252 
253         List<Long> viewableList = new ArrayList<Long>();
254 
255         for (long entryId : entryIds) {
256             if (TagsEntryPermission.contains(
257                     getPermissionChecker(), entryId, ActionKeys.VIEW)) {
258 
259                 viewableList.add(entryId);
260             }
261             else {
262                 notEntryIds = ArrayUtil.append(notEntryIds, entryId);
263             }
264         }
265 
266         entryIds = new long[viewableList.size()];
267 
268         for (int i = 0; i < viewableList.size(); i++) {
269             entryIds[i] = viewableList.get(i).longValue();
270         }
271 
272         return new long[][] {entryIds, notEntryIds};
273     }
274 
275 }