org.libreplan.business.users.daos.UserCoachDAO.java Source code

Java tutorial

Introduction

Here is the source code for org.libreplan.business.users.daos.UserCoachDAO.java

Source

/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundacin para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolxico de Galicia
 * Copyright (C) 2010-2012 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.users.daos;

import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.libreplan.business.common.daos.GenericDAOHibernate;
import org.libreplan.business.users.entities.User;
import org.libreplan.business.users.entities.UserCoach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Hibernate DAO for the <code>UserCoach</code> entity.
 *
 * @author Dergen Lee
 *
 */
@Repository
public class UserCoachDAO extends GenericDAOHibernate<UserCoach, Long> implements IUserCoachDAO {

    @Autowired
    private IUserDAO userDAO;

    @Override
    public List<User> findApprenticeOf(User user) {
        return findApprenticeOf(user, LocalDate.now());
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<User> findApprenticeOf(User user, LocalDate date) {
        Criteria c = getSession().createCriteria(UserCoach.class);
        c.add(Restrictions.eq("coach", user))
                .add(Restrictions.or(Restrictions.isNull("fromDate"), Restrictions.le("fromDate", date)))
                .add(Restrictions.or(Restrictions.isNull("toDate"), Restrictions.ge("toDate", date)));
        List<UserCoach> uhs = c.list();
        List<User> result = new ArrayList<User>();
        for (UserCoach uh : uhs) {
            result.add(uh.getUser());
        }
        return result;
    }

    @Override
    public UserCoach findByUser(User user) {
        return findByUser(user, LocalDate.now());
    }

    @Override
    @SuppressWarnings("unchecked")
    public UserCoach findByUser(User user, LocalDate date) {
        Criteria c = getSession().createCriteria(UserCoach.class);
        c.add(Restrictions.eq("user", user))
                .add(Restrictions.or(Restrictions.isNull("fromDate"), Restrictions.le("fromDate", date)))
                .add(Restrictions.or(Restrictions.isNull("toDate"), Restrictions.ge("toDate", date)))
                .addOrder(Order.desc("fromDate"));
        List<UserCoach> sups = c.list();
        return sups.size() == 0 ? null : sups.get(0);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<UserCoach> getCoachHistories(User user) {
        List<UserCoach> result;
        if (user != null) {
            Criteria c = getSession().createCriteria(UserCoach.class);
            c.add(Restrictions.eq("user", user));
            c.addOrder(Order.desc("fromDate"));
            result = c.list();
        } else {
            result = new LinkedList<UserCoach>();
        }
        return result;
    }

    @Override
    public boolean isCoach(User coach, User user, LocalDate date) {
        return isCoach(coach, user, date, true);
    }

    @Override
    public boolean isCoach(User coach, User user, LocalDate date, boolean includeSupCoach) {
        if (coach == null || user == null)
            return false;
        UserCoach uc = findByUser(user, date);
        if (uc == null)
            return false;
        if (uc.getCoach().getId().equals(coach.getId()))
            return true;

        return isCoach(uc.getCoach(), user, date);
    }
}