1
22
23 package com.liferay.portlet.journal.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.OrderByComparator;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.model.ResourceConstants;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.util.DocumentUtil;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.portlet.journal.DuplicateFeedIdException;
35 import com.liferay.portlet.journal.FeedContentFieldException;
36 import com.liferay.portlet.journal.FeedDescriptionException;
37 import com.liferay.portlet.journal.FeedIdException;
38 import com.liferay.portlet.journal.FeedNameException;
39 import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
40 import com.liferay.portlet.journal.NoSuchFeedException;
41 import com.liferay.portlet.journal.model.JournalFeed;
42 import com.liferay.portlet.journal.model.JournalStructure;
43 import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
44 import com.liferay.portlet.journal.service.base.JournalFeedLocalServiceBaseImpl;
45 import com.liferay.util.RSSUtil;
46
47 import java.util.Date;
48 import java.util.List;
49
50 import org.dom4j.Document;
51 import org.dom4j.DocumentHelper;
52 import org.dom4j.Element;
53 import org.dom4j.XPath;
54
55
61 public class JournalFeedLocalServiceImpl
62 extends JournalFeedLocalServiceBaseImpl {
63
64 public JournalFeed addFeed(
65 long userId, long plid, String feedId, boolean autoFeedId,
66 String name, String description, String type, String structureId,
67 String templateId, String rendererTemplateId, int delta,
68 String orderByCol, String orderByType,
69 String targetLayoutFriendlyUrl, String targetPortletId,
70 String contentField, String feedType, double feedVersion,
71 boolean addCommunityPermissions, boolean addGuestPermissions)
72 throws PortalException, SystemException {
73
74 return addFeed(
75 null, userId, plid, feedId, autoFeedId, name, description, type,
76 structureId, templateId, rendererTemplateId, delta, orderByCol,
77 orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
78 feedType, feedVersion, Boolean.valueOf(addCommunityPermissions),
79 Boolean.valueOf(addGuestPermissions), null, null);
80 }
81
82 public JournalFeed addFeed(
83 long userId, long plid, String feedId, boolean autoFeedId,
84 String name, String description, String type, String structureId,
85 String templateId, String rendererTemplateId, int delta,
86 String orderByCol, String orderByType,
87 String targetLayoutFriendlyUrl, String targetPortletId,
88 String contentField, String feedType, double feedVersion,
89 String[] communityPermissions, String[] guestPermissions)
90 throws PortalException, SystemException {
91
92 return addFeed(
93 null, userId, plid, feedId, autoFeedId, name, description, type,
94 structureId, templateId, rendererTemplateId, delta, orderByCol,
95 orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
96 feedType, feedVersion, null, null, communityPermissions,
97 guestPermissions);
98 }
99
100 public JournalFeed addFeed(
101 String uuid, long userId, long plid, String feedId,
102 boolean autoFeedId, String name, String description, String type,
103 String structureId, String templateId, String rendererTemplateId,
104 int delta, String orderByCol, String orderByType,
105 String targetLayoutFriendlyUrl, String targetPortletId,
106 String contentField, String feedType, double feedVersion,
107 boolean addCommunityPermissions, boolean addGuestPermissions)
108 throws PortalException, SystemException {
109
110 return addFeed(
111 uuid, userId, plid, feedId, autoFeedId, name, description, type,
112 structureId, templateId, rendererTemplateId, delta, orderByCol,
113 orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
114 feedType, feedVersion, Boolean.valueOf(addCommunityPermissions),
115 Boolean.valueOf(addGuestPermissions), null, null);
116 }
117
118 public JournalFeed addFeed(
119 String uuid, long userId, long plid, String feedId,
120 boolean autoFeedId, String name, String description, String type,
121 String structureId, String templateId, String rendererTemplateId,
122 int delta, String orderByCol, String orderByType,
123 String targetLayoutFriendlyUrl, String targetPortletId,
124 String contentField, String feedType, double feedVersion,
125 String[] communityPermissions, String[] guestPermissions)
126 throws PortalException, SystemException {
127
128 return addFeed(
129 uuid, userId, plid, feedId, autoFeedId, name, description, type,
130 structureId, templateId, rendererTemplateId, delta, orderByCol,
131 orderByType,targetLayoutFriendlyUrl, targetPortletId, contentField,
132 feedType, feedVersion, null, null, communityPermissions,
133 guestPermissions);
134 }
135
136 public JournalFeed addFeed(
137 String uuid, long userId, long plid, String feedId,
138 boolean autoFeedId, String name, String description, String type,
139 String structureId, String templateId, String rendererTemplateId,
140 int delta, String orderByCol, String orderByType,
141 String targetLayoutFriendlyUrl, String targetPortletId,
142 String contentField, String feedType, double feedVersion,
143 Boolean addCommunityPermissions, Boolean addGuestPermissions,
144 String[] communityPermissions, String[] guestPermissions)
145 throws PortalException, SystemException {
146
147 long groupId = PortalUtil.getPortletGroupId(plid);
148
149 return addFeedToGroup(
150 uuid, userId, groupId, feedId, autoFeedId, name, description, type,
151 structureId, templateId, rendererTemplateId, delta, orderByCol,
152 orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
153 feedType, feedVersion, addCommunityPermissions, addGuestPermissions,
154 communityPermissions, guestPermissions);
155 }
156
157 public JournalFeed addFeedToGroup(
158 String uuid, long userId, long groupId, String feedId,
159 boolean autoFeedId, String name, String description, String type,
160 String structureId, String templateId, String rendererTemplateId,
161 int delta, String orderByCol, String orderByType,
162 String targetLayoutFriendlyUrl, String targetPortletId,
163 String contentField, String feedType, double feedVersion,
164 Boolean addCommunityPermissions, Boolean addGuestPermissions,
165 String[] communityPermissions, String[] guestPermissions)
166 throws PortalException, SystemException {
167
168
170 User user = userPersistence.findByPrimaryKey(userId);
171 feedId = feedId.trim().toUpperCase();
172 Date now = new Date();
173
174 validate(
175 groupId, feedId, autoFeedId, name, description, structureId,
176 targetLayoutFriendlyUrl, contentField);
177
178 if (autoFeedId) {
179 feedId = String.valueOf(counterLocalService.increment());
180 }
181
182 long id = counterLocalService.increment();
183
184 JournalFeed feed = journalFeedPersistence.create(id);
185
186 feed.setUuid(uuid);
187 feed.setGroupId(groupId);
188 feed.setCompanyId(user.getCompanyId());
189 feed.setUserId(user.getUserId());
190 feed.setUserName(user.getFullName());
191 feed.setCreateDate(now);
192 feed.setModifiedDate(now);
193 feed.setFeedId(feedId);
194 feed.setName(name);
195 feed.setDescription(description);
196 feed.setType(type);
197 feed.setStructureId(structureId);
198 feed.setTemplateId(templateId);
199 feed.setRendererTemplateId(rendererTemplateId);
200 feed.setDelta(delta);
201 feed.setOrderByCol(orderByCol);
202 feed.setOrderByType(orderByType);
203 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
204 feed.setTargetPortletId(targetPortletId);
205 feed.setContentField(contentField);
206
207 if (Validator.isNull(feedType)) {
208 feed.setFeedType(RSSUtil.DEFAULT_TYPE);
209 feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
210 }
211 else {
212 feed.setFeedType(feedType);
213 feed.setFeedVersion(feedVersion);
214 }
215
216 journalFeedPersistence.update(feed, false);
217
218
220 if ((addCommunityPermissions != null) &&
221 (addGuestPermissions != null)) {
222
223 addFeedResources(
224 feed, addCommunityPermissions.booleanValue(),
225 addGuestPermissions.booleanValue());
226 }
227 else {
228 addFeedResources(feed, communityPermissions, guestPermissions);
229 }
230
231 return feed;
232 }
233
234 public void addFeedResources(
235 long feedId, boolean addCommunityPermissions,
236 boolean addGuestPermissions)
237 throws PortalException, SystemException {
238
239 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
240
241 addFeedResources(feed, addCommunityPermissions, addGuestPermissions);
242 }
243
244 public void addFeedResources(
245 JournalFeed feed, boolean addCommunityPermissions,
246 boolean addGuestPermissions)
247 throws PortalException, SystemException {
248
249 resourceLocalService.addResources(
250 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
251 JournalFeed.class.getName(), feed.getId(), false,
252 addCommunityPermissions, addGuestPermissions);
253 }
254
255 public void addFeedResources(
256 long feedId, String[] communityPermissions,
257 String[] guestPermissions)
258 throws PortalException, SystemException {
259
260 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
261
262 addFeedResources(feed, communityPermissions, guestPermissions);
263 }
264
265 public void addFeedResources(
266 JournalFeed feed, String[] communityPermissions,
267 String[] guestPermissions)
268 throws PortalException, SystemException {
269
270 resourceLocalService.addModelResources(
271 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
272 JournalFeed.class.getName(), feed.getId(), communityPermissions,
273 guestPermissions);
274 }
275
276 public void deleteFeed(long feedId)
277 throws PortalException, SystemException {
278
279 JournalFeed feed = journalFeedPersistence.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, feedId);
288
289 deleteFeed(feed);
290 }
291
292 public void deleteFeed(JournalFeed feed)
293 throws PortalException, SystemException {
294
295
297 resourceLocalService.deleteResource(
298 feed.getCompanyId(), JournalFeed.class.getName(),
299 ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
300
301
303 journalFeedPersistence.remove(feed.getPrimaryKey());
304 }
305
306 public JournalFeed getFeed(long feedId)
307 throws PortalException, SystemException {
308
309 return journalFeedPersistence.findByPrimaryKey(feedId);
310 }
311
312 public JournalFeed getFeed(long groupId, String feedId)
313 throws PortalException, SystemException {
314
315 return journalFeedPersistence.findByG_F(groupId, feedId);
316 }
317
318 public List<JournalFeed> getFeeds() throws SystemException {
319 return journalFeedPersistence.findAll();
320 }
321
322 public List<JournalFeed> getFeeds(long groupId) throws SystemException {
323 return journalFeedPersistence.findByGroupId(groupId);
324 }
325
326 public List<JournalFeed> getFeeds(long groupId, int begin, int end)
327 throws SystemException {
328
329 return journalFeedPersistence.findByGroupId(groupId, begin, end);
330 }
331
332 public int getFeedsCount(long groupId) throws SystemException {
333 return journalFeedPersistence.countByGroupId(groupId);
334 }
335
336 public List<JournalFeed> search(
337 long companyId, long groupId, String keywords, int begin, int end,
338 OrderByComparator obc)
339 throws SystemException {
340
341 return journalFeedFinder.findByKeywords(
342 companyId, groupId, keywords, begin, end, obc);
343 }
344
345 public List<JournalFeed> search(
346 long companyId, long groupId, String feedId, String name,
347 String description, boolean andOperator, int begin, int end,
348 OrderByComparator obc)
349 throws SystemException {
350
351 return journalFeedFinder.findByC_G_F_N_D(
352 companyId, groupId, feedId, name, description, andOperator, begin,
353 end, obc);
354 }
355
356 public int searchCount(long companyId, long groupId, String keywords)
357 throws SystemException {
358
359 return journalFeedFinder.countByKeywords(
360 companyId, groupId, keywords);
361 }
362
363 public int searchCount(
364 long companyId, long groupId, String feedId, String name,
365 String description, boolean andOperator)
366 throws SystemException {
367
368 return journalFeedFinder.countByC_G_F_N_D(
369 companyId, groupId, feedId, name, description, andOperator);
370 }
371
372 public JournalFeed updateFeed(
373 long groupId, String feedId, String name, String description,
374 String type, String structureId, String templateId,
375 String rendererTemplateId, int delta, String orderByCol,
376 String orderByType, String targetLayoutFriendlyUrl,
377 String targetPortletId, String contentField, String feedType,
378 double feedVersion)
379 throws PortalException, SystemException{
380
381
383 validate(
384 groupId, name, description, structureId, targetLayoutFriendlyUrl,
385 contentField);
386
387 JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
388
389 feed.setModifiedDate(new Date());
390 feed.setName(name);
391 feed.setDescription(description);
392 feed.setType(type);
393 feed.setStructureId(structureId);
394 feed.setTemplateId(templateId);
395 feed.setRendererTemplateId(rendererTemplateId);
396 feed.setDelta(delta);
397 feed.setOrderByCol(orderByCol);
398 feed.setOrderByType(orderByType);
399 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
400 feed.setTargetPortletId(targetPortletId);
401 feed.setContentField(contentField);
402
403 if (Validator.isNull(feedType)) {
404 feed.setFeedType(RSSUtil.DEFAULT_TYPE);
405 feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
406 }
407 else {
408 feed.setFeedType(feedType);
409 feed.setFeedVersion(feedVersion);
410 }
411
412 journalFeedPersistence.update(feed, false);
413
414 return feed;
415 }
416
417 protected boolean isValidStructureField(
418 long groupId, String structureId, String contentField) {
419
420 if (contentField.equals(JournalFeedImpl.ARTICLE_DESCRIPTION) ||
421 contentField.equals(JournalFeedImpl.RENDERED_ARTICLE)) {
422
423 return true;
424 }
425 else {
426 try {
427 JournalStructure structure =
428 journalStructurePersistence.findByG_S(groupId, structureId);
429
430 Document doc = DocumentUtil.readDocumentFromXML(
431 structure.getXsd());
432
433 XPath xpathSelector = DocumentHelper.createXPath(
434 "//dynamic-element[@name='"+ contentField + "']");
435
436 Element el = (Element)xpathSelector.selectSingleNode(doc);
437
438 if (el != null) {
439 return true;
440 }
441 }
442 catch (Exception e) {
443 }
444 }
445
446 return false;
447 }
448
449 protected void validate(
450 long groupId, String feedId, boolean autoFeedId, String name,
451 String description, String structureId,
452 String targetLayoutFriendlyUrl, String contentField)
453 throws PortalException, SystemException {
454
455 if (!autoFeedId) {
456 if ((Validator.isNull(feedId)) || (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 }
467 catch (NoSuchFeedException nsfe) {
468 }
469 }
470
471 validate(
472 groupId, name, description, structureId, targetLayoutFriendlyUrl,
473 contentField);
474 }
475
476 protected void validate(
477 long groupId, String name, String description, String structureId,
478 String targetLayoutFriendlyUrl, String contentField)
479 throws PortalException {
480
481 if (Validator.isNull(name)) {
482 throw new FeedNameException();
483 }
484
485 if (Validator.isNull(description)) {
486 throw new FeedDescriptionException();
487 }
488
489 if (Validator.isNull(targetLayoutFriendlyUrl)) {
490 throw new FeedTargetLayoutFriendlyUrlException();
491 }
492
493 if (!isValidStructureField(groupId, structureId, contentField)) {
494 throw new FeedContentFieldException();
495 }
496 }
497
498 }