Index: src/main/java/com/xpn/xwiki/stats/impl/StatsUtil.java =================================================================== --- src/main/java/com/xpn/xwiki/stats/impl/StatsUtil.java (revision 0) +++ src/main/java/com/xpn/xwiki/stats/impl/StatsUtil.java (revision 0) @@ -0,0 +1,65 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + * + */ + +package com.xpn.xwiki.stats.impl; + +import java.util.Calendar; +import java.util.Date; + +/** + * Utility class for statistics + * + * @version $Id: $ + */ +public class StatsUtil +{ + public static int PERIOD_MONTH = 0; + + public static int PERIOD_DAY = 1; + + /** + * The integer representation of the specified date has the following format:
+ * + * + * @param date + * @param type Can be {@link #PERIOD_DAY} or {@link #PERIOD_MONTH} + * @return + * @see java.text.SimpleDateFormat + */ + public static int getPeriodAsInt(Date date, int type) + { + Calendar cal = Calendar.getInstance(); + if (date != null) { + cal.setTime(date); + } + if (type == PERIOD_MONTH) { + // The first month of the year is JANUARY which is 0 + return cal.get(Calendar.YEAR) * 100 + (cal.get(Calendar.MONTH) + 1); + } else { + // The first day of the month has value 1 + return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + + cal.get(Calendar.DAY_OF_MONTH); + } + } +} Index: src/main/java/com/xpn/xwiki/stats/impl/XWikiStats.java =================================================================== --- src/main/java/com/xpn/xwiki/stats/impl/XWikiStats.java (revision 5418) +++ src/main/java/com/xpn/xwiki/stats/impl/XWikiStats.java (working copy) @@ -34,9 +34,6 @@ import java.util.*; public class XWikiStats extends BaseCollection { - public static int PERIOD_MONTH = 0; - public static int PERIOD_DAY = 1; - public XWikiStats() { super(); } @@ -42,7 +39,7 @@ } public XWikiStats(Date period, int periodtype) { - setPeriod(getPeriodAsInt(period, periodtype)); + setPeriod(StatsUtil.getPeriodAsInt(period, periodtype)); } public int getPeriod() { @@ -53,15 +50,6 @@ setIntValue("period", period); } - public int getPeriodAsInt(Date date, int type) { - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - if (type == PERIOD_MONTH) - return cal.get(Calendar.YEAR) * 100 + (cal.get(Calendar.MONTH)+1); - else - return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH)+1) * 100 + (cal.get(Calendar.DAY_OF_MONTH)+1); - } - public int getPageViews() { return getIntValue("pageViews"); } Index: src/main/java/com/xpn/xwiki/stats/impl/XWikiStatsServiceImpl.java =================================================================== --- src/main/java/com/xpn/xwiki/stats/impl/XWikiStatsServiceImpl.java (revision 5418) +++ src/main/java/com/xpn/xwiki/stats/impl/XWikiStatsServiceImpl.java (working copy) @@ -97,7 +97,7 @@ */ public DocumentStats getDocMonthStats(String docname, String action, Date month, XWikiContext context) { XWikiHibernateStore store = context.getWiki().getHibernateStore(); - DocumentStats object = new DocumentStats(docname, action, month, XWikiStats.PERIOD_MONTH); + DocumentStats object = new DocumentStats(docname, action, month, StatsUtil.PERIOD_MONTH); try { store.loadXWikiCollection(object, context, true); return object; @@ -228,12 +228,12 @@ e.printStackTrace(); } - addPageView(doc.getFullName(), action, XWikiStats.PERIOD_MONTH, store, context, vobject); - addPageView(doc.getSpace(), action, XWikiStats.PERIOD_MONTH, store, context, vobject); - addPageView("", action, XWikiStats.PERIOD_MONTH, store, context, vobject); - addPageView(doc.getFullName(), action, XWikiStats.PERIOD_DAY, store, context, vobject); - addPageView(doc.getSpace(), action, XWikiStats.PERIOD_DAY, store, context, vobject); - addPageView("", action, XWikiStats.PERIOD_DAY, store, context, vobject); + addPageView(doc.getFullName(), action, StatsUtil.PERIOD_MONTH, store, context, vobject); + addPageView(doc.getSpace(), action, StatsUtil.PERIOD_MONTH, store, context, vobject); + addPageView("", action, StatsUtil.PERIOD_MONTH, store, context, vobject); + addPageView(doc.getFullName(), action, StatsUtil.PERIOD_DAY, store, context, vobject); + addPageView(doc.getSpace(), action, StatsUtil.PERIOD_DAY, store, context, vobject); + addPageView("", action, StatsUtil.PERIOD_DAY, store, context, vobject); } // In case of a "view" action we want to store referer info @@ -241,7 +241,7 @@ String referer = getReferer(context); if ((referer !=null)&&(!referer.equals(""))) { // Visits of the web - RefererStats robject = new RefererStats(doc.getFullName(), referer, new Date(), XWikiStats.PERIOD_MONTH); + RefererStats robject = new RefererStats(doc.getFullName(), referer, new Date(), StatsUtil.PERIOD_MONTH); synchronized(robject) { try { store.loadXWikiCollection(robject, context, true); @@ -408,7 +408,7 @@ } vobject = new VisitStats(context.getUser(), uniqueID, cookie.getValue(), - ip, ua, nowDate, XWikiStats.PERIOD_MONTH); + ip, ua, nowDate, StatsUtil.PERIOD_MONTH); vobject.setEndDate(nowDate); } else { if (!newcookie) { Index: src/test/java/com/xpn/xwiki/stats/impl/StatsUtilTest.java =================================================================== --- src/test/java/com/xpn/xwiki/stats/impl/StatsUtilTest.java (revision 0) +++ src/test/java/com/xpn/xwiki/stats/impl/StatsUtilTest.java (revision 0) @@ -0,0 +1,51 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package com.xpn.xwiki.stats.impl; + +import java.text.SimpleDateFormat; +import java.util.Calendar; + +import junit.framework.TestCase; + +/** + * Unit tests for the {@link StatsUtil} class. + * + * @version $Id: $ + */ +public class StatsUtilTest extends TestCase +{ + /** + * Test for the {@link StatsUtil#getPeriodAsInt(java.util.Date, int)} + */ + public void testGetPeriodAsInt() + { + Calendar cal = Calendar.getInstance(); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); + String a = sdf.format(cal.getTime()); + String b = StatsUtil.getPeriodAsInt(cal.getTime(), StatsUtil.PERIOD_MONTH) + ""; + assertTrue("Wrong month period format", a.equals(b)); + + sdf = new SimpleDateFormat("yyyyMMdd"); + a = sdf.format(cal.getTime()); + b = StatsUtil.getPeriodAsInt(cal.getTime(), StatsUtil.PERIOD_DAY) + ""; + assertTrue("Wrong day period format", a.equals(b)); + } +}