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.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.UserLevel; import org.libreplan.business.users.entities.UserLevel; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * Hibernate DAO for the <code>UserLevel</code> entity. * * @author Dergen Lee * */ @Repository public class UserLevelDAO extends GenericDAOHibernate<UserLevel, Long> implements IUserLevelDAO { @Override public UserLevel findByUser(User user) { return findByUser(user, LocalDate.now()); } @Override @SuppressWarnings("unchecked") public UserLevel findByUser(User user, LocalDate date) { Criteria c = getSession().createCriteria(UserLevel.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<UserLevel> lvls = c.list(); return lvls.size() == 0 ? null : lvls.get(0); } @Override @SuppressWarnings("unchecked") public List<UserLevel> getLevelHistories(User user) { List<UserLevel> result; if (user != null) { Criteria c = getSession().createCriteria(UserLevel.class).add(Restrictions.eq("user", user)) .addOrder(Order.desc("fromDate")); result = c.list(); } else { result = new LinkedList<UserLevel>(); } return result; } @Override public int findLevelOf(User user) { return findLevelOf(user, LocalDate.now()); } @Override @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW) public int findLevelOf(User user, LocalDate date) { UserLevel ul = findByUser(user, date); return (ul == null || ul.getLevel() == null) ? UserLevel.LOWESTLEVEL : ul.getLevel(); } }