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

File CountDocumentFilter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
17
3
1
100
50
10
0.59
5.67
3
3.33

Classes

Class Line # Actions
CountDocumentFilter 46 17 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

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 java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.slf4j.Logger;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.query.Query;
31   
32    /**
33    * Query filter transforming queries in order to make them return the total number of results instead of a list of
34    * results. In order to do so a <code>select count()</code> will be inserted and possible <code>order by</code>
35    * clauses will be dropped. This transformation will only work with queries selecting full names of XWikiDocuments.
36    *
37    * For example <code>select doc.fullName from XWikiDocument where doc.space='Main'</code> will be transformed into
38    * <code>select count(doc.fullName) from XWikiDocument where doc.space='Main'</code>.
39    *
40    * @version $Id: fcd59d67c84e193dd43ae91c4384b1f426973372 $
41    * @since 4.1M1
42    */
43    @Component
44    @Named("count")
45    @Singleton
 
46    public class CountDocumentFilter extends AbstractQueryFilter
47    {
48    /**
49    * Used to log debug information.
50    */
51    @Inject
52    private Logger logger;
53   
54    /**
55    * @param statement statement to filter.
56    * @return true if the filter can be applied to the passed statement, false otherwise.
57    */
 
58  155 toggle private boolean isFilterable(String statement)
59    {
60  155 List<String> selectColumns = getSelectColumns(statement);
61  155 return selectColumns.contains(FULLNAME_COLUMN) || selectColumns.contains(DISTINCT_FULLNAME_COLUMN);
62    }
63   
 
64  155 toggle @Override
65    public String filterStatement(String statement, String language)
66    {
67  155 String result = statement.trim();
68  155 String original = result;
69   
70  155 if (Query.HQL.equals(language) && isFilterable(original)) {
71  153 String distinct = getSelectColumns(original).contains(DISTINCT_FULLNAME_COLUMN) ? "distinct " : "";
72  153 result = "select count(" + distinct + "doc.fullName) "
73    + result.substring(original.indexOf("from XWikiDocument"));
74   
75  153 int oidx = result.indexOf("order by ");
76  153 int gidx = result.indexOf("group by ");
77  153 if (oidx > -1) {
78  41 if (gidx > -1 && gidx > oidx) {
79    // There's an order by and a group by after, remove the order by only.
80  2 result = result.substring(0, oidx) + result.substring(gidx);
81    } else {
82    // There's only an order by, remove it.
83  39 result = result.substring(0, oidx);
84    }
85    }
86    }
87   
88  155 if (!original.equals(result)) {
89  153 logger.debug("Query [{}] has been transformed into [{}]", original, result);
90    }
91   
92  155 return result;
93    }
94   
 
95  145 toggle @Override
96    public List filterResults(List results)
97    {
98  145 return results;
99    }
100    }