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

File DefaultRating.java

 

Coverage histogram

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

Code metrics

16
64
21
1
323
181
33
0.52
3.05
21
1.57

Classes

Class Line # Actions
DefaultRating 42 64 0% 33 101
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.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    * Default implementation of the Rating interface.
37    *
38    * @see Rating
39    * @version $Id: 5f40f315cc49e5b25e3a167c74264ed6a0211b9f $
40    * @since 6.4M3
41    */
 
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    * DefaultRating constructor.
56    *
57    * @param documentRef the document to which the rating is linked
58    * @param author the user that created the rating
59    * @param vote the author's vote
60    * @param context the context in which the rating was created
61    * @param ratingsManager the ratings manager used when the rating was created
62    * @throws XWikiException when failing to create object
63    */
 
64  0 toggle 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    * DefaultRating constructor.
72    *
73    * @param documentRef the document to which the rating is linked
74    * @param author the user that created the rating
75    * @param date the date when the rating took place
76    * @param vote the author's vote
77    * @param context the context in which the rating was created
78    * @param ratingsManager the ratings manager used when the rating was created
79    * @throws XWikiException when failing to create object
80    */
 
81  0 toggle 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    * DefaultRating constructor.
94    *
95    * @param documentRef the document to which the rating is linked
96    * @param obj the rating object containing the rating information
97    * @param context the context in which the rating was created
98    * @param ratingsManager the ratings manager used when the rating was created
99    */
 
100  0 toggle 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    * RatingId represents the rating ID. It is the object number in the default ratings case.
112    *
113    * @return the rating id
114    */
 
115  0 toggle @Override
116    public String getRatingId()
117    {
118  0 return "" + object.getNumber();
119    }
120   
121    /**
122    * RatingId represents the rating ID. It is the object number in the default ratings case.
123    *
124    * @return the global rating id
125    */
 
126  0 toggle @Override
127    public String getGlobalRatingId()
128    {
129  0 return document.getFullName() + ":" + object.getNumber();
130    }
131   
132    /**
133    * Gets the object representation of the rating.
134    *
135    * @return an object containing the rating information
136    */
 
137  0 toggle @Override
138    public BaseObject getAsObject()
139    {
140  0 return object;
141    }
142   
143    /**
144    * Gets the document to which the rating is linked from the document reference.
145    *
146    * @return the document to which the rating belongs to
147    */
 
148  0 toggle 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    // do nothing
155    }
156    }
157  0 return document;
158    }
159   
 
160  0 toggle @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   
 
167  0 toggle @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   
 
174  0 toggle @Override
175    public Date getDate()
176    {
177  0 return object.getDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE);
178    }
179   
 
180  0 toggle @Override
181    public void setDate(Date date)
182    {
183  0 object.setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date);
184    }
185   
 
186  0 toggle @Override
187    public int getVote()
188    {
189  0 return object.getIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE);
190    }
191   
 
192  0 toggle @Override
193    public void setVote(int vote)
194    {
195  0 object.setIntValue("vote", vote);
196    }
197   
 
198  0 toggle @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   
 
208  0 toggle @Override
209    public String display(String propertyName, String mode)
210    {
211  0 return document.display(propertyName, mode, object, context);
212    }
213   
 
214  0 toggle @Override
215    public DocumentReference getDocumentReference()
216    {
217  0 return this.documentRef;
218    }
219   
 
220  0 toggle @Override
221    public void save() throws RatingsException
222    {
223  0 try {
224    // Force content dirty to false, so that the content update date is not changed when saving the document.
225    // This should not be handled there, since it is not the responsibility of this plugin to decide if
226    // the content has actually been changed or not since current revision, but the implementation of
227    // this in XWiki core is wrong. See http://jira.xwiki.org/jira/XWIKI-2800 for more details.
228    // There is a draw-back to doing this, being that if the document content is being changed before
229    // the document is rated, the contentUpdateDate will not be modified. Although possible, this is very
230    // unlikely to happen, or to be a use case. The default rating application will use an asynchronous service
231    // to
232    // note a document, which service will only set the rating, so the behavior will be correct.
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   
 
240  0 toggle @Override
241    public boolean remove()
242    {
243  0 return remove(true);
244    }
245   
246    /**
247    * Remove the rating.
248    *
249    * @param withSave save the document after removing the rating or not
250    * @return the status of the action
251    */
 
252  0 toggle 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    // save is needed to remove effectively
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    * Creates a new ratings object.
273    *
274    * @param documentName the document being rated
275    * @param author the author of the rating
276    * @param date the date when the rating took place
277    * @param vote the vote that the author gave
278    * @throws XWikiException when failig to create new object
279    */
 
280  0 toggle 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    // read data from map
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    // set the internal variable
293  0 object = obj;
294    }
295   
296    /**
297    * Creates a string representation of the rating.
298    *
299    * @return the string representation of the rating
300    */
 
301  0 toggle @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    }