From dd501faba847e6cb884b2c177f5ebe7ae406b9f8 Mon Sep 17 00:00:00 2001 From: Manuel Rego Casasnovas Date: Wed, 9 May 2012 08:51:16 +0200 Subject: [PATCH] Move logic to remove order authorizations when removing a user to UserDAO * Create a new method UserDao.remove(User user) that is in charge of remove OrderAuthorizations and the User too * Use this method from UserModel FEA: ItEr76S27ResourceBinding --- .../business/users/daos/IUserDAO.java | 8 ++++-- .../business/users/daos/UserDAO.java | 18 +++++++++++-- .../org/libreplan/web/users/UserModel.java | 25 ++++--------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/libreplan-business/src/main/java/org/libreplan/business/users/daos/IUserDAO.java b/libreplan-business/src/main/java/org/libreplan/business/users/daos/IUserDAO.java index d9b6aefa2..cf78cfb0b 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/users/daos/IUserDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/users/daos/IUserDAO.java @@ -83,8 +83,6 @@ public interface IUserDAO extends IGenericDAO{ public List findByLastConnectedScenario(Scenario scenario); - List getOrderAuthorizationsByUser(User user); - /** * Returns the list of {@link User}s not bound to any {@link Worker} yet, * plus the {@link User} bound to the {@link Worker} specified as parameter @@ -94,4 +92,10 @@ public interface IUserDAO extends IGenericDAO{ User findOnAnotherTransaction(Long id); + /** + * Removes all the {@link OrderAuthorization}s of this {@link User} and then + * removes the {@link User} too + */ + void remove(User user) throws InstanceNotFoundException; + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/users/daos/UserDAO.java b/libreplan-business/src/main/java/org/libreplan/business/users/daos/UserDAO.java index 680b38d12..9280f03c7 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/users/daos/UserDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/users/daos/UserDAO.java @@ -33,6 +33,7 @@ import org.libreplan.business.scenarios.entities.Scenario; import org.libreplan.business.users.entities.OrderAuthorization; import org.libreplan.business.users.entities.User; import org.libreplan.business.users.entities.UserOrderAuthorization; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -48,6 +49,9 @@ import org.springframework.transaction.annotation.Transactional; public class UserDAO extends GenericDAOHibernate implements IUserDAO { + @Autowired + private IOrderAuthorizationDAO orderAuthorizationDAO; + @Override @Transactional(readOnly = true) public User findByLoginName(String loginName) @@ -123,8 +127,7 @@ public class UserDAO extends GenericDAOHibernate return c.list(); } - @Override - public List getOrderAuthorizationsByUser(User user) { + private List getOrderAuthorizationsByUser(User user) { List orderAuthorizations = getSession() .createCriteria(UserOrderAuthorization.class) .add(Restrictions.eq("user", user)).list(); @@ -159,4 +162,15 @@ public class UserDAO extends GenericDAOHibernate } } + @Override + public void remove(User user) throws InstanceNotFoundException { + List orderAuthorizations = getOrderAuthorizationsByUser(user); + if (!orderAuthorizations.isEmpty()) { + for (OrderAuthorization orderAuthorization : orderAuthorizations) { + orderAuthorizationDAO.remove(orderAuthorization.getId()); + } + } + super.remove(user.getId()); + } + } diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/users/UserModel.java b/libreplan-webapp/src/main/java/org/libreplan/web/users/UserModel.java index ca255e06c..18eb313c8 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/users/UserModel.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/users/UserModel.java @@ -3,7 +3,7 @@ * * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e * Desenvolvemento Tecnolóxico de Galicia - * Copyright (C) 2010-2011 Igalia, S.L. + * 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 @@ -29,9 +29,7 @@ import org.libreplan.business.common.Configuration; import org.libreplan.business.common.daos.IConfigurationDAO; import org.libreplan.business.common.exceptions.InstanceNotFoundException; import org.libreplan.business.common.exceptions.ValidationException; -import org.libreplan.business.users.daos.IOrderAuthorizationDAO; import org.libreplan.business.users.daos.IUserDAO; -import org.libreplan.business.users.entities.OrderAuthorization; import org.libreplan.business.users.entities.Profile; import org.libreplan.business.users.entities.User; import org.libreplan.business.users.entities.UserRole; @@ -45,8 +43,10 @@ import org.springframework.transaction.annotation.Transactional; /** * Model for UI operations related to {@link User} + * * @author Jacobo Aragunde Perez * @author Susana Montes Pedreira + * @author Manuel Rego Casasnovas */ @Service @Scope(BeanDefinition.SCOPE_PROTOTYPE) @@ -59,9 +59,6 @@ public class UserModel implements IUserModel { @Autowired private IConfigurationDAO configurationDAO; - @Autowired - private IOrderAuthorizationDAO orderAuthorizationDAO; - @Autowired private IDBPasswordEncoderService dbPasswordEncoderService; @@ -225,20 +222,8 @@ public class UserModel implements IUserModel { @Override @Transactional - public void confirmRemove(User user) - throws InstanceNotFoundException { - List orderAuthorizations = getReferencedByOtherEntities(user); - if (!orderAuthorizations.isEmpty()) { - for (OrderAuthorization orderAuthorization : orderAuthorizations) { - orderAuthorizationDAO.remove(orderAuthorization.getId()); - } - } - userDAO.remove(user.getId()); - } - - @Transactional(readOnly = true) - public List getReferencedByOtherEntities(User user){ - return userDAO.getOrderAuthorizationsByUser(user); + public void confirmRemove(User user) throws InstanceNotFoundException { + userDAO.remove(user); } @Transactional(readOnly = true)