<commit_before>package org.navalplanner.business.planner.daos;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.navalplanner.business.common.daos.GenericDAOHibernate;
import org.navalplanner.business.planner.entities.GenericResourceAllocation;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.Resource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

/**
 * DAO for {@ResourceAllocation}
 * @author <NAME> <<EMAIL>>
 */
@Repository
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class ResourceAllocationDAO extends
        GenericDAOHibernate<ResourceAllocation, Long> implements
        IResourceAllocationDAO {

    @Override
    public List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
            List<Resource> resources) {
        List<ResourceAllocation<?>> result = new ArrayList<ResourceAllocation<?>>();
        result.addAll(findSpecificAllocationsRelatedTo(resources));
        result.addAll(findGenericAllocationsFor(resources));
        return result;
    }

    private List<GenericResourceAllocation> findGenericAllocationsFor(
            List<Resource> resources) {
        return (List<GenericResourceAllocation>) getSession().createCriteria(
                GenericResourceAllocation.class).setResultTransformer(
                Criteria.DISTINCT_ROOT_ENTITY).createCriteria(
                "genericDayAssignments").add(
                Restrictions.in("resource", resources)).list();
    }

    private List<SpecificResourceAllocation> findSpecificAllocationsRelatedTo(
            List<Resource> resources) {
        return (List<SpecificResourceAllocation>) getSession().createCriteria(
                SpecificResourceAllocation.class).add(
                Restrictions.in("resource", resources)).list();
    }

    @Override
    public List<ResourceAllocation<?>> findAllocationsRelatedTo(
            Resource resource) {
        return findAllocationsRelatedToAnyOf(Arrays.asList(resource));
    }

    @Override
    public Map<Criterion, List<GenericResourceAllocation>> findGenericAllocationsByCriterion() {
        List<Object> results = getSession()
                .createQuery(
                "select generic, criterion "
                        + "from GenericResourceAllocation as generic "
                        + "join generic.criterions as criterion")
                .list();
        return byCriterion(results);
    }

    private Map<Criterion, List<GenericResourceAllocation>> byCriterion(
            List<Object> results) {

        Map<Criterion, List<GenericResourceAllocation>> result = new HashMap<Criterion, List<GenericResourceAllocation>>();
        for (Object row : results) {
            GenericResourceAllocation allocation = getAllocation(row);
            Criterion criterion = getCriterion(row);
            if (!result.containsKey(criterion)) {
                result.put(criterion,
                        new ArrayList<GenericResourceAllocation>());
            }
            result.get(criterion).add(allocation);
        }
        return result;
    }
<commit_msg>ItEr27S07CUVistaRecursosTempoPorProxectoItEr26S08: Supressing unchecked warnings. The castings are correct.<commit_after>package org.navalplanner.business.planner.daos;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.navalplanner.business.common.daos.GenericDAOHibernate;
import org.navalplanner.business.planner.entities.GenericResourceAllocation;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.Resource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

/**
 * DAO for {@ResourceAllocation}
 * @author <NAME> <<EMAIL>>
 */
@Repository
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class ResourceAllocationDAO extends
        GenericDAOHibernate<ResourceAllocation, Long> implements
        IResourceAllocationDAO {

    @Override
    public List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
            List<Resource> resources) {
        List<ResourceAllocation<?>> result = new ArrayList<ResourceAllocation<?>>();
        result.addAll(findSpecificAllocationsRelatedTo(resources));
        result.addAll(findGenericAllocationsFor(resources));
        return result;
    }

    @SuppressWarnings("unchecked")
    private List<GenericResourceAllocation> findGenericAllocationsFor(
            List<Resource> resources) {
        return (List<GenericResourceAllocation>) getSession().createCriteria(
                GenericResourceAllocation.class).setResultTransformer(
                Criteria.DISTINCT_ROOT_ENTITY).createCriteria(
                "genericDayAssignments").add(
                Restrictions.in("resource", resources)).list();
    }

    @SuppressWarnings("unchecked")
    private List<SpecificResourceAllocation> findSpecificAllocationsRelatedTo(
            List<Resource> resources) {
        return (List<SpecificResourceAllocation>) getSession().createCriteria(
                SpecificResourceAllocation.class).add(
                Restrictions.in("resource", resources)).list();
    }

    @Override
    public List<ResourceAllocation<?>> findAllocationsRelatedTo(
            Resource resource) {
        return findAllocationsRelatedToAnyOf(Arrays.asList(resource));
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<Criterion, List<GenericResourceAllocation>> findGenericAllocationsByCriterion() {
        List<Object> results = getSession()
                .createQuery(
                "select generic, criterion "
                        + "from GenericResourceAllocation as generic "
                        + "join generic.criterions as criterion")
                .list();
        return byCriterion(results);
    }

    private Map<Criterion, List<GenericResourceAllocation>> byCriterion(
            List<Object> results) {

        Map<Criterion, List<GenericResourceAllocation>> result = new HashMap<Criterion, List<GenericResourceAllocation>>();
        for (Object row : results) {
            GenericResourceAllocation allocation = getAllocation(row);
            Criterion criterion = getCriterion(row);
            if (!result.containsKey(criterion)) {
                result.put(criterion,
                        new ArrayList<GenericResourceAllocation>());
            }
            result.get(criterion).add(allocation);
        }
        return result;
    }

{'fraction_non_alphanumeric': 0.09349649388147944, 'fraction_numerical': 0.001099958751546817, 'mean_word_length': 3.411158277744087, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4159655', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1837, 'n_words': 308}