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