Java tutorial
/* * 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.UserSuperior; import org.springframework.stereotype.Repository; /** * Hibernate DAO for the <code>UserSuperior</code> entity. * * @author Dergen Lee * */ @Repository public class UserSuperiorDAO extends GenericDAOHibernate<UserSuperior, Long> implements IUserSuperiorDAO { @Override public List<User> findSubordinateOf(User user) { return findSubordinateOf(user, LocalDate.now()); } @Override @SuppressWarnings("unchecked") public List<User> findSubordinateOf(User user, LocalDate date) { Criteria c = getSession().createCriteria(UserSuperior.class).add(Restrictions.eq("superior", user)) .add(Restrictions.or(Restrictions.isNull("fromDate"), Restrictions.le("fromDate", date))) .add(Restrictions.or(Restrictions.isNull("toDate"), Restrictions.ge("toDate", date))); List<UserSuperior> uhs = c.list(); List<User> result = new ArrayList<User>(); for (UserSuperior uh : uhs) { result.add(uh.getUser()); } return result; } @Override public UserSuperior findByUser(User user) { return findByUser(user, LocalDate.now()); } @Override @SuppressWarnings("unchecked") public UserSuperior findByUser(User user, LocalDate date) { Criteria c = getSession().createCriteria(UserSuperior.class).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<UserSuperior> sups = c.list(); return sups.size() == 0 ? null : sups.get(0); } @Override @SuppressWarnings("unchecked") public List<UserSuperior> getSuperiorHistories(User user) { List<UserSuperior> result; if (user != null) { Criteria c = getSession().createCriteria(UserSuperior.class).add(Restrictions.eq("user", user)) .addOrder(Order.desc("fromDate")); result = c.list(); } else { result = new LinkedList<UserSuperior>(); } return result; } @Override public boolean isSuperior(User superior, User user, LocalDate date) { return isSuperior(superior, user, date, true); } @Override public boolean isSuperior(User superior, User user, LocalDate date, boolean includeSupSuperior) { if (superior == null || user == null) return false; UserSuperior uc = findByUser(user, date); if (uc == null) return false; if (uc.getSuperior().getId().equals(superior.getId())) return true; if (includeSupSuperior) return isSuperior(uc.getSuperior(), user, date); else return false; } }