1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.tag

File TagQueryUtils.java

 

Coverage histogram

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

Code metrics

12
54
5
1
211
114
15
0.28
10.8
5
3

Classes

Class Line # Actions
TagQueryUtils 46 54 0% 15 71
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 com.xpn.xwiki.plugin.tag;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27    import java.util.TreeMap;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.query.Query;
31    import org.xwiki.query.QueryException;
32    import org.xwiki.query.QueryFilter;
33    import org.xwiki.query.internal.HiddenDocumentFilter;
34    import org.xwiki.query.internal.UniqueDocumentFilter;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.web.Utils;
39   
40    /**
41    * TagQueryUtils handles queries allowing to search and count tags within the wiki.
42    *
43    * @version $Id: a22c589b5c61edbb685f12f0e1da11a5de2b7a1a $
44    * @since 5.0M1
45    */
 
46    public final class TagQueryUtils
47    {
48    /**
49    * Hint of the "hidden" QueryFilter.
50    */
51    public static final String HIDDEN_QUERYFILTER_HINT = HiddenDocumentFilter.HINT;
52   
53    /**
54    * Utility class, private constructor.
55    */
 
56  0 toggle private TagQueryUtils()
57    {
58    }
59   
60    /**
61    * Get all tags within the wiki.
62    *
63    * @param context XWiki context.
64    * @return list of tags (alphabetical order).
65    * @throws com.xpn.xwiki.XWikiException if search query fails (possible failures: DB access problems, etc).
66    */
 
67  0 toggle public static List<String> getAllTags(XWikiContext context) throws XWikiException
68    {
69  0 List<String> results;
70   
71  0 String hql = "select distinct elements(prop.list) from XWikiDocument as doc, BaseObject as obj, "
72    + "DBStringListProperty as prop where obj.name=doc.fullName and obj.className='XWiki.TagClass' and "
73    + "obj.id=prop.id.id and prop.id.name='tags'";
74   
75  0 try {
76  0 Query query = context.getWiki().getStore().getQueryManager().createQuery(hql, Query.HQL);
77  0 query.addFilter(Utils.<QueryFilter> getComponent(QueryFilter.class, HiddenDocumentFilter.HINT));
78  0 results = query.execute();
79    } catch (QueryException e) {
80  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN,
81    String.format("Failed to get all tags", hql), e);
82    }
83   
84  0 Collections.sort(results, String.CASE_INSENSITIVE_ORDER);
85   
86  0 return results;
87    }
88   
89    /**
90    * Get cardinality map of tags matching a parameterized hql query.
91    *
92    * @param fromHql the <code>from</code> fragment of the hql query
93    * @param whereHql the <code>where</code> fragment of the hql query
94    * @param parameterValues list of parameter values for the query
95    * @param context XWiki context.
96    * @return map of tags (alphabetical order) with their occurrences counts.
97    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
98    * @since 1.18
99    * @see TagPluginApi#getTagCountForQuery(String, String, java.util.List)
100    */
 
101  0 toggle public static Map<String, Integer> getTagCountForQuery(String fromHql, String whereHql, List< ? > parameterValues,
102    XWikiContext context) throws XWikiException
103    {
104  0 List<String> results = null;
105  0 Map<String, Integer> tagCount = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
106   
107  0 String from = "select elements(prop.list) from XWikiDocument as doc, BaseObject as tagobject, "
108    + "DBStringListProperty as prop";
109  0 String where = " where tagobject.name=doc.fullName and tagobject.className='XWiki.TagClass' and "
110    + "tagobject.id=prop.id.id and prop.id.name='tags'";
111   
112    // If at least one of the fragments is passed, the query should be matching XWiki documents
113  0 if (!StringUtils.isBlank(fromHql) || !StringUtils.isBlank(whereHql)) {
114  0 from += fromHql;
115    }
116  0 if (!StringUtils.isBlank(whereHql)) {
117  0 where += " and " + whereHql;
118    }
119   
120  0 List<?> params = parameterValues;
121  0 if (params == null) {
122  0 params = new ArrayList<String>();
123    }
124  0 String hql = from + where;
125   
126  0 try {
127  0 Query query = context.getWiki().getStore().getQueryManager().createQuery(hql, Query.HQL);
128  0 query.bindValues((List<Object>) params);
129  0 query.addFilter(Utils.<QueryFilter> getComponent(QueryFilter.class, HiddenDocumentFilter.HINT));
130  0 results = query.execute();
131    } catch (QueryException e) {
132  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN,
133    String.format("Failed to get tag count for query [%s], with parameters [%s]", hql, params.toString()),
134    e);
135    }
136   
137  0 Collections.sort(results, String.CASE_INSENSITIVE_ORDER);
138  0 Map<String, String> processedTags = new HashMap<String, String>();
139   
140    // We have to manually build a cardinality map since we have to ignore tags case.
141  0 for (String result : results) {
142    // This key allows to keep track of the case variants we've encountered.
143  0 String lowerTag = result.toLowerCase();
144   
145    // We store the first case variant to reuse it in the final result set.
146  0 if (!processedTags.containsKey(lowerTag)) {
147  0 processedTags.put(lowerTag, result);
148    }
149   
150  0 String tagCountKey = processedTags.get(lowerTag);
151  0 int tagCountForTag = 0;
152  0 if (tagCount.get(tagCountKey) != null) {
153  0 tagCountForTag = tagCount.get(tagCountKey);
154    }
155  0 tagCount.put(tagCountKey, tagCountForTag + 1);
156    }
157   
158  0 return tagCount;
159    }
160   
161    /**
162    * Get non-hidden documents with the passed tags.
163    *
164    * @param tag a list of tags to match.
165    * @param context XWiki context.
166    * @return list of docNames.
167    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
168    */
 
169  0 toggle public static List<String> getDocumentsWithTag(String tag, XWikiContext context) throws XWikiException
170    {
171  0 return getDocumentsWithTag(tag, false, context);
172    }
173   
174    /**
175    * Get documents with the passed tags with the result depending on whether the caller decides to include
176    * hidden documents or not.
177    *
178    * @param tag a list of tags to match.
179    * @param includeHiddenDocuments if true then include hidden documents
180    * @param context XWiki context.
181    * @return list of docNames.
182    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
183    * @since 6.2M1
184    */
 
185  0 toggle public static List<String> getDocumentsWithTag(String tag, boolean includeHiddenDocuments, XWikiContext context)
186    throws XWikiException
187    {
188  0 List<String> results;
189  0 List<Object> parameters = new ArrayList<>();
190  0 parameters.add(TagPlugin.TAG_CLASS);
191  0 parameters.add(tag);
192  0 String hql = ", BaseObject as obj, DBStringListProperty as prop join prop.list item where obj.className=? and "
193    + "obj.name=doc.fullName and obj.id=prop.id.id and prop.id.name='tags' and lower(item)=lower(?) order by "
194    + "doc.fullName";
195   
196  0 try {
197  0 Query query = context.getWiki().getStore().getQueryManager().createQuery(hql, Query.HQL);
198  0 query.bindValues(parameters);
199  0 query.addFilter(Utils.getComponent(QueryFilter.class, UniqueDocumentFilter.HINT));
200  0 if (!includeHiddenDocuments) {
201  0 query.addFilter(Utils.getComponent(QueryFilter.class, HiddenDocumentFilter.HINT));
202    }
203  0 results = query.execute();
204    } catch (QueryException e) {
205  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN,
206    String.format("Failed to search for document with tag [%s]", tag), e);
207    }
208   
209  0 return results;
210    }
211    }