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