Add assert to check that a worker is not bound to a user already bound with other worker

FEA: ItEr76S27ResourceBinding
This commit is contained in:
Manuel Rego Casasnovas 2012-05-07 17:29:20 +02:00
parent 9a011d35e9
commit 11b11a485d
4 changed files with 84 additions and 1 deletions

View file

@ -27,6 +27,7 @@ import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotEmpty;
import org.libreplan.business.common.Registry;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
/**
@ -214,4 +215,28 @@ public class Worker extends Resource {
this.user = user;
}
@AssertTrue(message = "User already bound to other worker")
public boolean checkUserNotBoundToOtherWorker() {
if (user == null || user.isNewObject()) {
return true;
}
IUserDAO userDAO = Registry.getUserDAO();
User foundUser = userDAO.findOnAnotherTransaction(user.getId());
if (foundUser == null) {
return true;
}
Worker worker = foundUser.getWorker();
if (worker == null) {
return true;
}
if (getId() == null) {
return false;
}
return getId().equals(worker.getId());
}
}

View file

@ -92,4 +92,6 @@ public interface IUserDAO extends IGenericDAO<User, Long>{
*/
List<User> getUnboundUsers(Worker worker);
User findOnAnotherTransaction(Long id);
}

View file

@ -149,4 +149,14 @@ public class UserDAO extends GenericDAOHibernate<User, Long>
org.hibernate.criterion.Order.asc("loginName")).list();
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public User findOnAnotherTransaction(Long id) {
try {
return find(id);
} catch (InstanceNotFoundException e) {
return null;
}
}
}

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
@ -46,6 +46,7 @@ import org.libreplan.business.calendars.entities.ResourceCalendar;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.resources.daos.ICriterionDAO;
import org.libreplan.business.resources.daos.ICriterionTypeDAO;
import org.libreplan.business.resources.daos.IResourceDAO;
@ -56,6 +57,8 @@ import org.libreplan.business.resources.entities.CriterionType;
import org.libreplan.business.resources.entities.Interval;
import org.libreplan.business.resources.entities.Resource;
import org.libreplan.business.resources.entities.Worker;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.context.ContextConfiguration;
@ -91,6 +94,9 @@ public class ResourceDAOTest {
@Autowired
private ICriterionTypeDAO criterionTypeDAO;
@Autowired
private IUserDAO userDAO;
@Test
public void saveResourceWithCalendar() throws InstanceNotFoundException {
Resource resource = givenValidWorker();
@ -233,4 +239,44 @@ public class ResourceDAOTest {
assertThat(result.size(), not(equalTo(1)));
}
private User givenStoredUser() {
return transactionService
.runOnAnotherTransaction(new IOnTransaction<User>() {
@Override
public User execute() {
User user = User.create("login" + UUID.randomUUID(),
"password", null);
userDAO.save(user);
user.dontPoseAsTransientObjectAnymore();
return user;
}
});
}
@Test(expected = ValidationException.class)
public void testWorkerBoundToUserAlreadyBound() {
final User user = givenStoredUser();
transactionService.runOnAnotherTransaction(new IOnTransaction<Void>() {
@Override
public Void execute() {
Worker worker1 = givenValidWorker();
worker1.setUser(user);
resourceDAO.save(worker1);
return null;
}
});
transactionService.runOnAnotherTransaction(new IOnTransaction<Void>() {
@Override
public Void execute() {
Worker worker2 = givenValidWorker();
worker2.setUser(user);
resourceDAO.save(worker2);
return null;
}
});
}
}