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