1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.HtmlUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.model.Company;
32  import com.liferay.portal.model.Group;
33  import com.liferay.portal.model.Organization;
34  import com.liferay.portal.security.permission.ActionKeys;
35  import com.liferay.portal.service.permission.PortletPermissionUtil;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.PortletKeys;
39  import com.liferay.portal.util.PropsKeys;
40  import com.liferay.portal.util.PropsUtil;
41  import com.liferay.portlet.blogs.model.BlogsEntry;
42  import com.liferay.portlet.blogs.service.base.BlogsEntryServiceBaseImpl;
43  import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
44  import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
45  import com.liferay.util.RSSUtil;
46  
47  import com.sun.syndication.feed.synd.SyndContent;
48  import com.sun.syndication.feed.synd.SyndContentImpl;
49  import com.sun.syndication.feed.synd.SyndEntry;
50  import com.sun.syndication.feed.synd.SyndEntryImpl;
51  import com.sun.syndication.feed.synd.SyndFeed;
52  import com.sun.syndication.feed.synd.SyndFeedImpl;
53  import com.sun.syndication.io.FeedException;
54  
55  import java.util.ArrayList;
56  import java.util.Date;
57  import java.util.Iterator;
58  import java.util.List;
59  
60  /**
61   * <a href="BlogsEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   */
65  public class BlogsEntryServiceImpl extends BlogsEntryServiceBaseImpl {
66  
67      public BlogsEntry addEntry(
68              long plid, String title, String content, int displayDateMonth,
69              int displayDateDay, int displayDateYear, int displayDateHour,
70              int displayDateMinute, boolean draft, boolean allowTrackbacks,
71              String[] trackbacks, String[] tagsEntries,
72              boolean addCommunityPermissions, boolean addGuestPermissions,
73              ThemeDisplay themeDisplay)
74          throws PortalException, SystemException {
75  
76          PortletPermissionUtil.check(
77              getPermissionChecker(), plid, PortletKeys.BLOGS,
78              ActionKeys.ADD_ENTRY);
79  
80          return blogsEntryLocalService.addEntry(
81              getUserId(), plid, title, content, displayDateMonth, displayDateDay,
82              displayDateYear, displayDateHour, displayDateMinute, draft,
83              allowTrackbacks, trackbacks, tagsEntries, addCommunityPermissions,
84              addGuestPermissions, themeDisplay);
85      }
86  
87      public BlogsEntry addEntry(
88              long plid, String title, String content, int displayDateMonth,
89              int displayDateDay, int displayDateYear, int displayDateHour,
90              int displayDateMinute, boolean draft, boolean allowTrackbacks,
91              String[] trackbacks, String[] tagsEntries,
92              String[] communityPermissions, String[] guestPermissions,
93              ThemeDisplay themeDisplay)
94          throws PortalException, SystemException {
95  
96          PortletPermissionUtil.check(
97              getPermissionChecker(), plid, PortletKeys.BLOGS,
98              ActionKeys.ADD_ENTRY);
99  
100         return blogsEntryLocalService.addEntry(
101             getUserId(), plid, title, content, displayDateMonth, displayDateDay,
102             displayDateYear, displayDateHour, displayDateMinute, draft,
103             allowTrackbacks, trackbacks, tagsEntries, communityPermissions,
104             guestPermissions, themeDisplay);
105     }
106 
107     public void deleteEntry(long entryId)
108         throws PortalException, SystemException {
109 
110         BlogsEntryPermission.check(
111             getPermissionChecker(), entryId, ActionKeys.DELETE);
112 
113         blogsEntryLocalService.deleteEntry(entryId);
114     }
115 
116     public List<BlogsEntry> getCompanyEntries(long companyId, int max)
117         throws PortalException, SystemException {
118 
119         List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
120 
121         int lastIntervalStart = 0;
122         boolean listNotExhausted = true;
123 
124         while ((entries.size() < max) && listNotExhausted) {
125             List<BlogsEntry> entryList =
126                 blogsEntryLocalService.getCompanyEntries(
127                     companyId, false, lastIntervalStart,
128                     lastIntervalStart + max, new EntryDisplayDateComparator());
129 
130             Iterator<BlogsEntry> itr = entryList.iterator();
131 
132             lastIntervalStart += max;
133             listNotExhausted = (entryList.size() == max);
134 
135             while (itr.hasNext() && (entries.size() < max)) {
136                 BlogsEntry entry = itr.next();
137 
138                 if (BlogsEntryPermission.contains(
139                         getPermissionChecker(), entry, ActionKeys.VIEW)) {
140 
141                     entries.add(entry);
142                 }
143             }
144         }
145 
146         return entries;
147     }
148 
149     public String getCompanyEntriesRSS(
150             long companyId, int max, String type, double version,
151             String displayStyle, String feedURL, String entryURL,
152             ThemeDisplay themeDisplay)
153         throws PortalException, SystemException {
154 
155         Company company = companyPersistence.findByPrimaryKey(companyId);
156 
157         String name = company.getName();
158         String description = name;
159         List<BlogsEntry> blogsEntries = getCompanyEntries(companyId, max);
160 
161         return exportToRSS(
162             name, description, type, version, displayStyle, feedURL, entryURL,
163             blogsEntries, themeDisplay);
164     }
165 
166     public BlogsEntry getEntry(long entryId)
167         throws PortalException, SystemException {
168 
169         BlogsEntryPermission.check(
170             getPermissionChecker(), entryId, ActionKeys.VIEW);
171 
172         return blogsEntryLocalService.getEntry(entryId);
173     }
174 
175     public BlogsEntry getEntry(long groupId, String urlTitle)
176         throws PortalException, SystemException {
177 
178         BlogsEntry entry = blogsEntryLocalService.getEntry(groupId, urlTitle);
179 
180         BlogsEntryPermission.check(
181             getPermissionChecker(), entry.getEntryId(), ActionKeys.VIEW);
182 
183         return entry;
184     }
185 
186     public List<BlogsEntry> getGroupEntries(long groupId, int max)
187         throws PortalException, SystemException {
188 
189         List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
190 
191         int lastIntervalStart = 0;
192         boolean listNotExhausted = true;
193 
194         while ((entries.size() < max) && listNotExhausted) {
195             List<BlogsEntry> entryList = blogsEntryLocalService.getGroupEntries(
196                 groupId, false, lastIntervalStart,
197                 lastIntervalStart + max);
198 
199             Iterator<BlogsEntry> itr = entryList.iterator();
200 
201             lastIntervalStart += max;
202             listNotExhausted = (entryList.size() == max);
203 
204             while (itr.hasNext() && (entries.size() < max)) {
205                 BlogsEntry entry = itr.next();
206 
207                 if (BlogsEntryPermission.contains(
208                         getPermissionChecker(), entry, ActionKeys.VIEW)) {
209 
210                     entries.add(entry);
211                 }
212             }
213         }
214 
215         return entries;
216     }
217 
218     public String getGroupEntriesRSS(
219             long groupId, int max, String type, double version,
220             String displayStyle, String feedURL, String entryURL,
221             ThemeDisplay themeDisplay)
222         throws PortalException, SystemException {
223 
224         Group group = groupPersistence.findByPrimaryKey(groupId);
225 
226         String name = group.getDescriptiveName();
227         String description = name;
228         List<BlogsEntry> blogsEntries = getGroupEntries(groupId, max);
229 
230         return exportToRSS(
231             name, description, type, version, displayStyle, feedURL, entryURL,
232             blogsEntries, themeDisplay);
233     }
234 
235     public List<BlogsEntry> getOrganizationEntries(long organizationId, int max)
236         throws PortalException, SystemException {
237 
238         List<BlogsEntry> entries = new ArrayList<BlogsEntry>();
239 
240         Date displayDate = new Date();
241         boolean draft = false;
242         int lastIntervalStart = 0;
243         boolean listNotExhausted = true;
244 
245         while ((entries.size() < max) && listNotExhausted) {
246             List<BlogsEntry> entryList = blogsEntryFinder.findByOrganizationId(
247                 organizationId, displayDate, draft, lastIntervalStart,
248                 lastIntervalStart + max);
249 
250             Iterator<BlogsEntry> itr = entryList.iterator();
251 
252             lastIntervalStart += max;
253             listNotExhausted = (entryList.size() == max);
254 
255             while (itr.hasNext() && (entries.size() < max)) {
256                 BlogsEntry entry = itr.next();
257 
258                 if (BlogsEntryPermission.contains(
259                         getPermissionChecker(), entry, ActionKeys.VIEW)) {
260 
261                     entries.add(entry);
262                 }
263             }
264         }
265 
266         return entries;
267     }
268 
269     public String getOrganizationEntriesRSS(
270             long organizationId, int max, String type, double version,
271             String displayStyle, String feedURL, String entryURL,
272             ThemeDisplay themeDisplay)
273         throws PortalException, SystemException {
274 
275         Organization organization = organizationPersistence.findByPrimaryKey(
276             organizationId);
277 
278         String name = organization.getName();
279         String description = name;
280         List<BlogsEntry> blogsEntries = getOrganizationEntries(
281             organizationId, max);
282 
283         return exportToRSS(
284             name, description, type, version, displayStyle, feedURL, entryURL,
285             blogsEntries, themeDisplay);
286     }
287 
288     public BlogsEntry updateEntry(
289             long entryId, String title, String content, int displayDateMonth,
290             int displayDateDay, int displayDateYear, int displayDateHour,
291             int displayDateMinute, boolean draft, boolean allowTrackbacks,
292             String[] trackbacks, String[] tagsEntries,
293             ThemeDisplay themeDisplay)
294         throws PortalException, SystemException {
295 
296         BlogsEntryPermission.check(
297             getPermissionChecker(), entryId, ActionKeys.UPDATE);
298 
299         return blogsEntryLocalService.updateEntry(
300             getUserId(), entryId, title, content, displayDateMonth,
301             displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
302             draft, allowTrackbacks, trackbacks, tagsEntries, themeDisplay);
303     }
304 
305     protected String exportToRSS(
306             String name, String description, String type, double version,
307             String displayStyle, String feedURL, String entryURL,
308             List<BlogsEntry> blogsEntries, ThemeDisplay themeDisplay)
309         throws SystemException {
310 
311         SyndFeed syndFeed = new SyndFeedImpl();
312 
313         syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
314         syndFeed.setTitle(name);
315         syndFeed.setLink(feedURL);
316         syndFeed.setDescription(description);
317 
318         List<SyndEntry> entries = new ArrayList<SyndEntry>();
319 
320         syndFeed.setEntries(entries);
321 
322         for (BlogsEntry entry : blogsEntries) {
323             String author = PortalUtil.getUserName(
324                 entry.getUserId(), entry.getUserName());
325 
326             String link = entryURL;
327 
328             if (link.endsWith("/blogs/rss")) {
329                 link =
330                     link.substring(0, link.length() - 3) + entry.getUrlTitle();
331             }
332             else {
333                 if (!link.endsWith("?")) {
334                     link += "&";
335                 }
336 
337                 link += "entryId=" + entry.getEntryId();
338             }
339 
340             String value = null;
341 
342             if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
343                 value = StringUtil.shorten(
344                     HtmlUtil.extractText(entry.getContent()),
345                     _RSS_ABSTRACT_LENGTH, StringPool.BLANK);
346             }
347             else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
348                 value = StringPool.BLANK;
349             }
350             else {
351                 value = StringUtil.replace(
352                     entry.getContent(),
353                     new String[] {
354                         "href=\"/",
355                         "src=\"/"
356                     },
357                     new String[] {
358                         "href=\"" + themeDisplay.getURLPortal() + "/",
359                         "src=\"" + themeDisplay.getURLPortal() + "/"
360                     }
361                 );
362             }
363 
364             SyndEntry syndEntry = new SyndEntryImpl();
365 
366             syndEntry.setAuthor(author);
367             syndEntry.setTitle(entry.getTitle());
368             syndEntry.setLink(link);
369             syndEntry.setUri(syndEntry.getLink());
370             syndEntry.setPublishedDate(entry.getCreateDate());
371             syndEntry.setUpdatedDate(entry.getModifiedDate());
372 
373             SyndContent syndContent = new SyndContentImpl();
374 
375             syndContent.setType(RSSUtil.DEFAULT_ENTRY_TYPE);
376             syndContent.setValue(value);
377 
378             syndEntry.setDescription(syndContent);
379 
380             entries.add(syndEntry);
381         }
382 
383         try {
384             return RSSUtil.export(syndFeed);
385         }
386         catch (FeedException fe) {
387             throw new SystemException(fe);
388         }
389     }
390 
391     private static final int _RSS_ABSTRACT_LENGTH = GetterUtil.getInteger(
392         PropsUtil.get(PropsKeys.BLOGS_RSS_ABSTRACT_LENGTH));
393 
394 }