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