001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.blogs.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.ActionKeys;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.StringUtil;
029: import com.liferay.portal.model.Company;
030: import com.liferay.portal.model.Group;
031: import com.liferay.portal.model.Organization;
032: import com.liferay.portal.service.permission.PortletPermissionUtil;
033: import com.liferay.portal.theme.ThemeDisplay;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.portal.util.PortletKeys;
036: import com.liferay.portal.util.PropsUtil;
037: import com.liferay.portlet.blogs.model.BlogsEntry;
038: import com.liferay.portlet.blogs.service.base.BlogsEntryServiceBaseImpl;
039: import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
040: import com.liferay.util.Html;
041: import com.liferay.util.RSSUtil;
042:
043: import com.sun.syndication.feed.synd.SyndContent;
044: import com.sun.syndication.feed.synd.SyndContentImpl;
045: import com.sun.syndication.feed.synd.SyndEntry;
046: import com.sun.syndication.feed.synd.SyndEntryImpl;
047: import com.sun.syndication.feed.synd.SyndFeed;
048: import com.sun.syndication.feed.synd.SyndFeedImpl;
049: import com.sun.syndication.io.FeedException;
050:
051: import java.io.IOException;
052:
053: import java.util.ArrayList;
054: import java.util.Iterator;
055: import java.util.List;
056:
057: /**
058: * <a href="BlogsEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
059: *
060: * @author Brian Wing Shun Chan
061: *
062: */
063: public class BlogsEntryServiceImpl extends BlogsEntryServiceBaseImpl {
064:
065: public BlogsEntry addEntry(long plid, String title, String content,
066: int displayDateMonth, int displayDateDay,
067: int displayDateYear, int displayDateHour,
068: int displayDateMinute, String[] tagsEntries,
069: boolean addCommunityPermissions,
070: boolean addGuestPermissions, ThemeDisplay themeDisplay)
071: throws PortalException, SystemException {
072:
073: PortletPermissionUtil.check(getPermissionChecker(), plid,
074: PortletKeys.BLOGS, ActionKeys.ADD_ENTRY);
075:
076: return blogsEntryLocalService.addEntry(getUserId(), plid,
077: title, content, displayDateMonth, displayDateDay,
078: displayDateYear, displayDateHour, displayDateMinute,
079: tagsEntries, addCommunityPermissions,
080: addGuestPermissions, themeDisplay);
081: }
082:
083: public BlogsEntry addEntry(long plid, String title, String content,
084: int displayDateMonth, int displayDateDay,
085: int displayDateYear, int displayDateHour,
086: int displayDateMinute, String[] tagsEntries,
087: String[] communityPermissions, String[] guestPermissions,
088: ThemeDisplay themeDisplay) throws PortalException,
089: SystemException {
090:
091: PortletPermissionUtil.check(getPermissionChecker(), plid,
092: PortletKeys.BLOGS, ActionKeys.ADD_ENTRY);
093:
094: return blogsEntryLocalService.addEntry(getUserId(), plid,
095: title, content, displayDateMonth, displayDateDay,
096: displayDateYear, displayDateHour, displayDateMinute,
097: tagsEntries, communityPermissions, guestPermissions,
098: themeDisplay);
099: }
100:
101: public void deleteEntry(long entryId) throws PortalException,
102: SystemException {
103:
104: BlogsEntryPermission.check(getPermissionChecker(), entryId,
105: ActionKeys.DELETE);
106:
107: blogsEntryLocalService.deleteEntry(entryId);
108: }
109:
110: public List getCompanyEntries(long companyId, int max)
111: throws PortalException, SystemException {
112:
113: List entries = new ArrayList();
114:
115: Iterator itr = blogsEntryLocalService.getCompanyEntries(
116: companyId, 0, _MAX_END).iterator();
117:
118: while (itr.hasNext() && (entries.size() < max)) {
119: BlogsEntry entry = (BlogsEntry) itr.next();
120:
121: if (BlogsEntryPermission.contains(getPermissionChecker(),
122: entry, ActionKeys.VIEW)) {
123:
124: entries.add(entry);
125: }
126: }
127:
128: return entries;
129: }
130:
131: public String getCompanyEntriesRSS(long companyId, int max,
132: String type, double version, String displayStyle,
133: String feedURL, String entryURL) throws PortalException,
134: SystemException {
135:
136: Company company = companyPersistence
137: .findByPrimaryKey(companyId);
138:
139: String name = company.getName();
140:
141: List blogsEntries = getCompanyEntries(companyId, max);
142:
143: return exportToRSS(name, null, type, version, displayStyle,
144: feedURL, entryURL, blogsEntries);
145: }
146:
147: public BlogsEntry getEntry(long entryId) throws PortalException,
148: SystemException {
149:
150: BlogsEntryPermission.check(getPermissionChecker(), entryId,
151: ActionKeys.VIEW);
152:
153: return blogsEntryLocalService.getEntry(entryId);
154: }
155:
156: public BlogsEntry getEntry(long groupId, String urlTitle)
157: throws PortalException, SystemException {
158:
159: BlogsEntry entry = blogsEntryLocalService.getEntry(groupId,
160: urlTitle);
161:
162: BlogsEntryPermission.check(getPermissionChecker(), entry
163: .getEntryId(), ActionKeys.VIEW);
164:
165: return entry;
166: }
167:
168: public List getGroupEntries(long groupId, int max)
169: throws PortalException, SystemException {
170:
171: List entries = new ArrayList();
172:
173: Iterator itr = blogsEntryLocalService.getGroupEntries(groupId,
174: 0, _MAX_END).iterator();
175:
176: while (itr.hasNext() && (entries.size() < max)) {
177: BlogsEntry entry = (BlogsEntry) itr.next();
178:
179: if (BlogsEntryPermission.contains(getPermissionChecker(),
180: entry, ActionKeys.VIEW)) {
181:
182: entries.add(entry);
183: }
184: }
185:
186: return entries;
187: }
188:
189: public String getGroupEntriesRSS(long groupId, int max,
190: String type, double version, String displayStyle,
191: String feedURL, String entryURL) throws PortalException,
192: SystemException {
193:
194: Group group = groupPersistence.findByPrimaryKey(groupId);
195:
196: String name = group.getDescriptiveName();
197:
198: List blogsEntries = getGroupEntries(groupId, max);
199:
200: return exportToRSS(name, null, type, version, displayStyle,
201: feedURL, entryURL, blogsEntries);
202: }
203:
204: public List getOrganizationEntries(long organizationId, int max)
205: throws PortalException, SystemException {
206:
207: List entries = new ArrayList();
208:
209: Iterator itr = blogsEntryFinder.findByOrganizationId(
210: organizationId, 0, _MAX_END).iterator();
211:
212: while (itr.hasNext() && (entries.size() < max)) {
213: BlogsEntry entry = (BlogsEntry) itr.next();
214:
215: if (BlogsEntryPermission.contains(getPermissionChecker(),
216: entry, ActionKeys.VIEW)) {
217:
218: entries.add(entry);
219: }
220: }
221:
222: return entries;
223: }
224:
225: public String getOrganizationEntriesRSS(long organizationId,
226: int max, String type, double version, String displayStyle,
227: String feedURL, String entryURL) throws PortalException,
228: SystemException {
229:
230: Organization organization = organizationPersistence
231: .findByPrimaryKey(organizationId);
232:
233: String name = organization.getName();
234:
235: List blogsEntries = getOrganizationEntries(organizationId, max);
236:
237: return exportToRSS(name, null, type, version, displayStyle,
238: feedURL, entryURL, blogsEntries);
239: }
240:
241: public BlogsEntry updateEntry(long entryId, String title,
242: String content, int displayDateMonth, int displayDateDay,
243: int displayDateYear, int displayDateHour,
244: int displayDateMinute, String[] tagsEntries,
245: ThemeDisplay themeDisplay) throws PortalException,
246: SystemException {
247:
248: BlogsEntryPermission.check(getPermissionChecker(), entryId,
249: ActionKeys.UPDATE);
250:
251: return blogsEntryLocalService.updateEntry(getUserId(), entryId,
252: title, content, displayDateMonth, displayDateDay,
253: displayDateYear, displayDateHour, displayDateMinute,
254: tagsEntries, themeDisplay);
255: }
256:
257: protected String exportToRSS(String name, String description,
258: String type, double version, String displayStyle,
259: String feedURL, String entryURL, List blogsEntries)
260: throws SystemException {
261:
262: SyndFeed syndFeed = new SyndFeedImpl();
263:
264: syndFeed.setFeedType(type + "_" + version);
265: syndFeed.setTitle(name);
266: syndFeed.setLink(feedURL);
267: syndFeed
268: .setDescription(GetterUtil.getString(description, name));
269:
270: List entries = new ArrayList();
271:
272: syndFeed.setEntries(entries);
273:
274: Iterator itr = blogsEntries.iterator();
275:
276: while (itr.hasNext()) {
277: BlogsEntry entry = (BlogsEntry) itr.next();
278:
279: String author = PortalUtil.getUserName(entry.getUserId(),
280: entry.getUserName());
281:
282: String link = entryURL;
283:
284: if (link.endsWith("/blogs/rss")) {
285: link = link.substring(0, link.length() - 3)
286: + entry.getUrlTitle();
287: } else {
288: if (!link.endsWith("?")) {
289: link += "&";
290: }
291:
292: link += "entryId=" + entry.getEntryId();
293: }
294:
295: String value = null;
296:
297: if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
298: value = StringUtil.shorten(Html.stripHtml(entry
299: .getContent()), _RSS_ABSTRACT_LENGTH,
300: StringPool.BLANK);
301: } else {
302: value = entry.getContent();
303: }
304:
305: SyndEntry syndEntry = new SyndEntryImpl();
306:
307: syndEntry.setAuthor(author);
308: syndEntry.setTitle(entry.getTitle());
309: syndEntry.setLink(link);
310: syndEntry.setPublishedDate(entry.getCreateDate());
311:
312: SyndContent syndContent = new SyndContentImpl();
313:
314: syndContent.setType("html");
315: syndContent.setValue(value);
316:
317: syndEntry.setDescription(syndContent);
318:
319: entries.add(syndEntry);
320: }
321:
322: try {
323: return RSSUtil.export(syndFeed);
324: } catch (FeedException fe) {
325: throw new SystemException(fe);
326: } catch (IOException ioe) {
327: throw new SystemException(ioe);
328: }
329: }
330:
331: private static final int _MAX_END = 200;
332:
333: private static final int _RSS_ABSTRACT_LENGTH = GetterUtil
334: .getInteger(PropsUtil
335: .get(PropsUtil.BLOGS_RSS_ABSTRACT_LENGTH));
336:
337: }
|