<commit_before>/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolóxico de Galicia
 * Copyright (C) 2010-2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.business.workreports.daos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.libreplan.business.common.daos.IntegrationEntityDAO;
import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.business.reports.dtos.WorkReportLineDTO;
import org.libreplan.business.resources.entities.Resource;
import org.libreplan.business.workreports.entities.WorkReportLine;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

/**
 * Dao for {@link WorkReportLineDAO}
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

@Repository
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class WorkReportLineDAO extends IntegrationEntityDAO<WorkReportLine>
        implements IWorkReportLineDAO {

    @SuppressWarnings("unchecked")
    @Override
    public List<WorkReportLine> findByOrderElement(OrderElement orderElement){
        Criteria c = getSession().createCriteria(WorkReportLine.class).createCriteria("orderElement");
        c.add(Restrictions.idEq(orderElement.getId()));
        return (List<WorkReportLine>) c.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<WorkReportLineDTO> findByOrderElementGroupByResourceAndHourTypeAndDate(
            OrderElement orderElement) {

        String strQuery = "SELECT new org.libreplan.business.reports.dtos.WorkReportLineDTO(wrl.resource, wrl.typeOfWorkHours, wrl.date, SUM(wrl.effort)) "
                + "FROM WorkReportLine wrl "
                + "LEFT OUTER JOIN wrl.orderElement orderElement "
                + "WHERE orderElement = :orderElement "
                + "GROUP BY wrl.resource, wrl.typeOfWorkHours, wrl.date "
                + "ORDER BY wrl.resource, wrl.typeOfWorkHours, wrl.date";

        // Set parameters
        Query query = getSession().createQuery(strQuery);
        query.setParameter("orderElement", orderElement);

        return (List<WorkReportLineDTO>) query.list();
    }

    @Override
    public List<WorkReportLine> findByOrderElementAndChildren(
            OrderElement orderElement) {
        if (orderElement.isNewObject()) {
            return new ArrayList<WorkReportLine>();
        }
        return findByOrderElementAndChildren(orderElement, false);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<WorkReportLine> findByOrderElementAndChildren(OrderElement orderElement, boolean sortByDate) {
        // Create collection with current orderElement and all its children
        Collection<OrderElement> orderElements = orderElement.getAllChildren();
        orderElements.add(orderElement);

        // Prepare criteria
        final Criteria criteria = getSession().createCriteria(WorkReportLine.class);
        criteria.add(Restrictions.in("orderElement", orderElements));
        if (sortByDate) {
            criteria.addOrder(org.hibernate.criterion.Order.asc("date"));
        }
        return criteria.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<WorkReportLine> findFilteredByDate(Date start, Date end) {
        Criteria criteria  = getSession().createCriteria(WorkReportLine.class);
        if(start != null) {
            criteria.add(Restrictions.ge("date", start));
        }
        if(end != null) {
            criteria.add(Restrictions.le("date", end));
        }
        return criteria.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<WorkReportLine> findByResources(List<Resource> resourcesList) {
        if (resourcesList.isEmpty()) {
            return Collections.emptyList();
        }
        return getSession().createCriteria(WorkReportLine.class).add(
                Restrictions.in("resource", resourcesList)).list();
    }

}
<commit_msg>Set a method transactional.<commit_after>/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolóxico de Galicia
 * Copyright (C) 2010-2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.business.workreports.daos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.libreplan.business.common.daos.IntegrationEntityDAO;
import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.business.reports.dtos.WorkReportLineDTO;
import org.libreplan.business.resources.entities.Resource;
import org.libreplan.business.workreports.entities.WorkReportLine;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Dao for {@link WorkReportLineDAO}
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

@Repository
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class WorkReportLineDAO extends IntegrationEntityDAO<WorkReportLine>
        implements IWorkReportLineDAO {

    @SuppressWarnings("unchecked")
    @Override
    public List<WorkReportLine> findByOrderElement(OrderElement orderElement){
        Criteria c = getSession().createCriteria(WorkReportLine.class).createCriteria("orderElement");
        c.add(Restrictions.idEq(orderElement.getId()));
        return (List<WorkReportLine>) c.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<WorkReportLineDTO> findByOrderElementGroupByResourceAndHourTypeAndDate(
            OrderElement orderElement) {

        String strQuery = "SELECT new org.libreplan.business.reports.dtos.WorkReportLineDTO(wrl.resource, wrl.typeOfWorkHours, wrl.date, SUM(wrl.effort)) "
                + "FROM WorkReportLine wrl "
                + "LEFT OUTER JOIN wrl.orderElement orderElement "
                + "WHERE orderElement = :orderElement "
                + "GROUP BY wrl.resource, wrl.typeOfWorkHours, wrl.date "
                + "ORDER BY wrl.resource, wrl.typeOfWorkHours, wrl.date";

        // Set parameters
        Query query = getSession().createQuery(strQuery);
        query.setParameter("orderElement", orderElement);

        return (List<WorkReportLineDTO>) query.list();
    }

    @Override
    public List<WorkReportLine> findByOrderElementAndChildren(
            OrderElement orderElement) {
        if (orderElement.isNewObject()) {
            return new ArrayList<WorkReportLine>();
        }
        return findByOrderElementAndChildren(orderElement, false);
    }

    @SuppressWarnings("unchecked")
    @Override
    @Transactional(readOnly=true)
    public List<WorkReportLine> findByOrderElementAndChildren(OrderElement orderElement, boolean sortByDate) {
        // Create collection with current orderElement and all its children
        Collection<OrderElement> orderElements = orderElement.getAllChildren();
        orderElements.add(orderElement);

        // Prepare criteria
        final Criteria criteria = getSession().createCriteria(WorkReportLine.class);
        criteria.add(Restrictions.in("orderElement", orderElements));
        if (sortByDate) {
            criteria.addOrder(org.hibernate.criterion.Order.asc("date"));
        }
        return criteria.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<WorkReportLine> findFilteredByDate(Date start, Date end) {
        Criteria criteria  = getSession().createCriteria(WorkReportLine.class);
        if(start != null) {
            criteria.add(Restrictions.ge("date", start));
        }
        if(end != null) {
            criteria.add(Restrictions.le("date", end));
        }
        return criteria.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<WorkReportLine> findByResources(List<Resource> resourcesList) {
        if (resourcesList.isEmpty()) {
            return Collections.emptyList();
        }
        return getSession().createCriteria(WorkReportLine.class).add(
                Restrictions.in("resource", resourcesList)).list();
    }

}

{'fraction_non_alphanumeric': 0.09765351972041937, 'fraction_numerical': 0.0033949076385421865, 'mean_word_length': 3.4754244861483468, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '4269177', 'n_tokens_mistral': 2767, 'n_tokens_neox': 2710, 'n_words': 654}