<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 org.phenotips.measurements.internal;

import org.xwiki.component.annotation.Component;

import java.util.List;

import javax.inject.Named;
import javax.inject.Singleton;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.objects.FloatProperty;
import com.xpn.xwiki.objects.IntegerProperty;
import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
import com.xpn.xwiki.store.migration.DataMigrationException;
import com.xpn.xwiki.store.migration.XWikiDBVersion;
import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;

/**
 * Migration for PhenoTips issue #1428: Measurement age will now be stored as a float not an int.
 *
 * @version $Id$
 * @since 1.1RC1
 */
@Component
@Named("R54691Phenotips#1428")
@Singleton
public class R54691PhenoTips1428DataMigration extends AbstractHibernateDataMigration
{
    @Override
    public String getDescription()
    {
        return "Changing the measurement age property from an int to a float";
    }

    @Override
    public XWikiDBVersion getVersion()
    {
        return new XWikiDBVersion(54691);
    }

    @Override
    public void hibernateMigrate() throws DataMigrationException, XWikiException
    {
        getStore().executeWrite(getXWikiContext(), new MigrateAgePropertyCallback());
    }

    /**
     * Searches for all measurement objects and updates their age property to a float
     */
    private class MigrateAgePropertyCallback implements HibernateCallback<Object>
    {
        @Override
        public Object doInHibernate(Session session) throws HibernateException, XWikiException
        {
            String measurementsClassReference = "Phenotips.MeasurementsClass";
            Query q = session.createQuery("select p from " + IntegerProperty.class.getName() + " as p, BaseObject as o"
                + " where o.className='" + measurementsClassReference + "' and p.id.id=o.id and p.id.name='age'");
            @SuppressWarnings("unchecked")
            List<IntegerProperty> oldAgeProperties = q.list();
            for (IntegerProperty oldAgeProperty : oldAgeProperties) {
                FloatProperty newAgeProperty = new FloatProperty();
                newAgeProperty.setId(oldAgeProperty.getId());
                newAgeProperty.setName(oldAgeProperty.getName());
                newAgeProperty.setValue((float) oldAgeProperty.getValue());
                session.delete(oldAgeProperty);
                session.save(newAgeProperty);
            }
            return null;
        }
    }
}
<commit_msg>[misc] Fixed checkstyle<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 org.phenotips.measurements.internal;

import org.xwiki.component.annotation.Component;

import java.util.List;

import javax.inject.Named;
import javax.inject.Singleton;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.objects.FloatProperty;
import com.xpn.xwiki.objects.IntegerProperty;
import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
import com.xpn.xwiki.store.migration.DataMigrationException;
import com.xpn.xwiki.store.migration.XWikiDBVersion;
import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;

/**
 * Migration for PhenoTips issue #1428: Measurement age will now be stored as a float not an int.
 *
 * @version $Id$
 * @since 1.1RC1
 */
@Component
@Named("R54691Phenotips#1428")
@Singleton
public class R54691PhenoTips1428DataMigration extends AbstractHibernateDataMigration
{
    @Override
    public String getDescription()
    {
        return "Changing the measurement age property from an int to a float";
    }

    @Override
    public XWikiDBVersion getVersion()
    {
        return new XWikiDBVersion(54691);
    }

    @Override
    public void hibernateMigrate() throws DataMigrationException, XWikiException
    {
        getStore().executeWrite(getXWikiContext(), new MigrateAgePropertyCallback());
    }

    /**
     * Searches for all measurement objects and updates their age property to a float.
     */
    private class MigrateAgePropertyCallback implements HibernateCallback<Object>
    {
        @Override
        public Object doInHibernate(Session session) throws HibernateException, XWikiException
        {
            String measurementsClassReference = "Phenotips.MeasurementsClass";
            Query q = session.createQuery("select p from " + IntegerProperty.class.getName() + " as p, BaseObject as o"
                + " where o.className='" + measurementsClassReference + "' and p.id.id=o.id and p.id.name='age'");
            @SuppressWarnings("unchecked")
            List<IntegerProperty> oldAgeProperties = q.list();
            for (IntegerProperty oldAgeProperty : oldAgeProperties) {
                FloatProperty newAgeProperty = new FloatProperty();
                newAgeProperty.setId(oldAgeProperty.getId());
                newAgeProperty.setName(oldAgeProperty.getName());
                newAgeProperty.setValue((float) oldAgeProperty.getValue());
                session.delete(oldAgeProperty);
                session.save(newAgeProperty);
            }
            return null;
        }
    }
}

{'fraction_non_alphanumeric': 0.08212424285110578, 'fraction_numerical': 0.012114382307367234, 'mean_word_length': 3.4209215442092153, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6749965', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1887, 'n_words': 592}