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

File AbstractRatingsManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

24
92
21
1
338
241
39
0.42
4.38
21
1.86

Classes

Class Line # Actions
AbstractRatingsManager 48 92 0% 39 97
0.291970829.2%
 

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.internal;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Provider;
26   
27    import org.slf4j.Logger;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.observation.ObservationManager;
30    import org.xwiki.ratings.AverageRating;
31    import org.xwiki.ratings.Rating;
32    import org.xwiki.ratings.RatingsConfiguration;
33    import org.xwiki.ratings.RatingsException;
34    import org.xwiki.ratings.RatingsManager;
35    import org.xwiki.ratings.ReputationException;
36   
37    import com.xpn.xwiki.XWiki;
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.XWikiException;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.objects.BaseObject;
42   
43    /**
44    * @version $Id: 2878b55a1406b5dabd637f9f05a725713f50c4c7 $
45    * @see RatingsManager
46    * @since 6.4M3
47    */
 
48    public abstract class AbstractRatingsManager implements RatingsManager
49    {
50    @Inject
51    protected ObservationManager observationManager;
52   
53    @Inject
54    private Logger logger;
55   
56    @Inject
57    private Provider<XWikiContext> xcontextProvider;
58   
59    @Inject
60    private RatingsConfiguration ratingsConfiguration;
61   
 
62  148 toggle @Override
63    public String getRatingsClassName()
64    {
65  148 return RATINGS_CLASSNAME;
66    }
67   
68    /**
69    * Gets the average ratings class.
70    *
71    * @return the XWiki document representing the AverageRatingsClass.
72    */
 
73  68 toggle public String getAverageRatingsClassName()
74    {
75  68 return AVERAGE_RATINGS_CLASSNAME;
76    }
77   
78    /**
79    * Gets the ratings configuration component.
80    *
81    * @return the ratingsConfiguration representing the RatingsConfiguration component.
82    */
 
83  0 toggle protected RatingsConfiguration getRatingsConfiguration()
84    {
85  0 return ratingsConfiguration;
86    }
87   
88    /**
89    * Retrieves the XWiki context from the current execution context.
90    *
91    * @return the XWiki context
92    * @throws RuntimeException if there was an error retrieving the context
93    */
 
94  488 toggle protected XWikiContext getXWikiContext()
95    {
96  488 return this.xcontextProvider.get();
97    }
98   
99    /**
100    * Retrieves the XWiki private API object.
101    *
102    * @return The XWiki private API object
103    */
 
104  136 toggle protected XWiki getXWiki()
105    {
106  136 return getXWikiContext().getWiki();
107    }
108   
109    /**
110    * Checks if ratings are active.
111    *
112    * @return answer to: are ratings active?
113    */
 
114  0 toggle public boolean hasRatings()
115    {
116  0 int result = (int) getXWiki().ParamAsLong("xwiki.ratings", 0);
117  0 return (getXWiki().getXWikiPreferenceAsInt("ratings", result, getXWikiContext()) == 1);
118    }
119   
 
120  68 toggle @Override
121    public boolean isAverageRatingStored(DocumentReference documentRef)
122    {
123  68 String result = getXWiki().Param("xwiki.ratings.averagerating.stored", "1");
124  68 result = getXWiki().getXWikiPreference("ratings_averagerating_stored", result, getXWikiContext());
125  68 return (ratingsConfiguration.getConfigurationParameter(documentRef,
126    RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_STORE_AVERAGE_RATING, result).equals("1"));
127    }
128   
 
129  0 toggle @Override
130    public boolean isReputationStored(DocumentReference documentRef)
131    {
132  0 String result = getXWiki().Param("xwiki.ratings.reputation.stored", "0");
133  0 result = getXWiki().getXWikiPreference("ratings_reputation_stored", result, getXWikiContext());
134  0 return (ratingsConfiguration.getConfigurationParameter(documentRef,
135    RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_STORED, result).equals("1"));
136    }
137   
 
138  0 toggle @Override
139    public boolean hasReputation(DocumentReference documentRef)
140    {
141  0 String result = getXWiki().Param("xwiki.ratings.reputation", "0");
142  0 result = getXWiki().getXWikiPreference("ratings_reputation", result, getXWikiContext());
143  0 return (ratingsConfiguration.getConfigurationParameter(documentRef,
144    RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION, result).equals("1"));
145    }
146   
 
147  0 toggle @Override
148    public String[] getDefaultReputationMethods(DocumentReference documentRef)
149    {
150  0 String method = getXWiki().Param("xwiki.ratings.reputation.defaultmethod", RATING_REPUTATION_METHOD_DEFAULT);
151  0 method = getXWiki().getXWikiPreference("ratings_reputation_defaultmethod", method, getXWikiContext());
152  0 method = ratingsConfiguration.getConfigurationParameter(documentRef,
153    RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_METHOD, method);
154  0 return method.split(",");
155    }
156   
 
157  0 toggle @Override
158    public void updateAverageRatings(DocumentReference documentRef, Rating rating, int oldVote) throws RatingsException
159    {
160  0 String[] methods = getDefaultReputationMethods(documentRef);
161  0 for (int i = 0; i < methods.length; i++) {
162  0 updateAverageRating(documentRef, rating, oldVote, methods[i]);
163    }
164    }
165   
 
166  0 toggle @Override
167    public AverageRating getAverageRatingFromQuery(String fromsql, String wheresql) throws RatingsException
168    {
169  0 return getAverageRatingFromQuery(fromsql, wheresql, RATING_REPUTATION_METHOD_AVERAGE);
170    }
171   
 
172  68 toggle @Override
173    public AverageRating getAverageRating(DocumentReference documentRef) throws RatingsException
174    {
175  68 return getAverageRating(documentRef, RATING_REPUTATION_METHOD_AVERAGE);
176    }
177   
 
178  0 toggle @Override
179    public AverageRating getAverageRatingFromQuery(String fromsql, String wheresql, String method)
180    throws RatingsException
181    {
182  0 try {
183  0 String fromsql2 =
184    fromsql + ", BaseObject as avgobj, FloatProperty as avgvote, StringProperty as avgmethod ";
185  0 String wheresql2 =
186  0 (wheresql.equals("") ? "where " : wheresql + " and ")
187    + "doc.fullName=avgobj.name and avgobj.className='" + getAverageRatingsClassName()
188    + "' and avgobj.id=avgvote.id.id and avgvote.id.name='" + AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE
189    + "' and avgobj.id=avgmethod.id.id and avgmethod.id.name='"
190    + AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE_METHOD + "' and avgmethod.value='" + method + "'";
191  0 String sql =
192    "select sum(avgvote.value) as vote, count(avgvote.value) as nbvotes from XWikiDocument as doc "
193    + fromsql2 + wheresql2;
194   
195  0 if (logger.isDebugEnabled()) {
196  0 logger.debug("Running average rating with sql " + sql);
197    }
198  0 getXWikiContext().put("lastsql", sql);
199   
200  0 List result = getXWiki().getStore().search(sql, 0, 0, getXWikiContext());
201  0 float vote = ((Number) ((Object[]) result.get(0))[0]).floatValue();
202  0 int nbvotes = ((Number) ((Object[]) result.get(0))[1]).intValue();
203   
204  0 AverageRating avgr = new MemoryAverageRating(null, nbvotes, vote / nbvotes, method);
205  0 return avgr;
206    } catch (XWikiException e) {
207  0 throw new RatingsException(e);
208    }
209    }
210   
 
211  0 toggle @Override
212    public boolean removeRating(Rating rating) throws RatingsException
213    {
214  0 return rating.remove();
215    }
216   
 
217  0 toggle @Override
218    public AverageRating getUserReputation(DocumentReference username) throws ReputationException
219    {
220  0 try {
221  0 return getAverageRating(username, RatingsManager.RATING_REPUTATION_METHOD_AVERAGE);
222    } catch (RatingsException e) {
223  0 throw new ReputationException(e);
224    }
225    }
226   
 
227  68 toggle @Override
228    public AverageRating calcAverageRating(DocumentReference documentRef, String method) throws RatingsException
229    {
230  68 int nbVotes = 0;
231  68 int balancedNbVotes = 0;
232  68 float totalVote = 0;
233  68 float averageVote = 0;
234  68 List<Rating> ratings = getRatings(documentRef, 0, 0, true);
235  68 if (ratings == null) {
236  0 return null;
237    }
238  68 for (Rating rating : ratings) {
239  0 if (method.equals(RATING_REPUTATION_METHOD_BALANCED)) {
240  0 DocumentReference author = rating.getAuthor();
241    // in case we are evaluating the average rating of a user
242    // we should not include votes of himself to a user
243  0 if (!author.equals(documentRef)) {
244  0 AverageRating reputation = getUserReputation(author);
245  0 if ((reputation == null) || (reputation.getAverageVote() == 0)) {
246  0 totalVote += rating.getVote();
247  0 balancedNbVotes++;
248    } else {
249  0 totalVote += rating.getVote() * reputation.getAverageVote();
250  0 balancedNbVotes += reputation.getAverageVote();
251    }
252    }
253    } else {
254  0 totalVote += rating.getVote();
255  0 balancedNbVotes++;
256    }
257  0 nbVotes++;
258    }
259   
260  67 if (balancedNbVotes != 0) {
261  0 averageVote = totalVote / balancedNbVotes;
262    }
263  67 return new MemoryAverageRating(documentRef, nbVotes, averageVote, method);
264    }
265   
 
266  0 toggle @Override
267    public void updateAverageRating(DocumentReference documentRef, Rating rating, int oldVote, String method)
268    throws RatingsException
269    {
270    // we only update if we are in stored mode and if the vote changed
271  0 if (isAverageRatingStored(documentRef) && oldVote != rating.getVote()) {
272  0 AverageRating aRating = calcAverageRating(documentRef, method);
273  0 AverageRating averageRating = getAverageRating(documentRef, method, true);
274  0 averageRating.setAverageVote(aRating.getAverageVote());
275  0 averageRating.setNbVotes(aRating.getNbVotes());
276  0 averageRating.save();
277    /*
278    * StoredAverageRating averageRating = (StoredAverageRating) getAverageRating(container, method, true,
279    * context); int diffTotal = rating.getVote() - oldVote; int diffNbVotes = (oldVote==0) ? 1 : 0; int
280    * oldNbVotes = averageRating.getNbVotes(); averageRating.setNbVotes(oldNbVotes + diffNbVotes);
281    * averageRating.setAverageVote((averageRating.getAverageVote()*oldNbVotes + diffTotal) / (oldNbVotes +
282    * diffNbVotes));
283    */
284    }
285    }
286   
 
287  0 toggle @Override
288    public void updateUserReputation(DocumentReference author, AverageRating voterRating) throws RatingsException
289    {
290  0 try {
291    // We should update the user rating
292  0 AverageRating rating = getAverageRating(author, voterRating.getMethod(), true);
293  0 rating.setAverageVote(voterRating.getAverageVote());
294  0 rating.setMethod(voterRating.getMethod());
295  0 rating.setNbVotes(voterRating.getNbVotes());
296  0 rating.save();
297    } catch (XWikiException e) {
298  0 throw new RatingsException(e);
299    }
300    }
301   
 
302  68 toggle @Override
303    public AverageRating getAverageRating(DocumentReference documentRef, String method) throws RatingsException
304    {
305  68 return getAverageRating(documentRef, method, false);
306    }
307   
 
308  68 toggle @Override
309    public AverageRating getAverageRating(DocumentReference documentRef, String method, boolean create)
310    throws RatingsException
311    {
312  68 try {
313  68 if (isAverageRatingStored(documentRef)) {
314  68 String className = getAverageRatingsClassName();
315  68 XWikiDocument doc = getXWikiContext().getWiki().getDocument(documentRef, getXWikiContext());
316  68 BaseObject averageRatingObject =
317    doc.getObject(className, RatingsManager.AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE_METHOD, method,
318    false);
319  68 if (averageRatingObject == null) {
320  68 if (!create) {
321  68 return calcAverageRating(documentRef, method);
322    }
323   
324    // initiate a new average rating object
325  0 averageRatingObject = doc.newObject(className, getXWikiContext());
326  0 averageRatingObject.setStringValue(RatingsManager.AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE_METHOD,
327    method);
328    }
329   
330  0 return new StoredAverageRating(doc, averageRatingObject, getXWikiContext());
331    } else {
332  0 return calcAverageRating(documentRef, method);
333    }
334    } catch (XWikiException e) {
335  0 throw new RatingsException(e);
336    }
337    }
338    }