View Javadoc

1   /**
2    * This file Copyright (c) 2008-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.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.jcr.util.PropertyUtil;
42  import info.magnolia.jcr.util.SessionUtil;
43  import info.magnolia.module.data.DataConsts;
44  import info.magnolia.rendering.model.RenderingModel;
45  import info.magnolia.rendering.model.RenderingModelImpl;
46  import info.magnolia.rendering.template.RenderableDefinition;
47  import info.magnolia.templating.functions.TemplatingFunctions;
48  
49  import java.util.ArrayList;
50  import java.util.Collection;
51  import java.util.Date;
52  import java.util.List;
53  
54  import javax.inject.Inject;
55  import javax.jcr.LoginException;
56  import javax.jcr.Node;
57  import javax.jcr.NodeIterator;
58  import javax.jcr.RepositoryException;
59  import javax.jcr.query.Query;
60  import javax.jcr.query.QueryManager;
61  import javax.jcr.query.QueryResult;
62  
63  import org.apache.commons.lang.StringUtils;
64  import org.slf4j.Logger;
65  import org.slf4j.LoggerFactory;
66  
67  /**
68   * Base class for rendering models.
69   * 
70   * @param <RD> Definition type.
71   * @author had
72   * @version $Id:$
73   */
74  public abstract class AbstractFeedModel<RD extends RenderableDefinition> extends RenderingModelImpl<RD> implements RenderingModel<RD> {
75      protected final static Logger log = LoggerFactory.getLogger(AbstractFeedModel.class);
76  
77      public static final String ORDER_BY_PUBLICATION_DATE = "@pubDate";
78      public static final String ORDER_BY_TITLE_NAME = "@title";
79      public static final String SORT_ASCENDING = "ascending";
80      public static final String SORT_DESCENDING = "descending";
81  
82      protected static final String AGGREGATOR_NODE = "/rssaggregator";
83      protected static final String DATA_WORKSPACE = "data";
84      protected static final String FEEDS_NODE = "feeds";
85  
86      protected static final int MAX_RESULTS = 20;
87  
88      protected final TemplatingFunctions templatingFunctions;
89  
90      @Inject
91      public AbstractFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
92          super(content, definition, parent);
93          this.templatingFunctions = templatingFunctions;
94      }
95  
96      /**
97       * 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.
98       * 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
99       */
100     @SuppressWarnings("unused")
101     public Date convertPubDate(Object timestamp) {
102         if (timestamp == null) {
103             return null;
104         }
105         if (timestamp instanceof String) {
106             if (StringUtils.isEmpty((String) timestamp)) {
107                 return null;
108             }
109             return new Date(Long.parseLong((String) timestamp));
110         }
111 
112         return new Date((Long) timestamp);
113     }
114 
115     /**
116      * Select items from the data module for planet components.
117      * 
118      * @param feedPath path where the items should be taken from
119      * @param condition query conditions
120      * @param order order of the resulting list
121      * @param limit if set to true, the resulting list will be shortend to the maximum number of entries allowed (if specified)
122      */
123     public Collection<Node> getPlanetItems(String feedPath, String condition, String order, boolean limit) {
124         int maxResults = MAX_RESULTS;
125 
126         List<Node> results = new ArrayList<Node>();
127         try {
128             if (limit && content.hasProperty("maxResults")) {
129                 maxResults = (int) content.getProperty("maxResults").getLong();
130             }
131             String feedName = NodeUtil.getNodeByIdentifier(DATA_WORKSPACE, PropertyUtil.getString(content, "feed")).getName();
132 
133             String sql = "select * from [dataItemNode] as t where ISDESCENDANTNODE([" + AGGREGATOR_NODE + "/" + feedName + "/" + feedPath + "])";
134             if (StringUtils.isNotBlank(condition)) {
135                 sql += " " + condition;
136             }
137             if (StringUtils.isNotBlank(order)) {
138                 sql += " " + order;
139             }
140             NodeIterator nit = QueryUtil.search("data", sql);
141 
142             int resCount = 0;
143             while (nit.hasNext()) {
144                 resCount++;
145                 Node statNode = nit.nextNode();
146                 if (limit) {
147                     if (resCount <= maxResults) {
148                         results.add(statNode);
149                     } else {
150                         break;
151                     }
152 
153                 } else {
154                     results.add(statNode);
155                 }
156             }
157             return results;
158         } catch (RepositoryException e) {
159             log.error(e.getLocalizedMessage(), e);
160         }
161         return null;
162     }
163 
164     /**
165      * Retrieve the title attribute of an individual feed subscription.
166      * 
167      * @param feedLink Link to the feed.
168      * @return Assigned title of a feed if defined in the dialog or null.
169      */
170     @SuppressWarnings("unused")
171     public String getFeedTitle(String feedLink) {
172         try {
173             if (content.hasProperty("feed")) {
174                 Node rssParent = SessionUtil.getNodeByIdentifier(DATA_WORKSPACE, content.getProperty("feed").getString());
175                 return getFeedProperty(rssParent, FEEDS_NODE, "link", feedLink, "title");
176             }
177         } catch (RepositoryException e) {
178             log.error("Problem while fetching feed title for statistics: " + e.getMessage());
179         }
180         return null;
181     }
182 
183     /**
184      * Find a feed node and return a property.
185      * 
186      * @param parentNode Planet feed node.
187      * @param subNode Node of the feed node where search should happen.
188      * @param searchProperty Node Property that should be searched.
189      * @param searchValue Search value.
190      * @param resultProperty Property value od found node to be returned or null.
191      */
192     public String getFeedProperty(Node parentNode, String subNode, String searchProperty, String searchValue, String resultProperty) {
193         String propertyValue = null;
194         try {
195             if (parentNode != null) {
196                 String searchNode = AGGREGATOR_NODE + "/" + parentNode.getName() + "/" + subNode;
197                 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + searchNode + "]) "
198                         + "and t." + searchProperty + "='" + searchValue + "'";
199 
200                 NodeIterator nit = QueryUtil.search(DATA_WORKSPACE, sql);
201 
202                 if (nit != null && nit.hasNext()) {
203                     Node fsn = nit.nextNode();
204                     // there shoul dbe only one node in the result list
205                     if (NodeUtil.getCollectionFromNodeIterator(nit).size() == 0) {
206                         if (fsn.hasProperty(resultProperty)) {
207                             propertyValue = fsn.getProperty(resultProperty).getString();
208                         }
209                     }
210                 }
211             }
212         } catch (RepositoryException e) {
213             log.error("Problem while getting node property: " + e.getMessage());
214         }
215         return propertyValue;
216     }
217 
218     public String getContextPath() {
219         return MgnlContext.getContextPath();
220     }
221 
222     protected NodeIterator runQuery(String queryString) throws LoginException, RepositoryException {
223 
224         QueryManager qm = MgnlContext.getJCRSession("data").getWorkspace().getQueryManager();
225         log.debug(queryString);
226         Query q = qm.createQuery(queryString, "xpath");
227         QueryResult res = q.execute();
228         NodeIterator feeds = res.getNodes();
229         FilteringNodeIterator iterator = new FilteringNodeIterator(feeds, new NodeTypePredicate(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE));
230         return iterator;
231     }
232 }