1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.ratings.script; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Provider; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
33 |
|
import org.xwiki.model.reference.EntityReference; |
34 |
|
import org.xwiki.ratings.AverageRatingApi; |
35 |
|
import org.xwiki.ratings.ConfiguredProvider; |
36 |
|
import org.xwiki.ratings.Rating; |
37 |
|
import org.xwiki.ratings.RatingsConfiguration; |
38 |
|
import org.xwiki.ratings.RatingsManager; |
39 |
|
import org.xwiki.script.service.ScriptService; |
40 |
|
import org.xwiki.stability.Unstable; |
41 |
|
|
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.api.Document; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
51 |
|
@Component |
52 |
|
@Singleton |
53 |
|
@Named("ratings") |
|
|
| 18.7% |
Uncovered Elements: 74 (91) |
Complexity: 31 |
Complexity Density: 0.47 |
|
54 |
|
public class RatingsScriptService implements ScriptService |
55 |
|
{ |
56 |
|
@Inject |
57 |
|
private Provider<XWikiContext> xcontextProvider; |
58 |
|
|
59 |
|
@Inject |
60 |
|
private ConfiguredProvider<RatingsManager> ratingsManagerProvider; |
61 |
|
|
62 |
|
@Inject |
63 |
|
@Named("current") |
64 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
65 |
|
|
66 |
|
@Inject |
67 |
|
@Named("current") |
68 |
|
private DocumentReferenceResolver<EntityReference> referenceDocumentReferenceResolver; |
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("user/current") |
72 |
|
private DocumentReferenceResolver<String> userReferenceResolver; |
73 |
|
|
74 |
|
@Inject |
75 |
|
private RatingsConfiguration ratingsConfiguration; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@return |
81 |
|
@throws |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
45 |
private XWikiContext getXWikiContext()... |
84 |
|
{ |
85 |
45 |
return this.xcontextProvider.get(); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@param |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
93 |
12 |
private void setError(Throwable e)... |
94 |
|
{ |
95 |
12 |
getXWikiContext().put("exception", e); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@return |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
0 |
private DocumentReference getGlobalConfig()... |
104 |
|
{ |
105 |
0 |
return this.referenceDocumentReferenceResolver.resolve(RatingsManager.RATINGS_CONFIG_GLOBAL_REFERENCE); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
@param |
112 |
|
@return |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
114 |
0 |
private static List<RatingApi> wrapRatings(List<Rating> ratings)... |
115 |
|
{ |
116 |
0 |
if (ratings == null) { |
117 |
0 |
return null; |
118 |
|
} |
119 |
|
|
120 |
0 |
List<RatingApi> ratingsResult = new ArrayList<RatingApi>(); |
121 |
0 |
for (Rating rating : ratings) { |
122 |
0 |
ratingsResult.add(new RatingApi(rating)); |
123 |
|
} |
124 |
|
|
125 |
0 |
return ratingsResult; |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
@param |
132 |
|
@param |
133 |
|
@param |
134 |
|
@return |
135 |
|
@deprecated@link |
136 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
137 |
0 |
@Deprecated... |
138 |
|
public RatingApi setRating(Document doc, String author, int vote) |
139 |
|
{ |
140 |
0 |
DocumentReference documentRef = doc.getDocumentReference(); |
141 |
0 |
DocumentReference authorRef = this.userReferenceResolver.resolve(author); |
142 |
0 |
return setRating(documentRef, authorRef, vote); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
@param |
150 |
|
@param |
151 |
|
@return |
152 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
153 |
0 |
public RatingApi setRating(DocumentReference document, DocumentReference author, int vote)... |
154 |
|
{ |
155 |
|
|
156 |
|
|
157 |
0 |
setError(null); |
158 |
|
|
159 |
0 |
try { |
160 |
0 |
return new RatingApi(this.ratingsManagerProvider.get(document).setRating(document, author, vote)); |
161 |
|
} catch (Throwable e) { |
162 |
0 |
setError(e); |
163 |
0 |
return null; |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
@param |
171 |
|
@param |
172 |
|
@return |
173 |
|
@deprecated@link |
174 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
175 |
0 |
@Deprecated... |
176 |
|
public RatingApi getRating(Document doc, String author) |
177 |
|
{ |
178 |
0 |
DocumentReference documentRef = doc.getDocumentReference(); |
179 |
0 |
DocumentReference authorRef = this.userReferenceResolver.resolve(author); |
180 |
0 |
return getRating(documentRef, authorRef); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
@param |
187 |
|
@param |
188 |
|
@return |
189 |
|
|
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
190 |
6 |
public RatingApi getRating(DocumentReference document, DocumentReference author)... |
191 |
|
{ |
192 |
6 |
setError(null); |
193 |
|
|
194 |
6 |
try { |
195 |
6 |
Rating rating = this.ratingsManagerProvider.get(document).getRating(document, author); |
196 |
6 |
if (rating == null) { |
197 |
6 |
return null; |
198 |
|
} |
199 |
0 |
return new RatingApi(rating); |
200 |
|
} catch (Throwable e) { |
201 |
0 |
setError(e); |
202 |
0 |
return null; |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
@param |
210 |
|
@param |
211 |
|
@param |
212 |
|
@return |
213 |
|
@deprecated@link |
214 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
0 |
@Deprecated... |
216 |
|
public List<RatingApi> getRatings(Document doc, int start, int count) |
217 |
|
{ |
218 |
0 |
return getRatings(doc.getDocumentReference(), start, count); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
@param |
225 |
|
@param |
226 |
|
@param |
227 |
|
@return |
228 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
0 |
public List<RatingApi> getRatings(DocumentReference document, int start, int count)... |
230 |
|
{ |
231 |
0 |
return getRatings(document, start, count, true); |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
@param |
238 |
|
@param |
239 |
|
@param |
240 |
|
@param |
241 |
|
@return |
242 |
|
@deprecated@link |
243 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
0 |
@Deprecated... |
245 |
|
public List<RatingApi> getRatings(Document doc, int start, int count, boolean asc) |
246 |
|
{ |
247 |
0 |
return getRatings(doc.getDocumentReference(), start, count, asc); |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
@param |
255 |
|
@param |
256 |
|
@param |
257 |
|
@return |
258 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
259 |
0 |
public List<RatingApi> getRatings(DocumentReference document, int start, int count, boolean asc)... |
260 |
|
{ |
261 |
0 |
setError(null); |
262 |
|
|
263 |
0 |
try { |
264 |
0 |
return wrapRatings(this.ratingsManagerProvider.get(document).getRatings(document, start, count, asc)); |
265 |
|
} catch (Exception e) { |
266 |
0 |
setError(e); |
267 |
0 |
return null; |
268 |
|
} |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
@param |
275 |
|
@param |
276 |
|
@return |
277 |
|
@deprecated@link |
278 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
279 |
0 |
@Deprecated... |
280 |
|
public AverageRatingApi getAverageRating(Document doc, String method) |
281 |
|
{ |
282 |
0 |
return getAverageRating(doc.getDocumentReference(), method); |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
@param |
289 |
|
@param |
290 |
|
@return |
291 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
292 |
0 |
public AverageRatingApi getAverageRating(DocumentReference document, String method)... |
293 |
|
{ |
294 |
0 |
setError(null); |
295 |
|
|
296 |
0 |
try { |
297 |
0 |
return new AverageRatingApi(this.ratingsManagerProvider.get(document).getAverageRating(document, method)); |
298 |
|
} catch (Throwable e) { |
299 |
0 |
setError(e); |
300 |
0 |
return null; |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
@param |
308 |
|
@return |
309 |
|
@deprecated@link |
310 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
311 |
0 |
@Deprecated... |
312 |
|
public AverageRatingApi getAverageRating(Document doc) |
313 |
|
{ |
314 |
0 |
return getAverageRating(doc.getDocumentReference()); |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
@param |
321 |
|
@return |
322 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
323 |
6 |
public AverageRatingApi getAverageRating(DocumentReference document)... |
324 |
|
{ |
325 |
6 |
setError(null); |
326 |
|
|
327 |
6 |
try { |
328 |
6 |
return new AverageRatingApi(this.ratingsManagerProvider.get(document).getAverageRating(document)); |
329 |
|
} catch (Throwable e) { |
330 |
0 |
setError(e); |
331 |
0 |
return null; |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
|
338 |
|
@param |
339 |
|
@param |
340 |
|
@param |
341 |
|
@return |
342 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
343 |
0 |
public AverageRatingApi getAverageRating(String fromsql, String wheresql, String method)... |
344 |
|
{ |
345 |
0 |
setError(null); |
346 |
|
|
347 |
0 |
try { |
348 |
0 |
return new AverageRatingApi(this.ratingsManagerProvider.get(getGlobalConfig()).getAverageRatingFromQuery( |
349 |
|
fromsql, wheresql, method)); |
350 |
|
} catch (Throwable e) { |
351 |
0 |
setError(e); |
352 |
0 |
return null; |
353 |
|
} |
354 |
|
} |
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
@param |
360 |
|
@param |
361 |
|
@return |
362 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
363 |
0 |
public AverageRatingApi getAverageRating(String fromsql, String wheresql)... |
364 |
|
{ |
365 |
0 |
setError(null); |
366 |
|
|
367 |
0 |
try { |
368 |
0 |
return new AverageRatingApi(this.ratingsManagerProvider.get(getGlobalConfig()).getAverageRatingFromQuery( |
369 |
|
fromsql, wheresql)); |
370 |
|
} catch (Throwable e) { |
371 |
0 |
setError(e); |
372 |
0 |
return null; |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
@param |
380 |
|
@return |
381 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
382 |
0 |
public AverageRatingApi getUserReputation(String username)... |
383 |
|
{ |
384 |
0 |
DocumentReference userRef = this.userReferenceResolver.resolve(username); |
385 |
0 |
return getUserReputation(userRef); |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
@param |
392 |
|
@return |
393 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
394 |
0 |
public AverageRatingApi getUserReputation(DocumentReference username)... |
395 |
|
{ |
396 |
0 |
setError(null); |
397 |
|
|
398 |
0 |
try { |
399 |
0 |
return new AverageRatingApi(this.ratingsManagerProvider.get(getGlobalConfig()).getUserReputation(username)); |
400 |
|
} catch (Throwable e) { |
401 |
0 |
setError(e); |
402 |
0 |
return null; |
403 |
|
} |
404 |
|
} |
405 |
|
|
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
@param |
410 |
|
@return |
411 |
|
@since |
412 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
413 |
33 |
@Unstable... |
414 |
|
public Document getConfigurationDocument(DocumentReference documentReference) |
415 |
|
{ |
416 |
33 |
return ratingsConfiguration.getConfigurationDocument(documentReference).newDocument(getXWikiContext()); |
417 |
|
} |
418 |
|
} |