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.jcr.util.SessionUtil;
38  import info.magnolia.module.rssaggregator.util.PagerUtil;
39  import info.magnolia.module.rssaggregator.util.PlanetUtil;
40  import info.magnolia.rendering.model.RenderingModel;
41  import info.magnolia.rendering.template.TemplateDefinition;
42  import info.magnolia.templating.functions.TemplatingFunctions;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.Collections;
47  import java.util.Comparator;
48  import java.util.Date;
49  import java.util.List;
50  
51  import javax.inject.Inject;
52  import javax.jcr.Node;
53  import javax.jcr.NodeIterator;
54  import javax.jcr.RepositoryException;
55  
56  import org.apache.commons.lang.StringUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  /**
61   * PlanetFeedModel.
62   *
63   * @param <RD> Definition type.
64   * @author lfischer
65   */
66  public class PlanetFeedModel<RD extends TemplateDefinition> extends AbstractFeedModel<TemplateDefinition> {
67  
68      private final static Logger log = LoggerFactory.getLogger(PlanetFeedModel.class);
69  
70      private static final String ARCHIVE_NODE = "planetData";
71      private static final String FEEDS_NODE = "feeds";
72      private static final String DATE_PROPERTY = "pubDate";
73  
74      @Inject
75      public PlanetFeedModel(Node content, TemplateDefinition definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
76          super(content, definition, parent, templatingFunctions);
77      }
78  
79      @Override
80      public String execute() {
81          return "success";
82      }
83  
84      public PagerUtil getPager() throws RepositoryException {
85          PagerUtil pager = null;
86          Collection<Node> listItems = getItems();
87          if (!listItems.isEmpty()) {
88  
89              final String currentPageLink;
90              if (MgnlContext.isWebContext() && MgnlContext.getAggregationState() != null) {
91                  currentPageLink = MgnlContext.getAggregationState().getOriginalURL();
92              } else {
93                  currentPageLink = templatingFunctions.link(MgnlContext.getAggregationState().getMainContent().getJCRNode());
94              }
95  
96              pager = new PagerUtil(currentPageLink, listItems, content);
97          }
98          return pager;
99      }
100 
101     public Collection<Node> getItems() {
102         List<Node> listItems = new ArrayList<Node>();
103 
104         try {
105             if (content.hasProperty("link")) {
106                 Node rssParent = SessionUtil.getNode("data", content.getProperty("link").getString());
107 
108                 if (rssParent.hasNode(ARCHIVE_NODE)) {
109                     NodeIterator nit = rssParent.getNode(ARCHIVE_NODE).getNodes();
110                     while (nit.hasNext()) {
111                         Node postsNode = nit.nextNode();
112 
113                         NodeIterator pit = postsNode.getNodes();
114                         while (pit.hasNext()) {
115                             Node nn = pit.nextNode();
116                             boolean hidden = false;
117                             if (nn.hasProperty("hidden")) {
118                                 hidden = nn.getProperty("hidden").getBoolean();
119                             }
120                             if (!hidden) {
121                                 listItems.add(nn);
122                             }
123                         }
124                     }
125                     sort(listItems);
126                 }
127             } else {
128                 log.info("No feed specified for planet feeds.");
129             }
130         } catch (Exception e) {
131             log.error("Problem while retrieving post items: " + e.getMessage());
132         }
133         return listItems;
134     }
135 
136     protected void sort(Collection<Node> listItems) {
137         Collections.sort((List<Node>) listItems, new Comparator<Node>() {
138             public int compare(Node n1, Node n2) {
139                 try {
140                     Date d1 = n1.getProperty(DATE_PROPERTY).getDate().getTime();
141                     Date d2 = n2.getProperty(DATE_PROPERTY).getDate().getTime();
142                     // ascending sort
143                     return d2.compareTo(d1);
144                 } catch (RepositoryException e) {
145                     log.error("Problem while sorting posts: " + e.getMessage());
146                 }
147                 return 0;
148             }
149         });
150     }
151 
152     public String getDescription(String entryDescription, Integer abbreviation) {
153         return PlanetUtil.formatDescription(entryDescription, abbreviation);
154     }
155 
156     public String getHackergotchi(String feedLink) {
157         // TODO refactor and make generic method in base class
158         String uuid = null;
159         try {
160             Node rssParent = SessionUtil.getNode("data", content.getProperty("link").getString());
161             if (rssParent.hasNode(FEEDS_NODE)) {
162                 Node settingsNode = rssParent.getNode(FEEDS_NODE);
163                 NodeIterator nit = settingsNode.getNodes();
164                 while (nit.hasNext()) {
165                     Node fsn = nit.nextNode();
166                     if (fsn.hasProperty("img") && fsn.hasProperty("link") && fsn.hasProperty("imgUUID")) {
167                         if (StringUtils.equalsIgnoreCase(feedLink, fsn.getProperty("link").getString())) {
168                             uuid = fsn.getProperty("imgUUID").getString();
169                             break;
170                         }
171                     }
172                 }
173             }
174         } catch (RepositoryException e) {
175             log.error("Problem while fetching Hackergotchi: " + e.getMessage());
176         }
177         return uuid;
178     }
179 
180     /**
181      * Retrieve the title attribute of an individual feed subscription.
182      *
183      * @param feedLink Link to the feed.
184      * @return Assigned title of a feed if defined in the dialog or null.
185      */
186     public String getFeedTitle(String feedLink) {
187         // TODO refactor and make generic method in base class
188         String title = null;
189         try {
190             Node rssParent = SessionUtil.getNode("data", content.getProperty("link").getString());
191             if (rssParent.hasNode(FEEDS_NODE)) {
192                 Node settingsNode = rssParent.getNode(FEEDS_NODE);
193                 NodeIterator nit = settingsNode.getNodes();
194                 while (nit.hasNext()) {
195                     Node fsn = nit.nextNode();
196                     if (fsn.hasProperty("link") && fsn.hasProperty("title")) {
197                         if (StringUtils.equalsIgnoreCase(feedLink, fsn.getProperty("link").getString())) {
198                             title = StringUtils.defaultString(fsn.getProperty("title").getString(), null);
199                             break;
200                         }
201                     }
202                 }
203             }
204         } catch (RepositoryException e) {
205             log.error("Problem while fetching feed title for the main planet feed: " + e.getMessage());
206         }
207         return title;
208     }
209 
210 }