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