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