1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.stats.impl

File XWikiStats.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart1.png
82% of files have more coverage

Code metrics

24
61
12
2
271
145
24
0.39
5.08
6
2

Classes

Class Line # Actions
XWikiStats 47 61 0% 24 96
0.0103092781%
XWikiStats.Property 54 0 - 0 0
-1.0 -
 

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 com.xpn.xwiki.stats.impl;
21   
22    import java.util.Date;
23    import java.util.Iterator;
24    import java.util.List;
25   
26    import org.apache.commons.lang3.StringUtils;
27    import org.dom4j.Element;
28    import org.dom4j.dom.DOMElement;
29    import org.xwiki.model.EntityType;
30    import org.xwiki.model.reference.EntityReference;
31    import org.xwiki.model.reference.EntityReferenceResolver;
32   
33    import com.xpn.xwiki.XWikiException;
34    import com.xpn.xwiki.objects.BaseCollection;
35    import com.xpn.xwiki.objects.BaseProperty;
36    import com.xpn.xwiki.objects.PropertyInterface;
37    import com.xpn.xwiki.objects.classes.BaseClass;
38    import com.xpn.xwiki.objects.classes.PropertyClass;
39    import com.xpn.xwiki.stats.impl.StatsUtil.PeriodType;
40    import com.xpn.xwiki.web.Utils;
41   
42    /**
43    * Base class for all stored statistics object.
44    *
45    * @version $Id: 5b559d46239e1516cce5ba103b9cf03f51a6f1ff $
46    */
 
47    public class XWikiStats extends BaseCollection
48    {
49    /**
50    * The properties of statistics object.
51    *
52    * @version $Id: 5b559d46239e1516cce5ba103b9cf03f51a6f1ff $
53    */
 
54    public enum Property
55    {
56    /**
57    * The name of the object database property <code>period</code>.
58    */
59    period,
60   
61    /**
62    * The name of the object database property <code>pageViews</code>.
63    */
64    pageViews
65    }
66   
67    /**
68    * The name of the XML node <code>object</code>.
69    */
70    private static final String XMLNODE_OBJECT = "object";
71   
72    /**
73    * The name of the XML node <code>name</code>.
74    */
75    private static final String XMLNODE_NAME = "name";
76   
77    /**
78    * The name of the XML node <code>number</code>.
79    */
80    private static final String XMLNODE_NUMBER = "number";
81   
82    /**
83    * The name of the XML node <code>className</code>.
84    */
85    private static final String XMLNODE_CLASSNAME = "className";
86   
87    /**
88    * The name of the XML node <code>property</code>.
89    */
90    private static final String XMLNODE_PROPERTY = "property";
91   
92    /** Resolve names into reference for uid string serialization. */
93    private final EntityReferenceResolver<String> resolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING);
94   
95    /**
96    * Default constructor.
97    */
 
98  87 toggle public XWikiStats()
99    {
100    }
101   
102    /**
103    * @param periodDate the period date.
104    * @param periodtype the period type.
105    */
 
106  0 toggle public XWikiStats(Date periodDate, PeriodType periodtype)
107    {
108  0 setPeriod(StatsUtil.getPeriodAsInt(periodDate, periodtype));
109    }
110   
111    /**
112    * @return the time when statistic was stored.
113    */
 
114  0 toggle public int getPeriod()
115    {
116  0 return getIntValue(Property.period.toString());
117    }
118   
119    /**
120    * @param period the time when statistic was stored.
121    */
 
122  0 toggle public void setPeriod(int period)
123    {
124  0 setIntValue(Property.period.toString(), period);
125    }
126   
127    /**
128    * @return the counter of view action of this statistic.
129    */
 
130  0 toggle public int getPageViews()
131    {
132  0 return getIntValue(Property.pageViews.toString());
133    }
134   
135    /**
136    * @param pageViews the counter of view action of this statistic.
137    */
 
138  0 toggle public void setPageViews(int pageViews)
139    {
140  0 setIntValue(Property.pageViews.toString(), pageViews);
141    }
142   
143    /**
144    * Add 1 to the counter of view action of this statistic.
145    */
 
146  0 toggle public void incPageViews()
147    {
148  0 setIntValue(Property.pageViews.toString(), getPageViews() + 1);
149    }
150   
 
151  0 toggle @Override
152    protected String getLocalKey()
153    {
154  0 StringBuilder sb = new StringBuilder(64);
155   
156    // The R40000XWIKI6990DataMigration use a stubbed class to provide the above two values. If the ids depends
157    // on other non-static requirements, it should be adapted accordingly.
158  0 String name = getName();
159  0 int nb = getNumber();
160   
161  0 if (!StringUtils.isEmpty(name)) {
162    // TODO: Refactor to get the original reference and fix the confusion when a space contains escaped chars
163  0 EntityReference ref = this.resolver.resolve(name, EntityType.DOCUMENT);
164  0 if (ref.getName().equals(name)) {
165  0 ref = new EntityReference(name, EntityType.SPACE);
166    }
167  0 sb.append(getLocalUidStringEntityReferenceSerializer().serialize(ref));
168    }
169   
170    // if number used, serialize it as well. It may happened that the hash is 0, but this is really unlikely
171    // and it will not hurt anyway.
172  0 if (nb != 0) {
173    // TODO: Avoid the hashed number, and use the original info (referer, period, etc...)
174  0 String str = Integer.toString(nb);
175  0 sb.append(str.length()).append(':').append(str);
176    }
177   
178  0 return sb.toString();
179    }
180   
181    // Satisfy checkstyle !
 
182  0 toggle @Override
183    public int hashCode()
184    {
185  0 return super.hashCode();
186    }
187   
 
188  0 toggle @Override
189    public boolean equals(Object obj)
190    {
191    // Same Java object, they sure are equal
192  0 if (this == obj) {
193  0 return true;
194    }
195   
196  0 if (!super.equals(obj)) {
197  0 return false;
198    }
199   
200  0 if (getNumber() != ((BaseCollection) obj).getNumber()) {
201  0 return false;
202    }
203   
204  0 return true;
205    }
206   
 
207  0 toggle @Override
208    // TODO: implement an EntityEventGenerator for XWikiStats
209    public Element toXML(BaseClass bclass)
210    {
211  0 Element oel = new DOMElement(XMLNODE_OBJECT);
212   
213    // Add Class
214  0 if (bclass != null) {
215  0 if (bclass.getFieldList().size() > 0) {
216  0 oel.add(bclass.toXML());
217    }
218    }
219   
220  0 Element el = new DOMElement(XMLNODE_NAME);
221  0 el.addText(getName());
222  0 oel.add(el);
223   
224  0 el = new DOMElement(XMLNODE_NUMBER);
225  0 el.addText(getNumber() + "");
226  0 oel.add(el);
227   
228  0 el = new DOMElement(XMLNODE_CLASSNAME);
229  0 el.addText(getClassName());
230  0 oel.add(el);
231   
232  0 for (Iterator<?> it = getFieldList().iterator(); it.hasNext();) {
233  0 Element pel = new DOMElement(XMLNODE_PROPERTY);
234  0 PropertyInterface bprop = (PropertyInterface) it.next();
235  0 pel.add(bprop.toXML());
236  0 oel.add(pel);
237    }
238   
239  0 return oel;
240    }
241   
242    /**
243    * Initialize statistics object from XML schema.
244    *
245    * @param oel the XML root node containing statistics datas.
246    * @throws XWikiException error when parsing XML schema.
247    */
 
248  0 toggle public void fromXML(Element oel) throws XWikiException
249    {
250  0 Element cel = oel.element("class");
251  0 BaseClass bclass = new BaseClass();
252  0 if (cel != null) {
253  0 bclass.fromXML(cel);
254  0 setClassName(bclass.getName());
255    }
256   
257  0 setName(oel.element(XMLNODE_NAME).getText());
258  0 List<?> list = oel.elements(XMLNODE_PROPERTY);
259  0 for (int i = 0; i < list.size(); i++) {
260  0 Element pcel = (Element) ((Element) list.get(i)).elements().get(0);
261  0 String name = pcel.getName();
262  0 PropertyClass pclass = (PropertyClass) bclass.get(name);
263  0 if (pclass != null) {
264  0 BaseProperty property = pclass.newPropertyfromXML(pcel);
265  0 property.setName(name);
266  0 property.setObject(this);
267  0 safeput(name, property);
268    }
269    }
270    }
271    }