/*
 * 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.xwiki;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.stats.impl.VisitStats;
import com.xpn.xwiki.stats.impl.StatsUtil.PeriodType;
import com.xpn.xwiki.store.XWikiHibernateStore;

/**
 * Store visit statistics into the database.
 * 
 * @version $Id$
 * @since 1.4M2
 */
public class VisitStatsStoreItem extends AbstractStatsStoreItem
{
    /**
     * Logging tools.
     */
    private static final Log LOG = LogFactory.getLog(DocumentStatsStoreItem.class);

    /**
     * The {@link VisitStats} object to store.
     */
    private VisitStats visitStats;

    /**
     * Create new instance of {@link VisitStatsStoreItem}.
     * 
     * @param visitStats the {@link VisitStats} object to store.
     * @param context the XWiki context.
     */
    public VisitStatsStoreItem(VisitStats visitStats, XWikiContext context)
    {
        super(visitStats.getName(), new Date(), PeriodType.MONTH, context);
        this.period = visitStats.getPeriod();

        this.visitStats = (VisitStats) visitStats.clone();
    }

    /**
     * {@inheritDoc}
     * 
     * @see com.xpn.xwiki.stats.impl.xwiki.XWikiStatsStoreItem#getId()
     */
    public String getId()
    {
        return String.format("%s %s %s %s", getClass(), visitStats.getName(), visitStats
            .getUniqueID(), visitStats.getCookie());
    }

    /**
     * {@inheritDoc}
     *
     * @see com.xpn.xwiki.stats.impl.xwiki.XWikiStatsStoreItem#store(java.util.List)
     */
    public void store(List<XWikiStatsStoreItem> stats)
    {
        VisitStatsStoreItem firstItem = (VisitStatsStoreItem) stats.get(0);
        VisitStats oldVisitStats = firstItem.visitStats.getOldObject();

        VisitStatsStoreItem lastItem = (VisitStatsStoreItem) stats.get(stats.size() - 1);
        VisitStats newVisitStats = lastItem.visitStats;

        XWikiHibernateStore store = context.getWiki().getHibernateStore();
        if (store == null) {
            return;
        }

        try {
            // In case we have store the old object then we need to remove it before saving the
            // other one because the ID info have changed
            if (oldVisitStats != null) {
                try {
                    store.deleteXWikiCollection(oldVisitStats, context, true, true);
                } catch (Exception e) {
                    if (LOG.isWarnEnabled()) {
                        LOG.warn("Failed to delete old visit statistics object from database ["
                            + getId() + "]");
                    }
                }
            }

            store.saveXWikiCollection(newVisitStats, context, true);
        } catch (XWikiException e) {
            LOG.error("Failed to save visit statictics object [" + getId() + "]");
        }
    }
}

@@ -2322,24 +2322,29 @@
 getClass(), 
+this.
 visitStats.g
@@ -2349,24 +2349,29 @@
 .getName(), 
+this.
 visitStats%0A 
@@ -2368,29 +2368,16 @@
 sitStats
-%0A            
 .getUniq
@@ -2383,17 +2383,34 @@
 queID(),
- 
+%0A            this.
 visitSta
@@ -2468,16 +2468,17 @@
 %7D%0A     *
+ 
 %0A     * 
@@ -2956,16 +2956,21 @@
 store = 
+this.
 context.
@@ -3346,32 +3346,37 @@
 (oldVisitStats, 
+this.
 context, true, t
@@ -3567,36 +3567,8 @@
 e %5B%22
-%0A                           
  + g
@@ -3690,16 +3690,21 @@
 tStats, 
+this.
 context,

Apply codestyle