| 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.Map; |
| 23 |
|
|
| 24 |
|
import javax.inject.Inject; |
| 25 |
|
import javax.inject.Singleton; |
| 26 |
|
|
| 27 |
|
import org.slf4j.Logger; |
| 28 |
|
import org.xwiki.component.annotation.Component; |
| 29 |
|
import org.xwiki.context.Execution; |
| 30 |
|
import org.xwiki.model.reference.DocumentReference; |
| 31 |
|
import org.xwiki.ratings.AverageRating; |
| 32 |
|
import org.xwiki.ratings.ConfiguredProvider; |
| 33 |
|
import org.xwiki.ratings.Rating; |
| 34 |
|
import org.xwiki.ratings.RatingsException; |
| 35 |
|
import org.xwiki.ratings.RatingsManager; |
| 36 |
|
import org.xwiki.ratings.ReputationAlgorithm; |
| 37 |
|
import org.xwiki.ratings.ReputationException; |
| 38 |
|
|
| 39 |
|
import com.xpn.xwiki.XWiki; |
| 40 |
|
import com.xpn.xwiki.XWikiContext; |
| 41 |
|
import com.xpn.xwiki.XWikiException; |
| 42 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@version |
| 48 |
|
@see |
| 49 |
|
@since |
| 50 |
|
|
| 51 |
|
@Component |
| 52 |
|
@Singleton |
| |
|
| 0% |
Uncovered Elements: 68 (68) |
Complexity: 25 |
Complexity Density: 0.64 |
|
| 53 |
|
public class DefaultReputationAlgorithm implements ReputationAlgorithm |
| 54 |
|
{ |
| 55 |
|
@Inject |
| 56 |
|
private Logger logger; |
| 57 |
|
|
| 58 |
|
@Inject |
| 59 |
|
private Execution execution; |
| 60 |
|
|
| 61 |
|
@Inject |
| 62 |
|
private ConfiguredProvider<RatingsManager> ratingsManagerProvider; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@return |
| 68 |
|
@throws |
| 69 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 70 |
0 |
protected XWikiContext getXWikiContext()... |
| 71 |
|
{ |
| 72 |
0 |
return (XWikiContext) execution.getContext().getProperty("xwikicontext"); |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
@return |
| 79 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
0 |
protected XWiki getXWiki()... |
| 81 |
|
{ |
| 82 |
0 |
return getXWikiContext().getWiki(); |
| 83 |
|
} |
| 84 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 85 |
0 |
@Override... |
| 86 |
|
public RatingsManager getRatingsManager(DocumentReference documentRef) |
| 87 |
|
{ |
| 88 |
0 |
return ratingsManagerProvider.get(documentRef); |
| 89 |
|
} |
| 90 |
|
|
| |
|
| 0% |
Uncovered Elements: 47 (47) |
Complexity: 17 |
Complexity Density: 0.63 |
|
| 91 |
0 |
@Override... |
| 92 |
|
public void updateReputation(DocumentReference documentRef, Rating rating, int oldVote) |
| 93 |
|
{ |
| 94 |
|
|
| 95 |
0 |
if (oldVote != rating.getVote()) { |
| 96 |
|
|
| 97 |
0 |
try { |
| 98 |
0 |
AverageRating voterRating = calcNewVoterReputation(rating.getAuthor(), documentRef, rating, oldVote); |
| 99 |
|
|
| 100 |
0 |
try { |
| 101 |
0 |
getRatingsManager(documentRef).updateUserReputation(rating.getAuthor(), voterRating); |
| 102 |
|
} catch (RatingsException re) { |
| 103 |
0 |
if (logger.isErrorEnabled()) { |
| 104 |
0 |
logger.error("Error while storing reputation for user " + rating.getAuthor(), re); |
| 105 |
|
} |
| 106 |
|
} |
| 107 |
|
} catch (ReputationException e) { |
| 108 |
0 |
if (e.getCode() != ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED) { |
| 109 |
|
|
| 110 |
0 |
if (logger.isErrorEnabled()) { |
| 111 |
0 |
logger.error("Error while calculating voter reputation " + rating.getAuthor() |
| 112 |
|
+ " for document " + documentRef, e); |
| 113 |
|
} |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| 118 |
0 |
try { |
| 119 |
0 |
XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext()); |
| 120 |
0 |
AverageRating authorRating = |
| 121 |
|
calcNewContributorReputation(doc.getCreatorReference(), documentRef, rating, oldVote); |
| 122 |
|
|
| 123 |
0 |
try { |
| 124 |
0 |
getRatingsManager(documentRef).updateUserReputation(doc.getCreatorReference(), authorRating); |
| 125 |
|
} catch (RatingsException re) { |
| 126 |
0 |
if (logger.isErrorEnabled()) { |
| 127 |
0 |
logger.error("Error while storing reputation for user " + doc.getCreatorReference().getName(), |
| 128 |
|
re); |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
} catch (ReputationException e) { |
| 133 |
0 |
if (e.getCode() != ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED) { |
| 134 |
|
|
| 135 |
0 |
if (logger.isErrorEnabled()) { |
| 136 |
0 |
logger.error("Error while calculating author reputation for document " + documentRef, e); |
| 137 |
|
} |
| 138 |
|
} |
| 139 |
|
} catch (XWikiException e) { |
| 140 |
0 |
if (logger.isErrorEnabled()) { |
| 141 |
0 |
logger.error("Error while calculating author reputation for document " + documentRef, e); |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
0 |
try { |
| 147 |
0 |
Map<String, AverageRating> authorsRatings = calcNewAuthorsReputation(documentRef, rating, oldVote); |
| 148 |
|
|
| 149 |
|
} catch (ReputationException e) { |
| 150 |
0 |
if (e.getCode() != ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED) { |
| 151 |
|
|
| 152 |
0 |
if (logger.isErrorEnabled()) { |
| 153 |
0 |
logger.error("Error while calculating authors reputation for document " + documentRef, e); |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 160 |
0 |
@Override... |
| 161 |
|
public AverageRating calcNewVoterReputation(DocumentReference voter, DocumentReference documentRef, Rating rating, |
| 162 |
|
int oldVote) throws ReputationException |
| 163 |
|
{ |
| 164 |
0 |
notimplemented(); |
| 165 |
0 |
return null; |
| 166 |
|
} |
| 167 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 168 |
0 |
@Override... |
| 169 |
|
public AverageRating calcNewContributorReputation(DocumentReference contributor, DocumentReference documentRef, |
| 170 |
|
Rating rating, int oldVote) throws ReputationException |
| 171 |
|
{ |
| 172 |
0 |
notimplemented(); |
| 173 |
0 |
return null; |
| 174 |
|
} |
| 175 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 176 |
0 |
@Override... |
| 177 |
|
public Map<String, AverageRating> calcNewAuthorsReputation(DocumentReference documentRef, Rating rating, int oldVote) |
| 178 |
|
throws ReputationException |
| 179 |
|
{ |
| 180 |
0 |
notimplemented(); |
| 181 |
0 |
return null; |
| 182 |
|
} |
| 183 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 184 |
0 |
@Override... |
| 185 |
|
public Map<String, AverageRating> recalcAllReputation() throws ReputationException |
| 186 |
|
{ |
| 187 |
0 |
notimplemented(); |
| 188 |
0 |
return null; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
@throws |
| 195 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 196 |
0 |
protected void notimplemented() throws ReputationException... |
| 197 |
|
{ |
| 198 |
0 |
throw new ReputationException(ReputationException.MODULE_PLUGIN_RATINGS_REPUTATION, |
| 199 |
|
ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED, "Not implemented"); |
| 200 |
|
} |
| 201 |
|
} |