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.Arrays; |
24 |
|
import java.util.Date; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.model.reference.DocumentReference; |
34 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
35 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
36 |
|
import org.xwiki.ratings.Rating; |
37 |
|
import org.xwiki.ratings.RatingsException; |
38 |
|
import org.xwiki.ratings.RatingsManager; |
39 |
|
import org.xwiki.ratings.UpdateRatingEvent; |
40 |
|
import org.xwiki.ratings.UpdatingRatingEvent; |
41 |
|
|
42 |
|
import com.xpn.xwiki.XWikiException; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
import com.xpn.xwiki.objects.BaseObject; |
45 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
@see |
49 |
|
@see |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
54 |
|
@Named("separate") |
|
|
| 18.8% |
Uncovered Elements: 78 (96) |
Complexity: 25 |
Complexity Density: 0.37 |
|
55 |
|
public class SeparatePageRatingsManager extends AbstractRatingsManager |
56 |
|
{ |
57 |
|
public static final String SEPARATERATINGS_CONFIG_PARAM_PREFIX = "xwiki.ratings.separatepage."; |
58 |
|
|
59 |
|
public static final String SEPARATERATINGS_CONFIG_FIELDNAME_SEPARATEPAGE_SPACE = "space"; |
60 |
|
|
61 |
|
public static final String SEPARATERATINGS_CONFIG_FIELDNAME_SEPARATEPAGE_RATINGS_SPACE_PER_SPACE = |
62 |
|
"ratingsSpacePerSpace"; |
63 |
|
|
64 |
|
@Inject |
65 |
|
private Logger LOGGER; |
66 |
|
|
67 |
|
@Inject |
68 |
|
@Named("user/current") |
69 |
|
protected DocumentReferenceResolver<String> userReferenceResolver; |
70 |
|
|
71 |
|
@Inject |
72 |
|
@Named("compactwiki") |
73 |
|
protected EntityReferenceSerializer<String> entityReferenceSerializer; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
2 |
public SeparatePageRatingsManager()... |
79 |
|
{ |
80 |
2 |
super(); |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@param |
87 |
|
@return |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
89 |
0 |
public String getRatingsSpaceName(DocumentReference documentRef)... |
90 |
|
{ |
91 |
0 |
String ratingsSpaceName = getXWiki().Param("xwiki.ratings.separatepagemanager.spacename", ""); |
92 |
0 |
ratingsSpaceName = |
93 |
|
getXWiki().getXWikiPreference("ratings_separatepagemanager_spacename", ratingsSpaceName, getXWikiContext()); |
94 |
0 |
return getRatingsConfiguration().getConfigurationParameter(documentRef, |
95 |
|
RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_STORAGE_SPACE, ratingsSpaceName); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@param |
102 |
|
@return |
103 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
104 |
0 |
public boolean hasRatingsSpaceForeachSpace(DocumentReference documentRef)... |
105 |
|
{ |
106 |
0 |
String result = getXWiki().Param("xwiki.ratings.separatepagemanager.ratingsspaceforeachspace", "0"); |
107 |
0 |
result = |
108 |
|
getXWiki().getXWikiPreference("ratings_separatepagemanager_ratingsspaceforeachspace", result, |
109 |
|
getXWikiContext()); |
110 |
0 |
return (getRatingsConfiguration().getConfigurationParameter(documentRef, |
111 |
|
RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_STORAGE_SEPARATE_SPACES, result) == "1"); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
@param |
118 |
|
@throws |
119 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
120 |
0 |
protected void saveRating(Rating rating) throws RatingsException... |
121 |
|
{ |
122 |
0 |
try { |
123 |
0 |
rating.save(); |
124 |
|
} catch (XWikiException e) { |
125 |
0 |
throw new RatingsException(e); |
126 |
|
} |
127 |
|
} |
128 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 3 |
Complexity Density: 0.17 |
|
129 |
0 |
@Override... |
130 |
|
public Rating setRating(DocumentReference documentRef, DocumentReference author, int vote) throws RatingsException |
131 |
|
{ |
132 |
0 |
Rating rating = getRating(documentRef, author); |
133 |
0 |
int oldVote; |
134 |
0 |
if (rating == null) { |
135 |
0 |
oldVote = 0; |
136 |
0 |
rating = new SeparatePageRating(documentRef, author, vote, getXWikiContext(), this); |
137 |
|
} else { |
138 |
0 |
oldVote = rating.getVote(); |
139 |
0 |
rating.setVote(vote); |
140 |
0 |
rating.setDate(new Date()); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
0 |
this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null); |
145 |
|
|
146 |
0 |
boolean updateFailed = true; |
147 |
0 |
try { |
148 |
|
|
149 |
0 |
rating.save(); |
150 |
|
|
151 |
|
|
152 |
0 |
updateAverageRatings(documentRef, rating, oldVote); |
153 |
|
|
154 |
0 |
updateFailed = false; |
155 |
|
} finally { |
156 |
0 |
if (updateFailed) { |
157 |
|
|
158 |
0 |
this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null); |
159 |
|
} else { |
160 |
|
|
161 |
0 |
this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
0 |
return rating; |
166 |
|
} |
167 |
|
|
|
|
| 73.3% |
Uncovered Elements: 4 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
168 |
74 |
@Override... |
169 |
|
public List<Rating> getRatings(DocumentReference documentRef, int start, int count, boolean asc) |
170 |
|
throws RatingsException |
171 |
|
{ |
172 |
74 |
if (LOGGER.isDebugEnabled()) { |
173 |
0 |
LOGGER.debug("Calling separate page manager code for ratings"); |
174 |
|
} |
175 |
|
|
176 |
74 |
String sql = ", BaseObject as obj, StringProperty as parentprop where doc.fullName=obj.name and obj.className=?" |
177 |
|
+ " and obj.id=parentprop.id.id and parentprop.id.name=?" + " and parentprop.value=?" |
178 |
|
+ " and obj.name not in (select obj2.name from BaseObject as obj2, StringProperty as statusprop where obj2.className=?" |
179 |
|
+ " and obj2.id=statusprop.id.id and statusprop.id.name=? and (statusprop.value=? or statusprop.value= ?) and obj.id=obj2.id) order by doc.date " |
180 |
74 |
+ (asc ? "asc" : "desc"); |
181 |
|
|
182 |
74 |
List<?> params = new ArrayList<String>(Arrays.asList(getRatingsClassName(), RATING_CLASS_FIELDNAME_PARENT, |
183 |
|
entityReferenceSerializer.serialize(documentRef), getRatingsClassName(), "status", "moderated", "refused")); |
184 |
|
|
185 |
74 |
List<Rating> ratings = new ArrayList<Rating>(); |
186 |
74 |
try { |
187 |
74 |
List<DocumentReference> ratingPageReferenceList = |
188 |
|
getXWikiContext().getWiki().getStore().searchDocumentReferences(sql, params, getXWikiContext()); |
189 |
|
|
190 |
74 |
for (DocumentReference ratingPageReference : ratingPageReferenceList) { |
191 |
0 |
ratings.add(getRatingFromDocument(documentRef, |
192 |
|
getXWiki().getDocument(ratingPageReference, getXWikiContext()))); |
193 |
|
} |
194 |
|
} catch (XWikiException e) { |
195 |
0 |
throw new RatingsException(e); |
196 |
|
} |
197 |
|
|
198 |
74 |
return ratings; |
199 |
|
} |
200 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.5 |
|
201 |
0 |
@Override... |
202 |
|
public Rating getRating(DocumentReference documentRef, int id) throws RatingsException |
203 |
|
{ |
204 |
0 |
String sql = ", BaseObject as obj, StringProperty as parentprop where doc.fullName=obj.name and obj.className=?" |
205 |
|
+ " and obj.id=parentprop.id.id and parentprop.id.name=?" + " and parentprop.value=?" |
206 |
|
+ " and obj.name not in (select obj2.name from BaseObject as obj2, StringProperty as statusprop where obj2.className=?" |
207 |
|
+ " and obj2.id=statusprop.id.id and statusprop.id.name=? and (statusprop.value=? or statusprop.value=?) and obj.id=obj2.id) order by doc.date desc"; |
208 |
|
|
209 |
0 |
List<?> params = new ArrayList<String>(Arrays.asList(getRatingsClassName(), RATING_CLASS_FIELDNAME_PARENT, |
210 |
|
entityReferenceSerializer.serialize(documentRef), getRatingsClassName(), "status", "moderated", "refused")); |
211 |
|
|
212 |
0 |
try { |
213 |
0 |
List<DocumentReference> ratingPageReferenceList = |
214 |
|
getXWikiContext().getWiki().getStore().searchDocumentReferences(sql, 1, id, params, getXWikiContext()); |
215 |
0 |
if ((ratingPageReferenceList == null) || (ratingPageReferenceList.size() == 0)) { |
216 |
0 |
return null; |
217 |
|
} else { |
218 |
0 |
return new SeparatePageRatingsManager().getRatingFromDocument(documentRef, |
219 |
|
getXWiki().getDocument(ratingPageReferenceList.get(0), getXWikiContext())); |
220 |
|
} |
221 |
|
} catch (XWikiException e) { |
222 |
0 |
throw new RatingsException(e); |
223 |
|
} |
224 |
|
} |
225 |
|
|
|
|
| 37.5% |
Uncovered Elements: 5 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
226 |
6 |
@Override... |
227 |
|
public Rating getRating(DocumentReference documentRef, DocumentReference author) throws RatingsException |
228 |
|
{ |
229 |
6 |
try { |
230 |
6 |
for (Rating rating : getRatings(documentRef, 0, 0, false)) { |
231 |
0 |
if (author.equals(rating.getAuthor())) { |
232 |
0 |
return rating; |
233 |
|
} |
234 |
|
} |
235 |
|
} catch (XWikiException e) { |
236 |
0 |
return null; |
237 |
|
} |
238 |
6 |
return null; |
239 |
|
} |
240 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 5 |
Complexity Density: 0.36 |
|
241 |
0 |
@Override... |
242 |
|
public Rating getRating(String ratingId) throws RatingsException |
243 |
|
{ |
244 |
0 |
try { |
245 |
0 |
int i1 = ratingId.indexOf("."); |
246 |
0 |
if (i1 == -1) { |
247 |
0 |
throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, |
248 |
|
RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, cannot parse rating id"); |
249 |
|
} |
250 |
|
|
251 |
0 |
XWikiDocument doc = getXWiki().getDocument(ratingId, getXWikiContext()); |
252 |
0 |
if (doc.isNew()) { |
253 |
0 |
throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, |
254 |
|
RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, rating does not exist"); |
255 |
|
} |
256 |
|
|
257 |
0 |
BaseObject object = doc.getObject(getRatingsClassName()); |
258 |
0 |
if (object == null) { |
259 |
0 |
throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, |
260 |
|
RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, rating does not exist"); |
261 |
|
} |
262 |
|
|
263 |
0 |
String parentDocName = object.getStringValue(RATING_CLASS_FIELDNAME_PARENT); |
264 |
0 |
XWikiDocument parentDoc = getXWikiContext().getWiki().getDocument(parentDocName, getXWikiContext()); |
265 |
|
|
266 |
0 |
return new SeparatePageRating(parentDoc.getDocumentReference(), doc, getXWikiContext(), this); |
267 |
|
} catch (XWikiException e) { |
268 |
0 |
throw new RatingsException(e); |
269 |
|
} |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
@param |
276 |
|
@param |
277 |
|
@return |
278 |
|
@throws |
279 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
280 |
0 |
public Rating getRatingFromDocument(DocumentReference documentRef, XWikiDocument doc) throws RatingsException... |
281 |
|
{ |
282 |
0 |
return new SeparatePageRating(documentRef, doc, getXWikiContext(), this); |
283 |
|
} |
284 |
|
} |