1   /**
2    * Copyright (c) 2000-2008 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.blogs.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.HtmlUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.model.Company;
32  import com.liferay.portal.model.Group;
33  import com.liferay.portal.model.Organization;
34  import com.liferay.portal.security.permission.ActionKeys;
35  import com.liferay.portal.service.permission.PortletPermissionUtil;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.PortletKeys;
39  import com.liferay.portal.util.PropsUtil;
40  import com.liferay.portlet.blogs.model.BlogsEntry;
41  import com.liferay.portlet.blogs.service.base.BlogsEntryServiceBaseImpl;
42  import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
43  import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
44  import com.liferay.util.RSSUtil;
45  
46  import com.sun.syndication.feed.synd.SyndContent;
47  import com.sun.syndication.feed.synd.SyndContentImpl;
48  import com.sun.syndication.feed.synd.SyndEntry;
49  import com.sun.syndication.feed.synd.SyndEntryImpl;
50  import com.sun.syndication.feed.synd.SyndFeed;
51  import com.sun.syndication.feed.synd.SyndFeedImpl;
52  import com.sun.syndication.io.FeedException;
53  
54  import java.io.IOException;
55  
56  import java.util.ArrayList;
57  import java.util.Iterator;
58  import java.util.List;
59  
60  /**
61   * <a href="BlogsEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class BlogsEntryServiceImpl extends BlogsEntryServiceBaseImpl {
67  
68      public BlogsEntry addEntry(
69              long plid, String title, String content, int displayDateMonth,
70              int displayDateDay, int displayDateYear, int displayDateHour,
71              int displayDateMinute, String[] tagsEntries,
72              boolean addCommunityPermissions, boolean addGuestPermissions,
73              ThemeDisplay themeDisplay)
74          throws PortalException, SystemException {
75  
76          PortletPermissionUtil.check(
77              getPermissionChecker(), plid, PortletKeys.BLOGS,
78              ActionKeys.ADD_ENTRY);
79  
80          return blogsEntryLocalService.addEntry(
81              getUserId(), plid, title, content, displayDateMonth, displayDateDay,
82              displayDateYear, displayDateHour, displayDateMinute, tagsEntries,
83              addCommunityPermissions, addGuestPermissions, themeDisplay);
84      }
85  
86      public BlogsEntry addEntry(
87              long plid, String title, String content, int displayDateMonth,
88              int displayDateDay, int displayDateYear, int displayDateHour,
89              int displayDateMinute, String[] tagsEntries,
90              String[] communityPermissions, String[] guestPermissions,
91              ThemeDisplay themeDisplay)
92          throws PortalException, SystemException {
93  
94          PortletPermissionUtil.check(
95              getPermissionChecker(), plid, PortletKeys.BLOGS,
96              ActionKeys.ADD_ENTRY);
97  
98          return blogsEntryLocalService.addEntry(
99              getUserId(), plid, title, content, displayDateMonth, displayDateDay,
100             displayDateYear, displayDateHour, displayDateMinute, tagsEntries,
101             communityPermissions, guestPermissions, themeDisplay);
102     }
103 
104     public void deleteEntry(long entryId)
105         throws PortalException, SystemException {
106 
107         BlogsEntryPermission.check(
108             getPermissionChecker(), entryId, ActionKeys.DELETE);
109 
110         blogsEntryLocalService.deleteEntry(entryId);
111     }
112 
113     public List<BlogsEntry> getCompanyEntries(long companyId, int max)
114         throws PortalException, SystemException {
115 
116         List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
117 
118         Iterator<BlogsEntry> itr = blogsEntryLocalService.getCompanyEntries(
119             companyId, 0, _MAX_END, new EntryDisplayDateComparator())
120                 .iterator();
121 
122         while (itr.hasNext() && (entries.size() < max)) {
123             BlogsEntry entry = itr.next();
124 
125             if (BlogsEntryPermission.contains(
126                     getPermissionChecker(), entry, ActionKeys.VIEW)) {
127 
128                 entries.add(entry);
129             }
130         }
131 
132         return entries;
133     }
134 
135     public String getCompanyEntriesRSS(
136             long companyId, int max, String type, double version,
137             String displayStyle, String feedURL, String entryURL)
138         throws PortalException, SystemException {
139 
140         Company company = companyPersistence.findByPrimaryKey(companyId);
141 
142         String name = company.getName();
143         String description = name;
144         List<BlogsEntry> blogsEntries = getCompanyEntries(companyId, max);
145 
146         return exportToRSS(
147             name, description, type, version, displayStyle, feedURL, entryURL,
148             blogsEntries);
149     }
150 
151     public BlogsEntry getEntry(long entryId)
152         throws PortalException, SystemException {
153 
154         BlogsEntryPermission.check(
155             getPermissionChecker(), entryId, ActionKeys.VIEW);
156 
157         return blogsEntryLocalService.getEntry(entryId);
158     }
159 
160     public BlogsEntry getEntry(long groupId, String urlTitle)
161         throws PortalException, SystemException {
162 
163         BlogsEntry entry = blogsEntryLocalService.getEntry(groupId, urlTitle);
164 
165         BlogsEntryPermission.check(
166             getPermissionChecker(), entry.getEntryId(), ActionKeys.VIEW);
167 
168         return entry;
169     }
170 
171     public List<BlogsEntry> getGroupEntries(long groupId, int max)
172         throws PortalException, SystemException {
173 
174         List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
175 
176         Iterator<BlogsEntry> itr = blogsEntryLocalService.getGroupEntries(
177             groupId, 0, _MAX_END).iterator();
178 
179         while (itr.hasNext() && (entries.size() < max)) {
180             BlogsEntry entry = itr.next();
181 
182             if (BlogsEntryPermission.contains(
183                     getPermissionChecker(), entry, ActionKeys.VIEW)) {
184 
185                 entries.add(entry);
186             }
187         }
188 
189         return entries;
190     }
191 
192     public String getGroupEntriesRSS(
193             long groupId, int max, String type, double version,
194             String displayStyle, String feedURL, String entryURL)
195         throws PortalException, SystemException {
196 
197         Group group = groupPersistence.findByPrimaryKey(groupId);
198 
199         String name = group.getDescriptiveName();
200         String description = name;
201         List<BlogsEntry> blogsEntries = getGroupEntries(groupId, max);
202 
203         return exportToRSS(
204             name, description, type, version, displayStyle, feedURL, entryURL,
205             blogsEntries);
206     }
207 
208     public List<BlogsEntry> getOrganizationEntries(long organizationId, int max)
209         throws PortalException, SystemException {
210 
211         List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
212 
213         Iterator<BlogsEntry> itr = blogsEntryFinder.findByOrganizationId(
214             organizationId, 0, _MAX_END).iterator();
215 
216         while (itr.hasNext() && (entries.size() < max)) {
217             BlogsEntry entry = itr.next();
218 
219             if (BlogsEntryPermission.contains(
220                     getPermissionChecker(), entry, ActionKeys.VIEW)) {
221 
222                 entries.add(entry);
223             }
224         }
225 
226         return entries;
227     }
228 
229     public String getOrganizationEntriesRSS(
230             long organizationId, int max, String type, double version,
231             String displayStyle, String feedURL, String entryURL)
232         throws PortalException, SystemException {
233 
234         Organization organization = organizationPersistence.findByPrimaryKey(
235             organizationId);
236 
237         String name = organization.getName();
238         String description = name;
239         List<BlogsEntry> blogsEntries = getOrganizationEntries(
240             organizationId, max);
241 
242         return exportToRSS(
243             name, description, type, version, displayStyle, feedURL, entryURL,
244             blogsEntries);
245     }
246 
247     public BlogsEntry updateEntry(
248             long entryId, String title, String content, int displayDateMonth,
249             int displayDateDay, int displayDateYear, int displayDateHour,
250             int displayDateMinute, String[] tagsEntries,
251             ThemeDisplay themeDisplay)
252         throws PortalException, SystemException {
253 
254         BlogsEntryPermission.check(
255             getPermissionChecker(), entryId, ActionKeys.UPDATE);
256 
257         return blogsEntryLocalService.updateEntry(
258             getUserId(), entryId, title, content, displayDateMonth,
259             displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
260             tagsEntries, themeDisplay);
261     }
262 
263     protected String exportToRSS(
264             String name, String description, String type, double version,
265             String displayStyle, String feedURL, String entryURL,
266             List<BlogsEntry> blogsEntries)
267         throws SystemException {
268 
269         SyndFeed syndFeed = new SyndFeedImpl();
270 
271         syndFeed.setFeedType(type + "_" + version);
272         syndFeed.setTitle(name);
273         syndFeed.setLink(feedURL);
274         syndFeed.setDescription(description);
275 
276         List<SyndEntry> entries = new ArrayList<SyndEntry>();
277 
278         syndFeed.setEntries(entries);
279 
280         for (BlogsEntry entry : blogsEntries) {
281             String author = PortalUtil.getUserName(
282                 entry.getUserId(), entry.getUserName());
283 
284             String link = entryURL;
285 
286             if (link.endsWith("/blogs/rss")) {
287                 link =
288                     link.substring(0, link.length() - 3) + entry.getUrlTitle();
289             }
290             else {
291                 if (!link.endsWith("?")) {
292                     link += "&";
293                 }
294 
295                 link += "entryId=" + entry.getEntryId();
296             }
297 
298             String value = null;
299 
300             if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
301                 value = StringUtil.shorten(
302                     HtmlUtil.extractText(entry.getContent()),
303                     _RSS_ABSTRACT_LENGTH, StringPool.BLANK);
304             }
305             else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
306                 value = StringPool.BLANK;
307             }
308             else {
309                 value = entry.getContent();
310             }
311 
312             SyndEntry syndEntry = new SyndEntryImpl();
313 
314             syndEntry.setAuthor(author);
315             syndEntry.setTitle(entry.getTitle());
316             syndEntry.setLink(link);
317             syndEntry.setPublishedDate(entry.getCreateDate());
318 
319             SyndContent syndContent = new SyndContentImpl();
320 
321             syndContent.setType("html");
322             syndContent.setValue(value);
323 
324             syndEntry.setDescription(syndContent);
325 
326             entries.add(syndEntry);
327         }
328 
329         try {
330             return RSSUtil.export(syndFeed);
331         }
332         catch (FeedException fe) {
333             throw new SystemException(fe);
334         }
335         catch (IOException ioe) {
336             throw new SystemException(ioe);
337         }
338     }
339 
340     private static final int _MAX_END = 200;
341 
342     private static final int _RSS_ABSTRACT_LENGTH = GetterUtil.getInteger(
343         PropsUtil.get(PropsUtil.BLOGS_RSS_ABSTRACT_LENGTH));
344 
345 }