View Javadoc

1   /**
2    * This file Copyright (c) 2008-2010 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 com.sun.syndication.feed.synd.SyndCategory;
37  import com.sun.syndication.feed.synd.SyndCategoryImpl;
38  import com.sun.syndication.feed.synd.SyndContent;
39  import com.sun.syndication.feed.synd.SyndContentImpl;
40  import com.sun.syndication.feed.synd.SyndEntry;
41  import com.sun.syndication.feed.synd.SyndEntryImpl;
42  import com.sun.syndication.feed.synd.SyndFeed;
43  
44  import info.magnolia.cms.beans.config.ServerConfiguration;
45  import info.magnolia.cms.core.Content;
46  import info.magnolia.cms.core.HierarchyManager;
47  import info.magnolia.cms.core.ItemType;
48  import info.magnolia.cms.core.NodeData;
49  import info.magnolia.context.MgnlContext;
50  import info.magnolia.module.data.DataConsts;
51  import info.magnolia.module.data.DataModule;
52  import info.magnolia.module.rssaggregator.util.ContentMapper;
53  import info.magnolia.module.rssaggregator.util.MagnoliaTemplate;
54  
55  import javax.jcr.RepositoryException;
56  
57  import org.apache.commons.lang.StringUtils;
58  
59  import static java.lang.String.*;
60  import java.util.ArrayList;
61  import java.util.Collection;
62  import java.util.List;
63  
64  /**
65   * Generates a {@link SyndFeed} based on aggregate feeds defined via the RSS Aggregator Module.
66   *
67   * @author Rob van der Linden Vooren
68   */
69  public class RSSModuleFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
70  
71      private static final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper();
72  
73      private MagnoliaTemplate magnoliaTemplate;
74  
75      private String feedPath;
76  
77      /**
78       * Construct a new RSSModuleSyndFeedGenerator that generates a SyndFeed for the aggregate feed defined via the RSS
79       * Aggregator Module at the given {@code feedPath}.
80       *
81       * @param feedPath the path of the aggregate feed in the RSS Aggregator module (eg. "/rssaggregator/blogsaggregate")
82       * @throws IllegalArgumentException when {@code feedPath} is null
83       */
84      public RSSModuleFeedGenerator() {
85          this.magnoliaTemplate = new MagnoliaTemplate();
86      }
87  
88      public void setFeedPath(String feedPath) {
89          this.feedPath = feedPath;
90      }
91  
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          HierarchyManager hm = MgnlContext.getHierarchyManager(DataModule.getRepository());
96          try {
97              Content feedDescr = hm.getContent(feedPath);
98              feed.setTitle(StringUtils.defaultIfEmpty(feedDescr.getNodeData("title").getString(), ""));
99              feed.setLink(ServerConfiguration.getInstance().getDefaultBaseUrl());
100             feed.setDescription(StringUtils.defaultIfEmpty(feedDescr.getNodeData("description").getString(), ""));
101         } catch (RepositoryException e) {
102             throw new FeedGenerationException("Failed to retrieve feed description from " + feedPath, e);
103         }
104     }
105 
106     public List<SyndEntry> loadFeedEntries() {
107         String entriesQuery = format("/jcr:root%s/data[1]/*/* order by @pubDate descending", feedPath);
108         return magnoliaTemplate.xpathQueryForList(DataModule.getRepository(), entriesQuery, new ItemType(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE), MAPPER);
109     }
110 
111     @Override
112     public Object clone() throws CloneNotSupportedException {
113         return super.clone();
114     }
115 
116     private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
117 
118         public SyndEntry map(Content content) throws RepositoryException {
119             SyndEntry entry = new SyndEntryImpl();
120             entry.setAuthor(content.getNodeData("author").getString());
121             entry.setTitle(content.getNodeData("title").getString());
122             entry.setLink(content.getNodeData("link").getString());
123             if (content.getNodeData("pubDate").getDate() != null) {
124                 entry.setPublishedDate(content.getNodeData("pubDate").getDate().getTime());
125             }
126             SyndContent description = new SyndContentImpl();
127             description.setType("text/html");
128             description.setValue(content.getNodeData("description").getString());
129             entry.setDescription(description);
130             List<SyndCategory> categories = new ArrayList<SyndCategory>();
131             Collection<NodeData> categoriesNodeData = content.getContent("categories").getNodeDataCollection();
132             for (NodeData nodeData : categoriesNodeData) {
133                 SyndCategory category = new SyndCategoryImpl();
134                 category.setName(nodeData.getString());
135                 categories.add(category);
136             }
137             entry.setCategories(categories);
138             return entry;
139         }
140 
141     }
142 
143 }