1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.index.tree.internal.parentchild

File DocumentQueryHelper.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

4
27
2
1
124
71
4
0.15
13.5
2
2

Classes

Class Line # Actions
DocumentQueryHelper 51 27 0% 4 33
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.index.tree.internal.parentchild;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import javax.inject.Inject;
29    import javax.inject.Named;
30    import javax.inject.Singleton;
31   
32    import org.apache.commons.lang3.StringUtils;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.DocumentReferenceResolver;
36    import org.xwiki.model.reference.EntityReference;
37    import org.xwiki.query.Query;
38    import org.xwiki.query.QueryException;
39    import org.xwiki.query.QueryFilter;
40    import org.xwiki.query.QueryManager;
41   
42    /**
43    * Helper component for querying the documents.
44    *
45    * @version $Id: 4f5c26ac5a9594d9e073c3a8ca4f03540dc5d88b $
46    * @since 8.3M2
47    * @since 7.4.5
48    */
49    @Component(roles = DocumentQueryHelper.class)
50    @Singleton
 
51    public class DocumentQueryHelper
52    {
53    @Inject
54    @Named("explicit")
55    private DocumentReferenceResolver<String> explicitDocumentReferenceResolver;
56   
57    @Inject
58    @Named("hidden/document")
59    private QueryFilter hiddenDocumentQueryFilter;
60   
61    @Inject
62    private QueryManager queryManager;
63   
64    /**
65    * Creates a query that returns documents matching the specified constraints.
66    *
67    * @param constraints the document constraints
68    * @param parameters the query parameters
69    * @param config the tree configuration properties
70    * @return the query
71    * @throws QueryException if creating the query fails
72    */
 
73  0 toggle public Query getQuery(List<String> constraints, Map<String, Object> parameters, Map<String, Object> config)
74    throws QueryException
75    {
76  0 String fromClause = "";
77  0 List<String> finalConstraints = new ArrayList<String>();
78  0 Map<String, Object> finalParameters = new HashMap<String, Object>();
79  0 String xclass = (String) config.get("filterByClass");
80  0 if (!StringUtils.isEmpty(xclass)) {
81  0 fromClause = ", BaseObject as obj";
82  0 finalConstraints.add("obj.name = doc.fullName");
83  0 finalConstraints.add("obj.className = :class");
84  0 finalConstraints.add("doc.fullName <> :template");
85  0 finalParameters.put("class", xclass);
86  0 finalParameters.put("template", StringUtils.removeEnd(xclass, "Class") + "Template");
87    }
88  0 finalConstraints.addAll(constraints);
89  0 finalParameters.putAll(parameters);
90  0 String whereClause = "where " + StringUtils.join(finalConstraints, " and ");
91  0 String statement =
92    StringUtils.join(Arrays.asList(fromClause, whereClause, "order by lower(doc.name), doc.name"), ' ');
93  0 Query query = this.queryManager.createQuery(statement, Query.HQL);
94  0 for (Map.Entry<String, Object> entry : finalParameters.entrySet()) {
95  0 query.bindValue(entry.getKey(), entry.getValue());
96    }
97  0 if (Boolean.TRUE.equals(config.get("filterHiddenDocuments"))) {
98  0 query.addFilter(this.hiddenDocumentQueryFilter);
99    }
100  0 return query;
101    }
102   
103    /**
104    * Resolves the results obtained by executing {@link #getQuery(List, Map, Map)}.
105    *
106    * @param query a query created with {@link #getQuery(List, Map, Map)}
107    * @param offset the offset in the list of results
108    * @param limit the maximum number of results to return
109    * @param parentReference the base reference used when resolving the results
110    * @return the list of document references
111    * @throws QueryException if executing the query fails
112    */
 
113  0 toggle public List<DocumentReference> resolve(Query query, int offset, int limit, EntityReference parentReference)
114    throws QueryException
115    {
116  0 query.setOffset(offset);
117  0 query.setLimit(limit);
118  0 List<DocumentReference> documentReferences = new ArrayList<DocumentReference>();
119  0 for (Object result : query.execute()) {
120  0 documentReferences.add(this.explicitDocumentReferenceResolver.resolve((String) result, parentReference));
121    }
122  0 return documentReferences;
123    }
124    }