View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 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.jcr.iterator.FilteringNodeIterator;
39  import info.magnolia.jcr.predicate.NodeTypePredicate;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.jcr.util.NodeUtil;
42  import info.magnolia.module.rssaggregator.RSSAggregatorConstants;
43  import info.magnolia.rendering.model.RenderingModel;
44  import info.magnolia.rendering.model.RenderingModelImpl;
45  import info.magnolia.rendering.template.RenderableDefinition;
46  import info.magnolia.templating.functions.TemplatingFunctions;
47  
48  import java.util.Date;
49  
50  import javax.inject.Inject;
51  import javax.jcr.Node;
52  import javax.jcr.NodeIterator;
53  import javax.jcr.RepositoryException;
54  import javax.jcr.query.Query;
55  import javax.jcr.query.QueryManager;
56  import javax.jcr.query.QueryResult;
57  
58  import org.apache.commons.lang.StringUtils;
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  /**
63   * Base class for rendering models.
64   * 
65   * @param <RD> Definition type.
66   * @author had
67   * @version $Id:$
68   */
69  public abstract class AbstractFeedModel<RD extends RenderableDefinition> extends RenderingModelImpl<RD> implements RenderingModel<RD> {
70      protected final static Logger log = LoggerFactory.getLogger(AbstractFeedModel.class);
71  
72      public static final String ORDER_BY_PUBLICATION_DATE = "@pubDate";
73      public static final String ORDER_BY_TITLE_NAME = "@title";
74      public static final String SORT_ASCENDING = "ascending";
75      public static final String SORT_DESCENDING = "descending";
76  
77      protected static final String FEEDS_NODE = "feeds";
78  
79      protected static final int MAX_RESULTS = 20;
80  
81      protected final TemplatingFunctions templatingFunctions;
82  
83      @Inject
84      public AbstractFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
85          super(content, definition, parent);
86          this.templatingFunctions = templatingFunctions;
87      }
88  
89      /**
90       * This can get sometimes confusing. Freemarker will not create date from the java long time stamp format so we need to convert it to date first.
91       * Depending on circumstances (whether coming from node or from node wrapper) the time stamp will be passed in as a String or as a Long object
92       */
93      public Date convertPubDate(Object timestamp) {
94          if (timestamp == null) {
95              return null;
96          }
97          if (timestamp instanceof String) {
98              if (StringUtils.isEmpty((String) timestamp)) {
99                  return null;
100             }
101             return new Date(Long.parseLong((String) timestamp));
102         }
103 
104         return new Date((Long) timestamp);
105     }
106 
107 
108     /**
109      * Find a feed node and return a property.
110      * 
111      * @param parentNode Planet feed node.
112      * @param subNode Node of the feed node where search should happen.
113      * @param searchProperty Node Property that should be searched.
114      * @param searchValue Search value.
115      * @param resultProperty Property value od found node to be returned or null.
116      */
117     public String getFeedProperty(Node parentNode, String subNode, String searchProperty, String searchValue, String resultProperty) {
118         String propertyValue = null;
119         try {
120             if (parentNode != null) {
121                 String searchNode = "/" + parentNode.getName() + "/" + subNode;
122                 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + searchNode + "]) "
123                         + "and t." + searchProperty + "='" + searchValue + "'";
124                 // log.debug(sql);
125 
126                 NodeIterator nit = QueryUtil.search(RSSAggregatorConstants.WORKSPACE, sql);
127 
128                 if (nit != null && nit.hasNext()) {
129                     Node fsn = nit.nextNode();
130                     // there should be only one node in the result list
131                     if (NodeUtil.getCollectionFromNodeIterator(nit).size() == 0) {
132                         if (fsn.hasProperty(resultProperty)) {
133                             propertyValue = fsn.getProperty(resultProperty).getString();
134                         }
135                     }
136                 }
137             }
138         } catch (RepositoryException e) {
139             log.error("Problem while getting node property: " + e.getMessage());
140         }
141         return propertyValue;
142     }
143 
144     public String getContextPath() {
145         return MgnlContext.getContextPath();
146     }
147 
148     protected NodeIterator runQuery(String queryString) throws RepositoryException {
149         QueryManager qm = MgnlContext.getJCRSession(RSSAggregatorConstants.WORKSPACE).getWorkspace().getQueryManager();
150         log.debug(queryString);
151         Query q = qm.createQuery(queryString, "xpath");
152         QueryResult res = q.execute();
153         NodeIterator feeds = res.getNodes();
154         FilteringNodeIterator iterator = new FilteringNodeIterator(feeds, new NodeTypePredicate(NodeTypes.Content.NAME));
155         return iterator;
156     }
157 }