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