1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 0% |
Uncovered Elements: 71 (71) |
Complexity: 15 |
Complexity Density: 0.28 |
|
46 |
|
public final class TagQueryUtils |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
public static final String HIDDEN_QUERYFILTER_HINT = HiddenDocumentFilter.HINT; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
56 |
0 |
private TagQueryUtils()... |
57 |
|
{ |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@param |
64 |
|
@return |
65 |
|
@throws |
66 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
67 |
0 |
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 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
@param |
94 |
|
@param |
95 |
|
@param |
96 |
|
@return |
97 |
|
@throws |
98 |
|
@since |
99 |
|
@see |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 40 (40) |
Complexity: 8 |
Complexity Density: 0.27 |
|
101 |
0 |
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 |
|
|
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 |
|
|
141 |
0 |
for (String result : results) { |
142 |
|
|
143 |
0 |
String lowerTag = result.toLowerCase(); |
144 |
|
|
145 |
|
|
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 |
|
|
163 |
|
|
164 |
|
@param |
165 |
|
@param |
166 |
|
@return |
167 |
|
@throws |
168 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
169 |
0 |
public static List<String> getDocumentsWithTag(String tag, XWikiContext context) throws XWikiException... |
170 |
|
{ |
171 |
0 |
return getDocumentsWithTag(tag, false, context); |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@param |
179 |
|
@param |
180 |
|
@param |
181 |
|
@return |
182 |
|
@throws |
183 |
|
@since |
184 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 3 |
Complexity Density: 0.21 |
|
185 |
0 |
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 |
|
} |