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

File SeparatePageRating.java

 

Coverage histogram

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

Code metrics

22
93
22
1
346
226
39
0.42
4.23
22
1.77

Classes

Class Line # Actions
SeparatePageRating 45 93 0% 39 137
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.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 $Id: 01696d309a8c10422070a96cb0b370924b2f3f2e $
42    * @see Rating
43    * @since 6.4M3
44    */
 
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    * SeparatePageRating constructor.
57    *
58    * @param documentRef the reference of the document with which the rating is associated
59    * @param author the author of the rating
60    * @param vote the authors vote
61    * @param context the context in which the rating took place
62    * @param ratingsManager the RatingsManager to be used in processing the rating
63    * @throws RatingsException when encountering an error while instantiating a SeparatePageRating
64    */
 
65  0 toggle 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    * SeparatePageRating constructor.
73    *
74    * @param documentRef the reference of the document with which the rating is associated
75    * @param author the author of the rating
76    * @param date the date when the rating took place
77    * @param vote the authors vote
78    * @param context the context in which the rating took place
79    * @param ratingsManager the RatingsManager to be used in processing the rating
80    * @throws RatingsException when encountering an error while instantiating a SeparatePageRating
81    */
 
82  0 toggle 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    * SeparatePageRating constructor.
93    *
94    * @param documentRef the document reference of the document with which the rating is associated
95    * @param doc the document with which the rating is associated
96    * @param context the context in which the rating took place
97    * @param ratingsManager the RatingsManager to be used in processing the rating
98    * @throws RatingsException when encountering an error while instantiating a SeparatePageRating
99    */
 
100  0 toggle 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   
 
109  0 toggle @Override
110    public String getRatingId()
111    {
112  0 return getDocument().getFullName();
113    }
114   
 
115  0 toggle @Override
116    public String getGlobalRatingId()
117    {
118  0 return getRatingId();
119    }
120   
 
121  0 toggle @Override
122    public BaseObject getAsObject()
123    {
124  0 return getDocument().getXObject(RatingsManager.RATINGS_CLASSREFERENCE);
125    }
126   
 
127  0 toggle 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   
 
139  0 toggle @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   
 
146  0 toggle @Override
147    public Date getDate()
148    {
149   
150  0 return getAsObject().getDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE);
151    }
152   
 
153  0 toggle @Override
154    public void setAuthor(DocumentReference author)
155    {
156  0 getAsObject().setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR,
157    ratingsManager.entityReferenceSerializer.serialize(author));
158    }
159   
 
160  0 toggle @Override
161    public void setDate(Date date)
162    {
163  0 getAsObject().setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date);
164    }
165   
 
166  0 toggle @Override
167    public int getVote()
168    {
169  0 return getAsObject().getIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE);
170    }
171   
 
172  0 toggle @Override
173    public void setVote(int vote)
174    {
175  0 getAsObject().setIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE, vote);
176    }
177   
 
178  0 toggle @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   
 
188  0 toggle @Override
189    public String display(String propertyName, String mode)
190    {
191  0 return document.display(propertyName, mode, getAsObject(), context);
192    }
193   
 
194  0 toggle @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   
 
217  0 toggle @Override
218    public boolean remove() throws RatingsException
219    {
220  0 try {
221  0 XWikiDocument doc = getDocument();
222    // remove the rating
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    * Generate page name from the container page We add Rating and getUniquePageName will add us a counter to our page.
232    *
233    * @param documentRef reference to the document with which the rating is associated
234    * @return a reference to the document in which the rating is stored
235    */
 
236  0 toggle 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    * Gets a unique page name.
265    *
266    * @param spaceReference the reference of the space in which the document should be
267    * @param name the name of the document
268    * @param postfix post fix to add to the document name
269    * @param forcepostfix force post fix or not
270    * @return the unique document name
271    */
 
272  0 toggle 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    * Adds a new document in which to store ratings.
287    *
288    * @param documentRef reference to the document with which the rating is associated
289    * @param author the author of the rating
290    * @param date the date when the rating was done
291    * @param vote the author's vote
292    * @return the newly created document
293    * @throws RatingsException when an error occurs while creating the document
294    */
 
295  0 toggle 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   
 
318  0 toggle @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   
 
341  0 toggle @Override
342    public DocumentReference getDocumentReference()
343    {
344  0 return this.documentRef;
345    }
346    }