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.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 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
50 |
|
@Component |
51 |
|
@Singleton |
|
|
| 0% |
Uncovered Elements: 118 (118) |
Complexity: 29 |
Complexity Density: 0.36 |
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0.2 |
|
65 |
0 |
@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 |
|
|
85 |
0 |
this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null); |
86 |
|
|
87 |
0 |
boolean updateFailed = true; |
88 |
0 |
try { |
89 |
|
|
90 |
0 |
rating.save(); |
91 |
|
|
92 |
|
|
93 |
0 |
updateAverageRatings(documentRef, rating, oldVote); |
94 |
|
|
95 |
0 |
updateFailed = false; |
96 |
|
} finally { |
97 |
0 |
if (updateFailed) { |
98 |
|
|
99 |
0 |
this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null); |
100 |
|
} else { |
101 |
|
|
102 |
0 |
this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null); |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
0 |
return rating; |
107 |
|
} |
108 |
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 8 |
Complexity Density: 0.4 |
|
109 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 5 |
Complexity Density: 0.31 |
|
144 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
178 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 6 |
Complexity Density: 0.55 |
|
202 |
0 |
@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 |
|
|
226 |
|
|
227 |
|
@param |
228 |
|
@param |
229 |
|
@return |
230 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
231 |
0 |
private DefaultRating getDefaultRating(DocumentReference documentRef, BaseObject bobj)... |
232 |
|
{ |
233 |
0 |
return new DefaultRating(documentRef, bobj, getXWikiContext(), this); |
234 |
|
} |
235 |
|
} |