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 groupId, 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, groupId, 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 groupId, 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, groupId, 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 groupId, 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, groupId, 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 groupId, 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, groupId, 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 groupId, 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
146 User user = userPersistence.findByPrimaryKey(userId);
147 feedId = feedId.trim().toUpperCase();
148 Date now = new Date();
149
150 validate(
151 user.getCompanyId(), groupId, feedId, autoFeedId, name, description,
152 structureId, targetLayoutFriendlyUrl, contentField);
153
154 if (autoFeedId) {
155 feedId = String.valueOf(counterLocalService.increment());
156 }
157
158 long id = counterLocalService.increment();
159
160 JournalFeed feed = journalFeedPersistence.create(id);
161
162 feed.setUuid(uuid);
163 feed.setGroupId(groupId);
164 feed.setCompanyId(user.getCompanyId());
165 feed.setUserId(user.getUserId());
166 feed.setUserName(user.getFullName());
167 feed.setCreateDate(now);
168 feed.setModifiedDate(now);
169 feed.setFeedId(feedId);
170 feed.setName(name);
171 feed.setDescription(description);
172 feed.setType(type);
173 feed.setStructureId(structureId);
174 feed.setTemplateId(templateId);
175 feed.setRendererTemplateId(rendererTemplateId);
176 feed.setDelta(delta);
177 feed.setOrderByCol(orderByCol);
178 feed.setOrderByType(orderByType);
179 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
180 feed.setTargetPortletId(targetPortletId);
181 feed.setContentField(contentField);
182
183 if (Validator.isNull(feedType)) {
184 feed.setFeedType(RSSUtil.DEFAULT_TYPE);
185 feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
186 }
187 else {
188 feed.setFeedType(feedType);
189 feed.setFeedVersion(feedVersion);
190 }
191
192 journalFeedPersistence.update(feed, false);
193
194
196 if ((addCommunityPermissions != null) &&
197 (addGuestPermissions != null)) {
198
199 addFeedResources(
200 feed, addCommunityPermissions.booleanValue(),
201 addGuestPermissions.booleanValue());
202 }
203 else {
204 addFeedResources(feed, communityPermissions, guestPermissions);
205 }
206
207 return feed;
208 }
209
210 public void addFeedResources(
211 long feedId, boolean addCommunityPermissions,
212 boolean addGuestPermissions)
213 throws PortalException, SystemException {
214
215 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
216
217 addFeedResources(feed, addCommunityPermissions, addGuestPermissions);
218 }
219
220 public void addFeedResources(
221 JournalFeed feed, boolean addCommunityPermissions,
222 boolean addGuestPermissions)
223 throws PortalException, SystemException {
224
225 resourceLocalService.addResources(
226 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
227 JournalFeed.class.getName(), feed.getId(), false,
228 addCommunityPermissions, addGuestPermissions);
229 }
230
231 public void addFeedResources(
232 long feedId, String[] communityPermissions,
233 String[] guestPermissions)
234 throws PortalException, SystemException {
235
236 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
237
238 addFeedResources(feed, communityPermissions, guestPermissions);
239 }
240
241 public void addFeedResources(
242 JournalFeed feed, String[] communityPermissions,
243 String[] guestPermissions)
244 throws PortalException, SystemException {
245
246 resourceLocalService.addModelResources(
247 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
248 JournalFeed.class.getName(), feed.getId(), communityPermissions,
249 guestPermissions);
250 }
251
252 public void deleteFeed(long feedId)
253 throws PortalException, SystemException {
254
255 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
256
257 deleteFeed(feed);
258 }
259
260 public void deleteFeed(long groupId, String feedId)
261 throws PortalException, SystemException {
262
263 JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
264
265 deleteFeed(feed);
266 }
267
268 public void deleteFeed(JournalFeed feed)
269 throws PortalException, SystemException {
270
271
273 resourceLocalService.deleteResource(
274 feed.getCompanyId(), JournalFeed.class.getName(),
275 ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
276
277
279 journalFeedPersistence.remove(feed);
280 }
281
282 public JournalFeed getFeed(long feedId)
283 throws PortalException, SystemException {
284
285 return journalFeedPersistence.findByPrimaryKey(feedId);
286 }
287
288 public JournalFeed getFeed(long groupId, String feedId)
289 throws PortalException, SystemException {
290
291 return journalFeedPersistence.findByG_F(groupId, feedId);
292 }
293
294 public List<JournalFeed> getFeeds() throws SystemException {
295 return journalFeedPersistence.findAll();
296 }
297
298 public List<JournalFeed> getFeeds(long groupId) throws SystemException {
299 return journalFeedPersistence.findByGroupId(groupId);
300 }
301
302 public List<JournalFeed> getFeeds(long groupId, int start, int end)
303 throws SystemException {
304
305 return journalFeedPersistence.findByGroupId(groupId, start, end);
306 }
307
308 public int getFeedsCount(long groupId) throws SystemException {
309 return journalFeedPersistence.countByGroupId(groupId);
310 }
311
312 public List<JournalFeed> search(
313 long companyId, long groupId, String keywords, int start, int end,
314 OrderByComparator obc)
315 throws SystemException {
316
317 return journalFeedFinder.findByKeywords(
318 companyId, groupId, keywords, start, end, obc);
319 }
320
321 public List<JournalFeed> search(
322 long companyId, long groupId, String feedId, String name,
323 String description, boolean andOperator, int start, int end,
324 OrderByComparator obc)
325 throws SystemException {
326
327 return journalFeedFinder.findByC_G_F_N_D(
328 companyId, groupId, feedId, name, description, andOperator, start,
329 end, obc);
330 }
331
332 public int searchCount(long companyId, long groupId, String keywords)
333 throws SystemException {
334
335 return journalFeedFinder.countByKeywords(
336 companyId, groupId, keywords);
337 }
338
339 public int searchCount(
340 long companyId, long groupId, String feedId, String name,
341 String description, boolean andOperator)
342 throws SystemException {
343
344 return journalFeedFinder.countByC_G_F_N_D(
345 companyId, groupId, feedId, name, description, andOperator);
346 }
347
348 public JournalFeed updateFeed(
349 long groupId, String feedId, String name, String description,
350 String type, String structureId, String templateId,
351 String rendererTemplateId, int delta, String orderByCol,
352 String orderByType, String targetLayoutFriendlyUrl,
353 String targetPortletId, String contentField, String feedType,
354 double feedVersion)
355 throws PortalException, SystemException{
356
357
359 JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
360
361 validate(
362 feed.getCompanyId(), groupId, name, description, structureId,
363 targetLayoutFriendlyUrl, contentField);
364
365 feed.setModifiedDate(new Date());
366 feed.setName(name);
367 feed.setDescription(description);
368 feed.setType(type);
369 feed.setStructureId(structureId);
370 feed.setTemplateId(templateId);
371 feed.setRendererTemplateId(rendererTemplateId);
372 feed.setDelta(delta);
373 feed.setOrderByCol(orderByCol);
374 feed.setOrderByType(orderByType);
375 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
376 feed.setTargetPortletId(targetPortletId);
377 feed.setContentField(contentField);
378
379 if (Validator.isNull(feedType)) {
380 feed.setFeedType(RSSUtil.DEFAULT_TYPE);
381 feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
382 }
383 else {
384 feed.setFeedType(feedType);
385 feed.setFeedVersion(feedVersion);
386 }
387
388 journalFeedPersistence.update(feed, false);
389
390 return feed;
391 }
392
393 protected boolean isValidStructureField(
394 long groupId, String structureId, String contentField) {
395
396 if (contentField.equals(JournalFeedImpl.WEB_CONTENT_DESCRIPTION) ||
397 contentField.equals(JournalFeedImpl.RENDERED_WEB_CONTENT)) {
398
399 return true;
400 }
401 else {
402 try {
403 JournalStructure structure =
404 journalStructurePersistence.findByG_S(groupId, structureId);
405
406 Document doc = SAXReaderUtil.read(structure.getXsd());
407
408 XPath xpathSelector = SAXReaderUtil.createXPath(
409 "//dynamic-element[@name='"+ contentField + "']");
410
411 Element el = (Element)xpathSelector.selectSingleNode(doc);
412
413 if (el != null) {
414 return true;
415 }
416 }
417 catch (Exception e) {
418 }
419 }
420
421 return false;
422 }
423
424 protected void validate(
425 long companyId, long groupId, String feedId, boolean autoFeedId,
426 String name, String description, String structureId,
427 String targetLayoutFriendlyUrl, String contentField)
428 throws PortalException, SystemException {
429
430 if (!autoFeedId) {
431 if ((Validator.isNull(feedId)) || (Validator.isNumber(feedId)) ||
432 (feedId.indexOf(StringPool.SPACE) != -1)) {
433
434 throw new FeedIdException();
435 }
436
437 JournalFeed feed = journalFeedPersistence.fetchByG_F(
438 groupId, feedId);
439
440 if (feed != null) {
441 throw new DuplicateFeedIdException();
442 }
443 }
444
445 validate(
446 companyId, groupId, name, description, structureId,
447 targetLayoutFriendlyUrl, contentField);
448 }
449
450 protected void validate(
451 long companyId, long groupId, String name, String description,
452 String structureId, String targetLayoutFriendlyUrl,
453 String contentField)
454 throws PortalException {
455
456 if (Validator.isNull(name)) {
457 throw new FeedNameException();
458 }
459
460 if (Validator.isNull(description)) {
461 throw new FeedDescriptionException();
462 }
463
464 long plid = PortalUtil.getPlidFromFriendlyURL(
465 companyId, targetLayoutFriendlyUrl);
466
467 if (plid <= 0) {
468 throw new FeedTargetLayoutFriendlyUrlException();
469 }
470
471 if (!isValidStructureField(groupId, structureId, contentField)) {
472 throw new FeedContentFieldException();
473 }
474 }
475
476 }