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

File AverageRatingProtectionListener.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

6
9
2
1
79
41
5
0.56
4.5
2
2.5

Classes

Class Line # Actions
AverageRatingProtectionListener 46 9 0% 5 10
0.411764741.2%
 

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 javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.bridge.event.DocumentUpdatingEvent;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.observation.AbstractEventListener;
29    import org.xwiki.observation.ObservationContext;
30    import org.xwiki.observation.event.Event;
31    import org.xwiki.ratings.RatingsManager;
32    import org.xwiki.ratings.UpdatingRatingEvent;
33   
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.objects.BaseObject;
36   
37    /**
38    * Make sure that rating average is modified only trough rating API.
39    *
40    * @version $Id: 2acbce1b2cf4ba3c64e96eb3ed0dc2ed2fc46ec0 $
41    * @since 7.1M1
42    */
43    @Component
44    @Singleton
45    @Named("AverageRatingProtectionListener")
 
46    public class AverageRatingProtectionListener extends AbstractEventListener
47    {
48    private static final UpdatingRatingEvent PARENT = new UpdatingRatingEvent();
49   
50    @Inject
51    private ObservationContext observationContext;
52   
53    /**
54    * Default constructor.
55    */
 
56  2 toggle public AverageRatingProtectionListener()
57    {
58  2 super("AverageRatingProtectionListener", new DocumentUpdatingEvent());
59    }
60   
 
61  41 toggle @Override
62    public void onEvent(Event event, Object source, Object data)
63    {
64  41 XWikiDocument document = (XWikiDocument) source;
65  41 BaseObject ratingObject = document.getXObject(RatingsManager.AVERAGE_RATINGS_CLASSREFERENCE);
66   
67  41 if (ratingObject != null) {
68    // If the modification is not part of an official rating cancel it
69  0 if (!this.observationContext.isIn(PARENT)) {
70  0 XWikiDocument previousDocument = document.getOriginalDocument();
71  0 BaseObject previousObject = previousDocument.getXObject(RatingsManager.AVERAGE_RATINGS_CLASSREFERENCE);
72   
73  0 if (previousObject != null) {
74  0 ratingObject.apply(previousObject, true);
75    }
76    }
77    }
78    }
79    }