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.web.users.hierarchy; import static org.libreplan.web.I18nHelper._; import static org.libreplan.business.common.exceptions.ValidationException.invalidValue; import java.util.List; import org.apache.commons.lang.Validate; import org.joda.time.LocalDate; import org.libreplan.business.common.exceptions.InstanceNotFoundException; import org.libreplan.business.common.exceptions.ValidationException; import org.libreplan.business.departments.daos.IDepartmentDAO; import org.libreplan.business.users.daos.IUserDAO; import org.libreplan.business.users.daos.IUserCoachDAO; import org.libreplan.business.users.entities.User; import org.libreplan.business.users.entities.UserCoach; import org.libreplan.web.common.concurrentdetection.OnConcurrentModification; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /* * Model for UI operations related to {@link UserCoach} * * @author Dergen Lee */ @Service @Scope(BeanDefinition.SCOPE_PROTOTYPE) @OnConcurrentModification(goToPage = "/userCoach/coach.zul") public class UserCoachModel implements IUserCoachModel { private UserCoach coach; @Autowired private IUserCoachDAO userCoachDAO; @Autowired private IUserDAO userDAO; @Autowired private IDepartmentDAO departmentDAO; @Override @Transactional public void confirmDelete(UserCoach coach) { try { userCoachDAO.remove(coach.getId()); } catch (InstanceNotFoundException e) { throw new RuntimeException(e); } } @Override public UserCoach getUserCoach() { return coach; } @Override @Transactional public void confirmSave() throws ValidationException { userCoachDAO.save(coach); } @Override @Transactional public void save(UserCoach uh) { validate(coach); userCoachDAO.save(uh); userCoachDAO.flush(); } private void validate(UserCoach coach) { User ch = coach.getCoach(); if (ch == null) { throw new ValidationException(invalidValue(_("Coach cannot be empty"), "name", coach.getId(), coach)); } User user = coach.getUser(); if (userCoachDAO.isCoach(user, ch, coach.getFromDate())) { throw new ValidationException(invalidValue( _("{0} is coach of {1}, cannot be apprentice of {1}", user.getFullName(), ch.getFullName()), "coach", ch.getId(), coach)); } } @Override public void initCreate(User user) { coach = UserCoach.create(); coach.setUser(user); coach.setFromDate(LocalDate.now()); } @Override @Transactional(readOnly = true) public void initEdit(UserCoach coach) { Validate.notNull(coach); this.coach = getFromDB(coach); } private UserCoach getFromDB(UserCoach coach) { if (!coach.isNewObject()) { try { return userCoachDAO.find(coach.getId()); } catch (InstanceNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } } return coach; } /* * @Override * * @Transactional(readOnly = true) public List<UserCoach> * getUserHierachies() { return userCoachDAO.list(UserCoach.class); * } */ @Override public UserCoach findUserCoach(User user) { return findUserCoach(user, LocalDate.now()); } @Override @Transactional public UserCoach findUserCoach(User user, LocalDate date) { return userCoachDAO.findByUser(user, date); } @Override @Transactional public List<UserCoach> getCoachHistories(User user) { return userCoachDAO.getCoachHistories(user); } }