1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tags.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ArrayUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.model.Company;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portlet.tags.model.TagsAsset;
34  import com.liferay.portlet.tags.model.TagsAssetDisplay;
35  import com.liferay.portlet.tags.model.TagsAssetType;
36  import com.liferay.portlet.tags.service.base.TagsAssetServiceBaseImpl;
37  import com.liferay.portlet.tags.service.permission.TagsEntryPermission;
38  import com.liferay.util.RSSUtil;
39  
40  import com.sun.syndication.feed.synd.SyndContent;
41  import com.sun.syndication.feed.synd.SyndContentImpl;
42  import com.sun.syndication.feed.synd.SyndEntry;
43  import com.sun.syndication.feed.synd.SyndEntryImpl;
44  import com.sun.syndication.feed.synd.SyndFeed;
45  import com.sun.syndication.feed.synd.SyndFeedImpl;
46  import com.sun.syndication.io.FeedException;
47  
48  import java.util.ArrayList;
49  import java.util.Date;
50  import java.util.List;
51  
52  /**
53   * <a href="TagsAssetServiceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   * @author Jorge Ferrer
57   * @author Bruno Farache
58   *
59   */
60  public class TagsAssetServiceImpl extends TagsAssetServiceBaseImpl {
61  
62      public void deleteAsset(long assetId)
63          throws PortalException, SystemException {
64  
65          tagsAssetLocalService.deleteAsset(assetId);
66      }
67  
68      public TagsAsset getAsset(long assetId)
69          throws PortalException, SystemException {
70  
71          return tagsAssetLocalService.getAsset(assetId);
72      }
73  
74      public List<TagsAsset> getAssets(
75              long groupId, long[] classNameIds, long[] entryIds,
76              long[] notEntryIds, boolean andOperator, String orderByCol1,
77              String orderByCol2, String orderByType1, String orderByType2,
78              boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
79              int start, int end)
80          throws PortalException, SystemException {
81  
82          long[][] viewableEntryIds = getViewableEntryIds(entryIds, notEntryIds);
83  
84          entryIds = viewableEntryIds[0];
85          notEntryIds = viewableEntryIds[1];
86  
87          return tagsAssetLocalService.getAssets(
88              groupId, classNameIds, entryIds, notEntryIds, andOperator,
89              orderByCol1, orderByCol2, orderByType1, orderByType2,
90              excludeZeroViewCount, publishDate, expirationDate, start, end);
91      }
92  
93      public int getAssetsCount(
94              long groupId, long[] classNameIds, long[] entryIds,
95              long[] notEntryIds, boolean andOperator,
96              boolean excludeZeroViewCount, Date publishDate, Date expirationDate)
97          throws PortalException, SystemException {
98  
99          long[][] viewableEntryIds = getViewableEntryIds(entryIds, notEntryIds);
100 
101         entryIds = viewableEntryIds[0];
102         notEntryIds = viewableEntryIds[1];
103 
104         return tagsAssetLocalService.getAssetsCount(
105             groupId, classNameIds, entryIds, notEntryIds, andOperator,
106             excludeZeroViewCount, publishDate, expirationDate);
107     }
108 
109     public String getAssetsRSS(
110             long groupId, long[] classNameIds, long[] entryIds,
111             long[] notEntryIds, boolean andOperator, String orderByCol1,
112             String orderByCol2, String orderByType1, String orderByType2,
113             boolean excludeZeroViewCount, Date publishDate, Date expirationDate,
114             int max, String type, double version, String displayStyle,
115             String feedURL, String entryURL)
116         throws PortalException, SystemException {
117 
118         Group group = groupPersistence.findByPrimaryKey(groupId);
119 
120         String name = group.getName();
121 
122         List<TagsAsset> assets = tagsAssetLocalService.getAssets(
123             groupId, classNameIds, entryIds, notEntryIds, andOperator,
124             orderByCol1, orderByCol2, orderByType1, orderByType2,
125             excludeZeroViewCount, publishDate, expirationDate, 0, max);
126 
127         return exportToRSS(
128             name, null, type, version, displayStyle, feedURL, entryURL, assets);
129     }
130 
131     public TagsAssetType[] getAssetTypes(String languageId) {
132         return tagsAssetLocalService.getAssetTypes(languageId);
133     }
134 
135     public TagsAssetDisplay[] getCompanyAssetDisplays(
136             long companyId, int start, int end, String languageId)
137         throws SystemException {
138 
139         return tagsAssetLocalService.getCompanyAssetDisplays(
140             companyId, start, end, languageId);
141     }
142 
143     public List<TagsAsset> getCompanyAssets(long companyId, int start, int end)
144         throws SystemException {
145 
146         return tagsAssetLocalService.getCompanyAssets(companyId, start, end);
147     }
148 
149     public int getCompanyAssetsCount(long companyId) throws SystemException {
150         return tagsAssetLocalService.getCompanyAssetsCount(companyId);
151     }
152 
153     public String getCompanyAssetsRSS(
154             long companyId, int max, String type, double version,
155             String displayStyle, String feedURL, String entryURL)
156         throws PortalException, SystemException {
157 
158         Company company = companyPersistence.findByPrimaryKey(companyId);
159 
160         String name = company.getName();
161 
162         List<TagsAsset> assets = getCompanyAssets(companyId, 0, max);
163 
164         return exportToRSS(
165             name, null, type, version, displayStyle, feedURL, entryURL, assets);
166     }
167 
168     public TagsAsset incrementViewCounter(String className, long classPK)
169         throws SystemException {
170 
171         return tagsAssetLocalService.incrementViewCounter(className, classPK);
172     }
173 
174     public TagsAssetDisplay[] searchAssetDisplays(
175             long companyId, String portletId, String keywords,
176             String languageId, int start, int end)
177         throws SystemException {
178 
179         return tagsAssetLocalService.searchAssetDisplays(
180             companyId, portletId, keywords, languageId, start, end);
181     }
182 
183     public int searchAssetDisplaysCount(
184             long companyId, String portletId, String keywords,
185             String languageId)
186         throws SystemException {
187 
188         return tagsAssetLocalService.searchAssetDisplaysCount(
189             companyId, portletId, keywords, languageId);
190     }
191 
192     public TagsAsset updateAsset(
193             long groupId, String className, long classPK,
194             String[] categoryNames, String[] entryNames, boolean visible,
195             Date startDate, Date endDate, Date publishDate, Date expirationDate,
196             String mimeType, String title, String description, String summary,
197             String url, int height, int width, Integer priority)
198         throws PortalException, SystemException {
199 
200         return tagsAssetLocalService.updateAsset(
201             getUserId(), groupId, className, classPK, categoryNames, entryNames,
202             visible, startDate, endDate, publishDate, expirationDate, mimeType,
203             title, description, summary, url, height, width, priority);
204     }
205 
206     protected String exportToRSS(
207             String name, String description, String type, double version,
208             String displayStyle, String feedURL, String entryURL,
209             List<TagsAsset> assets)
210         throws SystemException {
211 
212         SyndFeed syndFeed = new SyndFeedImpl();
213 
214         syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
215         syndFeed.setTitle(name);
216         syndFeed.setLink(feedURL);
217         syndFeed.setDescription(GetterUtil.getString(description, name));
218 
219         List<SyndEntry> entries = new ArrayList<SyndEntry>();
220 
221         syndFeed.setEntries(entries);
222 
223         for (TagsAsset asset : assets) {
224             String author = PortalUtil.getUserName(
225                 asset.getUserId(), asset.getUserName());
226 
227             String link = entryURL + "assetId=" + asset.getAssetId();
228 
229             String value = asset.getSummary();
230 
231             SyndEntry syndEntry = new SyndEntryImpl();
232 
233             syndEntry.setAuthor(author);
234             syndEntry.setTitle(asset.getTitle());
235             syndEntry.setLink(link);
236             syndEntry.setPublishedDate(asset.getCreateDate());
237 
238             SyndContent syndContent = new SyndContentImpl();
239 
240             syndContent.setType(RSSUtil.DEFAULT_ENTRY_TYPE);
241             syndContent.setValue(value);
242 
243             syndEntry.setDescription(syndContent);
244 
245             entries.add(syndEntry);
246         }
247 
248         try {
249             return RSSUtil.export(syndFeed);
250         }
251         catch (FeedException fe) {
252             throw new SystemException(fe);
253         }
254     }
255 
256     protected long[][] getViewableEntryIds(long[] entryIds, long[] notEntryIds)
257         throws PortalException, SystemException {
258 
259         List<Long> viewableList = new ArrayList<Long>();
260 
261         for (long entryId : entryIds) {
262             if (TagsEntryPermission.contains(
263                     getPermissionChecker(), entryId, ActionKeys.VIEW)) {
264 
265                 viewableList.add(entryId);
266             }
267             else {
268                 notEntryIds = ArrayUtil.append(notEntryIds, entryId);
269             }
270         }
271 
272         entryIds = new long[viewableList.size()];
273 
274         for (int i = 0; i < viewableList.size(); i++) {
275             entryIds[i] = viewableList.get(i).longValue();
276         }
277 
278         return new long[][] {entryIds, notEntryIds};
279     }
280 
281 }