1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
45 |
|
@see |
46 |
|
@since |
47 |
|
|
|
|
| 29.2% |
Uncovered Elements: 97 (137) |
Complexity: 39 |
Complexity Density: 0.42 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
62 |
148 |
@Override... |
63 |
|
public String getRatingsClassName() |
64 |
|
{ |
65 |
148 |
return RATINGS_CLASSNAME; |
66 |
|
} |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@return |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
68 |
public String getAverageRatingsClassName()... |
74 |
|
{ |
75 |
68 |
return AVERAGE_RATINGS_CLASSNAME; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@return |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0 |
protected RatingsConfiguration getRatingsConfiguration()... |
84 |
|
{ |
85 |
0 |
return ratingsConfiguration; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@return |
92 |
|
@throws |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
488 |
protected XWikiContext getXWikiContext()... |
95 |
|
{ |
96 |
488 |
return this.xcontextProvider.get(); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@return |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
104 |
136 |
protected XWiki getXWiki()... |
105 |
|
{ |
106 |
136 |
return getXWikiContext().getWiki(); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
@return |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
114 |
0 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
120 |
68 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
129 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
138 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
147 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
157 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
0 |
@Override... |
167 |
|
public AverageRating getAverageRatingFromQuery(String fromsql, String wheresql) throws RatingsException |
168 |
|
{ |
169 |
0 |
return getAverageRatingFromQuery(fromsql, wheresql, RATING_REPUTATION_METHOD_AVERAGE); |
170 |
|
} |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
68 |
@Override... |
173 |
|
public AverageRating getAverageRating(DocumentReference documentRef) throws RatingsException |
174 |
|
{ |
175 |
68 |
return getAverageRating(documentRef, RATING_REPUTATION_METHOD_AVERAGE); |
176 |
|
} |
177 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
178 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0 |
@Override... |
212 |
|
public boolean removeRating(Rating rating) throws RatingsException |
213 |
|
{ |
214 |
0 |
return rating.remove(); |
215 |
|
} |
216 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
217 |
0 |
@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 |
|
|
|
|
| 33.3% |
Uncovered Elements: 22 (33) |
Complexity: 7 |
Complexity Density: 0.3 |
|
227 |
68 |
@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 |
|
|
242 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
266 |
0 |
@Override... |
267 |
|
public void updateAverageRating(DocumentReference documentRef, Rating rating, int oldVote, String method) |
268 |
|
throws RatingsException |
269 |
|
{ |
270 |
|
|
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 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
} |
285 |
|
} |
286 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
287 |
0 |
@Override... |
288 |
|
public void updateUserReputation(DocumentReference author, AverageRating voterRating) throws RatingsException |
289 |
|
{ |
290 |
0 |
try { |
291 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
302 |
68 |
@Override... |
303 |
|
public AverageRating getAverageRating(DocumentReference documentRef, String method) throws RatingsException |
304 |
|
{ |
305 |
68 |
return getAverageRating(documentRef, method, false); |
306 |
|
} |
307 |
|
|
|
|
| 57.9% |
Uncovered Elements: 8 (19) |
Complexity: 5 |
Complexity Density: 0.38 |
|
308 |
68 |
@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 |
|
|
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 |
|
} |