Clover icon

magnolia-module-rssaggregator 2.3.3

  1. Project Clover database Mon Feb 2 2015 11:21:55 CET
  2. Package info.magnolia.module.rssaggregator.templates.components

File PlanetFeedModel.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
62% of files have more coverage

Code metrics

10
45
9
1
184
111
19
0.42
5
9
2.11

Classes

Class Line # Actions
PlanetFeedModel 67 45 0% 19 64
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2013-2015 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.templates.components;
35   
36    import info.magnolia.cms.util.QueryUtil;
37    import info.magnolia.context.MgnlContext;
38    import info.magnolia.dam.templating.functions.DamTemplatingFunctions;
39    import info.magnolia.jcr.util.SessionUtil;
40    import info.magnolia.module.rssaggregator.RSSAggregatorConstants;
41    import info.magnolia.module.rssaggregator.util.PagerUtil;
42    import info.magnolia.module.rssaggregator.util.PlanetUtil;
43    import info.magnolia.rendering.model.RenderingModel;
44    import info.magnolia.rendering.template.TemplateDefinition;
45    import info.magnolia.templating.functions.TemplatingFunctions;
46   
47    import java.util.ArrayList;
48    import java.util.Collection;
49    import java.util.Collections;
50    import java.util.Comparator;
51    import java.util.Date;
52    import java.util.List;
53   
54    import javax.inject.Inject;
55    import javax.jcr.Node;
56    import javax.jcr.NodeIterator;
57    import javax.jcr.RepositoryException;
58   
59    import org.apache.commons.lang.StringUtils;
60   
61   
62    /**
63    * PlanetFeedModel.
64    *
65    * @param <RD> Definition type.
66    */
 
67    public class PlanetFeedModel<RD extends TemplateDefinition> extends AbstractFeedModel<TemplateDefinition> {
68   
69    private static final String ARCHIVE_NODE = "planetData";
70    private static final String DATE_PROPERTY = "pubDate";
71    private final DamTemplatingFunctions damTemplatingFunction;
72   
 
73  0 toggle @Inject
74    public PlanetFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions, DamTemplatingFunctions damTemplatingFunction) {
75  0 super(content, definition, parent, templatingFunctions);
76  0 this.damTemplatingFunction = damTemplatingFunction;
77    }
78   
 
79  0 toggle @Override
80    public String execute() {
81  0 return "success";
82    }
83   
 
84  0 toggle public PagerUtil getPager() throws RepositoryException {
85  0 PagerUtil pager = null;
86  0 Collection<Node> listItems = getItems();
87  0 if (!listItems.isEmpty()) {
88   
89  0 final String currentPageLink;
90  0 if (MgnlContext.isWebContext() && MgnlContext.getAggregationState() != null) {
91  0 currentPageLink = MgnlContext.getAggregationState().getOriginalURL();
92    } else {
93  0 currentPageLink = templatingFunctions.link(MgnlContext.getAggregationState().getMainContent().getJCRNode());
94    }
95   
96  0 pager = new PagerUtil(currentPageLink, listItems, content);
97    }
98  0 return pager;
99    }
100   
 
101  0 toggle public Collection<Node> getItems() {
102  0 List<Node> listItems = new ArrayList<Node>();
103   
104  0 try {
105  0 if (content.hasProperty("link")) {
106  0 Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
107   
108  0 String sql2 = "select * from [mgnl:content] as t where ISDESCENDANTNODE([" + rssParent.getPath() + "/" + ARCHIVE_NODE
109    + "]) and t.hidden = 'false'";
110   
111  0 NodeIterator nit = QueryUtil.search(RSSAggregatorConstants.WORKSPACE, sql2);
112  0 while (nit.hasNext()) {
113  0 Node post = nit.nextNode();
114  0 listItems.add(post);
115    }
116  0 sort(listItems);
117    } else {
118  0 log.info("No feed specified for planet feeds.");
119    }
120    } catch (Exception e) {
121  0 log.error("Problem while retrieving post items: " + e.getMessage());
122    }
123  0 return listItems;
124    }
125   
 
126  0 toggle protected void sort(Collection<Node> listItems) {
127  0 Collections.sort((List<Node>) listItems, new Comparator<Node>() {
128   
 
129  0 toggle @Override
130    public int compare(Node n1, Node n2) {
131  0 try {
132  0 Date d1 = n1.getProperty(DATE_PROPERTY).getDate().getTime();
133  0 Date d2 = n2.getProperty(DATE_PROPERTY).getDate().getTime();
134    // ascending sort
135  0 return d2.compareTo(d1);
136    } catch (RepositoryException e) {
137  0 log.error("Problem while sorting posts: " + e.getMessage());
138    }
139  0 return 0;
140    }
141    });
142    }
143   
 
144  0 toggle public String getDescription(String entryDescription, Integer abbreviation) {
145  0 return PlanetUtil.formatDescription(entryDescription, abbreviation);
146    }
147   
148    /**
149    * Retrieve an author's Hackergotchi image if available.
150    *
151    * @param feedLink Link to the feed where the settings are fetched.
152    * @return Link to the Hackergotchi image stored in DMS or null.
153    */
 
154  0 toggle public String getHackergotchi(String feedLink) {
155  0 try {
156  0 Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
157  0 String assetId = getFeedProperty(rssParent, FEEDS_NODE, "link", feedLink, "img");
158  0 if (!StringUtils.isBlank(assetId)) {
159  0 return damTemplatingFunction.getAssetLink(assetId);
160    }
161    } catch (RepositoryException e) {
162  0 log.error("Problem while fetching Hackergotchi: " + e.getMessage());
163    }
164  0 return null;
165    }
166   
167    /**
168    * Retrieve the title attribute of an individual feed subscription.
169    *
170    * @param feedLink Link to the feed.
171    * @return Assigned title of a feed if defined in the dialog or null.
172    */
 
173  0 toggle @Override
174    public String getFeedTitle(String feedLink) {
175  0 try {
176  0 Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
177  0 return getFeedProperty(rssParent, "feeds", "link", feedLink, "title");
178    } catch (RepositoryException e) {
179  0 log.error("Problem while fetching feed title for the main planet feed: " + e.getMessage());
180    }
181  0 return null;
182    }
183   
184    }