diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/NewVersionDescriptionTemplatePersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/NewVersionDescriptionTemplatePersist.java index 6a1d09c7f..2b492c132 100644 --- a/backend/core/src/main/java/org/opencdmp/model/persist/NewVersionDescriptionTemplatePersist.java +++ b/backend/core/src/main/java/org/opencdmp/model/persist/NewVersionDescriptionTemplatePersist.java @@ -6,7 +6,6 @@ import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.specification.Specification; import org.opencdmp.convention.ConventionService; import org.opencdmp.data.DescriptionTemplateEntity; -import org.opencdmp.data.DescriptionTemplateTypeEntity; import org.opencdmp.errorcode.ErrorThesaurusProperties; import org.opencdmp.model.persist.descriptiontemplatedefinition.DefinitionPersist; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -27,10 +26,6 @@ public class NewVersionDescriptionTemplatePersist { public static final String _label = "label"; - private String code = null; - - public static final String _code = "code"; - private String description = null; public static final String _description = "description"; @@ -75,14 +70,6 @@ public class NewVersionDescriptionTemplatePersist { this.label = label; } - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - public String getDescription() { return description; } @@ -174,14 +161,6 @@ public class NewVersionDescriptionTemplatePersist { .iff(() -> !this.isEmpty(item.getLabel())) .must(() -> this.lessEqualLength(item.getLabel(), DescriptionTemplateEntity._labelLength)) .failOn(NewVersionDescriptionTemplatePersist._label).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{NewVersionDescriptionTemplatePersist._label}, LocaleContextHolder.getLocale())), - this.spec() - .iff(() -> item.getStatus() == DescriptionTemplateStatus.Finalized) - .must(() -> !this.isEmpty(item.getCode())) - .failOn(NewVersionDescriptionTemplatePersist._code).failWith(messageSource.getMessage("Validation_Required", new Object[]{NewVersionDescriptionTemplatePersist._code}, LocaleContextHolder.getLocale())), - this.spec() - .iff(() -> !this.isEmpty(item.getCode())) - .must(() -> this.lessEqualLength(item.getCode(), DescriptionTemplateTypeEntity._codeLength)) - .failOn(NewVersionDescriptionTemplatePersist._code).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{NewVersionDescriptionTemplatePersist._code}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getDescription())) .failOn(NewVersionDescriptionTemplatePersist._description).failWith(messageSource.getMessage("Validation_Required", new Object[]{NewVersionDescriptionTemplatePersist._description}, LocaleContextHolder.getLocale())), 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 39bd8561e..b2d2f2a11 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 @@ -181,6 +181,8 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); if (data.getStatus().equals(DescriptionTemplateStatus.Finalized)) throw new MyForbiddenException("Can not update finalized template"); + if (!data.getCode().equals(model.getCode())) + throw new MyForbiddenException("Code can not change"); } else { this.usageLimitService.checkIncrease(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT); @@ -212,10 +214,10 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic data.setUpdatedAt(Instant.now()); data.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(model.getDefinition()))); - Long descriptionTemplateCodes = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().codes(model.getCode()).count(); + Long descriptionTemplateCodes = 0L; + if (model.getCode() != null) descriptionTemplateCodes = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().codes(model.getCode()).count(); if (isUpdate) { - if (descriptionTemplateCodes > 1) throw new MyValidationException(this.errors.getDescriptionTemplateCodeExists().getCode(), this.errors.getDescriptionTemplateCodeExists().getMessage()); this.entityManager.merge(data); } else { if (descriptionTemplateCodes > 0) throw new MyValidationException(this.errors.getDescriptionTemplateCodeExists().getCode(), this.errors.getDescriptionTemplateCodeExists().getMessage()); @@ -722,14 +724,12 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic data.setVersion((short) (oldDescriptionTemplateEntity.getVersion() + 1)); data.setDescription(model.getDescription()); data.setLabel(model.getLabel()); - data.setCode(model.getCode()); + data.setCode(oldDescriptionTemplateEntity.getCode()); data.setTypeId(model.getType()); data.setLanguage(model.getLanguage()); data.setStatus(model.getStatus()); data.setDefinition(this.xmlHandlingService.toXml(this.buildDefinitionEntity(model.getDefinition()))); - Long descriptionTemplateCodes = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().codes(model.getCode()).count(); - if (descriptionTemplateCodes > 0) throw new MyValidationException(this.errors.getDescriptionTemplateCodeExists().getCode(), this.errors.getDescriptionTemplateCodeExists().getMessage()); this.entityManager.persist(data); this.persistUsers(data.getId(), model.getUsers()); @@ -786,7 +786,6 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic NewVersionDescriptionTemplatePersist persist = new NewVersionDescriptionTemplatePersist(); persist.setId(latestVersionDescriptionTemplate.getId()); persist.setLabel(label); - persist.setCode(importXml.getCode()); persist.setStatus(DescriptionTemplateStatus.Draft); persist.setDescription(importXml.getDescription()); persist.setLanguage(importXml.getLanguage()); diff --git a/frontend/src/app/ui/admin/description-template/editor/description-template-editor.component.ts b/frontend/src/app/ui/admin/description-template/editor/description-template-editor.component.ts index 436312088..ee16e4046 100644 --- a/frontend/src/app/ui/admin/description-template/editor/description-template-editor.component.ts +++ b/frontend/src/app/ui/admin/description-template/editor/description-template-editor.component.ts @@ -158,9 +158,9 @@ export class DescriptionTemplateEditorComponent extends BaseEditor this.availableReferenceTypes = referenceTypes.items as ReferenceType[]); } private initModelFlags(action: string): void { @@ -211,7 +211,7 @@ export class DescriptionTemplateEditorComponent extends BaseEditor x === AppPermission.EditDescriptionTemplate))); + this.formGroup = this.editorModel.buildForm(null, this.isDeleted || !(this.authService.hasPermission(AppPermission.EditDescriptionTemplate) || this.item?.authorizationFlags?.some(x => x === AppPermission.EditDescriptionTemplate)), (this.isNew || this.isClone)); this.descriptionTemplateEditorService.setValidationErrorModel(this.editorModel.validationErrorModel); this.isFinalized = this.editorModel.status == DescriptionTemplateStatus.Finalized; if (this.isFinalized || this.isDeleted) { @@ -220,6 +220,7 @@ export class DescriptionTemplateEditorComponent extends BaseEditor void): void { if (this.isNewVersion == false) { const formData = this.formService.getValue(this.formGroup.value) as DescriptionTemplatePersist; + formData.code = this.formGroup.get('code').getRawValue(); this.descriptionTemplateService.persist(formData) .pipe(takeUntil(this._destroyed)).subscribe( diff --git a/frontend/src/app/ui/admin/description-template/editor/description-template-editor.model.ts b/frontend/src/app/ui/admin/description-template/editor/description-template-editor.model.ts index 81018f6d7..0f37dde27 100644 --- a/frontend/src/app/ui/admin/description-template/editor/description-template-editor.model.ts +++ b/frontend/src/app/ui/admin/description-template/editor/description-template-editor.model.ts @@ -45,13 +45,13 @@ export class DescriptionTemplateEditorModel extends BaseEditorModel implements D return this; } - buildForm(context: ValidationContext = null, disabled: boolean = false): UntypedFormGroup { + buildForm(context: ValidationContext = null, disabled: boolean = false, isNewOrClone: boolean = false): UntypedFormGroup { if (context == null) { context = this.createValidationContext(); } return this.formBuilder.group({ id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators], label: [{ value: this.label, disabled: disabled }, context.getValidation('label').validators], - code: [{ value: this.code, disabled: disabled }, context.getValidation('code').validators], + code: [{ value: this.code, disabled: !isNewOrClone }, context.getValidation('code').validators], description: [{ value: this.description, disabled: disabled }, context.getValidation('description').validators], language: [{ value: this.language, disabled: disabled }, context.getValidation('language').validators], type: [{ value: this.type, disabled: disabled }, context.getValidation('type').validators],