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

File DefaultReputationAlgorithm.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

20
39
9
1
201
130
25
0.64
4.33
9
2.78

Classes

Class Line # Actions
DefaultReputationAlgorithm 53 39 0% 25 68
0.00%
 

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.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    * Default very simple reputation algorithm. It won't include recalculation put only flow level reputation
46    *
47    * @version $Id: 56934876cfd665352d3bf1f45cd3a54fa20d846a $
48    * @see ReputationAlgorithm
49    * @since 6.4M3
50    */
51    @Component
52    @Singleton
 
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    * Retrieves the XWiki context from the current execution context.
66    *
67    * @return the XWiki context.
68    * @throws RuntimeException if there was an error retrieving the context.
69    */
 
70  0 toggle protected XWikiContext getXWikiContext()
71    {
72  0 return (XWikiContext) execution.getContext().getProperty("xwikicontext");
73    }
74   
75    /**
76    * Retrieves the XWiki private API object.
77    *
78    * @return the XWiki private API object.
79    */
 
80  0 toggle protected XWiki getXWiki()
81    {
82  0 return getXWikiContext().getWiki();
83    }
84   
 
85  0 toggle @Override
86    public RatingsManager getRatingsManager(DocumentReference documentRef)
87    {
88  0 return ratingsManagerProvider.get(documentRef);
89    }
90   
 
91  0 toggle @Override
92    public void updateReputation(DocumentReference documentRef, Rating rating, int oldVote)
93    {
94    // we only update if we are in stored mode and if the vote changed
95  0 if (oldVote != rating.getVote()) {
96    // voter reputation. This will give points to the voter
97  0 try {
98  0 AverageRating voterRating = calcNewVoterReputation(rating.getAuthor(), documentRef, rating, oldVote);
99    // we need to save this reputation if it has changed
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    // we should log this error
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    // author reputation. This will be giving points to the creator of a document or comment
118  0 try {
119  0 XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext());
120  0 AverageRating authorRating =
121    calcNewContributorReputation(doc.getCreatorReference(), documentRef, rating, oldVote);
122    // we need to save the author reputation
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    // we should log this error
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    // all authors reputation. This will be used to give points to all participants to a document
146  0 try {
147  0 Map<String, AverageRating> authorsRatings = calcNewAuthorsReputation(documentRef, rating, oldVote);
148    // TODO this is not implemented yet
149    } catch (ReputationException e) {
150  0 if (e.getCode() != ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED) {
151    // we should log this error
152  0 if (logger.isErrorEnabled()) {
153  0 logger.error("Error while calculating authors reputation for document " + documentRef, e);
154    }
155    }
156    }
157    }
158    }
159   
 
160  0 toggle @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   
 
168  0 toggle @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   
 
176  0 toggle @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   
 
184  0 toggle @Override
185    public Map<String, AverageRating> recalcAllReputation() throws ReputationException
186    {
187  0 notimplemented();
188  0 return null;
189    }
190   
191    /**
192    * Marks methods that have not been implemented.
193    *
194    * @throws ReputationException when the method is called
195    */
 
196  0 toggle 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    }