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.NodeUtil;
41  import info.magnolia.module.data.DataConsts;
42  import info.magnolia.rendering.model.RenderingModel;
43  import info.magnolia.rendering.model.RenderingModelImpl;
44  import info.magnolia.rendering.template.RenderableDefinition;
45  import info.magnolia.templating.functions.TemplatingFunctions;
46  
47  import java.util.Date;
48  
49  import javax.inject.Inject;
50  import javax.jcr.LoginException;
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 AGGREGATOR_NODE = "/rssaggregator";
78      protected static final String DATA_WORKSPACE = "data";
79      protected static final String FEEDS_NODE = "feeds";
80  
81      protected static final int MAX_RESULTS = 20;
82  
83      protected final TemplatingFunctions templatingFunctions;
84  
85      @Inject
86      public AbstractFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
87          super(content, definition, parent);
88          this.templatingFunctions = templatingFunctions;
89      }
90  
91      /**
92       * 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.
93       * 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
94       */
95      public Date convertPubDate(Object timestamp) {
96          if (timestamp == null) {
97              return null;
98          }
99          if (timestamp instanceof String) {
100             if (StringUtils.isEmpty((String) timestamp)) {
101                 return null;
102             }
103             return new Date(Long.parseLong((String) timestamp));
104         }
105 
106         return new Date((Long) timestamp);
107     }
108 
109     /**
110      * Find a feed node and return a property.
111      * 
112      * @param parentNode Planet feed node.
113      * @param subNode Node of the feed node where search should happen.
114      * @param searchProperty Node Property that should be searched.
115      * @param searchValue Search value.
116      * @param resultProperty Property value od found node to be returned or null.
117      */
118     public String getFeedProperty(Node parentNode, String subNode, String searchProperty, String searchValue, String resultProperty) {
119         String propertyValue = null;
120         try {
121             if (parentNode != null) {
122                 String searchNode = AGGREGATOR_NODE + "/" + parentNode.getName() + "/" + subNode;
123                 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + searchNode + "]) "
124                         + "and t." + searchProperty + "='" + searchValue + "'";
125                 // log.debug(sql);
126 
127                 NodeIterator nit = QueryUtil.search(DATA_WORKSPACE, sql);
128 
129                 if (nit != null && nit.hasNext()) {
130                     Node fsn = nit.nextNode();
131                     // there shoul dbe only one node in the result list
132                     if (NodeUtil.getCollectionFromNodeIterator(nit).size() == 0) {
133                         if (fsn.hasProperty(resultProperty)) {
134                             propertyValue = fsn.getProperty(resultProperty).getString();
135                         }
136                     }
137                 }
138             }
139         } catch (RepositoryException e) {
140             log.error("Problem while getting node property: " + e.getMessage());
141         }
142         return propertyValue;
143     }
144 
145     public String getContextPath() {
146         return MgnlContext.getContextPath();
147     }
148 
149     protected NodeIterator runQuery(String queryString) throws LoginException, RepositoryException {
150 
151         QueryManager qm = MgnlContext.getJCRSession("data").getWorkspace().getQueryManager();
152         log.debug(queryString);
153         Query q = qm.createQuery(queryString, "xpath");
154         QueryResult res = q.execute();
155         NodeIterator feeds = res.getNodes();
156         FilteringNodeIterator iterator = new FilteringNodeIterator(feeds, new NodeTypePredicate(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE));
157         return iterator;
158     }
159 }