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