diff --git a/libreplan-business/src/main/java/org/libreplan/business/resources/entities/Resource.java b/libreplan-business/src/main/java/org/libreplan/business/resources/entities/Resource.java index f0038176e..9d4736b13 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/resources/entities/Resource.java +++ b/libreplan-business/src/main/java/org/libreplan/business/resources/entities/Resource.java @@ -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 resources = Registry.getResourceDAO().findAll(); - if (resources.size() > maxResources) { - return false; - } - } - return true; + return Registry.getTransactionService() + .runOnAnotherReadOnlyTransaction(new IOnTransaction() { + @Override + public Boolean execute() { + Configuration configuration = Registry + .getConfigurationDAO().getConfiguration(); + if (configuration == null) { + return true; + } + + Integer maxResources = configuration.getMaxResources(); + if (maxResources != null && maxResources > 0) { + List 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) { diff --git a/libreplan-business/src/main/java/org/libreplan/business/users/entities/User.java b/libreplan-business/src/main/java/org/libreplan/business/users/entities/User.java index 6580bf3e9..bf0f4a476 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/users/entities/User.java +++ b/libreplan-business/src/main/java/org/libreplan/business/users/entities/User.java @@ -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 users = Registry.getUserDAO().findAll(); - if (users.size() > maxUsers) { - return false; - } - } - return true; + return Registry.getTransactionService() + .runOnAnotherReadOnlyTransaction(new IOnTransaction() { + @Override + public Boolean execute() { + Configuration configuration = Registry + .getConfigurationDAO().getConfiguration(); + if (configuration == null) { + return true; + } + + Integer maxUsers = configuration.getMaxUsers(); + if (maxUsers != null && maxUsers > 0) { + List users = Registry.getUserDAO().findAll(); + int usersNumber = users.size(); + if (isNewObject()) { + usersNumber++; + } + if (usersNumber > maxUsers) { + return false; + } + } + return true; + } + }); } public Label getProjectsFilterLabel() {