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