View Javadoc

1   /**
2    * This file Copyright (c) 2003-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.context.MgnlContext;
37  import info.magnolia.dam.DamException;
38  import info.magnolia.dam.DamManager;
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 DamManager damManager;
73  
74      @Inject
75      public PlanetFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions, DamManager damManager) {
76          super(content, definition, parent, templatingFunctions);
77          this.damManager = damManager;
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                 if (rssParent.hasNode(ARCHIVE_NODE)) {
110                     NodeIterator nit = rssParent.getNode(ARCHIVE_NODE).getNodes();
111                     while (nit.hasNext()) {
112                         Node postsNode = nit.nextNode();
113 
114                         NodeIterator pit = postsNode.getNodes();
115                         while (pit.hasNext()) {
116                             Node nn = pit.nextNode();
117                             boolean hidden = false;
118                             if (nn.hasProperty("hidden")) {
119                                 hidden = nn.getProperty("hidden").getBoolean();
120                             }
121                             if (!hidden) {
122                                 listItems.add(nn);
123                             }
124                         }
125                     }
126                     sort(listItems);
127                 }
128             } else {
129                 log.info("No feed specified for planet feeds.");
130             }
131         } catch (Exception e) {
132             log.error("Problem while retrieving post items: " + e.getMessage());
133         }
134         return listItems;
135     }
136 
137     protected void sort(Collection<Node> listItems) {
138         Collections.sort((List<Node>) listItems, new Comparator<Node>() {
139             public int compare(Node n1, Node n2) {
140                 try {
141                     Date d1 = n1.getProperty(DATE_PROPERTY).getDate().getTime();
142                     Date d2 = n2.getProperty(DATE_PROPERTY).getDate().getTime();
143                     // ascending sort
144                     return d2.compareTo(d1);
145                 } catch (RepositoryException e) {
146                     log.error("Problem while sorting posts: " + e.getMessage());
147                 }
148                 return 0;
149             }
150         });
151     }
152 
153     public String getDescription(String entryDescription, Integer abbreviation) {
154         return PlanetUtil.formatDescription(entryDescription, abbreviation);
155     }
156 
157     /**
158      * Retrieve an author's Hackergotchi image if available.
159      *
160      * @param feedLink Link to the feed where the settings are fetched.
161      * @return Link to the Hackergotchi image stored in DMS or null.
162      */
163     public String getHackergotchi(String feedLink) {
164         try {
165             Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
166             String assetId = getFeedProperty(rssParent, FEEDS_NODE, "link", feedLink, "img");
167             if (!StringUtils.isBlank(assetId)) {
168                 return damManager.getAssetForId(assetId).getLink();
169             }
170         } catch (RepositoryException e) {
171             log.error("Problem while fetching Hackergotchi: " + e.getMessage());
172         } catch (DamException e) {
173             log.error("Problem while fetching Hackergotchi: " + e.getMessage());
174         }
175         return null;
176     }
177 
178     /**
179      * Retrieve the title attribute of an individual feed subscription.
180      *
181      * @param feedLink Link to the feed.
182      * @return Assigned title of a feed if defined in the dialog or null.
183      */
184     public String getFeedTitle(String feedLink) {
185         try {
186             Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
187             return getFeedProperty(rssParent, "feeds", "link", feedLink, "title");
188         } catch (RepositoryException e) {
189             log.error("Problem while fetching feed title for the main planet feed: " + e.getMessage());
190         }
191         return null;
192     }
193 
194 }