diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java index 587aa4016..4ed9ede03 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java @@ -28,12 +28,15 @@ import org.apache.commons.lang.Validate; import org.hibernate.Criteria; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; +import org.libreplan.business.common.daos.IConfigurationDAO; import org.libreplan.business.common.daos.IntegrationEntityDAO; +import org.libreplan.business.common.entities.Configuration; import org.libreplan.business.common.exceptions.InstanceNotFoundException; import org.libreplan.business.common.exceptions.ValidationException; import org.libreplan.business.costcategories.entities.HourCost; import org.libreplan.business.costcategories.entities.TypeOfWorkHours; import org.libreplan.business.workreports.entities.WorkReportLine; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; @@ -50,6 +53,9 @@ public class TypeOfWorkHoursDAO extends IntegrationEntityDAO implements ITypeOfWorkHoursDAO { + @Autowired + private IConfigurationDAO configurationDAO; + @Override public TypeOfWorkHours findUniqueByCode(TypeOfWorkHours typeOfWorkHours) throws InstanceNotFoundException { @@ -142,6 +148,7 @@ public class TypeOfWorkHoursDAO extends IntegrationEntityDAO public void checkIsReferencedByOtherEntities(TypeOfWorkHours type) throws ValidationException { checkHasHourCost(type); checkHasWorkReportLine(type); + checkIsMonthlyTimesheetsTypeOfWorkHours(type); } private void checkHasWorkReportLine(TypeOfWorkHours type) { @@ -167,6 +174,17 @@ public class TypeOfWorkHoursDAO extends IntegrationEntityDAO } } + private void checkIsMonthlyTimesheetsTypeOfWorkHours(TypeOfWorkHours type) { + Configuration configuration = configurationDAO.getConfiguration(); + if (configuration.getMonthlyTimesheetsTypeOfWorkHours().getId() + .equals(type.getId())) { + throw ValidationException + .invalidValue( + "Cannot delete the type of work hours. It is configured as type of work hours for monthly timesheets.", + type); + } + } + @Override public boolean existsByName(TypeOfWorkHours typeOfWorkHours) { Criteria c = getSession().createCriteria(TypeOfWorkHours.class).add( diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/TypeOfWorkHours.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/TypeOfWorkHours.java index d799e910c..c82024096 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/TypeOfWorkHours.java +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/TypeOfWorkHours.java @@ -143,6 +143,19 @@ public class TypeOfWorkHours extends IntegrationEntity implements IHumanIdentifi } } + @AssertTrue(message = "the type of work hours for monthly timesheets cannot be disabled") + public boolean checkMonthlyTimesheetsTypeOfWorkHoursNotDisabled() { + if (!isNewObject() && !getEnabled()) { + TypeOfWorkHours typeOfWorkHours = Registry.getConfigurationDAO() + .getConfiguration().getMonthlyTimesheetsTypeOfWorkHours(); + if (typeOfWorkHours.getId().equals(getId())) { + return false; + } + } + + return true; + } + public String toString() { return name; }