View Javadoc

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