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