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