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

File SeparatePageRatingsManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

18
68
10
1
284
189
25
0.37
6.8
10
2.5

Classes

Class Line # Actions
SeparatePageRatingsManager 55 68 0% 25 78
0.187518.8%
 

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.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 $Id: e94a34720619d169a99f5653860101e177d6ad85 $
48    * @see RatingsManager
49    * @see AbstractRatingsManager
50    * @since 6.4M3
51    */
52    @Component
53    @Singleton
54    @Named("separate")
 
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    * SeparatePageRatingsManager constructor.
77    */
 
78  2 toggle public SeparatePageRatingsManager()
79    {
80  2 super();
81    }
82   
83    /**
84    * Gets the ratings space name.
85    *
86    * @param documentRef reference to the document with which the ratings are associated
87    * @return the ratings space name
88    */
 
89  0 toggle 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    * Gets whether to associate a different space for every space which is ratable.
100    *
101    * @param documentRef reference to the document with which the ratings are associated
102    * @return whether to associate a different space for every space which is ratable
103    */
 
104  0 toggle 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    * Saves the rating.
116    *
117    * @param rating a Rating object
118    * @throws RatingsException when an error occurs while saving the rating
119    */
 
120  0 toggle 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   
 
129  0 toggle @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    // Indicate that we start modifying the rating
144  0 this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
145   
146  0 boolean updateFailed = true;
147  0 try {
148    // saving rating
149  0 rating.save();
150   
151    // update the average rating
152  0 updateAverageRatings(documentRef, rating, oldVote);
153   
154  0 updateFailed = false;
155    } finally {
156  0 if (updateFailed) {
157    // Indicate that the we start modifying the rating
158  0 this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
159    } else {
160    // Indicate that we finished updating the rating
161  0 this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null);
162    }
163    }
164   
165  0 return rating;
166    }
167   
 
168  74 toggle @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   
 
201  0 toggle @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   
 
226  6 toggle @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   
 
241  0 toggle @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    * Gets a SeparatePageRating instance from a document
274    *
275    * @param documentRef the reference of the document which the ratings are for
276    * @param doc the document which the ratings are for
277    * @return a SeparatePageRating
278    * @throws RatingsException when an error occurs while fetching the rating
279    */
 
280  0 toggle public Rating getRatingFromDocument(DocumentReference documentRef, XWikiDocument doc) throws RatingsException
281    {
282  0 return new SeparatePageRating(documentRef, doc, getXWikiContext(), this);
283    }
284    }