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

File RatingsScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

4
66
21
1
418
198
31
0.47
3.14
21
1.48

Classes

Class Line # Actions
RatingsScriptService 54 66 0% 31 74
0.1868131918.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.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    * Script service offering access to the ratings API.
47    *
48    * @version $Id: aa6e630dbdc1d2365132fff193a8467790a1a9e6 $
49    * @since 6.4M3
50    */
51    @Component
52    @Singleton
53    @Named("ratings")
 
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    * Retrieve the XWiki context from the current execution context.
79    *
80    * @return the XWiki context
81    * @throws RuntimeException If there was an error retrieving the context
82    */
 
83  45 toggle private XWikiContext getXWikiContext()
84    {
85  45 return this.xcontextProvider.get();
86    }
87   
88    /**
89    * Store a caught exception in the context.
90    *
91    * @param e the exception to store, can be null to clear the previously stored exception
92    */
 
93  12 toggle private void setError(Throwable e)
94    {
95  12 getXWikiContext().put("exception", e);
96    }
97   
98    /**
99    * Retrieve the global configuration document.
100    *
101    * @return an absolute reference to the global configuration document
102    */
 
103  0 toggle private DocumentReference getGlobalConfig()
104    {
105  0 return this.referenceDocumentReferenceResolver.resolve(RatingsManager.RATINGS_CONFIG_GLOBAL_REFERENCE);
106    }
107   
108    /**
109    * Wrap rating objects.
110    *
111    * @param ratings a list of rating object
112    * @return list of object wrapped with the RatingAPI
113    */
 
114  0 toggle 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    * Set a new rating.
130    *
131    * @param doc the document which is being rated
132    * @param author the author giving the rating
133    * @param vote the number of stars given (from 1 to 5)
134    * @return the new rating object
135    * @deprecated use {@link #setRating(DocumentReference, DocumentReference, int)} instead
136    */
 
137  0 toggle @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    * Set a new rating.
147    *
148    * @param document the document which is being rated
149    * @param author the author giving the rating
150    * @param vote the number of stars given (from 1 to 5)
151    * @return the new rating object
152    */
 
153  0 toggle public RatingApi setRating(DocumentReference document, DocumentReference author, int vote)
154    {
155    // TODO protect this with programming rights
156    // and add a setRating(docName), not protected but for which the author is retrieved from getXWikiContext().
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    * Retrieve a specific rating.
169    *
170    * @param doc the document to which the rating belongs to
171    * @param author the user that gave the rating
172    * @return a rating object
173    * @deprecated use {@link #getRating(DocumentReference, DocumentReference)} instead
174    */
 
175  0 toggle @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    * Retrieve a specific rating.
185    *
186    * @param document the document to which the rating belongs to
187    * @param author the user that gave the rating
188    * @return a rating object
189    */
 
190  6 toggle 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    * Get a list of ratings.
208    *
209    * @param doc the document to which the ratings belong to
210    * @param start the offset from which to start
211    * @param count number of ratings to return
212    * @return a list of rating objects
213    * @deprecated use {@link #getRatings(DocumentReference, int, int)} instead
214    */
 
215  0 toggle @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    * Get a list of ratings.
223    *
224    * @param document the document to which the ratings belong to
225    * @param start the offset from which to start
226    * @param count number of ratings to return
227    * @return a list of rating objects
228    */
 
229  0 toggle public List<RatingApi> getRatings(DocumentReference document, int start, int count)
230    {
231  0 return getRatings(document, start, count, true);
232    }
233   
234    /**
235    * Get a sorted list of ratings.
236    *
237    * @param doc the document to which the ratings belong to
238    * @param start the offset from which to start
239    * @param count number of ratings to return
240    * @param asc in ascending order or not
241    * @return a list of rating objects
242    * @deprecated use {@link #getRatings(DocumentReference, int, int, boolean)} instead
243    */
 
244  0 toggle @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    * Get a sorted list of ratings.
252    *
253    * @param document the document to which the ratings belong to
254    * @param start the offset from which to start
255    * @param count number of ratings to return
256    * @param asc in ascending order or not
257    * @return a list of rating objects
258    */
 
259  0 toggle 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    * Get average rating.
273    *
274    * @param doc the document to which the average rating belongs to
275    * @param method the method of calculating the average
276    * @return a average rating API object
277    * @deprecated use {@link #getAverageRating(DocumentReference, String)} instead
278    */
 
279  0 toggle @Deprecated
280    public AverageRatingApi getAverageRating(Document doc, String method)
281    {
282  0 return getAverageRating(doc.getDocumentReference(), method);
283    }
284   
285    /**
286    * Get average rating.
287    *
288    * @param document the document to which the average rating belongs to
289    * @param method the method of calculating the average
290    * @return a average rating API object
291    */
 
292  0 toggle 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    * Get average rating.
306    *
307    * @param doc the document to which the average rating belongs to
308    * @return a average rating API object
309    * @deprecated use {@link #getAverageRating(DocumentReference)} instead
310    */
 
311  0 toggle @Deprecated
312    public AverageRatingApi getAverageRating(Document doc)
313    {
314  0 return getAverageRating(doc.getDocumentReference());
315    }
316   
317    /**
318    * Get average rating.
319    *
320    * @param document the document to which the average rating belongs to
321    * @return a average rating API object
322    */
 
323  6 toggle 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    * Get average rating from query.
337    *
338    * @param fromsql the from clause of the query
339    * @param wheresql the where clause of the query
340    * @param method the method of calculating the average
341    * @return a average rating API object
342    */
 
343  0 toggle 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    * Get average rating from query.
358    *
359    * @param fromsql the from clause of the query
360    * @param wheresql the where clause of the query
361    * @return a average rating API object
362    */
 
363  0 toggle 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    * Get a user's reputation.
378    *
379    * @param username the user document
380    * @return a average rating API object
381    */
 
382  0 toggle public AverageRatingApi getUserReputation(String username)
383    {
384  0 DocumentReference userRef = this.userReferenceResolver.resolve(username);
385  0 return getUserReputation(userRef);
386    }
387   
388    /**
389    * Get a user's reputation.
390    *
391    * @param username the user document
392    * @return a average rating API object
393    */
 
394  0 toggle 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    * Get configuration document.
408    *
409    * @param documentReference the documentReference for which to return the configuration document
410    * @return the configuration document
411    * @since 8.2.1
412    */
 
413  33 toggle @Unstable
414    public Document getConfigurationDocument(DocumentReference documentReference)
415    {
416  33 return ratingsConfiguration.getConfigurationDocument(documentReference).newDocument(getXWikiContext());
417    }
418    }