View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.generator;
35  
36  import static java.lang.String.format;
37  
38  import info.magnolia.cms.beans.config.ServerConfiguration;
39  import info.magnolia.jcr.util.PropertyUtil;
40  import info.magnolia.jcr.util.SessionUtil;
41  import info.magnolia.module.data.DataConsts;
42  import info.magnolia.module.data.DataModule;
43  import info.magnolia.module.rssaggregator.util.ContentMapper;
44  import info.magnolia.module.rssaggregator.util.MagnoliaTemplate;
45  import info.magnolia.objectfactory.Components;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  
50  import javax.jcr.Node;
51  import javax.jcr.Property;
52  import javax.jcr.PropertyIterator;
53  import javax.jcr.RepositoryException;
54  
55  import com.sun.syndication.feed.synd.SyndCategory;
56  import com.sun.syndication.feed.synd.SyndCategoryImpl;
57  import com.sun.syndication.feed.synd.SyndContent;
58  import com.sun.syndication.feed.synd.SyndContentImpl;
59  import com.sun.syndication.feed.synd.SyndEntry;
60  import com.sun.syndication.feed.synd.SyndEntryImpl;
61  import com.sun.syndication.feed.synd.SyndFeed;
62  
63  /**
64   * Generates a {@link SyndFeed} based on aggregate feeds defined via the RSS Aggregator Module.
65   * 
66   * @author Rob van der Linden Vooren
67   */
68  public class RSSModuleFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
69  
70      private static final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper();
71  
72      private MagnoliaTemplate magnoliaTemplate;
73  
74      private String feedPath;
75  
76      /**
77       * Construct a new RSSModuleSyndFeedGenerator that generates a SyndFeed for the aggregate feed defined via the RSS
78       * Aggregator Module at the given {@code feedPath}.
79       * 
80       * @param feedPath the path of the aggregate feed in the RSS Aggregator module (eg. "/rssaggregator/blogsaggregate")
81       * @throws IllegalArgumentException when {@code feedPath} is null
82       */
83      public RSSModuleFeedGenerator() {
84          this.magnoliaTemplate = new MagnoliaTemplate();
85      }
86  
87      public void setFeedPath(String feedPath) {
88          this.feedPath = feedPath;
89      }
90  
91      @Override
92      public void setFeedInfo(SyndFeed feed) {
93          // Ideally retrieve this from the aggregate node definition. However the RSS Aggregate creation dialog does not
94          // provide for this information (yet).
95          String link = Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl();
96          Node feedDescr = SessionUtil.getNode(DataModule.getRepository(), feedPath);
97          if (feedDescr != null) {
98              feed.setTitle(PropertyUtil.getString(feedDescr, "title", ""));
99              feed.setDescription(PropertyUtil.getString(feedDescr, "description", ""));
100         } else {
101             feed.setTitle("");
102             feed.setDescription("");
103         }
104     }
105 
106     @Override
107     public List<SyndEntry> loadFeedEntries() {
108         String entriesQuery = format("/jcr:root%s/data[1]/*/* order by @pubDate descending", feedPath);
109         return magnoliaTemplate.xpathQueryForList(DataModule.getRepository(), entriesQuery, DataConsts.MODULE_DATA_CONTENT_NODE_TYPE, MAPPER);
110     }
111 
112     @Override
113     public Object clone() throws CloneNotSupportedException {
114         return super.clone();
115     }
116 
117     private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
118 
119         @Override
120         public SyndEntry map(Node content) throws RepositoryException {
121             SyndEntry entry = new SyndEntryImpl();
122             entry.setAuthor(PropertyUtil.getString(content, "author"));
123             entry.setTitle(PropertyUtil.getString(content, "title"));
124             entry.setLink(PropertyUtil.getString(content, "link"));
125             if (content.hasProperty("pubDate")) {
126                 entry.setPublishedDate(PropertyUtil.getProperty(content, "pubDate").getDate().getTime());
127             }
128             SyndContent description = new SyndContentImpl();
129             description.setType("text/html");
130             description.setValue(PropertyUtil.getString(content, "description"));
131             entry.setDescription(description);
132             List<SyndCategory> categories = new ArrayList<SyndCategory>();
133             PropertyIterator categoriesNodeData = content.getNode("categories").getProperties();
134             while (categoriesNodeData.hasNext()) {
135                 Property property = categoriesNodeData.nextProperty();
136                 SyndCategory category = new SyndCategoryImpl();
137                 category.setName(property.getString());
138                 categories.add(category);
139             }
140             entry.setCategories(categories);
141             return entry;
142         }
143 
144     }
145 
146 }