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.Date; import java.util.LinkedList; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.libreplan.business.common.daos.GenericDAOHibernate; import org.libreplan.business.departments.entities.Department; import org.libreplan.business.users.entities.User; import org.libreplan.business.users.entities.UserDepartment; import org.springframework.stereotype.Repository; /** * Hibernate DAO for the <code>UserDepartment</code> entity. * * @author Dergen Lee * */ @Repository public class UserDepartmentDAO extends GenericDAOHibernate<UserDepartment, Long> implements IUserDepartmentDAO { @Override public List<User> findDepartmentMembers(Department dept) { return findDepartmentMembers(dept, LocalDate.now()); } @Override @SuppressWarnings("unchecked") public List<User> findDepartmentMembers(Department dept, LocalDate date) { Session session = getSession(); if (date == null) date = LocalDate.now(); Criteria c = session.createCriteria(UserDepartment.class).add(Restrictions.eq("dept", dept)) .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<UserDepartment> uhs = c.list(); List<User> result = new ArrayList<User>(); for (UserDepartment uh : uhs) { result.add(uh.getUser()); } return result; } @Override public List<UserDepartment> findUserDepartments(Department dept) { return findUserDepartments(dept, LocalDate.now()); } @Override public List<UserDepartment> findUserDepartments(Department dept, LocalDate date) { Session session = getSession(); if (date == null) date = LocalDate.now(); Criteria c = session.createCriteria(UserDepartment.class).add(Restrictions.eq("dept", dept)) .add(Restrictions.or(Restrictions.isNull("fromDate"), Restrictions.le("fromDate", date))) .add(Restrictions.or(Restrictions.isNull("toDate"), Restrictions.ge("toDate", date))) .addOrder(Order.desc("fromDate")); return c.list(); } @Override public UserDepartment findByUser(User user) { return findByUser(user, LocalDate.now()); } @Override @SuppressWarnings("unchecked") public UserDepartment findByUser(User user, LocalDate date) { Criteria c = getSession().createCriteria(UserDepartment.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<UserDepartment> uds = c.list(); return uds.size() == 0 ? null : uds.get(0); } @Override @SuppressWarnings("unchecked") public List<UserDepartment> getDepartmentHistories(User user) { List<UserDepartment> result; if (user != null) { Criteria c = getSession().createCriteria(UserDepartment.class); c.add(Restrictions.eq("user", user)); c.addOrder(Order.desc("fromDate")); result = c.list(); } else { result = new LinkedList<UserDepartment>(); } return result; } @Override public boolean belongTo(Department dept, User user, LocalDate date) { return belongTo(dept, user, date, true); } @Override public boolean belongTo(Department dept, User user, LocalDate date, boolean includeSupDepts) { if (dept == null || user == null) return false; UserDepartment ud = findByUser(user, date); if (ud == null) return false; if (ud.getDept().getId().equals(dept.getId())) return true; if (includeSupDepts) { return belongTo(dept.getParent(), user, date, includeSupDepts); } else { return false; } } }