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.wiki.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.io.unsync.UnsyncStringReader;
020    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.template.Template;
023    import com.liferay.portal.kernel.template.TemplateConstants;
024    import com.liferay.portal.kernel.template.TemplateContextType;
025    import com.liferay.portal.kernel.template.TemplateManagerUtil;
026    import com.liferay.portal.kernel.template.TemplateResource;
027    import com.liferay.portal.kernel.template.URLTemplateResource;
028    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
029    import com.liferay.portal.kernel.util.Diff;
030    import com.liferay.portal.kernel.util.DiffResult;
031    import com.liferay.portal.kernel.util.DiffUtil;
032    import com.liferay.portal.kernel.util.HtmlUtil;
033    import com.liferay.portal.kernel.util.HttpUtil;
034    import com.liferay.portal.kernel.util.ObjectValuePair;
035    import com.liferay.portal.kernel.util.OrderByComparator;
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.workflow.WorkflowConstants;
040    import com.liferay.portal.security.permission.ActionKeys;
041    import com.liferay.portal.service.ServiceContext;
042    import com.liferay.portal.util.PortalUtil;
043    import com.liferay.portal.util.PortletKeys;
044    import com.liferay.portal.util.PropsValues;
045    import com.liferay.portlet.wiki.NoSuchPageException;
046    import com.liferay.portlet.wiki.model.WikiNode;
047    import com.liferay.portlet.wiki.model.WikiPage;
048    import com.liferay.portlet.wiki.model.WikiPageConstants;
049    import com.liferay.portlet.wiki.service.base.WikiPageServiceBaseImpl;
050    import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
051    import com.liferay.portlet.wiki.service.permission.WikiPagePermission;
052    import com.liferay.portlet.wiki.util.WikiUtil;
053    import com.liferay.portlet.wiki.util.comparator.PageCreateDateComparator;
054    import com.liferay.util.RSSUtil;
055    
056    import com.sun.syndication.feed.synd.SyndContent;
057    import com.sun.syndication.feed.synd.SyndContentImpl;
058    import com.sun.syndication.feed.synd.SyndEntry;
059    import com.sun.syndication.feed.synd.SyndEntryImpl;
060    import com.sun.syndication.feed.synd.SyndFeed;
061    import com.sun.syndication.feed.synd.SyndFeedImpl;
062    import com.sun.syndication.feed.synd.SyndLink;
063    import com.sun.syndication.feed.synd.SyndLinkImpl;
064    import com.sun.syndication.io.FeedException;
065    
066    import java.io.File;
067    import java.io.InputStream;
068    
069    import java.net.URL;
070    
071    import java.util.ArrayList;
072    import java.util.Calendar;
073    import java.util.Date;
074    import java.util.List;
075    import java.util.Locale;
076    
077    /**
078     * @author Brian Wing Shun Chan
079     * @author Jorge Ferrer
080     * @author Raymond Augé
081     */
082    public class WikiPageServiceImpl extends WikiPageServiceBaseImpl {
083    
084            public WikiPageServiceImpl() {
085                    Class<?> clazz = getClass();
086    
087                    ClassLoader classLoader = clazz.getClassLoader();
088    
089                    String templateId = "com/liferay/portlet/wiki/dependencies/rss.vm";
090    
091                    URL url = classLoader.getResource(templateId);
092    
093                    _templateResource = new URLTemplateResource(templateId, url);
094            }
095    
096            public WikiPage addPage(
097                            long nodeId, String title, String content, String summary,
098                            boolean minorEdit, ServiceContext serviceContext)
099                    throws PortalException, SystemException {
100    
101                    WikiNodePermission.check(
102                            getPermissionChecker(), nodeId, ActionKeys.ADD_PAGE);
103    
104                    return wikiPageLocalService.addPage(
105                            getUserId(), nodeId, title, content, summary, minorEdit,
106                            serviceContext);
107            }
108    
109            public WikiPage addPage(
110                            long nodeId, String title, String content, String summary,
111                            boolean minorEdit, String format, String parentTitle,
112                            String redirectTitle, ServiceContext serviceContext)
113                    throws PortalException, SystemException {
114    
115                    WikiNodePermission.check(
116                            getPermissionChecker(), nodeId, ActionKeys.ADD_PAGE);
117    
118                    return wikiPageLocalService.addPage(
119                            getUserId(), nodeId, title, WikiPageConstants.VERSION_DEFAULT,
120                            content, summary, minorEdit, format, true, parentTitle,
121                            redirectTitle, serviceContext);
122            }
123    
124            public void addPageAttachment(
125                            long nodeId, String title, String fileName, File file,
126                            String mimeType)
127                    throws PortalException, SystemException {
128    
129                    WikiNodePermission.check(
130                            getPermissionChecker(), nodeId, ActionKeys.ADD_ATTACHMENT);
131    
132                    wikiPageLocalService.addPageAttachment(
133                            getUserId(), nodeId, title, fileName, file, mimeType);
134            }
135    
136            public void addPageAttachment(
137                            long nodeId, String title, String fileName, InputStream inputStream,
138                            String mimeType)
139                    throws PortalException, SystemException {
140    
141                    WikiNodePermission.check(
142                            getPermissionChecker(), nodeId, ActionKeys.ADD_ATTACHMENT);
143    
144                    wikiPageLocalService.addPageAttachment(
145                            getUserId(), nodeId, title, fileName, inputStream, mimeType);
146            }
147    
148            public void addPageAttachments(
149                            long nodeId, String title,
150                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs)
151                    throws PortalException, SystemException {
152    
153                    WikiNodePermission.check(
154                            getPermissionChecker(), nodeId, ActionKeys.ADD_ATTACHMENT);
155    
156                    wikiPageLocalService.addPageAttachments(
157                            getUserId(), nodeId, title, inputStreamOVPs);
158            }
159    
160            public void addTempPageAttachment(
161                            long nodeId, String fileName, String tempFolderName,
162                            InputStream inputStream, String mimeType)
163                    throws PortalException, SystemException {
164    
165                    WikiNode node = wikiNodeLocalService.getNode(nodeId);
166    
167                    WikiNodePermission.check(
168                            getPermissionChecker(), node, ActionKeys.ADD_ATTACHMENT);
169    
170                    wikiPageLocalService.addTempPageAttachment(
171                            node.getGroupId(), getUserId(), fileName, tempFolderName,
172                            inputStream, mimeType);
173            }
174    
175            public void changeParent(
176                            long nodeId, String title, String newParentTitle,
177                            ServiceContext serviceContext)
178                    throws PortalException, SystemException {
179    
180                    WikiPagePermission.check(
181                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
182    
183                    WikiNodePermission.check(
184                            getPermissionChecker(), nodeId, ActionKeys.ADD_PAGE);
185    
186                    wikiPageLocalService.changeParent(
187                            getUserId(), nodeId, title, newParentTitle, serviceContext);
188            }
189    
190            public void deletePage(long nodeId, String title)
191                    throws PortalException, SystemException {
192    
193                    WikiPagePermission.check(
194                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
195    
196                    wikiPageLocalService.deletePage(nodeId, title);
197            }
198    
199            public void deletePage(long nodeId, String title, double version)
200                    throws PortalException, SystemException {
201    
202                    WikiPagePermission.check(
203                            getPermissionChecker(), nodeId, title, version, ActionKeys.DELETE);
204    
205                    wikiPageLocalService.deletePage(nodeId, title, version);
206            }
207    
208            public void deletePageAttachment(long nodeId, String title, String fileName)
209                    throws PortalException, SystemException {
210    
211                    WikiPagePermission.check(
212                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
213    
214                    wikiPageLocalService.deletePageAttachment(nodeId, title, fileName);
215            }
216    
217            public void deletePageAttachments(long nodeId, String title)
218                    throws PortalException, SystemException {
219    
220                    WikiPagePermission.check(
221                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
222    
223                    wikiPageLocalService.deletePageAttachments(nodeId, title);
224            }
225    
226            public void deleteTempPageAttachment(
227                            long nodeId, String fileName, String tempFolderName)
228                    throws PortalException, SystemException {
229    
230                    WikiNode node = wikiNodeLocalService.getNode(nodeId);
231    
232                    WikiNodePermission.check(
233                            getPermissionChecker(), node, ActionKeys.ADD_ATTACHMENT);
234    
235                    wikiPageLocalService.deleteTempPageAttachment(
236                            node.getGroupId(), getUserId(), fileName, tempFolderName);
237            }
238    
239            public void deleteTrashPageAttachments(long nodeId, String title)
240                    throws PortalException, SystemException {
241    
242                    WikiPagePermission.check(
243                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
244    
245                    wikiPageLocalService.deleteTrashPageAttachments(nodeId, title);
246            }
247    
248            public List<WikiPage> getChildren(
249                            long groupId, long nodeId, boolean head, String parentTitle)
250                    throws PortalException, SystemException {
251    
252                    WikiNodePermission.check(
253                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
254    
255                    return wikiPagePersistence.filterFindByG_N_H_P_S(
256                            groupId, nodeId, head, parentTitle,
257                            WorkflowConstants.STATUS_APPROVED);
258            }
259    
260            public WikiPage getDraftPage(long nodeId, String title)
261                    throws PortalException, SystemException {
262    
263                    WikiPagePermission.check(
264                            getPermissionChecker(), nodeId, title, ActionKeys.VIEW);
265    
266                    return wikiPageLocalService.getDraftPage(nodeId, title);
267            }
268    
269            public List<WikiPage> getNodePages(long nodeId, int max)
270                    throws PortalException, SystemException {
271    
272                    List<WikiPage> pages = new ArrayList<WikiPage>();
273    
274                    int lastIntervalStart = 0;
275                    boolean listNotExhausted = true;
276    
277                    while ((pages.size() < max) && listNotExhausted) {
278                            List<WikiPage> pageList = wikiPageLocalService.getPages(
279                                    nodeId, true, lastIntervalStart, lastIntervalStart + max);
280    
281                            lastIntervalStart += max;
282                            listNotExhausted = (pageList.size() == max);
283    
284                            for (WikiPage page : pageList) {
285                                    if (pages.size() >= max) {
286                                            break;
287                                    }
288    
289                                    if (WikiPagePermission.contains(
290                                                    getPermissionChecker(), page, ActionKeys.VIEW)) {
291    
292                                            pages.add(page);
293                                    }
294                            }
295                    }
296    
297                    return pages;
298            }
299    
300            public String getNodePagesRSS(
301                            long nodeId, int max, String type, double version,
302                            String displayStyle, String feedURL, String entryURL)
303                    throws PortalException, SystemException {
304    
305                    WikiNodePermission.check(
306                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
307    
308                    WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
309    
310                    List<WikiPage> pages = getNodePages(nodeId, max);
311    
312                    return exportToRSS(
313                            node.getCompanyId(), node.getName(), node.getDescription(), type,
314                            version, displayStyle, feedURL, entryURL, pages, false, null);
315            }
316    
317            public List<WikiPage> getOrphans(long groupId, long nodeId)
318                    throws PortalException, SystemException {
319    
320                    WikiNodePermission.check(
321                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
322    
323                    List<WikiPage> pages = wikiPagePersistence.filterFindByG_N_H_S(
324                            groupId, nodeId, true, WorkflowConstants.STATUS_APPROVED);
325    
326                    return WikiUtil.filterOrphans(pages);
327            }
328    
329            public WikiPage getPage(long groupId, long nodeId, String title)
330                    throws PortalException, SystemException {
331    
332                    List<WikiPage> pages = wikiPagePersistence.filterFindByG_N_T_H(
333                            groupId, nodeId, title, true, 0, 1);
334    
335                    if (!pages.isEmpty()) {
336                            return pages.get(0);
337                    }
338                    else {
339                            throw new NoSuchPageException();
340                    }
341            }
342    
343            public WikiPage getPage(long nodeId, String title)
344                    throws PortalException, SystemException {
345    
346                    WikiPagePermission.check(
347                            getPermissionChecker(), nodeId, title, ActionKeys.VIEW);
348    
349                    return wikiPageLocalService.getPage(nodeId, title);
350            }
351    
352            public WikiPage getPage(long nodeId, String title, Boolean head)
353                    throws PortalException, SystemException {
354    
355                    WikiPagePermission.check(
356                            getPermissionChecker(), nodeId, title, ActionKeys.VIEW);
357    
358                    return wikiPageLocalService.getPage(nodeId, title, head);
359            }
360    
361            public WikiPage getPage(long nodeId, String title, double version)
362                    throws PortalException, SystemException {
363    
364                    WikiPagePermission.check(
365                            getPermissionChecker(), nodeId, title, ActionKeys.VIEW);
366    
367                    return wikiPageLocalService.getPage(nodeId, title, version);
368            }
369    
370            public List<WikiPage> getPages(
371                            long groupId, long nodeId, boolean head, int status, int start,
372                            int end, OrderByComparator obc)
373                    throws PortalException, SystemException {
374    
375                    WikiNodePermission.check(
376                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
377    
378                    if (status == WorkflowConstants.STATUS_ANY) {
379                            return wikiPagePersistence.filterFindByG_N_H(
380                                    groupId, nodeId, head, start, end, obc);
381                    }
382                    else {
383                            return wikiPagePersistence.filterFindByG_N_H_S(
384                                    groupId, nodeId, head, status, start, end, obc);
385                    }
386            }
387    
388            public List<WikiPage> getPages(
389                            long groupId, long userId, long nodeId, int status, int start,
390                            int end)
391                    throws PortalException, SystemException {
392    
393                    WikiNodePermission.check(
394                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
395    
396                    if (userId > 0) {
397                            return wikiPagePersistence.filterFindByG_U_N_S(
398                                    groupId, userId, nodeId, status, start, end,
399                                    new PageCreateDateComparator(false));
400                    }
401                    else {
402                            return wikiPagePersistence.filterFindByG_N_S(
403                                    groupId, nodeId, status, start, end,
404                                    new PageCreateDateComparator(false));
405                    }
406            }
407    
408            public int getPagesCount(long groupId, long nodeId, boolean head)
409                    throws PortalException, SystemException {
410    
411                    WikiNodePermission.check(
412                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
413    
414                    return wikiPagePersistence.filterCountByG_N_H_S(
415                            groupId, nodeId, head, WorkflowConstants.STATUS_APPROVED);
416            }
417    
418            public int getPagesCount(long groupId, long userId, long nodeId, int status)
419                    throws PortalException, SystemException {
420    
421                    WikiNodePermission.check(
422                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
423    
424                    if (userId > 0) {
425                            return wikiPagePersistence.filterCountByG_U_N_S(
426                                    groupId, userId, nodeId, status);
427                    }
428                    else {
429                            return wikiPagePersistence.filterCountByG_N_S(
430                                    groupId, nodeId, status);
431                    }
432            }
433    
434            public String getPagesRSS(
435                            long companyId, long nodeId, String title, int max, String type,
436                            double version, String displayStyle, String feedURL,
437                            String entryURL, Locale locale)
438                    throws PortalException, SystemException {
439    
440                    WikiPagePermission.check(
441                            getPermissionChecker(), nodeId, title, ActionKeys.VIEW);
442    
443                    List<WikiPage> pages = wikiPageLocalService.getPages(
444                            nodeId, title, 0, max, new PageCreateDateComparator(true));
445    
446                    return exportToRSS(
447                            companyId, title, title, type, version, displayStyle, feedURL,
448                            entryURL, pages, true, locale);
449            }
450    
451            public List<WikiPage> getRecentChanges(
452                            long groupId, long nodeId, int start, int end)
453                    throws PortalException, SystemException {
454    
455                    WikiNodePermission.check(
456                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
457    
458                    Calendar calendar = CalendarFactoryUtil.getCalendar();
459    
460                    calendar.add(Calendar.WEEK_OF_YEAR, -1);
461    
462                    return wikiPageFinder.filterFindByCreateDate(
463                            groupId, nodeId, calendar.getTime(), false, start, end);
464            }
465    
466            public int getRecentChangesCount(long groupId, long nodeId)
467                    throws PortalException, SystemException {
468    
469                    WikiNodePermission.check(
470                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
471    
472                    Calendar calendar = CalendarFactoryUtil.getCalendar();
473    
474                    calendar.add(Calendar.WEEK_OF_YEAR, -1);
475    
476                    return wikiPageFinder.filterCountByCreateDate(
477                            groupId, nodeId, calendar.getTime(), false);
478            }
479    
480            public String[] getTempPageAttachmentNames(
481                            long nodeId, String tempFolderName)
482                    throws PortalException, SystemException {
483    
484                    WikiNode node = wikiNodeLocalService.getNode(nodeId);
485    
486                    WikiNodePermission.check(
487                            getPermissionChecker(), node, ActionKeys.ADD_ATTACHMENT);
488    
489                    return wikiPageLocalService.getTempPageAttachmentNames(
490                            node.getGroupId(), getUserId(), tempFolderName);
491            }
492    
493            public void movePage(
494                            long nodeId, String title, String newTitle,
495                            ServiceContext serviceContext)
496                    throws PortalException, SystemException {
497    
498                    WikiPagePermission.check(
499                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
500    
501                    WikiNodePermission.check(
502                            getPermissionChecker(), nodeId, ActionKeys.ADD_PAGE);
503    
504                    wikiPageLocalService.movePage(
505                            getUserId(), nodeId, title, newTitle, serviceContext);
506            }
507    
508            public long movePageAttachmentToTrash(
509                            long nodeId, String title, String fileName)
510                    throws PortalException, SystemException {
511    
512                    WikiPagePermission.check(
513                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
514    
515                    return wikiPageLocalService.movePageAttachmentToTrash(
516                            getUserId(), nodeId, title, fileName);
517            }
518    
519            public void movePageToTrash(long nodeId, String title)
520                    throws PortalException, SystemException {
521    
522                    WikiPagePermission.check(
523                            getPermissionChecker(), nodeId, title, ActionKeys.DELETE);
524    
525                    wikiPageLocalService.movePageToTrash(getUserId(), nodeId, title);
526            }
527    
528            public void movePageToTrash(long nodeId, String title, double version)
529                    throws PortalException, SystemException {
530    
531                    WikiPagePermission.check(
532                            getPermissionChecker(), nodeId, title, version, ActionKeys.DELETE);
533    
534                    wikiPageLocalService.movePageToTrash(
535                            getUserId(), nodeId, title, version);
536            }
537    
538            public void restorePageAttachmentFromTrash(
539                            long nodeId, String title, String fileName)
540                    throws PortalException, SystemException {
541    
542                    WikiNodePermission.check(
543                            getPermissionChecker(), nodeId, ActionKeys.ADD_ATTACHMENT);
544    
545                    wikiPageLocalService.restorePageAttachmentFromTrash(
546                            getUserId(), nodeId, title, fileName);
547            }
548    
549            public void restorePageFromTrash(long resourcePrimKey)
550                    throws PortalException, SystemException {
551    
552                    WikiPage page = wikiPageLocalService.getPage(resourcePrimKey);
553    
554                    WikiPagePermission.check(
555                            getPermissionChecker(), page, ActionKeys.DELETE);
556    
557                    wikiPageLocalService.restorePageFromTrash(getUserId(), page);
558            }
559    
560            public WikiPage revertPage(
561                            long nodeId, String title, double version,
562                            ServiceContext serviceContext)
563                    throws PortalException, SystemException {
564    
565                    WikiPagePermission.check(
566                            getPermissionChecker(), nodeId, title, ActionKeys.UPDATE);
567    
568                    return wikiPageLocalService.revertPage(
569                            getUserId(), nodeId, title, version, serviceContext);
570            }
571    
572            public void subscribePage(long nodeId, String title)
573                    throws PortalException, SystemException {
574    
575                    WikiPagePermission.check(
576                            getPermissionChecker(), nodeId, title, ActionKeys.SUBSCRIBE);
577    
578                    wikiPageLocalService.subscribePage(getUserId(), nodeId, title);
579            }
580    
581            public void unsubscribePage(long nodeId, String title)
582                    throws PortalException, SystemException {
583    
584                    WikiPagePermission.check(
585                            getPermissionChecker(), nodeId, title, ActionKeys.SUBSCRIBE);
586    
587                    wikiPageLocalService.unsubscribePage(getUserId(), nodeId, title);
588            }
589    
590            public WikiPage updatePage(
591                            long nodeId, String title, double version, String content,
592                            String summary, boolean minorEdit, String format,
593                            String parentTitle, String redirectTitle,
594                            ServiceContext serviceContext)
595                    throws PortalException, SystemException {
596    
597                    WikiPagePermission.check(
598                            getPermissionChecker(), nodeId, title, ActionKeys.UPDATE);
599    
600                    return wikiPageLocalService.updatePage(
601                            getUserId(), nodeId, title, version, content, summary, minorEdit,
602                            format, parentTitle, redirectTitle, serviceContext);
603            }
604    
605            protected String exportToRSS(
606                            long companyId, String name, String description, String type,
607                            double version, String displayStyle, String feedURL,
608                            String entryURL, List<WikiPage> pages, boolean diff, Locale locale)
609                    throws SystemException {
610    
611                    SyndFeed syndFeed = new SyndFeedImpl();
612    
613                    syndFeed.setDescription(description);
614    
615                    List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
616    
617                    syndFeed.setEntries(syndEntries);
618    
619                    WikiPage latestPage = null;
620    
621                    StringBundler sb = new StringBundler(6);
622    
623                    for (WikiPage page : pages) {
624                            SyndEntry syndEntry = new SyndEntryImpl();
625    
626                            String author = PortalUtil.getUserName(page);
627    
628                            syndEntry.setAuthor(author);
629    
630                            SyndContent syndContent = new SyndContentImpl();
631    
632                            syndContent.setType(RSSUtil.ENTRY_TYPE_DEFAULT);
633    
634                            sb.setIndex(0);
635    
636                            sb.append(entryURL);
637    
638                            if (entryURL.endsWith(StringPool.SLASH)) {
639                                    sb.append(HttpUtil.encodeURL(page.getTitle()));
640                            }
641    
642                            if (diff) {
643                                    if ((latestPage != null) || (pages.size() == 1)) {
644                                            sb.append(StringPool.QUESTION);
645                                            sb.append(PortalUtil.getPortletNamespace(PortletKeys.WIKI));
646                                            sb.append("version=");
647                                            sb.append(page.getVersion());
648    
649                                            String value = null;
650    
651                                            if (latestPage == null) {
652                                                    value = page.getContent();
653                                            }
654                                            else {
655                                                    value = getPageDiff(
656                                                            companyId, latestPage, page, locale);
657                                            }
658    
659                                            syndContent.setValue(value);
660    
661                                            syndEntry.setDescription(syndContent);
662    
663                                            syndEntries.add(syndEntry);
664                                    }
665                            }
666                            else {
667                                    String value = null;
668    
669                                    if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
670                                            value = StringUtil.shorten(
671                                                    HtmlUtil.extractText(page.getContent()),
672                                                    PropsValues.WIKI_RSS_ABSTRACT_LENGTH, StringPool.BLANK);
673                                    }
674                                    else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
675                                            value = StringPool.BLANK;
676                                    }
677                                    else {
678                                            value = page.getContent();
679                                    }
680    
681                                    syndContent.setValue(value);
682    
683                                    syndEntry.setDescription(syndContent);
684    
685                                    syndEntries.add(syndEntry);
686                            }
687    
688                            syndEntry.setLink(sb.toString());
689                            syndEntry.setPublishedDate(page.getCreateDate());
690    
691                            String title =
692                                    page.getTitle() + StringPool.SPACE + page.getVersion();
693    
694                            if (page.isMinorEdit()) {
695                                    title +=
696                                            StringPool.SPACE + StringPool.OPEN_PARENTHESIS +
697                                                    LanguageUtil.get(locale, "minor-edit") +
698                                                            StringPool.CLOSE_PARENTHESIS;
699                            }
700    
701                            syndEntry.setTitle(title);
702    
703                            syndEntry.setUpdatedDate(page.getModifiedDate());
704                            syndEntry.setUri(sb.toString());
705    
706                            latestPage = page;
707                    }
708    
709                    syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
710    
711                    List<SyndLink> syndLinks = new ArrayList<SyndLink>();
712    
713                    syndFeed.setLinks(syndLinks);
714    
715                    SyndLink syndLinkSelf = new SyndLinkImpl();
716    
717                    syndLinks.add(syndLinkSelf);
718    
719                    syndLinkSelf.setHref(feedURL);
720                    syndLinkSelf.setRel("self");
721    
722                    syndFeed.setPublishedDate(new Date());
723                    syndFeed.setTitle(name);
724                    syndFeed.setUri(feedURL);
725    
726                    try {
727                            return RSSUtil.export(syndFeed);
728                    }
729                    catch (FeedException fe) {
730                            throw new SystemException(fe);
731                    }
732            }
733    
734            protected String getPageDiff(
735                            long companyId, WikiPage latestPage, WikiPage page, Locale locale)
736                    throws SystemException {
737    
738                    try {
739                            Template template = TemplateManagerUtil.getTemplate(
740                                    TemplateConstants.LANG_TYPE_VM, _templateResource,
741                                    TemplateContextType.STANDARD);
742    
743                            template.put("companyId", companyId);
744                            template.put("contextLine", Diff.CONTEXT_LINE);
745                            template.put("diffUtil", new DiffUtil());
746                            template.put("languageUtil", LanguageUtil.getLanguage());
747                            template.put("locale", locale);
748    
749                            String sourceContent = WikiUtil.convert(
750                                    latestPage, null, null, null);
751                            String targetContent = WikiUtil.convert(page, null, null, null);
752    
753                            List<DiffResult>[] diffResults = DiffUtil.diff(
754                                    new UnsyncStringReader(sourceContent),
755                                    new UnsyncStringReader(targetContent));
756    
757                            template.put("sourceResults", diffResults[0]);
758                            template.put("targetResults", diffResults[1]);
759    
760                            UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
761    
762                            template.processTemplate(unsyncStringWriter);
763    
764                            return unsyncStringWriter.toString();
765                    }
766                    catch (Exception e) {
767                            throw new SystemException(e);
768                    }
769            }
770    
771            private TemplateResource _templateResource;
772    
773    }