001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.GetterUtil;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.Organization;
029    import com.liferay.portal.security.permission.ActionKeys;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.PropsUtil;
034    import com.liferay.portlet.blogs.model.BlogsEntry;
035    import com.liferay.portlet.blogs.service.base.BlogsEntryServiceBaseImpl;
036    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
037    import com.liferay.portlet.blogs.service.permission.BlogsPermission;
038    import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
039    import com.liferay.util.RSSUtil;
040    
041    import com.sun.syndication.feed.synd.SyndContent;
042    import com.sun.syndication.feed.synd.SyndContentImpl;
043    import com.sun.syndication.feed.synd.SyndEntry;
044    import com.sun.syndication.feed.synd.SyndEntryImpl;
045    import com.sun.syndication.feed.synd.SyndFeed;
046    import com.sun.syndication.feed.synd.SyndFeedImpl;
047    import com.sun.syndication.io.FeedException;
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     */
057    public class BlogsEntryServiceImpl extends BlogsEntryServiceBaseImpl {
058    
059            public BlogsEntry addEntry(
060                            String title, String content, int displayDateMonth,
061                            int displayDateDay, int displayDateYear, int displayDateHour,
062                            int displayDateMinute, boolean allowPingbacks,
063                            boolean allowTrackbacks, String[] trackbacks,
064                            ServiceContext serviceContext)
065                    throws PortalException, SystemException {
066    
067                    BlogsPermission.check(
068                            getPermissionChecker(), serviceContext.getScopeGroupId(),
069                            ActionKeys.ADD_ENTRY);
070    
071                    return blogsEntryLocalService.addEntry(
072                            getUserId(), title, content, displayDateMonth, displayDateDay,
073                            displayDateYear, displayDateHour, displayDateMinute,
074                            allowPingbacks, allowTrackbacks, trackbacks, serviceContext);
075            }
076    
077            public void deleteEntry(long entryId)
078                    throws PortalException, SystemException {
079    
080                    BlogsEntryPermission.check(
081                            getPermissionChecker(), entryId, ActionKeys.DELETE);
082    
083                    blogsEntryLocalService.deleteEntry(entryId);
084            }
085    
086            public List<BlogsEntry> getCompanyEntries(
087                            long companyId, int status, int max)
088                    throws PortalException, SystemException {
089    
090                    List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
091    
092                    int lastIntervalStart = 0;
093                    boolean listNotExhausted = true;
094    
095                    while ((entries.size() < max) && listNotExhausted) {
096                            List<BlogsEntry> entryList =
097                                    blogsEntryLocalService.getCompanyEntries(
098                                            companyId, status, lastIntervalStart,
099                                            lastIntervalStart + max, new EntryDisplayDateComparator());
100    
101                            Iterator<BlogsEntry> itr = entryList.iterator();
102    
103                            lastIntervalStart += max;
104                            listNotExhausted = (entryList.size() == max);
105    
106                            while (itr.hasNext() && (entries.size() < max)) {
107                                    BlogsEntry entry = itr.next();
108    
109                                    if (BlogsEntryPermission.contains(
110                                                    getPermissionChecker(), entry, ActionKeys.VIEW)) {
111    
112                                            entries.add(entry);
113                                    }
114                            }
115                    }
116    
117                    return entries;
118            }
119    
120            public String getCompanyEntriesRSS(
121                            long companyId, int status, int max, String type, double version,
122                            String displayStyle, String feedURL, String entryURL,
123                            ThemeDisplay themeDisplay)
124                    throws PortalException, SystemException {
125    
126                    Company company = companyPersistence.findByPrimaryKey(companyId);
127    
128                    String name = company.getName();
129                    String description = name;
130                    List<BlogsEntry> blogsEntries = getCompanyEntries(
131                            companyId, status, max);
132    
133                    return exportToRSS(
134                            name, description, type, version, displayStyle, feedURL, entryURL,
135                            blogsEntries, themeDisplay);
136            }
137    
138            public BlogsEntry getEntry(long entryId)
139                    throws PortalException, SystemException {
140    
141                    BlogsEntryPermission.check(
142                            getPermissionChecker(), entryId, ActionKeys.VIEW);
143    
144                    return blogsEntryLocalService.getEntry(entryId);
145            }
146    
147            public BlogsEntry getEntry(long groupId, String urlTitle)
148                    throws PortalException, SystemException {
149    
150                    BlogsEntry entry = blogsEntryLocalService.getEntry(groupId, urlTitle);
151    
152                    BlogsEntryPermission.check(
153                            getPermissionChecker(), entry.getEntryId(), ActionKeys.VIEW);
154    
155                    return entry;
156            }
157    
158            public List<BlogsEntry> getGroupEntries(long groupId, int status, int max)
159                    throws SystemException {
160    
161                    if (status == WorkflowConstants.STATUS_ANY) {
162                            return blogsEntryPersistence.filterFindByGroupId(groupId, 0, max);
163                    }
164                    else {
165                            return blogsEntryPersistence.filterFindByG_S(
166                                    groupId, status, 0, max);
167                    }
168            }
169    
170            public List<BlogsEntry> getGroupEntries(
171                            long groupId, int status, int start, int end)
172                    throws SystemException {
173    
174                    if (status == WorkflowConstants.STATUS_ANY) {
175                            return blogsEntryPersistence.filterFindByGroupId(
176                                    groupId, start, end);
177                    }
178                    else {
179                            return blogsEntryPersistence.filterFindByG_S(
180                                    groupId, status, start, end);
181                    }
182            }
183    
184            public int getGroupEntriesCount(long groupId, int status)
185                    throws SystemException {
186    
187                    if (status == WorkflowConstants.STATUS_ANY) {
188                            return blogsEntryPersistence.filterCountByGroupId(groupId);
189                    }
190                    else {
191                            return blogsEntryPersistence.filterCountByG_S(groupId, status);
192                    }
193            }
194    
195            public String getGroupEntriesRSS(
196                            long groupId, int status, int max, String type, double version,
197                            String displayStyle, String feedURL, String entryURL,
198                            ThemeDisplay themeDisplay)
199                    throws PortalException, SystemException {
200    
201                    Group group = groupPersistence.findByPrimaryKey(groupId);
202    
203                    String name = HtmlUtil.escape(group.getDescriptiveName());
204                    String description = name;
205                    List<BlogsEntry> blogsEntries = getGroupEntries(groupId, status, max);
206    
207                    return exportToRSS(
208                            name, description, type, version, displayStyle, feedURL, entryURL,
209                            blogsEntries, themeDisplay);
210            }
211    
212            public List<BlogsEntry> getGroupsEntries(
213                            long companyId, long groupId, int status, int max)
214                    throws PortalException, SystemException {
215    
216                    List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
217    
218                    int lastIntervalStart = 0;
219                    boolean listNotExhausted = true;
220    
221                    while ((entries.size() < max) && listNotExhausted) {
222                            List<BlogsEntry> entryList =
223                                    blogsEntryLocalService.getGroupsEntries(
224                                            companyId, groupId, status, lastIntervalStart,
225                                            lastIntervalStart + max);
226    
227                            Iterator<BlogsEntry> itr = entryList.iterator();
228    
229                            lastIntervalStart += max;
230                            listNotExhausted = (entryList.size() == max);
231    
232                            while (itr.hasNext() && (entries.size() < max)) {
233                                    BlogsEntry entry = itr.next();
234    
235                                    if (BlogsEntryPermission.contains(
236                                                    getPermissionChecker(), entry, ActionKeys.VIEW)) {
237    
238                                            entries.add(entry);
239                                    }
240                            }
241                    }
242    
243                    return entries;
244            }
245    
246            public List<BlogsEntry> getOrganizationEntries(
247                            long organizationId, int status, int max)
248                    throws PortalException, SystemException {
249    
250                    List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
251    
252                    Date displayDate = new Date();
253                    int lastIntervalStart = 0;
254                    boolean listNotExhausted = true;
255    
256                    while ((entries.size() < max) && listNotExhausted) {
257                            List<BlogsEntry> entryList = blogsEntryFinder.findByOrganizationId(
258                                    organizationId, displayDate, status, lastIntervalStart,
259                                    lastIntervalStart + max);
260    
261                            Iterator<BlogsEntry> itr = entryList.iterator();
262    
263                            lastIntervalStart += max;
264                            listNotExhausted = (entryList.size() == max);
265    
266                            while (itr.hasNext() && (entries.size() < max)) {
267                                    BlogsEntry entry = itr.next();
268    
269                                    if (BlogsEntryPermission.contains(
270                                                    getPermissionChecker(), entry, ActionKeys.VIEW)) {
271    
272                                            entries.add(entry);
273                                    }
274                            }
275                    }
276    
277                    return entries;
278            }
279    
280            public String getOrganizationEntriesRSS(
281                            long organizationId, int status, int max, String type,
282                            double version, String displayStyle, String feedURL,
283                            String entryURL, ThemeDisplay themeDisplay)
284                    throws PortalException, SystemException {
285    
286                    Organization organization = organizationPersistence.findByPrimaryKey(
287                            organizationId);
288    
289                    String name = organization.getName();
290                    String description = name;
291                    List<BlogsEntry> blogsEntries = getOrganizationEntries(
292                            organizationId, status, max);
293    
294                    return exportToRSS(
295                            name, description, type, version, displayStyle, feedURL, entryURL,
296                            blogsEntries, themeDisplay);
297            }
298    
299            public BlogsEntry updateEntry(
300                            long entryId, String title, String content, int displayDateMonth,
301                            int displayDateDay, int displayDateYear, int displayDateHour,
302                            int displayDateMinute, boolean allowPingbacks,
303                            boolean allowTrackbacks, String[] trackbacks,
304                            ServiceContext serviceContext)
305                    throws PortalException, SystemException {
306    
307                    BlogsEntryPermission.check(
308                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
309    
310                    return blogsEntryLocalService.updateEntry(
311                            getUserId(), entryId, title, content, displayDateMonth,
312                            displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
313                            allowPingbacks, allowTrackbacks, trackbacks, serviceContext);
314            }
315    
316            protected String exportToRSS(
317                            String name, String description, String type, double version,
318                            String displayStyle, String feedURL, String entryURL,
319                            List<BlogsEntry> blogsEntries, ThemeDisplay themeDisplay)
320                    throws SystemException {
321    
322                    SyndFeed syndFeed = new SyndFeedImpl();
323    
324                    syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
325                    syndFeed.setTitle(name);
326                    syndFeed.setLink(feedURL);
327                    syndFeed.setDescription(description);
328    
329                    List<SyndEntry> entries = new ArrayList<SyndEntry>();
330    
331                    syndFeed.setEntries(entries);
332    
333                    for (BlogsEntry entry : blogsEntries) {
334                            String author = HtmlUtil.escape(
335                                    PortalUtil.getUserName(entry.getUserId(), entry.getUserName()));
336    
337                            StringBundler link = new StringBundler(4);
338    
339                            if (entryURL.endsWith("/blogs/rss")) {
340                                    link.append(entryURL.substring(0, entryURL.length() - 3));
341                                    link.append(entry.getUrlTitle());
342                            }
343                            else {
344                                    link.append(entryURL);
345    
346                                    if (!entryURL.endsWith(StringPool.QUESTION)) {
347                                            link.append(StringPool.AMPERSAND);
348                                    }
349    
350                                    link.append("entryId=");
351                                    link.append(entry.getEntryId());
352                            }
353    
354                            String value = null;
355    
356                            if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
357                                    value = StringUtil.shorten(
358                                            HtmlUtil.extractText(entry.getContent()),
359                                            _RSS_ABSTRACT_LENGTH, StringPool.BLANK);
360                            }
361                            else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
362                                    value = StringPool.BLANK;
363                            }
364                            else {
365                                    value = StringUtil.replace(
366                                            entry.getContent(),
367                                            new String[] {
368                                                    "href=\"/",
369                                                    "src=\"/"
370                                            },
371                                            new String[] {
372                                                    "href=\"" + themeDisplay.getURLPortal() + "/",
373                                                    "src=\"" + themeDisplay.getURLPortal() + "/"
374                                            }
375                                    );
376                            }
377    
378                            SyndEntry syndEntry = new SyndEntryImpl();
379    
380                            syndEntry.setAuthor(author);
381                            syndEntry.setTitle(entry.getTitle());
382                            syndEntry.setLink(link.toString());
383                            syndEntry.setUri(syndEntry.getLink());
384                            syndEntry.setPublishedDate(entry.getCreateDate());
385                            syndEntry.setUpdatedDate(entry.getModifiedDate());
386    
387                            SyndContent syndContent = new SyndContentImpl();
388    
389                            syndContent.setType(RSSUtil.DEFAULT_ENTRY_TYPE);
390                            syndContent.setValue(value);
391    
392                            syndEntry.setDescription(syndContent);
393    
394                            entries.add(syndEntry);
395                    }
396    
397                    try {
398                            return RSSUtil.export(syndFeed);
399                    }
400                    catch (FeedException fe) {
401                            throw new SystemException(fe);
402                    }
403            }
404    
405            private static final int _RSS_ABSTRACT_LENGTH = GetterUtil.getInteger(
406                    PropsUtil.get(PropsKeys.BLOGS_RSS_ABSTRACT_LENGTH));
407    
408    }