001
014
015 package com.liferay.portlet.blogs.service.impl;
016
017 import com.liferay.blogs.kernel.model.BlogsEntry;
018 import com.liferay.blogs.kernel.util.comparator.EntryDisplayDateComparator;
019 import com.liferay.portal.kernel.dao.orm.QueryDefinition;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.model.Company;
025 import com.liferay.portal.kernel.model.Group;
026 import com.liferay.portal.kernel.model.Organization;
027 import com.liferay.portal.kernel.security.permission.ActionKeys;
028 import com.liferay.portal.kernel.service.ServiceContext;
029 import com.liferay.portal.kernel.servlet.taglib.ui.ImageSelector;
030 import com.liferay.portal.kernel.theme.ThemeDisplay;
031 import com.liferay.portal.kernel.util.FileUtil;
032 import com.liferay.portal.kernel.util.HtmlUtil;
033 import com.liferay.portal.kernel.util.MimeTypesUtil;
034 import com.liferay.portal.kernel.util.OrderByComparator;
035 import com.liferay.portal.kernel.util.PortalUtil;
036 import com.liferay.portal.kernel.util.StringBundler;
037 import com.liferay.portal.kernel.util.StringPool;
038 import com.liferay.portal.kernel.util.StringUtil;
039 import com.liferay.portal.kernel.util.Validator;
040 import com.liferay.portal.kernel.workflow.WorkflowConstants;
041 import com.liferay.portal.util.PropsValues;
042 import com.liferay.portlet.blogs.service.base.BlogsEntryServiceBaseImpl;
043 import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
044 import com.liferay.portlet.blogs.service.permission.BlogsPermission;
045 import com.liferay.util.RSSUtil;
046
047 import com.sun.syndication.feed.synd.SyndContent;
048 import com.sun.syndication.feed.synd.SyndContentImpl;
049 import com.sun.syndication.feed.synd.SyndEntry;
050 import com.sun.syndication.feed.synd.SyndEntryImpl;
051 import com.sun.syndication.feed.synd.SyndFeed;
052 import com.sun.syndication.feed.synd.SyndFeedImpl;
053 import com.sun.syndication.feed.synd.SyndLink;
054 import com.sun.syndication.feed.synd.SyndLinkImpl;
055 import com.sun.syndication.io.FeedException;
056
057 import java.io.IOException;
058 import java.io.InputStream;
059
060 import java.util.ArrayList;
061 import java.util.Date;
062 import java.util.List;
063
064
072 public class BlogsEntryServiceImpl extends BlogsEntryServiceBaseImpl {
073
074
080 @Deprecated
081 @Override
082 public BlogsEntry addEntry(
083 String title, String description, String content,
084 int displayDateMonth, int displayDateDay, int displayDateYear,
085 int displayDateHour, int displayDateMinute, boolean allowPingbacks,
086 boolean allowTrackbacks, String[] trackbacks, boolean smallImage,
087 String smallImageURL, String smallImageFileName,
088 InputStream smallImageInputStream, ServiceContext serviceContext)
089 throws PortalException {
090
091 BlogsPermission.check(
092 getPermissionChecker(), serviceContext.getScopeGroupId(),
093 ActionKeys.ADD_ENTRY);
094
095 ImageSelector coverImageImageSelector = null;
096 ImageSelector smallImageImageSelector = null;
097
098 if (smallImage) {
099 if (Validator.isNotNull(smallImageFileName) &&
100 (smallImageInputStream != null)) {
101
102 try {
103 byte[] bytes = FileUtil.getBytes(smallImageInputStream);
104
105 smallImageImageSelector = new ImageSelector(
106 bytes, smallImageFileName,
107 MimeTypesUtil.getContentType(smallImageFileName), null);
108 }
109 catch (IOException ioe) {
110 _log.error("Unable to create image selector", ioe);
111 }
112 }
113 else if (Validator.isNotNull(smallImageURL)) {
114 smallImageImageSelector = new ImageSelector(smallImageURL);
115 }
116 }
117
118 return addEntry(
119 title, StringPool.BLANK, description, content, displayDateMonth,
120 displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
121 allowPingbacks, allowTrackbacks, trackbacks, StringPool.BLANK,
122 coverImageImageSelector, smallImageImageSelector, serviceContext);
123 }
124
125 @Override
126 public BlogsEntry addEntry(
127 String title, String subtitle, String description, String content,
128 int displayDateMonth, int displayDateDay, int displayDateYear,
129 int displayDateHour, int displayDateMinute, boolean allowPingbacks,
130 boolean allowTrackbacks, String[] trackbacks,
131 String coverImageCaption, ImageSelector coverImageImageSelector,
132 ImageSelector smallImageImageSelector,
133 ServiceContext serviceContext)
134 throws PortalException {
135
136 BlogsPermission.check(
137 getPermissionChecker(), serviceContext.getScopeGroupId(),
138 ActionKeys.ADD_ENTRY);
139
140 return blogsEntryLocalService.addEntry(
141 getUserId(), title, subtitle, description, content,
142 displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
143 displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
144 coverImageCaption, coverImageImageSelector, smallImageImageSelector,
145 serviceContext);
146 }
147
148 @Override
149 public void deleteEntry(long entryId) throws PortalException {
150 BlogsEntryPermission.check(
151 getPermissionChecker(), entryId, ActionKeys.DELETE);
152
153 blogsEntryLocalService.deleteEntry(entryId);
154 }
155
156 @Override
157 public List<BlogsEntry> getCompanyEntries(
158 long companyId, Date displayDate, int status, int max)
159 throws PortalException {
160
161 List<BlogsEntry> entries = new ArrayList<>();
162
163 boolean listNotExhausted = true;
164
165 QueryDefinition<BlogsEntry> queryDefinition = new QueryDefinition<>(
166 status, false, 0, 0, new EntryDisplayDateComparator());
167
168 if (status == WorkflowConstants.STATUS_ANY) {
169 queryDefinition.setStatus(WorkflowConstants.STATUS_IN_TRASH, true);
170 }
171
172 while ((entries.size() < max) && listNotExhausted) {
173 queryDefinition.setEnd(queryDefinition.getStart() + max);
174
175 List<BlogsEntry> entryList =
176 blogsEntryLocalService.getCompanyEntries(
177 companyId, displayDate, queryDefinition);
178
179 queryDefinition.setStart(queryDefinition.getStart() + max);
180
181 listNotExhausted = (entryList.size() == max);
182
183 for (BlogsEntry entry : entryList) {
184 if (entries.size() >= max) {
185 break;
186 }
187
188 if (BlogsEntryPermission.contains(
189 getPermissionChecker(), entry, ActionKeys.VIEW)) {
190
191 entries.add(entry);
192 }
193 }
194 }
195
196 return entries;
197 }
198
199 @Override
200 public String getCompanyEntriesRSS(
201 long companyId, Date displayDate, int status, int max, String type,
202 double version, String displayStyle, String feedURL,
203 String entryURL, ThemeDisplay themeDisplay)
204 throws PortalException {
205
206 Company company = companyPersistence.findByPrimaryKey(companyId);
207
208 String name = company.getName();
209 List<BlogsEntry> blogsEntries = getCompanyEntries(
210 companyId, displayDate, status, max);
211
212 return exportToRSS(
213 name, name, type, version, displayStyle, feedURL, entryURL,
214 blogsEntries, themeDisplay);
215 }
216
217 @Override
218 public BlogsEntry getEntry(long entryId) throws PortalException {
219 BlogsEntryPermission.check(
220 getPermissionChecker(), entryId, ActionKeys.VIEW);
221
222 return blogsEntryLocalService.getEntry(entryId);
223 }
224
225 @Override
226 public BlogsEntry getEntry(long groupId, String urlTitle)
227 throws PortalException {
228
229 BlogsEntry entry = blogsEntryLocalService.getEntry(groupId, urlTitle);
230
231 BlogsEntryPermission.check(
232 getPermissionChecker(), entry.getEntryId(), ActionKeys.VIEW);
233
234 return entry;
235 }
236
237 @Override
238 public List<BlogsEntry> getGroupEntries(
239 long groupId, Date displayDate, int status, int max) {
240
241 return getGroupEntries(groupId, displayDate, status, 0, max);
242 }
243
244 @Override
245 public List<BlogsEntry> getGroupEntries(
246 long groupId, Date displayDate, int status, int start, int end) {
247
248 if (status == WorkflowConstants.STATUS_ANY) {
249 return blogsEntryPersistence.filterFindByG_LtD_NotS(
250 groupId, displayDate, WorkflowConstants.STATUS_IN_TRASH, start,
251 end);
252 }
253 else {
254 return blogsEntryPersistence.filterFindByG_LtD_S(
255 groupId, displayDate, status, start, end);
256 }
257 }
258
259 @Override
260 public List<BlogsEntry> getGroupEntries(long groupId, int status, int max) {
261 return getGroupEntries(groupId, status, 0, max);
262 }
263
264 @Override
265 public List<BlogsEntry> getGroupEntries(
266 long groupId, int status, int start, int end) {
267
268 if (status == WorkflowConstants.STATUS_ANY) {
269 return blogsEntryPersistence.filterFindByG_NotS(
270 groupId, WorkflowConstants.STATUS_IN_TRASH, start, end);
271 }
272 else {
273 return blogsEntryPersistence.filterFindByG_S(
274 groupId, status, start, end);
275 }
276 }
277
278 @Override
279 public List<BlogsEntry> getGroupEntries(
280 long groupId, int status, int start, int end,
281 OrderByComparator<BlogsEntry> obc) {
282
283 if (status == WorkflowConstants.STATUS_ANY) {
284 return blogsEntryPersistence.filterFindByG_NotS(
285 groupId, WorkflowConstants.STATUS_IN_TRASH, start, end, obc);
286 }
287 else {
288 return blogsEntryPersistence.filterFindByG_S(
289 groupId, status, start, end, obc);
290 }
291 }
292
293 @Override
294 public int getGroupEntriesCount(
295 long groupId, Date displayDate, int status) {
296
297 if (status == WorkflowConstants.STATUS_ANY) {
298 return blogsEntryPersistence.filterCountByG_LtD_NotS(
299 groupId, displayDate, WorkflowConstants.STATUS_IN_TRASH);
300 }
301 else {
302 return blogsEntryPersistence.filterCountByG_LtD_S(
303 groupId, displayDate, status);
304 }
305 }
306
307 @Override
308 public int getGroupEntriesCount(long groupId, int status) {
309 if (status == WorkflowConstants.STATUS_ANY) {
310 return blogsEntryPersistence.filterCountByG_NotS(
311 groupId, WorkflowConstants.STATUS_IN_TRASH);
312 }
313 else {
314 return blogsEntryPersistence.filterCountByG_S(groupId, status);
315 }
316 }
317
318 @Override
319 public String getGroupEntriesRSS(
320 long groupId, Date displayDate, int status, int max, String type,
321 double version, String displayStyle, String feedURL,
322 String entryURL, ThemeDisplay themeDisplay)
323 throws PortalException {
324
325 Group group = groupPersistence.findByPrimaryKey(groupId);
326
327 String name = group.getDescriptiveName();
328 List<BlogsEntry> blogsEntries = getGroupEntries(
329 groupId, displayDate, status, max);
330
331 return exportToRSS(
332 name, name, type, version, displayStyle, feedURL, entryURL,
333 blogsEntries, themeDisplay);
334 }
335
336 @Override
337 public List<BlogsEntry> getGroupsEntries(
338 long companyId, long groupId, Date displayDate, int status, int max)
339 throws PortalException {
340
341 List<BlogsEntry> entries = new ArrayList<>();
342
343 boolean listNotExhausted = true;
344
345 QueryDefinition<BlogsEntry> queryDefinition = new QueryDefinition<>(
346 status, false, 0, 0, new EntryDisplayDateComparator());
347
348 if (status == WorkflowConstants.STATUS_ANY) {
349 queryDefinition.setStatus(WorkflowConstants.STATUS_IN_TRASH, true);
350 }
351
352 while ((entries.size() < max) && listNotExhausted) {
353 queryDefinition.setEnd(queryDefinition.getStart() + max);
354
355 List<BlogsEntry> entryList =
356 blogsEntryLocalService.getGroupsEntries(
357 companyId, groupId, displayDate, queryDefinition);
358
359 queryDefinition.setStart(queryDefinition.getStart() + max);
360
361 listNotExhausted = (entryList.size() == max);
362
363 for (BlogsEntry entry : entryList) {
364 if (entries.size() >= max) {
365 break;
366 }
367
368 if (BlogsEntryPermission.contains(
369 getPermissionChecker(), entry, ActionKeys.VIEW)) {
370
371 entries.add(entry);
372 }
373 }
374 }
375
376 return entries;
377 }
378
379 @Override
380 public List<BlogsEntry> getGroupUserEntries(
381 long groupId, long userId, int status, int start, int end,
382 OrderByComparator<BlogsEntry> obc) {
383
384 if (status == WorkflowConstants.STATUS_ANY) {
385 return blogsEntryPersistence.filterFindByG_U_NotS(
386 groupId, userId, WorkflowConstants.STATUS_IN_TRASH, start, end,
387 obc);
388 }
389 else {
390 return blogsEntryPersistence.filterFindByG_U_S(
391 groupId, userId, status, start, end, obc);
392 }
393 }
394
395 @Override
396 public List<BlogsEntry> getGroupUserEntries(
397 long groupId, long userId, int[] statuses, int start, int end,
398 OrderByComparator<BlogsEntry> obc) {
399
400 return blogsEntryPersistence.filterFindByG_U_S(
401 groupId, userId, statuses, start, end, obc);
402 }
403
404 @Override
405 public int getGroupUserEntriesCount(long groupId, long userId, int status) {
406 if (status == WorkflowConstants.STATUS_ANY) {
407 return blogsEntryPersistence.filterCountByG_U_NotS(
408 groupId, userId, WorkflowConstants.STATUS_IN_TRASH);
409 }
410 else {
411 return blogsEntryPersistence.filterCountByG_U_S(
412 groupId, userId, status);
413 }
414 }
415
416 @Override
417 public int getGroupUserEntriesCount(
418 long groupId, long userId, int[] statuses) {
419
420 return blogsEntryPersistence.filterCountByG_U_S(
421 groupId, userId, statuses);
422 }
423
424 @Override
425 public List<BlogsEntry> getOrganizationEntries(
426 long organizationId, Date displayDate, int status, int max)
427 throws PortalException {
428
429 List<BlogsEntry> entries = new ArrayList<>();
430
431 boolean listNotExhausted = true;
432
433 QueryDefinition<BlogsEntry> queryDefinition = new QueryDefinition<>(
434 status, false, 0, 0, new EntryDisplayDateComparator());
435
436 if (status == WorkflowConstants.STATUS_ANY) {
437 queryDefinition.setStatus(WorkflowConstants.STATUS_IN_TRASH, true);
438 }
439
440 while ((entries.size() < max) && listNotExhausted) {
441 queryDefinition.setEnd(queryDefinition.getStart() + max);
442
443 List<BlogsEntry> entryList = blogsEntryFinder.findByOrganizationId(
444 organizationId, displayDate, queryDefinition);
445
446 queryDefinition.setStart(queryDefinition.getStart() + max);
447
448 listNotExhausted = (entryList.size() == max);
449
450 for (BlogsEntry entry : entryList) {
451 if (entries.size() >= max) {
452 break;
453 }
454
455 if (BlogsEntryPermission.contains(
456 getPermissionChecker(), entry, ActionKeys.VIEW)) {
457
458 entries.add(entry);
459 }
460 }
461 }
462
463 return entries;
464 }
465
466 @Override
467 public String getOrganizationEntriesRSS(
468 long organizationId, Date displayDate, int status, int max,
469 String type, double version, String displayStyle, String feedURL,
470 String entryURL, ThemeDisplay themeDisplay)
471 throws PortalException {
472
473 Organization organization = organizationPersistence.findByPrimaryKey(
474 organizationId);
475
476 String name = organization.getName();
477 List<BlogsEntry> blogsEntries = getOrganizationEntries(
478 organizationId, displayDate, status, max);
479
480 return exportToRSS(
481 name, name, type, version, displayStyle, feedURL, entryURL,
482 blogsEntries, themeDisplay);
483 }
484
485 @Override
486 public BlogsEntry moveEntryToTrash(long entryId) throws PortalException {
487 BlogsEntryPermission.check(
488 getPermissionChecker(), entryId, ActionKeys.DELETE);
489
490 return blogsEntryLocalService.moveEntryToTrash(getUserId(), entryId);
491 }
492
493 @Override
494 public void restoreEntryFromTrash(long entryId) throws PortalException {
495 BlogsEntryPermission.check(
496 getPermissionChecker(), entryId, ActionKeys.DELETE);
497
498 blogsEntryLocalService.restoreEntryFromTrash(getUserId(), entryId);
499 }
500
501 @Override
502 public void subscribe(long groupId) throws PortalException {
503 BlogsPermission.check(
504 getPermissionChecker(), groupId, ActionKeys.SUBSCRIBE);
505
506 blogsEntryLocalService.subscribe(getUserId(), groupId);
507 }
508
509 @Override
510 public void unsubscribe(long groupId) throws PortalException {
511 BlogsPermission.check(
512 getPermissionChecker(), groupId, ActionKeys.SUBSCRIBE);
513
514 blogsEntryLocalService.unsubscribe(getUserId(), groupId);
515 }
516
517
523 @Deprecated
524 @Override
525 public BlogsEntry updateEntry(
526 long entryId, String title, String description, String content,
527 int displayDateMonth, int displayDateDay, int displayDateYear,
528 int displayDateHour, int displayDateMinute, boolean allowPingbacks,
529 boolean allowTrackbacks, String[] trackbacks, boolean smallImage,
530 String smallImageURL, String smallImageFileName,
531 InputStream smallImageInputStream, ServiceContext serviceContext)
532 throws PortalException {
533
534 BlogsPermission.check(
535 getPermissionChecker(), serviceContext.getScopeGroupId(),
536 ActionKeys.UPDATE);
537
538 ImageSelector coverImageImageSelector = null;
539 ImageSelector smallImageImageSelector = null;
540
541 if (smallImage) {
542 if (Validator.isNotNull(smallImageFileName) &&
543 (smallImageInputStream != null)) {
544
545 try {
546 byte[] bytes = FileUtil.getBytes(smallImageInputStream);
547
548 smallImageImageSelector = new ImageSelector(
549 bytes, smallImageFileName,
550 MimeTypesUtil.getContentType(smallImageFileName), null);
551 }
552 catch (IOException ioe) {
553 _log.error("Unable to create image selector", ioe);
554 }
555 }
556 else if (Validator.isNotNull(smallImageURL)) {
557 smallImageImageSelector = new ImageSelector(smallImageURL);
558 }
559 }
560 else {
561 smallImageImageSelector = new ImageSelector();
562 }
563
564 return updateEntry(
565 entryId, title, StringPool.BLANK, description, content,
566 displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
567 displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
568 StringPool.BLANK, coverImageImageSelector, smallImageImageSelector,
569 serviceContext);
570 }
571
572 @Override
573 public BlogsEntry updateEntry(
574 long entryId, String title, String subtitle, String description,
575 String content, int displayDateMonth, int displayDateDay,
576 int displayDateYear, int displayDateHour, int displayDateMinute,
577 boolean allowPingbacks, boolean allowTrackbacks,
578 String[] trackbacks, String coverImageCaption,
579 ImageSelector coverImageImageSelector,
580 ImageSelector smallImageImageSelector,
581 ServiceContext serviceContext)
582 throws PortalException {
583
584 BlogsEntryPermission.check(
585 getPermissionChecker(), entryId, ActionKeys.UPDATE);
586
587 return blogsEntryLocalService.updateEntry(
588 getUserId(), entryId, title, subtitle, description, content,
589 displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
590 displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
591 coverImageCaption, coverImageImageSelector, smallImageImageSelector,
592 serviceContext);
593 }
594
595 protected String exportToRSS(
596 String name, String description, String type, double version,
597 String displayStyle, String feedURL, String entryURL,
598 List<BlogsEntry> blogsEntries, ThemeDisplay themeDisplay) {
599
600 SyndFeed syndFeed = new SyndFeedImpl();
601
602 syndFeed.setDescription(description);
603
604 List<SyndEntry> syndEntries = new ArrayList<>();
605
606 syndFeed.setEntries(syndEntries);
607
608 for (BlogsEntry entry : blogsEntries) {
609 SyndEntry syndEntry = new SyndEntryImpl();
610
611 String author = PortalUtil.getUserName(entry);
612
613 syndEntry.setAuthor(author);
614
615 SyndContent syndContent = new SyndContentImpl();
616
617 syndContent.setType(RSSUtil.ENTRY_TYPE_DEFAULT);
618
619 String value = null;
620
621 if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
622 String summary = entry.getDescription();
623
624 if (Validator.isNull(summary)) {
625 summary = entry.getContent();
626 }
627
628 value = StringUtil.shorten(
629 HtmlUtil.extractText(summary),
630 PropsValues.BLOGS_RSS_ABSTRACT_LENGTH, StringPool.BLANK);
631 }
632 else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
633 value = StringPool.BLANK;
634 }
635 else {
636 value = StringUtil.replace(
637 entry.getContent(), new String[] {"href=\"/", "src=\"/"},
638 new String[] {
639 "href=\"" + themeDisplay.getURLPortal() + "/",
640 "src=\"" + themeDisplay.getURLPortal() + "/"
641 });
642 }
643
644 syndContent.setValue(value);
645
646 syndEntry.setDescription(syndContent);
647
648 StringBundler sb = new StringBundler(4);
649
650 sb.append(entryURL);
651
652 if (!entryURL.endsWith(StringPool.QUESTION)) {
653 sb.append(StringPool.AMPERSAND);
654 }
655
656 sb.append("entryId=");
657 sb.append(entry.getEntryId());
658
659 String link = sb.toString();
660
661 syndEntry.setLink(link);
662
663 syndEntry.setPublishedDate(entry.getDisplayDate());
664 syndEntry.setTitle(entry.getTitle());
665 syndEntry.setUpdatedDate(entry.getModifiedDate());
666 syndEntry.setUri(link);
667
668 syndEntries.add(syndEntry);
669 }
670
671 syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
672
673 List<SyndLink> syndLinks = new ArrayList<>();
674
675 syndFeed.setLinks(syndLinks);
676
677 SyndLink selfSyndLink = new SyndLinkImpl();
678
679 syndLinks.add(selfSyndLink);
680
681 selfSyndLink.setHref(feedURL);
682 selfSyndLink.setRel("self");
683
684 syndFeed.setPublishedDate(new Date());
685 syndFeed.setTitle(name);
686 syndFeed.setUri(feedURL);
687
688 try {
689 return RSSUtil.export(syndFeed);
690 }
691 catch (FeedException fe) {
692 throw new SystemException(fe);
693 }
694 }
695
696 private static final Log _log = LogFactoryUtil.getLog(
697 BlogsEntryServiceImpl.class);
698
699 }