1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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.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.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  /**
50   * <a href="JournalFeedLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Raymond Augé
53   *
54   */
55  public class JournalFeedLocalServiceImpl
56      extends JournalFeedLocalServiceBaseImpl {
57  
58      public JournalFeed addFeed(
59              long userId, long plid, String feedId, boolean autoFeedId,
60              String name, String description, String type, String structureId,
61              String templateId, String rendererTemplateId, int delta,
62              String orderByCol, String orderByType,
63              String targetLayoutFriendlyUrl, String targetPortletId,
64              String contentField, String feedType, double feedVersion,
65              boolean addCommunityPermissions, boolean addGuestPermissions)
66          throws PortalException, SystemException {
67  
68          return addFeed(
69              null, userId, plid, feedId, autoFeedId, name, description, type,
70              structureId, templateId, rendererTemplateId, delta, orderByCol,
71              orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
72              feedType, feedVersion, Boolean.valueOf(addCommunityPermissions),
73              Boolean.valueOf(addGuestPermissions), null, null);
74      }
75  
76      public JournalFeed addFeed(
77              long userId, long plid, String feedId, boolean autoFeedId,
78              String name, String description, String type, String structureId,
79              String templateId, String rendererTemplateId, int delta,
80              String orderByCol, String orderByType,
81              String targetLayoutFriendlyUrl, String targetPortletId,
82              String contentField, String feedType, double feedVersion,
83              String[] communityPermissions, String[] guestPermissions)
84          throws PortalException, SystemException {
85  
86          return addFeed(
87              null, userId, plid, feedId, autoFeedId, name, description, type,
88              structureId, templateId, rendererTemplateId, delta, orderByCol,
89              orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
90              feedType, feedVersion, null, null, communityPermissions,
91              guestPermissions);
92      }
93  
94      public JournalFeed addFeed(
95              String uuid, long userId, long plid, String feedId,
96              boolean autoFeedId, String name, String description, String type,
97              String structureId, String templateId, String rendererTemplateId,
98              int delta, String orderByCol, String orderByType,
99              String targetLayoutFriendlyUrl, String targetPortletId,
100             String contentField, String feedType, double feedVersion,
101             boolean addCommunityPermissions, boolean addGuestPermissions)
102         throws PortalException, SystemException {
103 
104         return addFeed(
105             uuid, userId, plid, feedId, autoFeedId, name, description, type,
106             structureId, templateId, rendererTemplateId, delta, orderByCol,
107             orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
108             feedType, feedVersion, Boolean.valueOf(addCommunityPermissions),
109             Boolean.valueOf(addGuestPermissions), null, null);
110     }
111 
112     public JournalFeed addFeed(
113             String uuid, long userId, long plid, String feedId,
114             boolean autoFeedId, String name, String description, String type,
115             String structureId, String templateId, String rendererTemplateId,
116             int delta, String orderByCol, String orderByType,
117             String targetLayoutFriendlyUrl, String targetPortletId,
118             String contentField, String feedType, double feedVersion,
119             String[] communityPermissions, String[] guestPermissions)
120         throws PortalException, SystemException {
121 
122         return addFeed(
123             uuid, userId, plid, feedId, autoFeedId, name, description, type,
124             structureId, templateId, rendererTemplateId, delta, orderByCol,
125             orderByType,targetLayoutFriendlyUrl, targetPortletId, contentField,
126             feedType, feedVersion, null, null, communityPermissions,
127             guestPermissions);
128     }
129 
130     public JournalFeed addFeed(
131             String uuid, long userId, long plid, String feedId,
132             boolean autoFeedId, String name, String description, String type,
133             String structureId, String templateId, String rendererTemplateId,
134             int delta, String orderByCol, String orderByType,
135             String targetLayoutFriendlyUrl, String targetPortletId,
136             String contentField, String feedType, double feedVersion,
137             Boolean addCommunityPermissions, Boolean addGuestPermissions,
138             String[] communityPermissions, String[] guestPermissions)
139         throws PortalException, SystemException {
140 
141         long groupId = PortalUtil.getScopeGroupId(plid);
142 
143         return addFeedToGroup(
144             uuid, userId, groupId, feedId, autoFeedId, name, description, type,
145             structureId, templateId, rendererTemplateId, delta, orderByCol,
146             orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
147             feedType, feedVersion, addCommunityPermissions, addGuestPermissions,
148             communityPermissions, guestPermissions);
149     }
150 
151     public JournalFeed addFeedToGroup(
152             String uuid, long userId, long groupId, String feedId,
153             boolean autoFeedId, String name, String description, String type,
154             String structureId, String templateId, String rendererTemplateId,
155             int delta, String orderByCol, String orderByType,
156             String targetLayoutFriendlyUrl, String targetPortletId,
157             String contentField, String feedType, double feedVersion,
158             Boolean addCommunityPermissions, Boolean addGuestPermissions,
159             String[] communityPermissions, String[] guestPermissions)
160         throws PortalException, SystemException {
161 
162         // Feed
163 
164         User user = userPersistence.findByPrimaryKey(userId);
165         feedId = feedId.trim().toUpperCase();
166         Date now = new Date();
167 
168         validate(
169             user.getCompanyId(), groupId, feedId, autoFeedId, name, description,
170             structureId, targetLayoutFriendlyUrl, contentField);
171 
172         if (autoFeedId) {
173             feedId = String.valueOf(counterLocalService.increment());
174         }
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, false);
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             ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
294 
295         // Feed
296 
297         journalFeedPersistence.remove(feed);
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<JournalFeed> getFeeds() throws SystemException {
313         return journalFeedPersistence.findAll();
314     }
315 
316     public List<JournalFeed> getFeeds(long groupId) throws SystemException {
317         return journalFeedPersistence.findByGroupId(groupId);
318     }
319 
320     public List<JournalFeed> getFeeds(long groupId, int start, int end)
321         throws SystemException {
322 
323         return journalFeedPersistence.findByGroupId(groupId, start, end);
324     }
325 
326     public int getFeedsCount(long groupId) throws SystemException {
327         return journalFeedPersistence.countByGroupId(groupId);
328     }
329 
330     public List<JournalFeed> search(
331             long companyId, long groupId, String keywords, int start, int end,
332             OrderByComparator obc)
333         throws SystemException {
334 
335         return journalFeedFinder.findByKeywords(
336             companyId, groupId, keywords, start, end, obc);
337     }
338 
339     public List<JournalFeed> search(
340             long companyId, long groupId, String feedId, String name,
341             String description, boolean andOperator, int start, int end,
342             OrderByComparator obc)
343         throws SystemException {
344 
345         return journalFeedFinder.findByC_G_F_N_D(
346             companyId, groupId, feedId, name, description, andOperator, start,
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         JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
378 
379         validate(
380             feed.getCompanyId(), groupId, name, description, structureId,
381             targetLayoutFriendlyUrl, contentField);
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, false);
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 = SAXReaderUtil.read(structure.getXsd());
425 
426                 XPath xpathSelector = SAXReaderUtil.createXPath(
427                     "//dynamic-element[@name='"+ contentField + "']");
428 
429                 Element el = (Element)xpathSelector.selectSingleNode(doc);
430 
431                 if (el != null) {
432                     return true;
433                 }
434             }
435             catch (Exception e) {
436             }
437         }
438 
439         return false;
440     }
441 
442     protected void validate(
443             long companyId, long groupId, String feedId, boolean autoFeedId,
444             String name, String description, String structureId,
445             String targetLayoutFriendlyUrl, String contentField)
446         throws PortalException, SystemException {
447 
448         if (!autoFeedId) {
449             if ((Validator.isNull(feedId)) || (Validator.isNumber(feedId)) ||
450                 (feedId.indexOf(StringPool.SPACE) != -1)) {
451 
452                 throw new FeedIdException();
453             }
454 
455             JournalFeed feed = journalFeedPersistence.fetchByG_F(
456                 groupId, feedId);
457 
458             if (feed != null) {
459                 throw new DuplicateFeedIdException();
460             }
461         }
462 
463         validate(
464             companyId, groupId, name, description, structureId,
465             targetLayoutFriendlyUrl, contentField);
466     }
467 
468     protected void validate(
469             long companyId, long groupId, String name, String description,
470             String structureId, String targetLayoutFriendlyUrl,
471             String contentField)
472         throws PortalException {
473 
474         if (Validator.isNull(name)) {
475             throw new FeedNameException();
476         }
477 
478         if (Validator.isNull(description)) {
479             throw new FeedDescriptionException();
480         }
481 
482         long plid = PortalUtil.getPlidFromFriendlyURL(
483             companyId, targetLayoutFriendlyUrl);
484 
485         if (plid <= 0) {
486             throw new FeedTargetLayoutFriendlyUrlException();
487         }
488 
489         if (!isValidStructureField(groupId, structureId, contentField)) {
490             throw new FeedContentFieldException();
491         }
492     }
493 
494 }