diff --git a/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java index debac110a..cd8b81ecd 100644 --- a/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java @@ -194,12 +194,6 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic data.setGroupId(groupId != null ? groupId : UUID.randomUUID()); data.setVersionStatus(DescriptionTemplateVersionStatus.NotFinalized); data.setVersion((short) 1); - - long activeDescriptionTemplatesForTheGroup = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking() - .isActive(IsActive.Active) - .groupIds(data.getGroupId()) - .count(); - if (activeDescriptionTemplatesForTheGroup > 0) throw new MyApplicationException("Description template group id is in use please use new version endpoint"); } if (groupId != null && !data.getGroupId().equals(groupId)) throw new MyApplicationException("Can not change description template group id"); @@ -227,6 +221,15 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic this.entityManager.flush(); + if (!isUpdate) { + long activeDescriptionTemplatesForTheGroup = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking() + .isActive(IsActive.Active) + .excludedIds(data.getId()) + .groupIds(data.getGroupId()) + .count(); + if (activeDescriptionTemplatesForTheGroup > 0) throw new MyApplicationException("Description template group id is in use please use new version endpoint"); + } + this.updateVersionStatusAndSave(data, previousStatus, data.getStatus()); this.entityManager.flush(); diff --git a/backend/core/src/main/java/org/opencdmp/service/descriptiontemplatetype/DescriptionTemplateTypeServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/descriptiontemplatetype/DescriptionTemplateTypeServiceImpl.java index f41516b8c..9b9fff9c1 100644 --- a/backend/core/src/main/java/org/opencdmp/service/descriptiontemplatetype/DescriptionTemplateTypeServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/descriptiontemplatetype/DescriptionTemplateTypeServiceImpl.java @@ -101,8 +101,10 @@ public class DescriptionTemplateTypeServiceImpl implements DescriptionTemplateTy DescriptionTemplateTypeEntity data; if (isUpdate) { data = this.entityManager.find(DescriptionTemplateTypeEntity.class, model.getId()); - if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplateType.class.getSimpleName()}, LocaleContextHolder.getLocale())); - if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); + if (data == null) + throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplateType.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) + throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); } else { this.usageLimitService.checkIncrease(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT); data = new DescriptionTemplateTypeEntity(); @@ -116,19 +118,27 @@ public class DescriptionTemplateTypeServiceImpl implements DescriptionTemplateTy data.setStatus(model.getStatus()); data.setUpdatedAt(Instant.now()); - Long descriptionTemplateTypeCodes = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking().codes(model.getCode()).count(); if (isUpdate) { - if (descriptionTemplateTypeCodes > 1) throw new MyValidationException(this.errors.getDescriptionTemplateTypeCodeExists().getCode(), this.errors.getDescriptionTemplateTypeCodeExists().getMessage()); this.entityManager.merge(data); } else { - if (descriptionTemplateTypeCodes > 0) throw new MyValidationException(this.errors.getDescriptionTemplateTypeCodeExists().getCode(), this.errors.getDescriptionTemplateTypeCodeExists().getMessage()); this.entityManager.persist(data); this.accountingService.increase(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue()); } this.entityManager.flush(); + if (!isUpdate) { + Long descriptionTemplateTypeCodes = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking() + .isActive(IsActive.Active) + .excludedIds(data.getId()) + .codes(model.getCode()) + .count(); + + if (descriptionTemplateTypeCodes > 0) + throw new MyValidationException(this.errors.getDescriptionTemplateTypeCodeExists().getCode(), this.errors.getDescriptionTemplateTypeCodeExists().getMessage()); + } + this.eventBroker.emit(new DescriptionTemplateTypeTouchedEvent(data.getId())); return this.builderFactory.builder(DescriptionTemplateTypeBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(BaseFieldSet.build(fields, DescriptionTemplateType._id), data); } diff --git a/backend/core/src/main/java/org/opencdmp/service/language/LanguageServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/language/LanguageServiceImpl.java index b82fdcd79..7f00e69ff 100644 --- a/backend/core/src/main/java/org/opencdmp/service/language/LanguageServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/language/LanguageServiceImpl.java @@ -3,6 +3,7 @@ package org.opencdmp.service.language; import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.data.deleter.DeleterFactory; +import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyForbiddenException; import gr.cite.tools.exception.MyNotFoundException; @@ -22,6 +23,8 @@ import org.opencdmp.model.Language; import org.opencdmp.model.builder.LanguageBuilder; import org.opencdmp.model.deleter.LanguageDeleter; import org.opencdmp.model.persist.LanguagePersist; +import org.opencdmp.query.LanguageQuery; +import org.opencdmp.query.TenantQuery; import org.opencdmp.service.planblueprint.PlanBlueprintServiceImpl; import org.opencdmp.service.storage.StorageFileService; import org.slf4j.LoggerFactory; @@ -44,6 +47,7 @@ public class LanguageServiceImpl implements LanguageService { private final AuthorizationService authorizationService; private final DeleterFactory deleterFactory; private final BuilderFactory builderFactory; + private final QueryFactory queryFactory; private final ConventionService conventionService; private final MessageSource messageSource; private final ErrorThesaurusProperties errors; @@ -51,12 +55,13 @@ public class LanguageServiceImpl implements LanguageService { public LanguageServiceImpl( - TenantEntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory, + TenantEntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory, QueryFactory queryFactory, ConventionService conventionService, MessageSource messageSource, ErrorThesaurusProperties errors, StorageFileService storageFileService){ this.entityManager = entityManager; this.authorizationService = authorizationService; this.deleterFactory = deleterFactory; this.builderFactory = builderFactory; + this.queryFactory = queryFactory; this.conventionService = conventionService; this.messageSource = messageSource; this.errors = errors; @@ -93,6 +98,16 @@ public class LanguageServiceImpl implements LanguageService { this.entityManager.flush(); + if (!isUpdate) { + Long languagesWithThisCode = this.queryFactory.query(LanguageQuery.class).disableTracking() + .isActive(IsActive.Active) + .excludedIds(data.getId()) + .codes(data.getCode()) + .count(); + + if (languagesWithThisCode > 0) throw new MyValidationException(this.errors.getTenantCodeExists().getCode(), this.errors.getTenantCodeExists().getMessage()); + } + return this.builderFactory.builder(LanguageBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(BaseFieldSet.build(fields, Language._id), data); } diff --git a/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java index 64b6842a4..6c44c8ff9 100644 --- a/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java @@ -173,14 +173,22 @@ public class PlanBlueprintServiceImpl implements PlanBlueprintService { if (isUpdate) { this.entityManager.merge(data); } else { - Long planBlueprintCodes = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking().codes(model.getCode()).count(); - if (planBlueprintCodes > 0) throw new MyValidationException(this.errors.getPlanBlueprintCodeExists().getCode(), this.errors.getPlanBlueprintCodeExists().getMessage()); this.entityManager.persist(data); this.accountingService.increase(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue()); } this.entityManager.flush(); + if (!isUpdate) { + Long planBlueprintCodes = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking() + .isActive(IsActive.Active) + .excludedIds(data.getId()) + .codes(model.getCode()) + .count(); + + if (planBlueprintCodes > 0) throw new MyValidationException(this.errors.getPlanBlueprintCodeExists().getCode(), this.errors.getPlanBlueprintCodeExists().getMessage()); + } + this.updateVersionStatusAndSave(data, previousStatus, data.getStatus()); this.entityManager.flush(); diff --git a/backend/core/src/main/java/org/opencdmp/service/prefillingsource/PrefillingSourceServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/prefillingsource/PrefillingSourceServiceImpl.java index e4b6acba6..e67656f83 100644 --- a/backend/core/src/main/java/org/opencdmp/service/prefillingsource/PrefillingSourceServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/prefillingsource/PrefillingSourceServiceImpl.java @@ -163,8 +163,16 @@ public class PrefillingSourceServiceImpl implements PrefillingSourceService { this.entityManager.flush(); - Long prefillingSourcesWithThisCode = this.queryFactory.query(PrefillingSourceQuery.class).codes(data.getCode()).count(); - if (prefillingSourcesWithThisCode > 1) throw new MyValidationException(this.errors.getPrefillingSourceCodeExists().getCode(), this.errors.getPrefillingSourceCodeExists().getMessage()); + if (!isUpdate) { + Long prefillingSourcesWithThisCode = this.queryFactory.query(PrefillingSourceQuery.class).disableTracking() + .isActive(IsActive.Active) + .excludedIds(data.getId()) + .codes(data.getCode()) + .count(); + + if (prefillingSourcesWithThisCode > 1) throw new MyValidationException(this.errors.getPrefillingSourceCodeExists().getCode(), this.errors.getPrefillingSourceCodeExists().getMessage()); + } + return this.builderFactory.builder(PrefillingSourceBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(BaseFieldSet.build(fields, PrefillingSource._id), data); } diff --git a/backend/core/src/main/java/org/opencdmp/service/referencetype/ReferenceTypeServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/referencetype/ReferenceTypeServiceImpl.java index ce6bad57d..3e9b67643 100644 --- a/backend/core/src/main/java/org/opencdmp/service/referencetype/ReferenceTypeServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/referencetype/ReferenceTypeServiceImpl.java @@ -117,9 +117,16 @@ public class ReferenceTypeServiceImpl implements ReferenceTypeService { } this.entityManager.flush(); - - Long referenceTypesWithThisCode = this.queryFactory.query(ReferenceTypeQuery.class).codes(data.getCode()).count(); - if (referenceTypesWithThisCode > 1) throw new MyValidationException(this.errors.getReferenceTypeCodeExists().getCode(), this.errors.getReferenceTypeCodeExists().getMessage()); + + if (!isUpdate) { + Long referenceTypesWithThisCode = this.queryFactory.query(ReferenceTypeQuery.class).disableTracking() + .isActive(IsActive.Active) + .excludedIds(data.getId()) + .codes(data.getCode()) + .count(); + + if (referenceTypesWithThisCode > 0) throw new MyValidationException(this.errors.getReferenceTypeCodeExists().getCode(), this.errors.getReferenceTypeCodeExists().getMessage()); + } return this.builderFactory.builder(ReferenceTypeBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(BaseFieldSet.build(fields, ReferenceType._id), data); } diff --git a/backend/core/src/main/java/org/opencdmp/service/tenant/TenantServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/tenant/TenantServiceImpl.java index 1301d9c0a..7ad3f74c2 100644 --- a/backend/core/src/main/java/org/opencdmp/service/tenant/TenantServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/tenant/TenantServiceImpl.java @@ -154,8 +154,15 @@ public class TenantServiceImpl implements TenantService { this.entityManager.flush(); - Long tenantsWithThisCode = this.queryFactory.query(TenantQuery.class).codes(data.getCode()).count(); - if (tenantsWithThisCode > 1) throw new MyValidationException(this.errors.getTenantCodeExists().getCode(), this.errors.getTenantCodeExists().getMessage()); + if (!isUpdate) { + Long tenantsWithThisCode = this.queryFactory.query(TenantQuery.class).disableTracking() + .isActive(IsActive.Active) + .excludedIds(data.getId()) + .codes(data.getCode()) + .count(); + + if (tenantsWithThisCode > 0) throw new MyValidationException(this.errors.getTenantCodeExists().getCode(), this.errors.getTenantCodeExists().getMessage()); + } TenantTouchedIntegrationEvent tenantTouchedIntegrationEvent = new TenantTouchedIntegrationEvent(); tenantTouchedIntegrationEvent.setId(data.getId());