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

File ScriptQuery.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

8
55
24
1
269
176
30
0.55
2.29
24
1.25

Classes

Class Line # Actions
ScriptQuery 42 55 0% 30 29
0.666666766.7%
 

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.query.internal;
21   
22    import org.apache.commons.lang3.StringUtils;
23    import org.slf4j.Logger;
24    import org.slf4j.LoggerFactory;
25    import org.xwiki.component.manager.ComponentLookupException;
26    import org.xwiki.component.manager.ComponentManager;
27    import org.xwiki.query.Query;
28    import org.xwiki.query.QueryException;
29    import org.xwiki.query.QueryFilter;
30    import org.xwiki.query.QueryManager;
31    import org.xwiki.query.SecureQuery;
32   
33    import java.util.List;
34    import java.util.Map;
35   
36    /**
37    * Query wrapper that allows to set filter from the filter component hint.
38    *
39    * @version $Id: 397e222d151c9ab53faae8eb0d8600346c33f1a0 $
40    * @since 4.0RC1
41    */
 
42    public class ScriptQuery implements SecureQuery
43    {
44    /**
45    * Used to log possible warnings.
46    */
47    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptQuery.class);
48   
49    /**
50    * Used to retrieve {@link org.xwiki.query.QueryFilter} implementations.
51    */
52    private ComponentManager componentManager;
53   
54    /**
55    * The wrapped {@link Query}.
56    */
57    private Query query;
58   
59    /**
60    * Constructor.
61    *
62    * @param query the query object to wrap.
63    * @param cm the xwiki component manager.
64    */
 
65  363 toggle public ScriptQuery(Query query, ComponentManager cm)
66    {
67  363 this.query = query;
68  363 this.componentManager = cm;
69    }
70   
71    /**
72    * Set a filter in the wrapped query from the filter component hint.
73    *
74    * @param filter the hint of the component filter to set in the wrapped query.
75    * @return this query object.
76    */
 
77  288 toggle public Query addFilter(String filter)
78    {
79  288 if (!StringUtils.isBlank(filter)) {
80  288 try {
81  288 QueryFilter queryFilter = this.componentManager.getInstance(QueryFilter.class, filter);
82  288 addFilter(queryFilter);
83    } catch (ComponentLookupException e) {
84    // We need to avoid throwing exceptions in the wiki if the filter does not exist.
85  0 LOGGER.warn("Failed to load QueryFilter with component hint [{}]", filter);
86    }
87    }
88   
89  288 return this;
90    }
91   
92    /**
93    * Allow to retrieve the total count of items for the given query instead of the actual results. This method will
94    * only work for queries selecting document full names, see {@link CountDocumentFilter} for more information.
95    *
96    * @return the total number of results for this query.
97    */
 
98  79 toggle public long count()
99    {
100  79 long result = -1;
101   
102  79 try {
103    // Create a copy of the wrapped query.
104  79 QueryManager queryManager = (QueryManager) this.componentManager.getInstance(QueryManager.class);
105  79 Query countQuery = queryManager.createQuery(getStatement(), getLanguage());
106  79 countQuery.setWiki(getWiki());
107  79 for (Map.Entry<Integer, Object> entry : getPositionalParameters().entrySet()) {
108  139 countQuery.bindValue(entry.getKey(), entry.getValue());
109    }
110  79 for (Map.Entry<String, Object> entry : getNamedParameters().entrySet()) {
111  14 countQuery.bindValue(entry.getKey(), entry.getValue());
112    }
113  79 for (QueryFilter filter : getFilters()) {
114  102 countQuery.addFilter(filter);
115    }
116   
117    // Add the count filter to it.
118  79 countQuery.addFilter(this.componentManager.<QueryFilter>getInstance(QueryFilter.class, "count"));
119   
120    // Execute and retrieve the count result.
121  79 List<Long> results = countQuery.execute();
122  79 result = results.get(0);
123    } catch (Exception e) {
124  0 LOGGER.warn("Failed to create count query for query [{}]", getStatement());
125  0 e.printStackTrace();
126    }
127   
128  79 return result;
129    }
130   
 
131  79 toggle @Override
132    public String getStatement()
133    {
134  79 return this.query.getStatement();
135    }
136   
 
137  79 toggle @Override
138    public String getLanguage()
139    {
140  79 return this.query.getLanguage();
141    }
142   
 
143  0 toggle @Override
144    public boolean isNamed()
145    {
146  0 return this.query.isNamed();
147    }
148   
 
149  13 toggle @Override
150    public Query setWiki(String wiki)
151    {
152  13 this.query.setWiki(wiki);
153  13 return this;
154    }
155   
 
156  79 toggle @Override
157    public String getWiki()
158    {
159  79 return this.query.getWiki();
160    }
161   
 
162  38 toggle @Override
163    public Query bindValue(String var, Object val)
164    {
165  38 this.query.bindValue(var, val);
166  38 return this;
167    }
168   
 
169  0 toggle @Override
170    public Query bindValue(int index, Object val)
171    {
172  0 this.query.bindValue(index, val);
173  0 return this;
174    }
175   
 
176  275 toggle @Override
177    public Query bindValues(List<Object> values)
178    {
179  275 this.query.bindValues(values);
180  275 return this;
181    }
182   
 
183  79 toggle @Override
184    public Map<String, Object> getNamedParameters()
185    {
186  79 return this.query.getNamedParameters();
187    }
188   
 
189  79 toggle @Override
190    public Map<Integer, Object> getPositionalParameters()
191    {
192  79 return this.query.getPositionalParameters();
193    }
194   
 
195  288 toggle @Override
196    public Query addFilter(QueryFilter filter)
197    {
198  288 this.query.addFilter(filter);
199  288 return this;
200    }
201   
 
202  79 toggle @Override
203    public List<QueryFilter> getFilters()
204    {
205  79 return this.query.getFilters();
206    }
207   
 
208  76 toggle @Override
209    public Query setLimit(int limit)
210    {
211  76 this.query.setLimit(limit);
212  76 return this;
213    }
214   
 
215  76 toggle @Override
216    public Query setOffset(int offset)
217    {
218  76 this.query.setOffset(offset);
219  76 return this;
220    }
221   
 
222  0 toggle @Override
223    public int getLimit()
224    {
225  0 return this.query.getLimit();
226    }
227   
 
228  0 toggle @Override
229    public int getOffset()
230    {
231  0 return this.query.getOffset();
232    }
233   
 
234  337 toggle @Override
235    public <T> List<T> execute() throws QueryException
236    {
237  337 return this.query.execute();
238    }
239   
 
240  0 toggle @Override
241    public boolean isCurrentAuthorChecked()
242    {
243  0 return this.query instanceof SecureQuery ? ((SecureQuery) this.query).isCurrentAuthorChecked() : true;
244    }
245   
 
246  0 toggle @Override
247    public SecureQuery checkCurrentAuthor(boolean checkCurrentAuthor)
248    {
249    // Always check current author for scripts
250   
251  0 return this;
252    }
253   
 
254  0 toggle @Override
255    public boolean isCurrentUserChecked()
256    {
257  0 return this.query instanceof SecureQuery ? ((SecureQuery) this.query).isCurrentAuthorChecked() : false;
258    }
259   
 
260  0 toggle @Override
261    public SecureQuery checkCurrentUser(boolean checkCurrentUser)
262    {
263  0 if (this.query instanceof SecureQuery) {
264  0 ((SecureQuery) this.query).isCurrentAuthorChecked();
265    }
266   
267  0 return this;
268    }
269    }