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
This commit is contained in:
Manuel Rego Casasnovas 2012-05-09 08:51:16 +02:00
parent c607ba4584
commit dd501faba8
3 changed files with 27 additions and 24 deletions

View file

@ -83,8 +83,6 @@ public interface IUserDAO extends IGenericDAO<User, Long>{
public List<User> findByLastConnectedScenario(Scenario scenario);
List<OrderAuthorization> 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, Long>{
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;
}

View file

@ -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<User, Long>
implements IUserDAO {
@Autowired
private IOrderAuthorizationDAO orderAuthorizationDAO;
@Override
@Transactional(readOnly = true)
public User findByLoginName(String loginName)
@ -123,8 +127,7 @@ public class UserDAO extends GenericDAOHibernate<User, Long>
return c.list();
}
@Override
public List<OrderAuthorization> getOrderAuthorizationsByUser(User user) {
private List<OrderAuthorization> getOrderAuthorizationsByUser(User user) {
List orderAuthorizations = getSession()
.createCriteria(UserOrderAuthorization.class)
.add(Restrictions.eq("user", user)).list();
@ -159,4 +162,15 @@ public class UserDAO extends GenericDAOHibernate<User, Long>
}
}
@Override
public void remove(User user) throws InstanceNotFoundException {
List<OrderAuthorization> orderAuthorizations = getOrderAuthorizationsByUser(user);
if (!orderAuthorizations.isEmpty()) {
for (OrderAuthorization orderAuthorization : orderAuthorizations) {
orderAuthorizationDAO.remove(orderAuthorization.getId());
}
}
super.remove(user.getId());
}
}

View file

@ -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 <jaragunde@igalia.com>
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
@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<OrderAuthorization> orderAuthorizations = getReferencedByOtherEntities(user);
if (!orderAuthorizations.isEmpty()) {
for (OrderAuthorization orderAuthorization : orderAuthorizations) {
orderAuthorizationDAO.remove(orderAuthorization.getId());
}
}
userDAO.remove(user.getId());
}
@Transactional(readOnly = true)
public List<OrderAuthorization> getReferencedByOtherEntities(User user){
return userDAO.getOrderAuthorizationsByUser(user);
public void confirmRemove(User user) throws InstanceNotFoundException {
userDAO.remove(user);
}
@Transactional(readOnly = true)