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