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.journal.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.OrderByComparator;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.model.impl.ResourceImpl;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portlet.journal.DuplicateFeedIdException;
032: import com.liferay.portlet.journal.FeedContentFieldException;
033: import com.liferay.portlet.journal.FeedDescriptionException;
034: import com.liferay.portlet.journal.FeedIdException;
035: import com.liferay.portlet.journal.FeedNameException;
036: import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
037: import com.liferay.portlet.journal.NoSuchFeedException;
038: import com.liferay.portlet.journal.model.JournalFeed;
039: import com.liferay.portlet.journal.model.JournalStructure;
040: import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
041: import com.liferay.portlet.journal.service.base.JournalFeedLocalServiceBaseImpl;
042: import com.liferay.util.RSSUtil;
043:
044: import java.util.Date;
045: import java.util.List;
046:
047: import org.dom4j.Document;
048: import org.dom4j.DocumentHelper;
049: import org.dom4j.Element;
050: import org.dom4j.XPath;
051:
052: /**
053: * <a href="JournalFeedLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Raymond Aug�
056: *
057: */
058: public class JournalFeedLocalServiceImpl extends
059: JournalFeedLocalServiceBaseImpl {
060:
061: public JournalFeed addFeed(long userId, long plid, String feedId,
062: boolean autoFeedId, String name, String description,
063: String type, String structureId, String templateId,
064: String rendererTemplateId, int delta, String orderByCol,
065: String orderByType, String targetLayoutFriendlyUrl,
066: String targetPortletId, String contentField,
067: String feedType, double feedVersion,
068: boolean addCommunityPermissions, boolean addGuestPermissions)
069: throws PortalException, SystemException {
070:
071: return addFeed(null, userId, plid, feedId, autoFeedId, name,
072: description, type, structureId, templateId,
073: rendererTemplateId, delta, orderByCol, orderByType,
074: targetLayoutFriendlyUrl, targetPortletId, contentField,
075: feedType, feedVersion, Boolean
076: .valueOf(addCommunityPermissions), Boolean
077: .valueOf(addGuestPermissions), null, null);
078: }
079:
080: public JournalFeed addFeed(long userId, long plid, String feedId,
081: boolean autoFeedId, String name, String description,
082: String type, String structureId, String templateId,
083: String rendererTemplateId, int delta, String orderByCol,
084: String orderByType, String targetLayoutFriendlyUrl,
085: String targetPortletId, String contentField,
086: String feedType, double feedVersion,
087: String[] communityPermissions, String[] guestPermissions)
088: throws PortalException, SystemException {
089:
090: return addFeed(null, userId, plid, feedId, autoFeedId, name,
091: description, type, structureId, templateId,
092: rendererTemplateId, delta, orderByCol, orderByType,
093: targetLayoutFriendlyUrl, targetPortletId, contentField,
094: feedType, feedVersion, null, null,
095: communityPermissions, guestPermissions);
096: }
097:
098: public JournalFeed addFeed(String uuid, long userId, long plid,
099: String feedId, boolean autoFeedId, String name,
100: String description, String type, String structureId,
101: String templateId, String rendererTemplateId, int delta,
102: String orderByCol, String orderByType,
103: String targetLayoutFriendlyUrl, String targetPortletId,
104: String contentField, String feedType, double feedVersion,
105: boolean addCommunityPermissions, boolean addGuestPermissions)
106: throws PortalException, SystemException {
107:
108: return addFeed(uuid, userId, plid, feedId, autoFeedId, name,
109: description, type, structureId, templateId,
110: rendererTemplateId, delta, orderByCol, orderByType,
111: targetLayoutFriendlyUrl, targetPortletId, contentField,
112: feedType, feedVersion, Boolean
113: .valueOf(addCommunityPermissions), Boolean
114: .valueOf(addGuestPermissions), null, null);
115: }
116:
117: public JournalFeed addFeed(String uuid, long userId, long plid,
118: String feedId, boolean autoFeedId, String name,
119: String description, String type, String structureId,
120: String templateId, String rendererTemplateId, int delta,
121: String orderByCol, String orderByType,
122: String targetLayoutFriendlyUrl, String targetPortletId,
123: String contentField, String feedType, double feedVersion,
124: String[] communityPermissions, String[] guestPermissions)
125: throws PortalException, SystemException {
126:
127: return addFeed(uuid, userId, plid, feedId, autoFeedId, name,
128: description, type, structureId, templateId,
129: rendererTemplateId, delta, orderByCol, orderByType,
130: targetLayoutFriendlyUrl, targetPortletId, contentField,
131: feedType, feedVersion, null, null,
132: communityPermissions, guestPermissions);
133: }
134:
135: public JournalFeed addFeed(String uuid, long userId, long plid,
136: String feedId, boolean autoFeedId, String name,
137: String description, String type, String structureId,
138: String templateId, String rendererTemplateId, int delta,
139: String orderByCol, String orderByType,
140: String targetLayoutFriendlyUrl, String targetPortletId,
141: String contentField, String feedType, double feedVersion,
142: Boolean addCommunityPermissions,
143: Boolean addGuestPermissions, String[] communityPermissions,
144: String[] guestPermissions) throws PortalException,
145: SystemException {
146:
147: long groupId = PortalUtil.getPortletGroupId(plid);
148:
149: return addFeedToGroup(uuid, userId, groupId, feedId,
150: autoFeedId, name, description, type, structureId,
151: templateId, rendererTemplateId, delta, orderByCol,
152: orderByType, targetLayoutFriendlyUrl, targetPortletId,
153: contentField, feedType, feedVersion,
154: addCommunityPermissions, addGuestPermissions,
155: communityPermissions, guestPermissions);
156: }
157:
158: public JournalFeed addFeedToGroup(String uuid, long userId,
159: long groupId, String feedId, boolean autoFeedId,
160: String name, String description, String type,
161: String structureId, String templateId,
162: String rendererTemplateId, int delta, String orderByCol,
163: String orderByType, String targetLayoutFriendlyUrl,
164: String targetPortletId, String contentField,
165: String feedType, double feedVersion,
166: Boolean addCommunityPermissions,
167: Boolean addGuestPermissions, String[] communityPermissions,
168: String[] guestPermissions) throws PortalException,
169: SystemException {
170:
171: // Feed
172:
173: User user = userPersistence.findByPrimaryKey(userId);
174: feedId = feedId.trim().toUpperCase();
175: Date now = new Date();
176:
177: validate(groupId, feedId, autoFeedId, name, description,
178: structureId, targetLayoutFriendlyUrl, contentField);
179:
180: if (autoFeedId) {
181: feedId = String.valueOf(counterLocalService.increment());
182: }
183:
184: long id = counterLocalService.increment();
185:
186: JournalFeed feed = journalFeedPersistence.create(id);
187:
188: feed.setUuid(uuid);
189: feed.setGroupId(groupId);
190: feed.setCompanyId(user.getCompanyId());
191: feed.setUserId(user.getUserId());
192: feed.setUserName(user.getFullName());
193: feed.setCreateDate(now);
194: feed.setModifiedDate(now);
195: feed.setFeedId(feedId);
196: feed.setName(name);
197: feed.setDescription(description);
198: feed.setType(type);
199: feed.setStructureId(structureId);
200: feed.setTemplateId(templateId);
201: feed.setRendererTemplateId(rendererTemplateId);
202: feed.setDelta(delta);
203: feed.setOrderByCol(orderByCol);
204: feed.setOrderByType(orderByType);
205: feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
206: feed.setTargetPortletId(targetPortletId);
207: feed.setContentField(contentField);
208:
209: if (Validator.isNull(feedType)) {
210: feed.setFeedType(RSSUtil.DEFAULT_TYPE);
211: feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
212: } else {
213: feed.setFeedType(feedType);
214: feed.setFeedVersion(feedVersion);
215: }
216:
217: journalFeedPersistence.update(feed);
218:
219: // Resources
220:
221: if ((addCommunityPermissions != null)
222: && (addGuestPermissions != null)) {
223:
224: addFeedResources(feed, addCommunityPermissions
225: .booleanValue(), addGuestPermissions.booleanValue());
226: } else {
227: addFeedResources(feed, communityPermissions,
228: guestPermissions);
229: }
230:
231: return feed;
232: }
233:
234: public void addFeedResources(long feedId,
235: boolean addCommunityPermissions, boolean addGuestPermissions)
236: throws PortalException, SystemException {
237:
238: JournalFeed feed = journalFeedPersistence
239: .findByPrimaryKey(feedId);
240:
241: addFeedResources(feed, addCommunityPermissions,
242: addGuestPermissions);
243: }
244:
245: public void addFeedResources(JournalFeed feed,
246: boolean addCommunityPermissions, boolean addGuestPermissions)
247: throws PortalException, SystemException {
248:
249: resourceLocalService.addResources(feed.getCompanyId(), feed
250: .getGroupId(), feed.getUserId(), JournalFeed.class
251: .getName(), feed.getId(), false,
252: addCommunityPermissions, addGuestPermissions);
253: }
254:
255: public void addFeedResources(long feedId,
256: String[] communityPermissions, String[] guestPermissions)
257: throws PortalException, SystemException {
258:
259: JournalFeed feed = journalFeedPersistence
260: .findByPrimaryKey(feedId);
261:
262: addFeedResources(feed, communityPermissions, guestPermissions);
263: }
264:
265: public void addFeedResources(JournalFeed feed,
266: String[] communityPermissions, String[] guestPermissions)
267: throws PortalException, SystemException {
268:
269: resourceLocalService.addModelResources(feed.getCompanyId(),
270: feed.getGroupId(), feed.getUserId(), JournalFeed.class
271: .getName(), feed.getId(), communityPermissions,
272: guestPermissions);
273: }
274:
275: public void deleteFeed(long feedId) throws PortalException,
276: SystemException {
277:
278: JournalFeed feed = journalFeedPersistence
279: .findByPrimaryKey(feedId);
280:
281: deleteFeed(feed);
282: }
283:
284: public void deleteFeed(long groupId, String feedId)
285: throws PortalException, SystemException {
286:
287: JournalFeed feed = journalFeedPersistence.findByG_F(groupId,
288: feedId);
289:
290: deleteFeed(feed);
291: }
292:
293: public void deleteFeed(JournalFeed feed) throws PortalException,
294: SystemException {
295:
296: // Resources
297:
298: resourceLocalService.deleteResource(feed.getCompanyId(),
299: JournalFeed.class.getName(),
300: ResourceImpl.SCOPE_INDIVIDUAL, feed.getId());
301:
302: // Feed
303:
304: journalFeedPersistence.remove(feed.getPrimaryKey());
305: }
306:
307: public JournalFeed getFeed(long feedId) throws PortalException,
308: SystemException {
309:
310: return journalFeedPersistence.findByPrimaryKey(feedId);
311: }
312:
313: public JournalFeed getFeed(long groupId, String feedId)
314: throws PortalException, SystemException {
315:
316: return journalFeedPersistence.findByG_F(groupId, feedId);
317: }
318:
319: public List getFeeds() throws SystemException {
320: return journalFeedPersistence.findAll();
321: }
322:
323: public List getFeeds(long groupId) throws SystemException {
324: return journalFeedPersistence.findByGroupId(groupId);
325: }
326:
327: public List getFeeds(long groupId, int begin, int end)
328: throws SystemException {
329:
330: return journalFeedPersistence
331: .findByGroupId(groupId, begin, end);
332: }
333:
334: public int getFeedsCount(long groupId) throws SystemException {
335: return journalFeedPersistence.countByGroupId(groupId);
336: }
337:
338: public List search(long companyId, long groupId, String keywords,
339: int begin, int end, OrderByComparator obc)
340: throws SystemException {
341:
342: return journalFeedFinder.findByKeywords(companyId, groupId,
343: keywords, begin, end, obc);
344: }
345:
346: public List search(long companyId, long groupId, String feedId,
347: String name, String description, boolean andOperator,
348: int begin, int end, OrderByComparator obc)
349: throws SystemException {
350:
351: return journalFeedFinder
352: .findByC_G_F_N_D(companyId, groupId, feedId, name,
353: description, andOperator, begin, end, obc);
354: }
355:
356: public int searchCount(long companyId, long groupId, String keywords)
357: throws SystemException {
358:
359: return journalFeedFinder.countByKeywords(companyId, groupId,
360: keywords);
361: }
362:
363: public int searchCount(long companyId, long groupId, String feedId,
364: String name, String description, boolean andOperator)
365: throws SystemException {
366:
367: return journalFeedFinder.countByC_G_F_N_D(companyId, groupId,
368: feedId, name, description, andOperator);
369: }
370:
371: public JournalFeed updateFeed(long groupId, String feedId,
372: String name, String description, String type,
373: String structureId, String templateId,
374: String rendererTemplateId, int delta, String orderByCol,
375: String orderByType, String targetLayoutFriendlyUrl,
376: String targetPortletId, String contentField,
377: String feedType, double feedVersion)
378: throws PortalException, SystemException {
379:
380: // Feed
381:
382: validate(groupId, name, description, structureId,
383: targetLayoutFriendlyUrl, contentField);
384:
385: JournalFeed feed = journalFeedPersistence.findByG_F(groupId,
386: feedId);
387:
388: feed.setModifiedDate(new Date());
389: feed.setName(name);
390: feed.setDescription(description);
391: feed.setType(type);
392: feed.setStructureId(structureId);
393: feed.setTemplateId(templateId);
394: feed.setRendererTemplateId(rendererTemplateId);
395: feed.setDelta(delta);
396: feed.setOrderByCol(orderByCol);
397: feed.setOrderByType(orderByType);
398: feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
399: feed.setTargetPortletId(targetPortletId);
400: feed.setContentField(contentField);
401:
402: if (Validator.isNull(feedType)) {
403: feed.setFeedType(RSSUtil.DEFAULT_TYPE);
404: feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
405: } else {
406: feed.setFeedType(feedType);
407: feed.setFeedVersion(feedVersion);
408: }
409:
410: journalFeedPersistence.update(feed);
411:
412: return feed;
413: }
414:
415: protected boolean isValidStructureField(long groupId,
416: String structureId, String contentField) {
417:
418: if (contentField.equals(JournalFeedImpl.ARTICLE_DESCRIPTION)
419: || contentField
420: .equals(JournalFeedImpl.RENDERED_ARTICLE)) {
421:
422: return true;
423: } else {
424: try {
425: JournalStructure structure = journalStructurePersistence
426: .findByG_S(groupId, structureId);
427:
428: Document doc = PortalUtil.readDocumentFromXML(structure
429: .getXsd());
430:
431: XPath xpathSelector = DocumentHelper
432: .createXPath("//dynamic-element[@name='"
433: + contentField + "']");
434:
435: Element el = (Element) xpathSelector
436: .selectSingleNode(doc);
437:
438: if (el != null) {
439: return true;
440: }
441: } catch (Exception e) {
442: }
443: }
444:
445: return false;
446: }
447:
448: protected void validate(long groupId, String feedId,
449: boolean autoFeedId, String name, String description,
450: String structureId, String targetLayoutFriendlyUrl,
451: String contentField) throws PortalException,
452: SystemException {
453:
454: if (!autoFeedId) {
455: if ((Validator.isNull(feedId))
456: || (Validator.isNumber(feedId))
457: || (feedId.indexOf(StringPool.SPACE) != -1)) {
458:
459: throw new FeedIdException();
460: }
461:
462: try {
463: journalFeedPersistence.findByG_F(groupId, feedId);
464:
465: throw new DuplicateFeedIdException();
466: } catch (NoSuchFeedException nsfe) {
467: }
468: }
469:
470: validate(groupId, name, description, structureId,
471: targetLayoutFriendlyUrl, contentField);
472: }
473:
474: protected void validate(long groupId, String name,
475: String description, String structureId,
476: String targetLayoutFriendlyUrl, String contentField)
477: throws PortalException {
478:
479: if (Validator.isNull(name)) {
480: throw new FeedNameException();
481: }
482:
483: if (Validator.isNull(description)) {
484: throw new FeedDescriptionException();
485: }
486:
487: if (Validator.isNull(targetLayoutFriendlyUrl)) {
488: throw new FeedTargetLayoutFriendlyUrlException();
489: }
490:
491: if (!isValidStructureField(groupId, structureId, contentField)) {
492: throw new FeedContentFieldException();
493: }
494: }
495:
496: }
|