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.Date; |
23 |
|
|
24 |
|
import org.xwiki.localization.ContextualLocalizationManager; |
25 |
|
import org.xwiki.model.reference.DocumentReference; |
26 |
|
import org.xwiki.model.reference.SpaceReference; |
27 |
|
import org.xwiki.ratings.Rating; |
28 |
|
import org.xwiki.ratings.RatingsException; |
29 |
|
import org.xwiki.ratings.RatingsManager; |
30 |
|
|
31 |
|
import com.xpn.xwiki.XWiki; |
32 |
|
import com.xpn.xwiki.XWikiContext; |
33 |
|
import com.xpn.xwiki.XWikiException; |
34 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
35 |
|
import com.xpn.xwiki.objects.BaseObject; |
36 |
|
import com.xpn.xwiki.objects.BaseProperty; |
37 |
|
import com.xpn.xwiki.user.api.XWikiRightService; |
38 |
|
import com.xpn.xwiki.web.Utils; |
39 |
|
|
40 |
|
|
41 |
|
@version |
42 |
|
@see |
43 |
|
@since |
44 |
|
|
|
|
| 0% |
Uncovered Elements: 137 (137) |
Complexity: 39 |
Complexity Density: 0.42 |
|
45 |
|
public class SeparatePageRating implements Rating |
46 |
|
{ |
47 |
|
private DocumentReference documentRef; |
48 |
|
|
49 |
|
private XWikiDocument document; |
50 |
|
|
51 |
|
private XWikiContext context; |
52 |
|
|
53 |
|
private SeparatePageRatingsManager ratingsManager; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@param |
59 |
|
@param |
60 |
|
@param |
61 |
|
@param |
62 |
|
@param |
63 |
|
@throws |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
0 |
public SeparatePageRating(DocumentReference documentRef, DocumentReference author, int vote, XWikiContext context,... |
66 |
|
SeparatePageRatingsManager ratingsManager) throws RatingsException |
67 |
|
{ |
68 |
0 |
this(documentRef, author, new Date(), vote, context, ratingsManager); |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@param |
75 |
|
@param |
76 |
|
@param |
77 |
|
@param |
78 |
|
@param |
79 |
|
@param |
80 |
|
@throws |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
82 |
0 |
public SeparatePageRating(DocumentReference documentRef, DocumentReference author, Date date, int vote,... |
83 |
|
XWikiContext context, SeparatePageRatingsManager ratingsManager) throws RatingsException |
84 |
|
{ |
85 |
0 |
this.context = context; |
86 |
0 |
this.ratingsManager = ratingsManager; |
87 |
0 |
this.documentRef = documentRef; |
88 |
0 |
this.document = addDocument(documentRef, author, date, vote); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
@param |
95 |
|
@param |
96 |
|
@param |
97 |
|
@param |
98 |
|
@throws |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
100 |
0 |
public SeparatePageRating(DocumentReference documentRef, XWikiDocument doc, XWikiContext context,... |
101 |
|
SeparatePageRatingsManager ratingsManager) throws RatingsException |
102 |
|
{ |
103 |
0 |
this.context = context; |
104 |
0 |
this.ratingsManager = ratingsManager; |
105 |
0 |
this.documentRef = documentRef; |
106 |
0 |
this.document = doc; |
107 |
|
} |
108 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
109 |
0 |
@Override... |
110 |
|
public String getRatingId() |
111 |
|
{ |
112 |
0 |
return getDocument().getFullName(); |
113 |
|
} |
114 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
115 |
0 |
@Override... |
116 |
|
public String getGlobalRatingId() |
117 |
|
{ |
118 |
0 |
return getRatingId(); |
119 |
|
} |
120 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
121 |
0 |
@Override... |
122 |
|
public BaseObject getAsObject() |
123 |
|
{ |
124 |
0 |
return getDocument().getXObject(RatingsManager.RATINGS_CLASSREFERENCE); |
125 |
|
} |
126 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
127 |
0 |
private XWikiDocument getDocument()... |
128 |
|
{ |
129 |
0 |
if (document == null) { |
130 |
0 |
try { |
131 |
0 |
document = context.getWiki().getDocument(getPageReference(this.documentRef), context); |
132 |
|
} catch (XWikiException e) { |
133 |
0 |
return null; |
134 |
|
} |
135 |
|
} |
136 |
0 |
return document; |
137 |
|
} |
138 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
139 |
0 |
@Override... |
140 |
|
public DocumentReference getAuthor() |
141 |
|
{ |
142 |
0 |
String objectVal = getAsObject().getStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR); |
143 |
0 |
return ratingsManager.userReferenceResolver.resolve(objectVal, documentRef); |
144 |
|
} |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
0 |
@Override... |
147 |
|
public Date getDate() |
148 |
|
{ |
149 |
|
|
150 |
0 |
return getAsObject().getDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE); |
151 |
|
} |
152 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
153 |
0 |
@Override... |
154 |
|
public void setAuthor(DocumentReference author) |
155 |
|
{ |
156 |
0 |
getAsObject().setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR, |
157 |
|
ratingsManager.entityReferenceSerializer.serialize(author)); |
158 |
|
} |
159 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
160 |
0 |
@Override... |
161 |
|
public void setDate(Date date) |
162 |
|
{ |
163 |
0 |
getAsObject().setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date); |
164 |
|
} |
165 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
0 |
@Override... |
167 |
|
public int getVote() |
168 |
|
{ |
169 |
0 |
return getAsObject().getIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE); |
170 |
|
} |
171 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
0 |
@Override... |
173 |
|
public void setVote(int vote) |
174 |
|
{ |
175 |
0 |
getAsObject().setIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE, vote); |
176 |
|
} |
177 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
178 |
0 |
@Override... |
179 |
|
public Object get(String propertyName) |
180 |
|
{ |
181 |
0 |
try { |
182 |
0 |
return ((BaseProperty) getAsObject().get(propertyName)).getValue(); |
183 |
|
} catch (XWikiException e) { |
184 |
0 |
return null; |
185 |
|
} |
186 |
|
} |
187 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
188 |
0 |
@Override... |
189 |
|
public String display(String propertyName, String mode) |
190 |
|
{ |
191 |
0 |
return document.display(propertyName, mode, getAsObject(), context); |
192 |
|
} |
193 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
194 |
0 |
@Override... |
195 |
|
public void save() throws RatingsException |
196 |
|
{ |
197 |
0 |
DocumentReference superadmin = |
198 |
|
new DocumentReference("xwiki", XWiki.SYSTEM_SPACE, XWikiRightService.SUPERADMIN_USER); |
199 |
|
|
200 |
0 |
try { |
201 |
0 |
if (document == null) { |
202 |
0 |
throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, |
203 |
|
RatingsException.ERROR_RATINGS_SAVERATING_NULLDOCUMENT, |
204 |
|
"Cannot save invalid separate page rating, the rating document is null"); |
205 |
|
} |
206 |
|
|
207 |
0 |
document.setCreatorReference(superadmin); |
208 |
0 |
document.setAuthorReference(superadmin); |
209 |
0 |
ContextualLocalizationManager localization = Utils.getComponent(ContextualLocalizationManager.class); |
210 |
0 |
context.getWiki().saveDocument(getDocument(), localization.getTranslationPlain("rating.saveComment"), true, |
211 |
|
context); |
212 |
|
} catch (XWikiException e) { |
213 |
0 |
throw new RatingsException(e); |
214 |
|
} |
215 |
|
} |
216 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
217 |
0 |
@Override... |
218 |
|
public boolean remove() throws RatingsException |
219 |
|
{ |
220 |
0 |
try { |
221 |
0 |
XWikiDocument doc = getDocument(); |
222 |
|
|
223 |
0 |
context.getWiki().deleteDocument(doc, context); |
224 |
|
} catch (XWikiException e) { |
225 |
0 |
throw new RatingsException(e); |
226 |
|
} |
227 |
0 |
return true; |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
@param |
234 |
|
@return |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
236 |
0 |
private DocumentReference getPageReference(DocumentReference documentRef) throws XWikiException... |
237 |
|
{ |
238 |
0 |
XWikiDocument doc = context.getWiki().getDocument(documentRef, context); |
239 |
0 |
String ratingsSpace = ratingsManager.getRatingsSpaceName(documentRef); |
240 |
0 |
String pageSufix = "R"; |
241 |
|
|
242 |
0 |
boolean hasRatingsSpaceForeachSpace = ratingsManager.hasRatingsSpaceForeachSpace(documentRef); |
243 |
|
|
244 |
0 |
SpaceReference spaceReference = doc.getDocumentReference().getLastSpaceReference(); |
245 |
0 |
spaceReference.replaceParent(spaceReference.getWikiReference(), this.context.getWikiReference()); |
246 |
|
|
247 |
0 |
if (hasRatingsSpaceForeachSpace) { |
248 |
0 |
spaceReference = new SpaceReference(spaceReference.getName() + ratingsSpace, spaceReference.getParent()); |
249 |
0 |
String uniqueName = getUniquePageName(ratingsSpace, doc.getName(), pageSufix, true); |
250 |
|
|
251 |
0 |
return new DocumentReference(uniqueName, spaceReference); |
252 |
0 |
} else if (ratingsSpace == null) { |
253 |
0 |
String uniqueName = getUniquePageName(doc.getSpace(), doc.getName() + pageSufix, "", true); |
254 |
|
|
255 |
0 |
return new DocumentReference(uniqueName, spaceReference); |
256 |
|
} else { |
257 |
0 |
String uniqueName = getUniquePageName(ratingsSpace, doc.getSpace() + "_" + doc.getName(), pageSufix, true); |
258 |
|
|
259 |
0 |
return new DocumentReference(context.getWikiId(), ratingsSpace, uniqueName); |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
@param |
267 |
|
@param |
268 |
|
@param |
269 |
|
@param |
270 |
|
@return |
271 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
272 |
0 |
private String getUniquePageName(String spaceReference, String name, String postfix, boolean forcepostfix)... |
273 |
|
{ |
274 |
0 |
String pageName = context.getWiki().clearName(name, context); |
275 |
0 |
if (forcepostfix || context.getWiki().exists(spaceReference + '.' + pageName, context)) { |
276 |
0 |
int i = 1; |
277 |
0 |
while (context.getWiki().exists(spaceReference + '.' + pageName + postfix + i, context)) { |
278 |
0 |
i++; |
279 |
|
} |
280 |
0 |
return pageName + postfix + i; |
281 |
|
} |
282 |
0 |
return pageName; |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
@param |
289 |
|
@param |
290 |
|
@param |
291 |
|
@param |
292 |
|
@return |
293 |
|
@throws |
294 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 2 |
Complexity Density: 0.14 |
|
295 |
0 |
private XWikiDocument addDocument(DocumentReference documentRef, DocumentReference author, Date date, int vote)... |
296 |
|
throws RatingsException |
297 |
|
{ |
298 |
0 |
try { |
299 |
0 |
DocumentReference pageRef = getPageReference(documentRef); |
300 |
0 |
String parentDocName = ratingsManager.entityReferenceSerializer.serialize(documentRef); |
301 |
0 |
XWiki xwiki = context.getWiki(); |
302 |
0 |
XWikiDocument doc = xwiki.getDocument(pageRef, context); |
303 |
0 |
doc.setParent(parentDocName); |
304 |
0 |
doc.setHidden(true); |
305 |
0 |
BaseObject obj = doc.newXObject(RatingsManager.RATINGS_CLASSREFERENCE, context); |
306 |
0 |
obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR, |
307 |
|
ratingsManager.entityReferenceSerializer.serialize(author)); |
308 |
0 |
obj.setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date); |
309 |
0 |
obj.setIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE, vote); |
310 |
0 |
obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_PARENT, parentDocName); |
311 |
|
|
312 |
0 |
return doc; |
313 |
|
} catch (XWikiException e) { |
314 |
0 |
throw new RatingsException(e); |
315 |
|
} |
316 |
|
} |
317 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 6 |
Complexity Density: 0.43 |
|
318 |
0 |
@Override... |
319 |
|
public String toString() |
320 |
|
{ |
321 |
0 |
boolean shouldAddSpace = false; |
322 |
0 |
StringBuffer sb = new StringBuffer(); |
323 |
0 |
if (getAuthor() != null) { |
324 |
0 |
sb.append("\nAuthor=").append(getAuthor()); |
325 |
0 |
shouldAddSpace = true; |
326 |
|
} |
327 |
0 |
if (getDate() != null) { |
328 |
0 |
sb.append(shouldAddSpace ? " " : ""); |
329 |
0 |
sb.append("\nDate=").append(getDate()); |
330 |
0 |
shouldAddSpace = true; |
331 |
|
} |
332 |
0 |
if (getVote() != 0) { |
333 |
0 |
sb.append(shouldAddSpace ? " " : ""); |
334 |
0 |
sb.append("\nVote=").append(getVote()).append("\n"); |
335 |
0 |
shouldAddSpace = true; |
336 |
|
} |
337 |
|
|
338 |
0 |
return sb.toString(); |
339 |
|
} |
340 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
341 |
0 |
@Override... |
342 |
|
public DocumentReference getDocumentReference() |
343 |
|
{ |
344 |
0 |
return this.documentRef; |
345 |
|
} |
346 |
|
} |