1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
55   * <a href="JournalFeedLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Raymond Aug?
58   *
59   */
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         // Feed
168 
169         User user = userPersistence.findByPrimaryKey(userId);
170         Date now = new Date();
171 
172         validate(
173             groupId, feedId, autoFeedId, name, description, structureId,
174             targetLayoutFriendlyUrl, contentField);
175 
176         long id = counterLocalService.increment();
177 
178         JournalFeed feed = journalFeedPersistence.create(id);
179 
180         feed.setUuid(uuid);
181         feed.setGroupId(groupId);
182         feed.setCompanyId(user.getCompanyId());
183         feed.setUserId(user.getUserId());
184         feed.setUserName(user.getFullName());
185         feed.setCreateDate(now);
186         feed.setModifiedDate(now);
187         feed.setFeedId(feedId);
188         feed.setName(name);
189         feed.setDescription(description);
190         feed.setType(type);
191         feed.setStructureId(structureId);
192         feed.setTemplateId(templateId);
193         feed.setRendererTemplateId(rendererTemplateId);
194         feed.setDelta(delta);
195         feed.setOrderByCol(orderByCol);
196         feed.setOrderByType(orderByType);
197         feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
198         feed.setTargetPortletId(targetPortletId);
199         feed.setContentField(contentField);
200 
201         if (Validator.isNull(feedType)) {
202             feed.setFeedType(RSSUtil.DEFAULT_TYPE);
203             feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
204         }
205         else {
206             feed.setFeedType(feedType);
207             feed.setFeedVersion(feedVersion);
208         }
209 
210         journalFeedPersistence.update(feed);
211 
212         // Resources
213 
214         if ((addCommunityPermissions != null) &&
215             (addGuestPermissions != null)) {
216 
217             addFeedResources(
218                 feed, addCommunityPermissions.booleanValue(),
219                 addGuestPermissions.booleanValue());
220         }
221         else {
222             addFeedResources(feed, communityPermissions, guestPermissions);
223         }
224 
225         return feed;
226     }
227 
228     public void addFeedResources(
229             long feedId, boolean addCommunityPermissions,
230             boolean addGuestPermissions)
231         throws PortalException, SystemException {
232 
233         JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
234 
235         addFeedResources(feed, addCommunityPermissions, addGuestPermissions);
236     }
237 
238     public void addFeedResources(
239             JournalFeed feed, boolean addCommunityPermissions,
240             boolean addGuestPermissions)
241         throws PortalException, SystemException {
242 
243         resourceLocalService.addResources(
244             feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
245             JournalFeed.class.getName(), feed.getId(), false,
246             addCommunityPermissions, addGuestPermissions);
247     }
248 
249     public void addFeedResources(
250             long feedId, String[] communityPermissions,
251             String[] guestPermissions)
252         throws PortalException, SystemException {
253 
254         JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
255 
256         addFeedResources(feed, communityPermissions, guestPermissions);
257     }
258 
259     public void addFeedResources(
260             JournalFeed feed, String[] communityPermissions,
261             String[] guestPermissions)
262         throws PortalException, SystemException {
263 
264         resourceLocalService.addModelResources(
265             feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
266             JournalFeed.class.getName(), feed.getId(), communityPermissions,
267             guestPermissions);
268     }
269 
270     public void deleteFeed(long feedId)
271         throws PortalException, SystemException {
272 
273         JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
274 
275         deleteFeed(feed);
276     }
277 
278     public void deleteFeed(long groupId, String feedId)
279         throws PortalException, SystemException {
280 
281         JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
282 
283         deleteFeed(feed);
284     }
285 
286     public void deleteFeed(JournalFeed feed)
287         throws PortalException, SystemException {
288 
289         // Resources
290 
291         resourceLocalService.deleteResource(
292             feed.getCompanyId(), JournalFeed.class.getName(),
293             ResourceImpl.SCOPE_INDIVIDUAL, feed.getId());
294 
295         // Feed
296 
297         journalFeedPersistence.remove(feed.getPrimaryKey());
298     }
299 
300     public JournalFeed getFeed(long feedId)
301         throws PortalException, SystemException {
302 
303         return journalFeedPersistence.findByPrimaryKey(feedId);
304     }
305 
306     public JournalFeed getFeed(long groupId, String feedId)
307         throws PortalException, SystemException {
308 
309         return journalFeedPersistence.findByG_F(groupId, feedId);
310     }
311 
312     public List getFeeds() throws SystemException {
313         return journalFeedPersistence.findAll();
314     }
315 
316     public List getFeeds(long groupId) throws SystemException {
317         return journalFeedPersistence.findByGroupId(groupId);
318     }
319 
320     public List getFeeds(long groupId, int begin, int end)
321         throws SystemException {
322 
323         return journalFeedPersistence.findByGroupId(groupId, begin, end);
324     }
325 
326     public int getFeedsCount(long groupId) throws SystemException {
327         return journalFeedPersistence.countByGroupId(groupId);
328     }
329 
330     public List search(
331             long companyId, long groupId, String keywords, int begin, int end,
332             OrderByComparator obc)
333         throws SystemException {
334 
335         return journalFeedFinder.findByKeywords(
336             companyId, groupId, keywords, begin, end, obc);
337     }
338 
339     public List search(
340             long companyId, long groupId, String feedId, String name,
341             String description, boolean andOperator, int begin, int end,
342             OrderByComparator obc)
343         throws SystemException {
344 
345         return journalFeedFinder.findByC_G_F_N_D(
346             companyId, groupId, feedId, name, description, andOperator, begin,
347             end, obc);
348     }
349 
350     public int searchCount(long companyId, long groupId, String keywords)
351         throws SystemException {
352 
353         return journalFeedFinder.countByKeywords(
354             companyId, groupId, keywords);
355     }
356 
357     public int searchCount(
358             long companyId, long groupId, String feedId, String name,
359             String description, boolean andOperator)
360         throws SystemException {
361 
362         return journalFeedFinder.countByC_G_F_N_D(
363             companyId, groupId, feedId, name, description, andOperator);
364     }
365 
366     public JournalFeed updateFeed(
367             long groupId, String feedId, String name, String description,
368             String type, String structureId, String templateId,
369             String rendererTemplateId, int delta, String orderByCol,
370             String orderByType, String targetLayoutFriendlyUrl,
371             String targetPortletId, String contentField, String feedType,
372             double feedVersion)
373         throws PortalException, SystemException{
374 
375         // Feed
376 
377         validate(
378             groupId, name, description, structureId, targetLayoutFriendlyUrl,
379             contentField);
380 
381         JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
382 
383         feed.setModifiedDate(new Date());
384         feed.setName(name);
385         feed.setDescription(description);
386         feed.setType(type);
387         feed.setStructureId(structureId);
388         feed.setTemplateId(templateId);
389         feed.setRendererTemplateId(rendererTemplateId);
390         feed.setDelta(delta);
391         feed.setOrderByCol(orderByCol);
392         feed.setOrderByType(orderByType);
393         feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
394         feed.setTargetPortletId(targetPortletId);
395         feed.setContentField(contentField);
396 
397         if (Validator.isNull(feedType)) {
398             feed.setFeedType(RSSUtil.DEFAULT_TYPE);
399             feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
400         }
401         else {
402             feed.setFeedType(feedType);
403             feed.setFeedVersion(feedVersion);
404         }
405 
406         journalFeedPersistence.update(feed);
407 
408         return feed;
409     }
410 
411     protected boolean isValidStructureField(
412         long groupId, String structureId, String contentField) {
413 
414         if (contentField.equals(JournalFeedImpl.ARTICLE_DESCRIPTION) ||
415             contentField.equals(JournalFeedImpl.RENDERED_ARTICLE)) {
416 
417             return true;
418         }
419         else {
420             try {
421                 JournalStructure structure =
422                     journalStructurePersistence.findByG_S(groupId, structureId);
423 
424                 Document doc = PortalUtil.readDocumentFromXML(
425                     structure.getXsd());
426 
427                 XPath xpathSelector = DocumentHelper.createXPath(
428                     "//dynamic-element[@name='"+ contentField + "']");
429 
430                 Element el = (Element)xpathSelector.selectSingleNode(doc);
431 
432                 if (el != null) {
433                     return true;
434                 }
435             }
436             catch (Exception e) {
437             }
438         }
439 
440         return false;
441     }
442 
443     protected void validate(
444             long groupId, String feedId, boolean autoFeedId, String name,
445             String description, String structureId,
446             String targetLayoutFriendlyUrl, String contentField)
447         throws PortalException, SystemException {
448 
449         if (!autoFeedId) {
450             if ((Validator.isNull(feedId)) || (Validator.isNumber(feedId)) ||
451                 (feedId.indexOf(StringPool.SPACE) != -1)) {
452 
453                 throw new FeedIdException();
454             }
455 
456             try {
457                 journalFeedPersistence.findByG_F(groupId, feedId);
458 
459                 throw new DuplicateFeedIdException();
460             }
461             catch (NoSuchFeedException nsfe) {
462             }
463         }
464 
465         validate(
466             groupId, name, description, structureId, targetLayoutFriendlyUrl,
467             contentField);
468     }
469 
470     protected void validate(
471             long groupId, String name, String description, String structureId,
472             String targetLayoutFriendlyUrl, String contentField)
473         throws PortalException {
474 
475         if (Validator.isNull(name)) {
476             throw new FeedNameException();
477         }
478 
479         if (Validator.isNull(description)) {
480             throw new FeedDescriptionException();
481         }
482 
483         if (Validator.isNull(targetLayoutFriendlyUrl)) {
484             throw new FeedTargetLayoutFriendlyUrlException();
485         }
486 
487         if (!isValidStructureField(groupId, structureId, contentField)) {
488             throw new FeedContentFieldException();
489         }
490     }
491 
492 }