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