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