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