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