<commit_before>/*
 * 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.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.objects.BaseCollection;
import com.xpn.xwiki.objects.BaseProperty;
import com.xpn.xwiki.objects.PropertyInterface;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.PropertyClass;
import com.xpn.xwiki.stats.impl.StatsUtil.PeriodType;
import com.xpn.xwiki.web.Utils;

/**
 * Base class for all stored statistics object.
 * 
 * @version $Id$
 */
public class XWikiStats extends BaseCollection
{
    /**
     * The properties of statistics object.
     * 
     * @version $Id$
     */
    public enum Property
    {
        /**
         * The name of the object database property <code>period</code>.
         */
        period,

        /**
         * The name of the object database property <code>pageViews</code>.
         */
        pageViews
    }

    /**
     * The name of the XML node <code>object</code>.
     */
    private static final String XMLNODE_OBJECT = "object";

    /**
     * The name of the XML node <code>name</code>.
     */
    private static final String XMLNODE_NAME = "name";

    /**
     * The name of the XML node <code>number</code>.
     */
    private static final String XMLNODE_NUMBER = "number";

    /**
     * The name of the XML node <code>className</code>.
     */
    private static final String XMLNODE_CLASSNAME = "className";

    /**
     * The name of the XML node <code>property</code>.
     */
    private static final String XMLNODE_PROPERTY = "property";

    /** Override default page name to easily detect it after resolution. */
    private static final EntityReference EMPTY_REF = new EntityReference("$$EMPTY$$", EntityType.DOCUMENT);

    /** Resolve names into reference for uid string serialization. */
    private final EntityReferenceResolver<String> resolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING);

    /**
     * Default constructor.
     */
    public XWikiStats()
    {
    }

    /**
     * @param periodDate the period date.
     * @param periodtype the period type.
     */
    public XWikiStats(Date periodDate, PeriodType periodtype)
    {
        setPeriod(StatsUtil.getPeriodAsInt(periodDate, periodtype));
    }

    /**
     * @return the time when statistic was stored.
     */
    public int getPeriod()
    {
        return getIntValue(Property.period.toString());
    }

    /**
     * @param period the time when statistic was stored.
     */
    public void setPeriod(int period)
    {
        setIntValue(Property.period.toString(), period);
    }

    /**
     * @return the counter of view action of this statistic.
     */
    public int getPageViews()
    {
        return getIntValue(Property.pageViews.toString());
    }

    /**
     * @param pageViews the counter of view action of this statistic.
     */
    public void setPageViews(int pageViews)
    {
        setIntValue(Property.pageViews.toString(), pageViews);
    }

    /**
     * Add 1 to the counter of view action of this statistic.
     */
    public void incPageViews()
    {
        setIntValue(Property.pageViews.toString(), getPageViews() + 1);
    }

    @Override
    protected String getLocalKey() {
        StringBuilder sb = new StringBuilder(64);

        // The R40000XWIKI6990DataMigration use a stubbed class to provide the above two values. If the ids depends
        // on other non-static requirements, it should be adapted accordingly.
        String name = getName();
        int nb = getNumber();

        if (!StringUtils.isEmpty(name)) {
            // TODO: Refactor to get the original reference without this resolve.
            EntityReference ref = resolver.resolve(name, EntityType.DOCUMENT, EMPTY_REF);
            if (ref.getName().equals(EMPTY_REF.getName())) {
                ref = ref.getParent();
            }
            sb.append(getLocalUidStringEntityReferenceSerializer().serialize(ref));
        }
        
        if (nb > 0) {<commit_msg>XWIKI-7791 IDs of stats objects are not properly computed and cause duplicated primary key issues during migration to 4.0<commit_after>/*
 * 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.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.objects.BaseCollection;
import com.xpn.xwiki.objects.BaseProperty;
import com.xpn.xwiki.objects.PropertyInterface;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.PropertyClass;
import com.xpn.xwiki.stats.impl.StatsUtil.PeriodType;
import com.xpn.xwiki.web.Utils;

/**
 * Base class for all stored statistics object.
 * 
 * @version $Id$
 */
public class XWikiStats extends BaseCollection
{
    /**
     * The properties of statistics object.
     * 
     * @version $Id$
     */
    public enum Property
    {
        /**
         * The name of the object database property <code>period</code>.
         */
        period,

        /**
         * The name of the object database property <code>pageViews</code>.
         */
        pageViews
    }

    /**
     * The name of the XML node <code>object</code>.
     */
    private static final String XMLNODE_OBJECT = "object";

    /**
     * The name of the XML node <code>name</code>.
     */
    private static final String XMLNODE_NAME = "name";

    /**
     * The name of the XML node <code>number</code>.
     */
    private static final String XMLNODE_NUMBER = "number";

    /**
     * The name of the XML node <code>className</code>.
     */
    private static final String XMLNODE_CLASSNAME = "className";

    /**
     * The name of the XML node <code>property</code>.
     */
    private static final String XMLNODE_PROPERTY = "property";

    /** Override default page name to easily detect it after resolution. */
    private static final EntityReference EMPTY_REF = new EntityReference("$$EMPTY$$", EntityType.DOCUMENT);

    /** Resolve names into reference for uid string serialization. */
    private final EntityReferenceResolver<String> resolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING);

    /**
     * Default constructor.
     */
    public XWikiStats()
    {
    }

    /**
     * @param periodDate the period date.
     * @param periodtype the period type.
     */
    public XWikiStats(Date periodDate, PeriodType periodtype)
    {
        setPeriod(StatsUtil.getPeriodAsInt(periodDate, periodtype));
    }

    /**
     * @return the time when statistic was stored.
     */
    public int getPeriod()
    {
        return getIntValue(Property.period.toString());
    }

    /**
     * @param period the time when statistic was stored.
     */
    public void setPeriod(int period)
    {
        setIntValue(Property.period.toString(), period);
    }

    /**
     * @return the counter of view action of this statistic.
     */
    public int getPageViews()
    {
        return getIntValue(Property.pageViews.toString());
    }

    /**
     * @param pageViews the counter of view action of this statistic.
     */
    public void setPageViews(int pageViews)
    {
        setIntValue(Property.pageViews.toString(), pageViews);
    }

    /**
     * Add 1 to the counter of view action of this statistic.
     */
    public void incPageViews()
    {
        setIntValue(Property.pageViews.toString(), getPageViews() + 1);
    }

    @Override
    protected String getLocalKey() {
        StringBuilder sb = new StringBuilder(64);

        // The R40000XWIKI6990DataMigration use a stubbed class to provide the above two values. If the ids depends
        // on other non-static requirements, it should be adapted accordingly.
        String name = getName();
        int nb = getNumber();

        if (!StringUtils.isEmpty(name)) {
            // TODO: Refactor to get the original reference without this resolve.
            EntityReference ref = resolver.resolve(name, EntityType.DOCUMENT, EMPTY_REF);
            if (ref.getName().equals(EMPTY_REF.getName())) {
                ref = ref.getParent();
            }
            sb.append(getLocalUidStringEntityReferenceSerializer().serialize(ref));
        }

        // if number used, serialize it as well. It may happened that the hash is 0, but this is really unlikely
        // and it will not hurt anyway.
        if (nb != 0) {
{'fraction_non_alphanumeric': 0.09655366173440719, 'fraction_numerical': 0.006274583255291253, 'mean_word_length': 2.717020536025061, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3146285', 'n_tokens_mistral': 2979, 'n_tokens_neox': 2885, 'n_words': 980}