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.util.NodeUtil;
39  import info.magnolia.rendering.model.RenderingModel;
40  import info.magnolia.rendering.model.RenderingModelImpl;
41  import info.magnolia.rendering.template.RenderableDefinition;
42  import info.magnolia.templating.functions.TemplatingFunctions;
43  
44  import java.util.Date;
45  
46  import javax.inject.Inject;
47  import javax.jcr.Node;
48  import javax.jcr.NodeIterator;
49  import javax.jcr.RepositoryException;
50  
51  import org.apache.commons.lang.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  
56  /**
57   * Base class for rendering models.
58   *
59   * @param <RD> Definition type.
60   * @author had
61   * @version $Id:$
62   */
63  public abstract class AbstractFeedModel<RD extends RenderableDefinition> extends RenderingModelImpl<RD> implements RenderingModel<RD> {
64      protected final static Logger log = LoggerFactory.getLogger(AbstractFeedModel.class);
65  
66      public static final String ORDER_BY_PUBLICATION_DATE = "@pubDate";
67      public static final String ORDER_BY_TITLE_NAME = "@title";
68      public static final String SORT_ASCENDING = "ascending";
69      public static final String SORT_DESCENDING = "descending";
70  
71      protected static final String AGGREGATOR_NODE = "/rssaggregator";
72      protected static final String DATA_WORKSPACE = "data";
73      protected static final String FEEDS_NODE = "feeds";
74  
75      protected static final int MAX_RESULTS = 20;
76  
77      protected final TemplatingFunctions templatingFunctions;
78  
79      @Inject
80      public AbstractFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
81          super(content, definition, parent);
82          this.templatingFunctions = templatingFunctions;
83      }
84  
85      /**
86       * 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.
87       * 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
88       */
89      public Date convertPubDate(Object timestamp) {
90          if (timestamp == null) {
91              return null;
92          }
93          if (timestamp instanceof String) {
94              if (StringUtils.isEmpty((String) timestamp)) {
95                  return null;
96              }
97              return new Date(Long.parseLong((String) timestamp));
98          }
99  
100         return new Date((Long) timestamp);
101     }
102 
103     /**
104      * Find a feed node and return a property.
105      *
106      * @param parentNode Planet feed node.
107      * @param subNode Node of the feed node where search should happen.
108      * @param searchProperty Node Property that should be searched.
109      * @param searchValue Search value.
110      * @param resultProperty Property value od found node to be returned or null.
111      */
112     public String getFeedProperty(Node parentNode, String subNode, String searchProperty, String searchValue, String resultProperty) {
113         String propertyValue = null;
114         try {
115             if (parentNode != null) {
116                 String searchNode = AGGREGATOR_NODE + "/" + parentNode.getName() + "/" + subNode;
117                 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + searchNode + "]) "
118                         + "and t." + searchProperty + "='" + searchValue + "'";
119                 // log.debug(sql);
120 
121                 NodeIterator nit = QueryUtil.search(DATA_WORKSPACE, sql);
122 
123                 if (nit != null && nit.hasNext()) {
124                     Node fsn = nit.nextNode();
125                     // there shoul dbe only one node in the result list
126                     if (NodeUtil.getCollectionFromNodeIterator(nit).size() == 0) {
127                         if (fsn.hasProperty(resultProperty)) {
128                             propertyValue = fsn.getProperty(resultProperty).getString();
129                         }
130                     }
131                 }
132             }
133         } catch (RepositoryException e) {
134             log.error("Problem while getting node property: " + e.getMessage());
135         }
136         return propertyValue;
137     }
138 
139     public String getContextPath() {
140         return MgnlContext.getContextPath();
141     }
142 
143 }