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