View Javadoc
1   /**
2    * This file Copyright (c) 2008-2016 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.NodeTypes;
40  import info.magnolia.jcr.util.PropertyUtil;
41  import info.magnolia.jcr.util.SessionUtil;
42  import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
43  import info.magnolia.module.rssaggregator.RSSAggregatorConstants;
44  import info.magnolia.module.rssaggregator.util.ContentMapper;
45  import info.magnolia.module.rssaggregator.util.MagnoliaTemplate;
46  import info.magnolia.objectfactory.Components;
47  
48  import java.util.ArrayList;
49  import java.util.List;
50  
51  import javax.jcr.Node;
52  import javax.jcr.Property;
53  import javax.jcr.PropertyIterator;
54  import javax.jcr.RepositoryException;
55  
56  import com.rometools.rome.feed.synd.SyndCategory;
57  import com.rometools.rome.feed.synd.SyndCategoryImpl;
58  import com.rometools.rome.feed.synd.SyndContent;
59  import com.rometools.rome.feed.synd.SyndContentImpl;
60  import com.rometools.rome.feed.synd.SyndEntry;
61  import com.rometools.rome.feed.synd.SyndEntryImpl;
62  import com.rometools.rome.feed.synd.SyndFeed;
63  
64  /**
65   * Generates a {@link SyndFeed} based on aggregate feeds defined via the RSS Aggregator Module.
66   */
67  public class RSSModuleFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
68  
69      private static final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper();
70  
71      private MagnoliaTemplate magnoliaTemplate;
72  
73      private String feedPath;
74  
75      /**
76       * Construct a new RSSModuleSyndFeedGenerator that generates a SyndFeed for the aggregate feed defined via the RSS
77       * Aggregator Module at the given {@code feedPath}.
78       */
79      public RSSModuleFeedGenerator() {
80          this.magnoliaTemplate = new MagnoliaTemplate();
81      }
82  
83      /**
84       * @param feedPath the path of the aggregate feed in the RSS Aggregator module (eg. "/rssaggregator/blogsaggregate")
85       */
86      public void setFeedPath(String feedPath) {
87          this.feedPath = feedPath;
88      }
89  
90      @Override
91      public void setFeedInfo(SyndFeed feed) {
92          // Ideally retrieve this from the aggregate node definition. However the RSS Aggregate creation dialog does not
93          // provide for this information (yet).
94          String link = Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl();
95          Node feedDescr = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, feedPath);
96          if (feedDescr != null) {
97              feed.setTitle(PropertyUtil.getString(feedDescr, "title", ""));
98              feed.setDescription(PropertyUtil.getString(feedDescr, "description", ""));
99          } else {
100             feed.setTitle("");
101             feed.setDescription("");
102         }
103         feed.setLink(link);
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(RSSAggregatorConstants.WORKSPACE, entriesQuery, NodeTypes.Content.NAME, 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             if (content.hasNode("categories")) {
134                 PropertyIterator categoriesNodeData = new JCRMgnlPropertiesFilteringNodeWrapper(content.getNode("categories")).getProperties();
135                 while (categoriesNodeData.hasNext()) {
136                     Property property = categoriesNodeData.nextProperty();
137                     SyndCategory category = new SyndCategoryImpl();
138                     category.setName(property.getString());
139                     categories.add(category);
140                 }
141             }
142             entry.setCategories(categories);
143             return entry;
144         }
145 
146     }
147 
148 }