001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.blogs.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Organization;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PropsValues;
032    import com.liferay.portlet.blogs.model.BlogsEntry;
033    import com.liferay.portlet.blogs.service.base.BlogsEntryServiceBaseImpl;
034    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
035    import com.liferay.portlet.blogs.service.permission.BlogsPermission;
036    import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
037    import com.liferay.util.RSSUtil;
038    
039    import com.sun.syndication.feed.synd.SyndContent;
040    import com.sun.syndication.feed.synd.SyndContentImpl;
041    import com.sun.syndication.feed.synd.SyndEntry;
042    import com.sun.syndication.feed.synd.SyndEntryImpl;
043    import com.sun.syndication.feed.synd.SyndFeed;
044    import com.sun.syndication.feed.synd.SyndFeedImpl;
045    import com.sun.syndication.io.FeedException;
046    
047    import java.io.InputStream;
048    
049    import java.util.ArrayList;
050    import java.util.Date;
051    import java.util.Iterator;
052    import java.util.List;
053    
054    /**
055     * @author Brian Wing Shun Chan
056     * @author Mate Thurzo
057     */
058    public class BlogsEntryServiceImpl extends BlogsEntryServiceBaseImpl {
059    
060            public BlogsEntry addEntry(
061                            String title, String description, String content,
062                            int displayDateMonth, int displayDateDay, int displayDateYear,
063                            int displayDateHour, int displayDateMinute, boolean allowPingbacks,
064                            boolean allowTrackbacks, String[] trackbacks, boolean smallImage,
065                            String smallImageURL, String smallImageFileName,
066                            InputStream smallImageInputStream, ServiceContext serviceContext)
067                    throws PortalException, SystemException {
068    
069                    BlogsPermission.check(
070                            getPermissionChecker(), serviceContext.getScopeGroupId(),
071                            ActionKeys.ADD_ENTRY);
072    
073                    return blogsEntryLocalService.addEntry(
074                            getUserId(), title, description, content, displayDateMonth,
075                            displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
076                            allowPingbacks, allowTrackbacks, trackbacks, smallImage,
077                            smallImageURL, smallImageFileName, smallImageInputStream,
078                            serviceContext);
079            }
080    
081            public void deleteEntry(long entryId)
082                    throws PortalException, SystemException {
083    
084                    BlogsEntryPermission.check(
085                            getPermissionChecker(), entryId, ActionKeys.DELETE);
086    
087                    blogsEntryLocalService.deleteEntry(entryId);
088            }
089    
090            public List<BlogsEntry> getCompanyEntries(
091                            long companyId, Date displayDate, int status, int max)
092                    throws PortalException, SystemException {
093    
094                    List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
095    
096                    int lastIntervalStart = 0;
097                    boolean listNotExhausted = true;
098    
099                    while ((entries.size() < max) && listNotExhausted) {
100                            List<BlogsEntry> entryList =
101                                    blogsEntryLocalService.getCompanyEntries(
102                                            companyId, displayDate, status, lastIntervalStart,
103                                            lastIntervalStart + max, new EntryDisplayDateComparator());
104    
105                            Iterator<BlogsEntry> itr = entryList.iterator();
106    
107                            lastIntervalStart += max;
108                            listNotExhausted = (entryList.size() == max);
109    
110                            while (itr.hasNext() && (entries.size() < max)) {
111                                    BlogsEntry entry = itr.next();
112    
113                                    if (BlogsEntryPermission.contains(
114                                                    getPermissionChecker(), entry, ActionKeys.VIEW)) {
115    
116                                            entries.add(entry);
117                                    }
118                            }
119                    }
120    
121                    return entries;
122            }
123    
124            public String getCompanyEntriesRSS(
125                            long companyId, Date displayDate, int status, int max, String type,
126                            double version, String displayStyle, String feedURL,
127                            String entryURL, ThemeDisplay themeDisplay)
128                    throws PortalException, SystemException {
129    
130                    Company company = companyPersistence.findByPrimaryKey(companyId);
131    
132                    String name = company.getName();
133                    String description = name;
134                    List<BlogsEntry> blogsEntries = getCompanyEntries(
135                            companyId, displayDate, status, max);
136    
137                    return exportToRSS(
138                            name, description, type, version, displayStyle, feedURL, entryURL,
139                            blogsEntries, themeDisplay);
140            }
141    
142            public BlogsEntry getEntry(long entryId)
143                    throws PortalException, SystemException {
144    
145                    BlogsEntryPermission.check(
146                            getPermissionChecker(), entryId, ActionKeys.VIEW);
147    
148                    return blogsEntryLocalService.getEntry(entryId);
149            }
150    
151            public BlogsEntry getEntry(long groupId, String urlTitle)
152                    throws PortalException, SystemException {
153    
154                    BlogsEntry entry = blogsEntryLocalService.getEntry(groupId, urlTitle);
155    
156                    BlogsEntryPermission.check(
157                            getPermissionChecker(), entry.getEntryId(), ActionKeys.VIEW);
158    
159                    return entry;
160            }
161    
162            public List<BlogsEntry> getGroupEntries(
163                            long groupId, Date displayDate, int status, int max)
164                    throws SystemException {
165    
166                    if (status == WorkflowConstants.STATUS_ANY) {
167                            return blogsEntryPersistence.filterFindByG_LtD(
168                                    groupId, displayDate, 0, max);
169                    }
170                    else {
171                            return blogsEntryPersistence.filterFindByG_LtD_S(
172                                    groupId, displayDate, status, 0, max);
173                    }
174            }
175    
176            public List<BlogsEntry> getGroupEntries(
177                            long groupId, Date displayDate, int status, int start, int end)
178                    throws SystemException {
179    
180                    if (status == WorkflowConstants.STATUS_ANY) {
181                            return blogsEntryPersistence.filterFindByG_LtD(
182                                    groupId, displayDate, start, end);
183                    }
184                    else {
185                            return blogsEntryPersistence.filterFindByG_LtD_S(
186                                    groupId, displayDate, status, start, end);
187                    }
188            }
189    
190            public List<BlogsEntry> getGroupEntries(long groupId, int status, int max)
191                    throws SystemException {
192    
193                    if (status == WorkflowConstants.STATUS_ANY) {
194                            return blogsEntryPersistence.filterFindByGroupId(groupId, 0, max);
195                    }
196                    else {
197                            return blogsEntryPersistence.filterFindByG_S(
198                                    groupId, status, 0, max);
199                    }
200            }
201    
202            public List<BlogsEntry> getGroupEntries(
203                            long groupId, int status, int start, int end)
204                    throws SystemException {
205    
206                    if (status == WorkflowConstants.STATUS_ANY) {
207                            return blogsEntryPersistence.filterFindByGroupId(
208                                    groupId, start, end);
209                    }
210                    else {
211                            return blogsEntryPersistence.filterFindByG_S(
212                                    groupId, status, start, end);
213                    }
214            }
215    
216            public int getGroupEntriesCount(long groupId, Date displayDate, int status)
217                    throws SystemException {
218    
219                    if (status == WorkflowConstants.STATUS_ANY) {
220                            return blogsEntryPersistence.filterCountByG_LtD(
221                                    groupId, displayDate);
222                    }
223                    else {
224                            return blogsEntryPersistence.filterCountByG_LtD_S(
225                                    groupId, displayDate, status);
226                    }
227            }
228    
229            public int getGroupEntriesCount(long groupId, int status)
230                    throws SystemException {
231    
232                    if (status == WorkflowConstants.STATUS_ANY) {
233                            return blogsEntryPersistence.filterCountByGroupId(groupId);
234                    }
235                    else {
236                            return blogsEntryPersistence.filterCountByG_S(groupId, status);
237                    }
238            }
239    
240            public String getGroupEntriesRSS(
241                            long groupId, Date displayDate, int status, int max, String type,
242                            double version, String displayStyle, String feedURL,
243                            String entryURL, ThemeDisplay themeDisplay)
244                    throws PortalException, SystemException {
245    
246                    Group group = groupPersistence.findByPrimaryKey(groupId);
247    
248                    String name = HtmlUtil.escape(group.getDescriptiveName());
249                    String description = name;
250                    List<BlogsEntry> blogsEntries = getGroupEntries(
251                            groupId, displayDate, status, max);
252    
253                    return exportToRSS(
254                            name, description, type, version, displayStyle, feedURL, entryURL,
255                            blogsEntries, themeDisplay);
256            }
257    
258            public List<BlogsEntry> getGroupsEntries(
259                            long companyId, long groupId, Date displayDate, int status, int max)
260                    throws PortalException, SystemException {
261    
262                    List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
263    
264                    int lastIntervalStart = 0;
265                    boolean listNotExhausted = true;
266    
267                    while ((entries.size() < max) && listNotExhausted) {
268                            List<BlogsEntry> entryList =
269                                    blogsEntryLocalService.getGroupsEntries(
270                                            companyId, groupId, displayDate, status, lastIntervalStart,
271                                            lastIntervalStart + max);
272    
273                            Iterator<BlogsEntry> itr = entryList.iterator();
274    
275                            lastIntervalStart += max;
276                            listNotExhausted = (entryList.size() == max);
277    
278                            while (itr.hasNext() && (entries.size() < max)) {
279                                    BlogsEntry entry = itr.next();
280    
281                                    if (BlogsEntryPermission.contains(
282                                                    getPermissionChecker(), entry, ActionKeys.VIEW)) {
283    
284                                            entries.add(entry);
285                                    }
286                            }
287                    }
288    
289                    return entries;
290            }
291    
292            public List<BlogsEntry> getOrganizationEntries(
293                            long organizationId, Date displayDate, int status, int max)
294                    throws PortalException, SystemException {
295    
296                    List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
297    
298                    int lastIntervalStart = 0;
299                    boolean listNotExhausted = true;
300    
301                    while ((entries.size() < max) && listNotExhausted) {
302                            List<BlogsEntry> entryList = blogsEntryFinder.findByOrganizationId(
303                                    organizationId, displayDate, status, lastIntervalStart,
304                                    lastIntervalStart + max, new EntryDisplayDateComparator());
305    
306                            Iterator<BlogsEntry> itr = entryList.iterator();
307    
308                            lastIntervalStart += max;
309                            listNotExhausted = (entryList.size() == max);
310    
311                            while (itr.hasNext() && (entries.size() < max)) {
312                                    BlogsEntry entry = itr.next();
313    
314                                    if (BlogsEntryPermission.contains(
315                                                    getPermissionChecker(), entry, ActionKeys.VIEW)) {
316    
317                                            entries.add(entry);
318                                    }
319                            }
320                    }
321    
322                    return entries;
323            }
324    
325            public String getOrganizationEntriesRSS(
326                            long organizationId, Date displayDate, int status, int max,
327                            String type, double version, String displayStyle, String feedURL,
328                            String entryURL, ThemeDisplay themeDisplay)
329                    throws PortalException, SystemException {
330    
331                    Organization organization = organizationPersistence.findByPrimaryKey(
332                            organizationId);
333    
334                    String name = organization.getName();
335                    String description = name;
336                    List<BlogsEntry> blogsEntries = getOrganizationEntries(
337                            organizationId, displayDate, status, max);
338    
339                    return exportToRSS(
340                            name, description, type, version, displayStyle, feedURL, entryURL,
341                            blogsEntries, themeDisplay);
342            }
343    
344            public void subscribe(long groupId)
345                    throws PortalException, SystemException {
346    
347                    BlogsPermission.check(
348                            getPermissionChecker(), groupId, ActionKeys.SUBSCRIBE);
349    
350                    blogsEntryLocalService.subscribe(getUserId(), groupId);
351            }
352    
353            public void unsubscribe(long groupId)
354                    throws PortalException, SystemException {
355    
356                    BlogsPermission.check(
357                            getPermissionChecker(), groupId, ActionKeys.SUBSCRIBE);
358    
359                    blogsEntryLocalService.unsubscribe(getUserId(), groupId);
360            }
361    
362            public BlogsEntry updateEntry(
363                            long entryId, String title, String description, String content,
364                            int displayDateMonth, int displayDateDay, int displayDateYear,
365                            int displayDateHour, int displayDateMinute, boolean allowPingbacks,
366                            boolean allowTrackbacks, String[] trackbacks, boolean smallImage,
367                            String smallImageURL, String smallImageFileName,
368                            InputStream smallImageInputStream, ServiceContext serviceContext)
369                    throws PortalException, SystemException {
370    
371                    BlogsEntryPermission.check(
372                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
373    
374                    return blogsEntryLocalService.updateEntry(
375                            getUserId(), entryId, title, description, content, displayDateMonth,
376                            displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
377                            allowPingbacks, allowTrackbacks, trackbacks, smallImage,
378                            smallImageURL, smallImageFileName, smallImageInputStream,
379                            serviceContext);
380            }
381    
382            protected String exportToRSS(
383                            String name, String description, String type, double version,
384                            String displayStyle, String feedURL, String entryURL,
385                            List<BlogsEntry> blogsEntries, ThemeDisplay themeDisplay)
386                    throws SystemException {
387    
388                    SyndFeed syndFeed = new SyndFeedImpl();
389    
390                    syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
391                    syndFeed.setTitle(name);
392                    syndFeed.setLink(feedURL);
393                    syndFeed.setDescription(description);
394    
395                    List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
396    
397                    syndFeed.setEntries(syndEntries);
398    
399                    for (BlogsEntry entry : blogsEntries) {
400                            String author = HtmlUtil.escape(
401                                    PortalUtil.getUserName(entry.getUserId(), entry.getUserName()));
402    
403                            StringBundler link = new StringBundler(4);
404    
405                            if (entryURL.endsWith("/blogs/rss")) {
406                                    link.append(entryURL.substring(0, entryURL.length() - 3));
407                                    link.append(entry.getUrlTitle());
408                            }
409                            else {
410                                    link.append(entryURL);
411    
412                                    if (!entryURL.endsWith(StringPool.QUESTION)) {
413                                            link.append(StringPool.AMPERSAND);
414                                    }
415    
416                                    link.append("entryId=");
417                                    link.append(entry.getEntryId());
418                            }
419    
420                            String value = null;
421    
422                            if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
423                                    value = StringUtil.shorten(
424                                            HtmlUtil.extractText(entry.getDescription()),
425                                            PropsValues.BLOGS_RSS_ABSTRACT_LENGTH, StringPool.BLANK);
426                            }
427                            else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
428                                    value = StringPool.BLANK;
429                            }
430                            else {
431                                    value = StringUtil.replace(
432                                            entry.getContent(),
433                                            new String[] {
434                                                    "href=\"/",
435                                                    "src=\"/"
436                                            },
437                                            new String[] {
438                                                    "href=\"" + themeDisplay.getURLPortal() + "/",
439                                                    "src=\"" + themeDisplay.getURLPortal() + "/"
440                                            });
441                            }
442    
443                            SyndEntry syndEntry = new SyndEntryImpl();
444    
445                            syndEntry.setAuthor(author);
446                            syndEntry.setTitle(entry.getTitle());
447                            syndEntry.setLink(link.toString());
448                            syndEntry.setUri(syndEntry.getLink());
449                            syndEntry.setPublishedDate(entry.getCreateDate());
450                            syndEntry.setUpdatedDate(entry.getModifiedDate());
451    
452                            SyndContent syndContent = new SyndContentImpl();
453    
454                            syndContent.setType(RSSUtil.ENTRY_TYPE_DEFAULT);
455                            syndContent.setValue(value);
456    
457                            syndEntry.setDescription(syndContent);
458    
459                            syndEntries.add(syndEntry);
460                    }
461    
462                    try {
463                            return RSSUtil.export(syndFeed);
464                    }
465                    catch (FeedException fe) {
466                            throw new SystemException(fe);
467                    }
468            }
469    
470    }