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