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.templatingkit.templates.components;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.MgnlNodeType;
38  import info.magnolia.cms.core.search.Query;
39  import info.magnolia.cms.core.search.QueryResult;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.dam.Dam;
42  import info.magnolia.jcr.util.PropertyUtil;
43  import info.magnolia.jcr.util.SessionUtil;
44  import info.magnolia.module.dms.beans.Document;
45  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
46  import info.magnolia.module.templatingkit.templates.AbstractSTKTemplateModel;
47  import info.magnolia.rendering.model.RenderingModel;
48  import info.magnolia.rendering.template.TemplateDefinition;
49  import info.magnolia.templating.functions.TemplatingFunctions;
50  
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import javax.inject.Inject;
55  import javax.jcr.Node;
56  import javax.jcr.RepositoryException;
57  import javax.jcr.query.InvalidQueryException;
58  
59  import org.apache.commons.lang.StringUtils;
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  /**
64   * STK Rendable Model definition dedicated to Download List Link display and execution.
65   * @author Ryan Gardner, ochytil
66   * @version $Id$
67   * @param <RD>
68   */
69  public class DownloadListModel<RD extends TemplateDefinition> extends AbstractSTKTemplateModel<TemplateDefinition> {
70  
71      private static final Logger log = LoggerFactory.getLogger(DownloadListModel.class);
72  
73      private List<Document> documentList;
74  
75      @Inject
76      public DownloadListModel(Node content, RD definition,  RenderingModel<?> parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
77          super(content, definition, parent, stkFunctions, templatingFunctions);
78      }
79  
80      private String getLink() {
81          return PropertyUtil.getString(content, "link");
82      }
83  
84      private String getExtension() {
85          return PropertyUtil.getString(content, "extension");
86      }
87  
88      private String getQuery() {
89          return PropertyUtil.getString(content, "query");
90      }
91  
92      /**
93       * Get the documents.
94       * @return
95       */
96      public List<Document> getDocumentList() {
97          return documentList;
98      }
99  
100     /**
101      * FIXME usage of deprecated classes: {@link Content}.
102      */
103     @Override
104     public String execute() {
105         String link = getLink();
106         List<String> expressions = new ArrayList<String>();
107 
108         if (StringUtils.isNotEmpty(link)) {
109             Node node = SessionUtil.getNode(Dam.DAM_WORKSPACE, link);
110             expressions.add("jcr:path like '" + link + "/%'");
111         }
112 
113         String extension = getExtension();
114         if(StringUtils.isNotEmpty(extension)){
115             expressions.add("extension = '" + extension + "'");
116         }
117 
118         String query = getQuery();
119         if(StringUtils.isNotEmpty(query)){
120             expressions.add(query);
121         }
122 
123         if(expressions.size() > 0){
124             String where = StringUtils.join(expressions.iterator(), " and ");
125 
126             String queryStr= "SELECT * FROM nt:base WHERE " + where;
127 
128             try {
129                 Query q = MgnlContext.getQueryManager(Dam.DAM_WORKSPACE).createQuery(queryStr, "sql");
130 
131                 QueryResult result = q.execute();
132                 documentList = new ArrayList<Document>(result.getContent("mgnl:contentNode").size());
133                 for (Content node : result.getContent("mgnl:contentNode")) {
134                     if (Document.isDocument(node)) {
135                         Document doc = new Document(node);
136                         if(!node.hasMixin(MgnlNodeType.MIX_DELETED)){
137                             documentList.add(doc);
138                         }
139                     }
140                 }
141             }
142             catch (InvalidQueryException re){
143                 log.warn("Query cannot be solved with exception: " + re);
144             }
145             catch (RepositoryException re) {
146                 throw new RuntimeException(re);
147             }
148         }
149         return super.execute();
150     }
151 
152     /**
153      * FIXME usage of deprecated classes: {@link Content}.
154      */
155     public String getLinkToDocument(Document document){
156        return templatingFunctions.link(document.getNode().getJCRNode());
157     }
158 
159 
160 }