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

File DefaultRatingsManager.java

 

Coverage histogram

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

Code metrics

32
80
6
1
235
175
29
0.36
13.33
6
4.83

Classes

Class Line # Actions
DefaultRatingsManager 52 80 0% 29 118
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.ArrayList;
23    import java.util.Date;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.DocumentReferenceResolver;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35    import org.xwiki.ratings.Rating;
36    import org.xwiki.ratings.RatingsException;
37    import org.xwiki.ratings.UpdateRatingEvent;
38    import org.xwiki.ratings.UpdatingRatingEvent;
39   
40    import com.xpn.xwiki.XWikiException;
41    import com.xpn.xwiki.doc.XWikiDocument;
42    import com.xpn.xwiki.objects.BaseObject;
43   
44    /**
45    * The default ratings manager.
46    *
47    * @version $Id: 5ff97cea140a37dd12521ecdd9ada5683bd31048 $
48    * @since 6.4M3
49    */
50    @Component
51    @Singleton
 
52    public class DefaultRatingsManager extends AbstractRatingsManager
53    {
54    @Inject
55    @Named("user/current")
56    protected DocumentReferenceResolver<String> userReferenceResolver;
57   
58    @Inject
59    @Named("compactwiki")
60    protected EntityReferenceSerializer<String> entityReferenceSerializer;
61   
62    @Inject
63    private Logger logger;
64   
 
65  0 toggle @Override
66    public Rating setRating(DocumentReference documentRef, DocumentReference author, int vote) throws RatingsException
67    {
68  0 Rating rating = getRating(documentRef, author);
69  0 int oldVote;
70  0 if (rating == null) {
71  0 oldVote = 0;
72  0 try {
73  0 rating = new DefaultRating(documentRef, author, vote, getXWikiContext(), this);
74    } catch (XWikiException e) {
75  0 throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, e.getCode(),
76    "Failed to create new rating object", e);
77    }
78    } else {
79  0 oldVote = rating.getVote();
80  0 rating.setVote(vote);
81  0 rating.setDate(new Date());
82    }
83   
84    // Indicate that we start modifying the rating
85  0 this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
86   
87  0 boolean updateFailed = true;
88  0 try {
89    // update rating
90  0 rating.save();
91   
92    // update average rating count
93  0 updateAverageRatings(documentRef, rating, oldVote);
94   
95  0 updateFailed = false;
96    } finally {
97  0 if (updateFailed) {
98    // Indicate that the we start modifying the rating
99  0 this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
100    } else {
101    // Indicate that we finished updating the rating
102  0 this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null);
103    }
104    }
105   
106  0 return rating;
107    }
108   
 
109  0 toggle @Override
110    public List<Rating> getRatings(DocumentReference documentRef, int start, int count, boolean asc)
111    throws RatingsException
112    {
113  0 if (logger.isDebugEnabled()) {
114  0 logger.debug("Calling default manager code for ratings");
115    }
116  0 try {
117  0 int skipped = 0;
118  0 int nb = 0;
119  0 XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext());
120  0 List<BaseObject> bobjects = doc.getObjects(getRatingsClassName());
121  0 if (bobjects != null) {
122  0 List<Rating> ratings = new ArrayList<Rating>();
123  0 for (BaseObject bobj : bobjects) {
124  0 if (bobj != null) {
125  0 if (skipped < start) {
126  0 skipped++;
127    } else {
128  0 ratings.add(getDefaultRating(documentRef, bobj));
129  0 nb++;
130    }
131  0 if ((count != 0) && (nb == count)) {
132  0 break;
133    }
134    }
135    }
136  0 return ratings;
137    }
138    } catch (XWikiException e) {
139  0 e.printStackTrace();
140    }
141  0 return null;
142    }
143   
 
144  0 toggle @Override
145    public Rating getRating(String ratingId) throws RatingsException
146    {
147  0 try {
148  0 int i1 = ratingId.indexOf(":");
149  0 if (i1 == -1) {
150  0 throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS,
151    RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, cannot parse rating id");
152    }
153   
154  0 String docName = ratingId.substring(0, i1);
155  0 String sObjectNb = ratingId.substring(i1 + 1);
156  0 int objectNb = Integer.parseInt(sObjectNb);
157  0 XWikiDocument doc = getXWiki().getDocument(docName, getXWikiContext());
158  0 DocumentReference docRef = doc.getDocumentReference();
159   
160  0 if (doc.isNew()) {
161  0 throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS,
162    RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, document does not exist");
163    }
164   
165  0 BaseObject object = doc.getObject(getRatingsClassName(), objectNb);
166   
167  0 if (object == null) {
168  0 throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS,
169    RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, could not find rating");
170    }
171   
172  0 return new DefaultRating(docRef, object, getXWikiContext(), this);
173    } catch (XWikiException e) {
174  0 throw new RatingsException(e);
175    }
176    }
177   
 
178  0 toggle @Override
179    public Rating getRating(DocumentReference documentRef, int id) throws RatingsException
180    {
181  0 try {
182  0 int skipped = 0;
183  0 XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext());
184  0 List<BaseObject> bobjects = doc.getObjects(getRatingsClassName());
185  0 if (bobjects != null) {
186  0 for (BaseObject bobj : bobjects) {
187  0 if (bobj != null) {
188  0 if (skipped < id) {
189  0 skipped++;
190    } else {
191  0 return getDefaultRating(documentRef, bobj);
192    }
193    }
194    }
195    }
196    } catch (XWikiException e) {
197  0 throw new RatingsException(e);
198    }
199  0 return null;
200    }
201   
 
202  0 toggle @Override
203    public Rating getRating(DocumentReference documentRef, DocumentReference author) throws RatingsException
204    {
205  0 try {
206  0 if (author == null) {
207  0 return null;
208    }
209  0 List<Rating> ratings = getRatings(documentRef, 0, 0, false);
210  0 if (ratings == null) {
211  0 return null;
212    }
213  0 for (Rating rating : ratings) {
214  0 if (rating != null && author.equals(rating.getAuthor())) {
215  0 return rating;
216    }
217    }
218    } catch (XWikiException e) {
219  0 return null;
220    }
221  0 return null;
222    }
223   
224    /**
225    * Gets a DefaultRating.
226    *
227    * @param documentRef the document to which the rating is linked
228    * @param bobj the rating object containing the rating information
229    * @return an instance of DefaultRating containing the passed rating information
230    */
 
231  0 toggle private DefaultRating getDefaultRating(DocumentReference documentRef, BaseObject bobj)
232    {
233  0 return new DefaultRating(documentRef, bobj, getXWikiContext(), this);
234    }
235    }