Bug #1609: Fix problem using a different transaction in validation methods

In order to avoid to have the same user in the session twice, the validation
query of User entity has been done in a different transaction.

The issue happens with resources too, so the same solution has been used in
both cases.

FEA: ItEr77S04BugFixing
This commit is contained in:
Manuel Rego Casasnovas 2013-02-22 12:06:27 +01:00
parent 75ef2de231
commit 5d07599594
2 changed files with 53 additions and 18 deletions

View file

@ -49,8 +49,10 @@ import org.libreplan.business.calendars.entities.ResourceCalendar;
import org.libreplan.business.calendars.entities.SameWorkHoursEveryDay;
import org.libreplan.business.common.BaseEntity;
import org.libreplan.business.common.IHumanIdentifiable;
import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.IntegrationEntity;
import org.libreplan.business.common.Registry;
import org.libreplan.business.common.entities.Configuration;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.common.exceptions.MultipleInstancesException;
import org.libreplan.business.common.exceptions.ValidationException;
@ -1188,15 +1190,31 @@ public abstract class Resource extends IntegrationEntity implements
@AssertTrue(message = "You have exceeded the maximum limit of resources")
public boolean checkMaxResources() {
Integer maxResources = Registry.getConfigurationDAO()
.getConfiguration().getMaxResources();
if (maxResources != null && maxResources > 0) {
List<Resource> resources = Registry.getResourceDAO().findAll();
if (resources.size() > maxResources) {
return false;
}
}
return true;
return Registry.getTransactionService()
.runOnAnotherReadOnlyTransaction(new IOnTransaction<Boolean>() {
@Override
public Boolean execute() {
Configuration configuration = Registry
.getConfigurationDAO().getConfiguration();
if (configuration == null) {
return true;
}
Integer maxResources = configuration.getMaxResources();
if (maxResources != null && maxResources > 0) {
List<Resource> resources = Registry
.getResourceDAO().findAll();
int resourcesNumber = resources.size();
if (isNewObject()) {
resourcesNumber++;
}
if (resourcesNumber > maxResources) {
return false;
}
}
return true;
}
});
}
public boolean isActiveBetween(LocalDate startDate, LocalDate endDate) {

View file

@ -31,7 +31,9 @@ import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotEmpty;
import org.libreplan.business.common.BaseEntity;
import org.libreplan.business.common.IHumanIdentifiable;
import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.Registry;
import org.libreplan.business.common.entities.Configuration;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.labels.entities.Label;
import org.libreplan.business.resources.entities.Criterion;
@ -377,15 +379,30 @@ public class User extends BaseEntity implements IHumanIdentifiable{
@AssertTrue(message = "You have exceeded the maximum limit of users")
public boolean checkMaxUsers() {
Integer maxUsers = Registry.getConfigurationDAO().getConfiguration()
.getMaxUsers();
if (maxUsers != null && maxUsers > 0) {
List<User> users = Registry.getUserDAO().findAll();
if (users.size() > maxUsers) {
return false;
}
}
return true;
return Registry.getTransactionService()
.runOnAnotherReadOnlyTransaction(new IOnTransaction<Boolean>() {
@Override
public Boolean execute() {
Configuration configuration = Registry
.getConfigurationDAO().getConfiguration();
if (configuration == null) {
return true;
}
Integer maxUsers = configuration.getMaxUsers();
if (maxUsers != null && maxUsers > 0) {
List<User> users = Registry.getUserDAO().findAll();
int usersNumber = users.size();
if (isNewObject()) {
usersNumber++;
}
if (usersNumber > maxUsers) {
return false;
}
}
return true;
}
});
}
public Label getProjectsFilterLabel() {