<commit_before>/*
 * The Gemma project.
 * 
 * Copyright (c) 2006-2007 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package ubic.gemma.model.common.description;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import ubic.gemma.model.association.Gene2GOAssociationImpl;
import ubic.gemma.model.expression.experiment.ExperimentalFactorImpl;

/**
 * @author Luke
 * @author Paul
 * @see ubic.gemma.model.common.description.Characteristic
 * @version $Id$
 */
public class CharacteristicDaoImpl extends ubic.gemma.model.common.description.CharacteristicDaoBase {

    private static final int BATCH_SIZE = 1000;

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByParentClass(java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Map handleFindByParentClass( Class parentClass ) throws Exception {
        String field = "characteristics";
        if ( parentClass == ExperimentalFactorImpl.class )
            field = "category";
        else if ( parentClass == Gene2GOAssociationImpl.class ) field = "ontologyEntry";

        final String queryString = "select parent, char from " + parentClass.getSimpleName() + " as parent "
                + "inner join parent." + field + " as char";

        Map charToParent = new HashMap<Characteristic, Object>();
        for ( Object o : getHibernateTemplate().find( queryString ) ) {
            Object[] row = ( Object[] ) o;
            charToParent.put( row[1], row[0] );
        }
        return charToParent;
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByUri(java.util.Collection)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection<Characteristic> handleFindByUri( Collection uris ) throws Exception {
        int batchSize = 1000; // to avoid HQL parser barfing
        Collection<String> batch = new HashSet<String>();
        Collection<Characteristic> results = new HashSet<Characteristic>();
        final String queryString = "from VocabCharacteristicImpl where valueUri in (:uris)";

        for ( String uri : ( Collection<String> ) uris ) {
            batch.add( uri );
            if ( batch.size() >= batchSize ) {
                results.addAll( getHibernateTemplate().findByNamedParam( queryString, "uris", batch ) );
                batch.clear();
            }
        }
        if ( batch.size() > 0 ) {
            results.addAll( getHibernateTemplate().findByNamedParam( queryString, "uris", batch ) );
        }
        return results;
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByUri(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection<Characteristic> handleFindByUri( String searchString ) throws Exception {
        final String queryString = "select char from VocabCharacteristicImpl as char where  char.valueUri = :search";
        return getHibernateTemplate().findByNamedParam( queryString, "search", searchString );
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByvalue(java.lang.String)
     */
    @Override
    protected Collection handleFindByValue( String search ) throws Exception {
        final String queryString = "select distinct char from CharacteristicImpl as char where char.value like :search";
        return getHibernateTemplate().findByNamedParam( queryString, "search", search );
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindParents(java.lang.Class,
     * java.util.Collection)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Map handleGetParents( Class parentClass, Collection characteristics ) throws Exception {
        Collection<Characteristic> batch = new HashSet<Characteristic>();
        Map<Characteristic, Object> charToParent = new HashMap<Characteristic, Object>();
        if ( characteristics == null || characteristics.size() == 0 ) {
            return charToParent;
        }
        for ( Characteristic c : ( Collection<Characteristic> ) characteristics ) {
            batch.add( c );
            if ( batch.size() == BATCH_SIZE ) {
                batchGetParents( parentClass, batch, charToParent );
                batch.clear();
            }
        }
        batchGetParents( parentClass, batch, charToParent );
        return charToParent;
    }

    /**
     * @param parentClass
     * @param characteristics
     * @param charToParent
     */
    private void batchGetParents( Class parentClass, Collection<Characteristic> characteristics,
            Map<Characteristic, Object> charToParent ) {
        if ( characteristics.isEmpty() ) return;

        String field = "characteristics";
        if ( parentClass == ExperimentalFactorImpl.class )
            field = "category";
        else if ( parentClass == Gene2GOAssociationImpl.class ) field = "ontologyEntry";

        final String queryString = "select parent, char from " + parentClass.getSimpleName() + " as parent "
                + "inner join parent." + field + " as char " + "where char in (:chars)";

        for ( Object o : getHibernateTemplate().findByNamedParam( queryString, "chars", characteristics ) ) {
            Object[] row = ( Object[] ) o;
            charToParent.put( ( Characteristic ) row[1], row[0] );
        }
    }

}<commit_msg>Add wildcard to 'like' in where clause<commit_after>/*
 * The Gemma project.
 * 
 * Copyright (c) 2006-2007 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package ubic.gemma.model.common.description;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import ubic.gemma.model.association.Gene2GOAssociationImpl;
import ubic.gemma.model.expression.experiment.ExperimentalFactorImpl;

/**
 * @author Luke
 * @author Paul
 * @see ubic.gemma.model.common.description.Characteristic
 * @version $Id$
 */
public class CharacteristicDaoImpl extends ubic.gemma.model.common.description.CharacteristicDaoBase {

    private static final int BATCH_SIZE = 1000;

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByParentClass(java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Map handleFindByParentClass( Class parentClass ) throws Exception {
        String field = "characteristics";
        if ( parentClass == ExperimentalFactorImpl.class )
            field = "category";
        else if ( parentClass == Gene2GOAssociationImpl.class ) field = "ontologyEntry";

        final String queryString = "select parent, char from " + parentClass.getSimpleName() + " as parent "
                + "inner join parent." + field + " as char";

        Map charToParent = new HashMap<Characteristic, Object>();
        for ( Object o : getHibernateTemplate().find( queryString ) ) {
            Object[] row = ( Object[] ) o;
            charToParent.put( row[1], row[0] );
        }
        return charToParent;
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByUri(java.util.Collection)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection<Characteristic> handleFindByUri( Collection uris ) throws Exception {
        int batchSize = 1000; // to avoid HQL parser barfing
        Collection<String> batch = new HashSet<String>();
        Collection<Characteristic> results = new HashSet<Characteristic>();
        final String queryString = "from VocabCharacteristicImpl where valueUri in (:uris)";

        for ( String uri : ( Collection<String> ) uris ) {
            batch.add( uri );
            if ( batch.size() >= batchSize ) {
                results.addAll( getHibernateTemplate().findByNamedParam( queryString, "uris", batch ) );
                batch.clear();
            }
        }
        if ( batch.size() > 0 ) {
            results.addAll( getHibernateTemplate().findByNamedParam( queryString, "uris", batch ) );
        }
        return results;
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByUri(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection<Characteristic> handleFindByUri( String searchString ) throws Exception {
        final String queryString = "select char from VocabCharacteristicImpl as char where  char.valueUri = :search";
        return getHibernateTemplate().findByNamedParam( queryString, "search", searchString );
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindByvalue(java.lang.String)
     */
    @Override
    protected Collection<Characteristic> handleFindByValue( String search ) throws Exception {
        final String queryString = "select distinct char from CharacteristicImpl as char where char.value like :search";
        return getHibernateTemplate().findByNamedParam( queryString, "search", search + "%" );
    }

    /*
     * (non-Javadoc)
     * @see ubic.gemma.model.common.description.CharacteristicDaoBase#handleFindParents(java.lang.Class,
     * java.util.Collection)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Map handleGetParents( Class parentClass, Collection characteristics ) throws Exception {
        Collection<Characteristic> batch = new HashSet<Characteristic>();
        Map<Characteristic, Object> charToParent = new HashMap<Characteristic, Object>();
        if ( characteristics == null || characteristics.size() == 0 ) {
            return charToParent;
        }
        for ( Characteristic c : ( Collection<Characteristic> ) characteristics ) {
            batch.add( c );
            if ( batch.size() == BATCH_SIZE ) {
                batchGetParents( parentClass, batch, charToParent );
                batch.clear();
            }
        }
        batchGetParents( parentClass, batch, charToParent );
        return charToParent;
    }

    /**
     * @param parentClass
     * @param characteristics
     * @param charToParent
     */
    private void batchGetParents( Class parentClass, Collection<Characteristic> characteristics,
            Map<Characteristic, Object> charToParent ) {
        if ( characteristics.isEmpty() ) return;

        String field = "characteristics";
        if ( parentClass == ExperimentalFactorImpl.class )
            field = "category";
        else if ( parentClass == Gene2GOAssociationImpl.class ) field = "ontologyEntry";

        final String queryString = "select parent, char from " + parentClass.getSimpleName() + " as parent "
                + "inner join parent." + field + " as char " + "where char in (:chars)";

        for ( Object o : getHibernateTemplate().findByNamedParam( queryString, "chars", characteristics ) ) {
            Object[] row = ( Object[] ) o;
            charToParent.put( ( Characteristic ) row[1], row[0] );
        }
    }

}
{'fraction_non_alphanumeric': 0.10432428127737517, 'fraction_numerical': 0.004618937644341801, 'mean_word_length': 2.90121155638397, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 38, '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': '5221356', 'n_tokens_mistral': 3395, 'n_tokens_neox': 3325, 'n_words': 898}