2024-02-08 16:23:03 +01:00
|
|
|
import { FormControl, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms";
|
2023-12-06 22:10:01 +01:00
|
|
|
import { DescriptionStatus } from "@app/core/common/enum/description-status";
|
2024-02-07 13:15:25 +01:00
|
|
|
import { IsActive } from "@app/core/common/enum/is-active.enum";
|
|
|
|
import { DescriptionTemplate, DescriptionTemplateField, DescriptionTemplateFieldSet, DescriptionTemplateSection } from "@app/core/model/description-template/description-template";
|
|
|
|
import { Description, DescriptionExternalIdentifier, DescriptionExternalIdentifierPersist, DescriptionField, DescriptionFieldPersist, DescriptionPersist, DescriptionPropertyDefinition, DescriptionPropertyDefinitionFieldSet, DescriptionPropertyDefinitionFieldSetItem, DescriptionPropertyDefinitionFieldSetItemPersist, DescriptionPropertyDefinitionFieldSetPersist, DescriptionPropertyDefinitionPersist, DescriptionReference, DescriptionReferencePersist } from "@app/core/model/description/description";
|
2023-12-06 22:10:01 +01:00
|
|
|
import { ReferencePersist } from "@app/core/model/reference/reference";
|
|
|
|
import { BaseEditorModel } from "@common/base/base-form-editor-model";
|
|
|
|
import { BackendErrorValidator } from '@common/forms/validation/custom-validator';
|
|
|
|
import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model';
|
|
|
|
import { Validation, ValidationContext } from '@common/forms/validation/validation-context';
|
|
|
|
import { Guid } from "@common/types/guid";
|
|
|
|
|
|
|
|
export class DescriptionEditorModel extends BaseEditorModel implements DescriptionPersist {
|
|
|
|
label: string;
|
|
|
|
dmpId: Guid;
|
|
|
|
dmpDescriptionTemplateId: Guid;
|
|
|
|
descriptionTemplateId: Guid;
|
|
|
|
status: DescriptionStatus;
|
|
|
|
description: string;
|
2024-02-08 16:23:03 +01:00
|
|
|
properties: DescriptionPropertyDefinitionEditorModel = new DescriptionPropertyDefinitionEditorModel(this.validationErrorModel);
|
2023-12-06 22:10:01 +01:00
|
|
|
tags: string[];
|
|
|
|
references: DescriptionReferenceEditorModel[];
|
|
|
|
permissions: string[];
|
|
|
|
|
|
|
|
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel();
|
|
|
|
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
|
|
|
|
|
|
|
|
constructor() { super(); }
|
|
|
|
|
2024-01-25 19:55:42 +01:00
|
|
|
public fromModel(item: Description, descriptionTemplate?: DescriptionTemplate): DescriptionEditorModel {
|
2023-12-06 22:10:01 +01:00
|
|
|
if (item) {
|
|
|
|
super.fromModel(item);
|
|
|
|
this.label = item.label;
|
|
|
|
this.dmpId = item.dmp?.id;
|
|
|
|
this.dmpDescriptionTemplateId = item.dmpDescriptionTemplate?.id;
|
|
|
|
this.descriptionTemplateId = item.descriptionTemplate?.id;
|
2024-02-08 08:40:16 +01:00
|
|
|
this.status = item.status ?? DescriptionStatus.Draft;
|
2023-12-06 22:10:01 +01:00
|
|
|
this.description = item.description;
|
|
|
|
this.tags = item.descriptionTags?.map(x => x.tag?.label);
|
2024-02-08 16:23:03 +01:00
|
|
|
this.properties = new DescriptionPropertyDefinitionEditorModel(this.validationErrorModel).fromModel(item.properties, descriptionTemplate, item.descriptionReferences);
|
2023-12-06 22:10:01 +01:00
|
|
|
//if (item.references) { item.references.map(x => this.references.push(new DescriptionReferenceEditorModel().fromModel(x))); }
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(context: ValidationContext = null, disabled: 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],
|
|
|
|
dmpId: [{ value: this.dmpId, disabled: disabled }, context.getValidation('dmpId').validators],
|
|
|
|
dmpDescriptionTemplateId: [{ value: this.dmpDescriptionTemplateId, disabled: disabled }, context.getValidation('dmpDescriptionTemplateId').validators],
|
|
|
|
descriptionTemplateId: [{ value: this.descriptionTemplateId, disabled: disabled }, context.getValidation('descriptionTemplateId').validators],
|
|
|
|
status: [{ value: this.status, disabled: disabled }, context.getValidation('status').validators],
|
|
|
|
description: [{ value: this.description, disabled: disabled }, context.getValidation('description').validators],
|
|
|
|
tags: [{ value: this.tags, disabled: disabled }, context.getValidation('tags').validators],
|
2024-02-02 09:29:38 +01:00
|
|
|
properties: this.buildProperties(),
|
2023-12-06 22:10:01 +01:00
|
|
|
hash: [{ value: this.hash, disabled: disabled }, context.getValidation('hash').validators]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-02 09:29:38 +01:00
|
|
|
buildProperties() {
|
|
|
|
return this.properties.buildForm({
|
|
|
|
rootPath: `properties.`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:10:01 +01:00
|
|
|
createValidationContext(): ValidationContext {
|
|
|
|
const baseContext: ValidationContext = new ValidationContext();
|
|
|
|
const baseValidationArray: Validation[] = new Array<Validation>();
|
|
|
|
baseValidationArray.push({ key: 'id', validators: [BackendErrorValidator(this.validationErrorModel, 'id')] });
|
|
|
|
baseValidationArray.push({ key: 'label', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'label')] });
|
|
|
|
baseValidationArray.push({ key: 'dmpId', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'dmpId')] });
|
|
|
|
baseValidationArray.push({ key: 'dmpDescriptionTemplateId', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'dmpDescriptionTemplateId')] });
|
|
|
|
baseValidationArray.push({ key: 'descriptionTemplateId', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'descriptionTemplateId')] });
|
|
|
|
baseValidationArray.push({ key: 'status', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'status')] });
|
|
|
|
baseValidationArray.push({ key: 'description', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'description')] });
|
|
|
|
baseValidationArray.push({ key: 'tags', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'tags')] });
|
|
|
|
baseValidationArray.push({ key: 'hash', validators: [] });
|
|
|
|
|
|
|
|
baseContext.validation = baseValidationArray;
|
|
|
|
return baseContext;
|
|
|
|
}
|
2024-02-08 16:23:03 +01:00
|
|
|
|
|
|
|
static reApplyPropertiesValidators(params: {
|
|
|
|
formGroup: UntypedFormGroup,
|
|
|
|
validationErrorModel: ValidationErrorModel,
|
|
|
|
}): void {
|
|
|
|
|
|
|
|
const { formGroup, validationErrorModel } = params;
|
|
|
|
const control = formGroup?.get('properties');
|
|
|
|
DescriptionPropertyDefinitionEditorModel.reapplyValidators({
|
|
|
|
formArray: control.get('fieldSets') as UntypedFormArray,
|
|
|
|
rootPath: `properties.`,
|
|
|
|
validationErrorModel: validationErrorModel
|
|
|
|
});
|
|
|
|
}
|
2023-12-06 22:10:01 +01:00
|
|
|
}
|
|
|
|
|
2024-02-02 09:29:38 +01:00
|
|
|
export class DescriptionPropertyDefinitionEditorModel implements DescriptionPropertyDefinitionPersist {
|
|
|
|
fieldSets: Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> = new Map<string, DescriptionPropertyDefinitionFieldSetEditorModel>;
|
2023-12-06 22:10:01 +01:00
|
|
|
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
|
|
|
|
) { }
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
public fromModel(item: DescriptionPropertyDefinition, descriptionTemplate: DescriptionTemplate, descriptionReferences: DescriptionReference[]): DescriptionPropertyDefinitionEditorModel {
|
|
|
|
this.fieldSets = this.calculateProperties(item, descriptionTemplate, descriptionReferences);
|
2024-02-02 09:29:38 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(params?: {
|
|
|
|
context?: ValidationContext,
|
|
|
|
disabled?: boolean,
|
|
|
|
rootPath?: string
|
|
|
|
}): UntypedFormGroup {
|
|
|
|
let { context = null, disabled = false, rootPath } = params ?? {}
|
|
|
|
if (context == null) {
|
|
|
|
context = DescriptionPropertyDefinitionEditorModel.createValidationContext({
|
|
|
|
validationErrorModel: this.validationErrorModel,
|
|
|
|
rootPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const formGroup = this.formBuilder.group({});
|
|
|
|
const fieldSetsFormGroup = this.formBuilder.group({});
|
|
|
|
if (this.fieldSets.size > 0) {
|
|
|
|
this.fieldSets.forEach((value, key) => fieldSetsFormGroup.addControl(key.toString(), value.buildForm({
|
|
|
|
rootPath: `${rootPath}fieldSets[${key}].`
|
|
|
|
})), context.getValidation('fieldSets'));
|
|
|
|
formGroup.addControl('fieldSets', fieldSetsFormGroup);
|
|
|
|
}
|
|
|
|
return formGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
static createValidationContext(params: {
|
|
|
|
rootPath?: string,
|
|
|
|
validationErrorModel: ValidationErrorModel
|
|
|
|
}): ValidationContext {
|
|
|
|
const { rootPath = '', validationErrorModel } = params;
|
|
|
|
|
|
|
|
const baseContext: ValidationContext = new ValidationContext();
|
|
|
|
const baseValidationArray: Validation[] = new Array<Validation>();
|
|
|
|
baseValidationArray.push({ key: 'fieldSets', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}fieldSets`)] });
|
|
|
|
|
|
|
|
baseContext.validation = baseValidationArray;
|
|
|
|
return baseContext;
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:23:03 +01:00
|
|
|
static reapplyValidators(params: {
|
|
|
|
formArray: UntypedFormArray,
|
|
|
|
validationErrorModel: ValidationErrorModel,
|
|
|
|
rootPath: string
|
|
|
|
}): void {
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
private calculateProperties(item: DescriptionPropertyDefinition, descriptionTemplate: DescriptionTemplate, descriptionReferences: DescriptionReference[]): Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> {
|
2024-02-02 09:29:38 +01:00
|
|
|
let result: Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> = new Map<string, DescriptionPropertyDefinitionFieldSetEditorModel>();
|
|
|
|
if (descriptionTemplate) (
|
|
|
|
descriptionTemplate.definition.pages.forEach(definitionPage => {
|
|
|
|
|
|
|
|
definitionPage.sections.forEach(definitionSection => {
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
const sectionResult = this.calculateSectionProperties(definitionSection, item, descriptionReferences);
|
2024-02-02 09:29:38 +01:00
|
|
|
if (sectionResult != null) {
|
|
|
|
result = new Map([...result, ...sectionResult]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
private calculateSectionProperties(definitionSection: DescriptionTemplateSection, item: DescriptionPropertyDefinition, descriptionReferences: DescriptionReference[]): Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> {
|
2024-02-02 09:29:38 +01:00
|
|
|
if (definitionSection == null) return null;
|
|
|
|
let result: Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> = new Map<string, DescriptionPropertyDefinitionFieldSetEditorModel>();
|
|
|
|
|
|
|
|
definitionSection?.fieldSets?.forEach(definitionFieldSet => {
|
2024-02-07 13:15:25 +01:00
|
|
|
const fieldSetResult = this.calculateFieldSetProperties(definitionFieldSet, item, descriptionReferences);
|
2024-02-02 09:29:38 +01:00
|
|
|
if (fieldSetResult != null) {
|
2024-02-02 17:43:24 +01:00
|
|
|
result.set(definitionFieldSet.id, fieldSetResult);
|
2024-02-02 09:29:38 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (definitionSection.sections != null && definitionSection.sections?.length > 0) {
|
|
|
|
definitionSection.sections.forEach(nestedDefinitionSection => {
|
2024-02-07 13:15:25 +01:00
|
|
|
const nestedSectionResult = this.calculateSectionProperties(nestedDefinitionSection, item, descriptionReferences);
|
2024-02-02 09:29:38 +01:00
|
|
|
if (nestedSectionResult != null) {
|
|
|
|
result = new Map([...result, ...nestedSectionResult]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
public calculateFieldSetProperties(definitionFieldSet: DescriptionTemplateFieldSet, item: DescriptionPropertyDefinition, descriptionReferences: DescriptionReference[]): DescriptionPropertyDefinitionFieldSetEditorModel {
|
2024-02-02 09:29:38 +01:00
|
|
|
if (definitionFieldSet == null) return null;
|
|
|
|
|
|
|
|
// current saved values
|
|
|
|
const fieldSetValue: DescriptionPropertyDefinitionFieldSet = item?.fieldSets[definitionFieldSet.id] ?? {};
|
|
|
|
|
|
|
|
// new item case, where we need to add controls for all the containing fields.
|
|
|
|
if (fieldSetValue.items == null || fieldSetValue.items?.length == 0) {
|
|
|
|
|
|
|
|
const fields = new Map<string, DescriptionField>();
|
|
|
|
definitionFieldSet.fields.forEach(definitionField => {
|
2024-02-07 13:15:25 +01:00
|
|
|
fields.set(definitionField.id, {
|
|
|
|
textValue: undefined,
|
|
|
|
textListValue: undefined,
|
|
|
|
dateValue: undefined,
|
|
|
|
externalIdentifier: undefined,
|
|
|
|
});
|
2024-02-02 09:29:38 +01:00
|
|
|
})
|
|
|
|
fieldSetValue.items = [{
|
2024-02-08 16:48:44 +01:00
|
|
|
fields: fields,
|
|
|
|
ordinal: 0
|
2024-02-02 09:29:38 +01:00
|
|
|
} as DescriptionPropertyDefinitionFieldSetItem]
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:23:03 +01:00
|
|
|
return new DescriptionPropertyDefinitionFieldSetEditorModel(this.validationErrorModel).fromModel(fieldSetValue, descriptionReferences, definitionFieldSet);
|
2024-02-02 09:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DescriptionPropertyDefinitionFieldSetEditorModel implements DescriptionPropertyDefinitionFieldSetPersist {
|
|
|
|
items?: DescriptionPropertyDefinitionFieldSetItemEditorModel[] = [];
|
|
|
|
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
|
|
|
|
) { }
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
public fromModel(item: DescriptionPropertyDefinitionFieldSet, descriptionReferences: DescriptionReference[], definitionFieldSet: DescriptionTemplateFieldSet): DescriptionPropertyDefinitionFieldSetEditorModel {
|
2023-12-06 22:10:01 +01:00
|
|
|
if (item) {
|
2024-02-08 16:23:03 +01:00
|
|
|
if (item.items) { item.items.map(x => this.items.push(new DescriptionPropertyDefinitionFieldSetItemEditorModel(this.validationErrorModel).fromModel(x, descriptionReferences, definitionFieldSet))); }
|
2023-12-06 22:10:01 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(params?: {
|
|
|
|
context?: ValidationContext,
|
|
|
|
disabled?: boolean,
|
|
|
|
rootPath?: string
|
|
|
|
}): UntypedFormGroup {
|
|
|
|
let { context = null, disabled = false, rootPath } = params ?? {}
|
|
|
|
if (context == null) {
|
2024-02-02 09:29:38 +01:00
|
|
|
context = DescriptionPropertyDefinitionFieldSetEditorModel.createValidationContext({
|
|
|
|
validationErrorModel: this.validationErrorModel,
|
|
|
|
rootPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.formBuilder.group({
|
|
|
|
items: this.formBuilder.array(
|
|
|
|
(this.items ?? []).map(
|
|
|
|
(item, index) => item.buildForm({
|
|
|
|
rootPath: `${rootPath}items[${index}].`
|
|
|
|
})
|
2024-02-08 16:23:03 +01:00
|
|
|
), context.getValidation('items').validators
|
2024-02-02 09:29:38 +01:00
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static createValidationContext(params: {
|
|
|
|
rootPath?: string,
|
|
|
|
validationErrorModel: ValidationErrorModel
|
|
|
|
}): ValidationContext {
|
|
|
|
const { rootPath = '', validationErrorModel } = params;
|
|
|
|
|
|
|
|
const baseContext: ValidationContext = new ValidationContext();
|
|
|
|
const baseValidationArray: Validation[] = new Array<Validation>();
|
|
|
|
baseValidationArray.push({ key: 'items', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}items`)] });
|
|
|
|
|
|
|
|
baseContext.validation = baseValidationArray;
|
|
|
|
return baseContext;
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:23:03 +01:00
|
|
|
static reapplyValidators(params: {
|
|
|
|
formArray: UntypedFormArray,
|
|
|
|
validationErrorModel: ValidationErrorModel,
|
|
|
|
rootPath: string
|
|
|
|
}): void {
|
|
|
|
const { validationErrorModel, rootPath, formArray } = params;
|
|
|
|
formArray?.controls?.forEach(
|
|
|
|
(control, index) => DescriptionPropertyDefinitionFieldSetItemEditorModel.reapplyValidators({
|
|
|
|
formGroup: control as UntypedFormGroup,
|
|
|
|
rootPath: `${rootPath}items[${index}].`,
|
|
|
|
validationErrorModel: validationErrorModel
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-02 09:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DescriptionPropertyDefinitionFieldSetItemEditorModel implements DescriptionPropertyDefinitionFieldSetItemPersist {
|
|
|
|
fields: Map<string, DescriptionFieldEditorModel> = new Map<string, DescriptionFieldEditorModel>;
|
|
|
|
comment?: string;
|
|
|
|
ordinal?: number;
|
|
|
|
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
|
|
|
|
) { }
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
public fromModel(item: DescriptionPropertyDefinitionFieldSetItem, descriptionReferences: DescriptionReference[], definitionFieldSet: DescriptionTemplateFieldSet): DescriptionPropertyDefinitionFieldSetItemEditorModel {
|
2024-02-02 09:29:38 +01:00
|
|
|
if (item) {
|
|
|
|
this.comment = item.comment;
|
|
|
|
this.ordinal = item.ordinal;
|
2024-02-08 16:23:03 +01:00
|
|
|
if (item.fields) { item.fields?.forEach((value, key) => this.fields.set(key, new DescriptionFieldEditorModel(this.validationErrorModel).fromModel(value, definitionFieldSet?.fields?.find(x => x.id == key), descriptionReferences))); }
|
2024-02-02 09:29:38 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(params?: {
|
|
|
|
context?: ValidationContext,
|
|
|
|
disabled?: boolean,
|
|
|
|
rootPath?: string
|
|
|
|
}): UntypedFormGroup {
|
|
|
|
let { context = null, disabled = false, rootPath } = params ?? {}
|
|
|
|
if (context == null) {
|
|
|
|
context = DescriptionPropertyDefinitionFieldSetItemEditorModel.createValidationContext({
|
2023-12-06 22:10:01 +01:00
|
|
|
validationErrorModel: this.validationErrorModel,
|
|
|
|
rootPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-20 08:20:38 +01:00
|
|
|
const formGroup = this.formBuilder.group({});
|
2024-02-08 16:23:03 +01:00
|
|
|
formGroup.addControl('comment', new FormControl({ value: this.comment, disabled: disabled }, context.getValidation('comment').validators));
|
2024-02-08 16:14:25 +01:00
|
|
|
formGroup.addControl('ordinal', new FormControl({ value: this.ordinal, disabled: disabled }, context.getValidation('ordinal').validators));
|
2024-02-02 09:29:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
const fieldsFormGroup = this.formBuilder.group({});
|
|
|
|
this.fields.forEach((value, key) => fieldsFormGroup.addControl(key.toString(), value.buildForm({
|
|
|
|
rootPath: `${rootPath}fields[${key}].`
|
|
|
|
})), context.getValidation('fields')
|
2023-12-20 08:20:38 +01:00
|
|
|
)
|
2024-02-02 09:29:38 +01:00
|
|
|
formGroup.addControl('fields', fieldsFormGroup);
|
|
|
|
|
2023-12-20 08:20:38 +01:00
|
|
|
return formGroup;
|
2023-12-06 22:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static createValidationContext(params: {
|
|
|
|
rootPath?: string,
|
|
|
|
validationErrorModel: ValidationErrorModel
|
|
|
|
}): ValidationContext {
|
|
|
|
const { rootPath = '', validationErrorModel } = params;
|
|
|
|
|
|
|
|
const baseContext: ValidationContext = new ValidationContext();
|
|
|
|
const baseValidationArray: Validation[] = new Array<Validation>();
|
|
|
|
baseValidationArray.push({ key: 'fields', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}fields`)] });
|
2024-02-02 09:29:38 +01:00
|
|
|
baseValidationArray.push({ key: 'comment', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}comment`)] });
|
|
|
|
baseValidationArray.push({ key: 'ordinal', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}ordinal`)] });
|
2023-12-06 22:10:01 +01:00
|
|
|
|
|
|
|
baseContext.validation = baseValidationArray;
|
|
|
|
return baseContext;
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:23:03 +01:00
|
|
|
static reapplyValidators(params: {
|
|
|
|
formGroup: UntypedFormGroup,
|
|
|
|
validationErrorModel: ValidationErrorModel,
|
|
|
|
rootPath: string
|
|
|
|
}): void {
|
|
|
|
|
|
|
|
const { formGroup, rootPath, validationErrorModel } = params;
|
|
|
|
const context = DescriptionPropertyDefinitionFieldSetItemEditorModel.createValidationContext({
|
|
|
|
rootPath,
|
|
|
|
validationErrorModel
|
|
|
|
});
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
|
|
|
|
['comment', 'ordinal'].forEach(keyField => {
|
|
|
|
const control = formGroup?.get(keyField);
|
|
|
|
control?.clearValidators();
|
|
|
|
control?.addValidators(context.getValidation(keyField).validators);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DescriptionFieldEditorModel implements DescriptionFieldPersist {
|
2024-02-07 13:15:25 +01:00
|
|
|
textValue: string;
|
|
|
|
textListValue: string[];
|
|
|
|
dateValue: Date;
|
|
|
|
references: ReferencePersist[];
|
|
|
|
reference: ReferencePersist;
|
|
|
|
externalIdentifier?: DescriptionExternalIdentifierEditorModel;
|
2023-12-06 22:10:01 +01:00
|
|
|
|
|
|
|
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
|
|
|
|
) { }
|
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
public fromModel(item: DescriptionField, descriptionTemplateField: DescriptionTemplateField, descriptionReferences: DescriptionReference[]): DescriptionFieldEditorModel {
|
2023-12-06 22:10:01 +01:00
|
|
|
if (item) {
|
2024-02-07 13:15:25 +01:00
|
|
|
this.textValue = item.textValue;
|
|
|
|
this.textListValue = item.textListValue;
|
|
|
|
this.dateValue = item.dateValue;
|
|
|
|
this.references = descriptionReferences?.filter(x => x.data?.fieldId == descriptionTemplateField?.id && x.isActive == IsActive.Active).map(x => x.reference);
|
|
|
|
this.reference = descriptionReferences?.find(x => x.data?.fieldId == descriptionTemplateField?.id && x.isActive == IsActive.Active)?.reference;
|
2024-02-08 16:23:03 +01:00
|
|
|
this.externalIdentifier = new DescriptionExternalIdentifierEditorModel(this.validationErrorModel).fromModel(item.externalIdentifier);
|
2023-12-06 22:10:01 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(params?: {
|
|
|
|
context?: ValidationContext,
|
|
|
|
disabled?: boolean,
|
|
|
|
rootPath?: string
|
|
|
|
}): UntypedFormGroup {
|
|
|
|
let { context = null, disabled = false, rootPath } = params ?? {}
|
|
|
|
if (context == null) {
|
|
|
|
context = DescriptionFieldEditorModel.createValidationContext({
|
|
|
|
validationErrorModel: this.validationErrorModel,
|
|
|
|
rootPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.formBuilder.group({
|
2024-02-07 13:15:25 +01:00
|
|
|
textValue: [{ value: this.textValue, disabled: disabled }, context.getValidation('textValue').validators],
|
|
|
|
textListValue: [{ value: this.textListValue, disabled: disabled }, context.getValidation('textListValue').validators],
|
|
|
|
dateValue: [{ value: this.dateValue, disabled: disabled }, context.getValidation('dateValue').validators],
|
|
|
|
references: [{ value: this.references, disabled: disabled }, context.getValidation('references').validators],
|
|
|
|
reference: [{ value: this.reference, disabled: disabled }, context.getValidation('reference').validators]
|
2023-12-06 22:10:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static createValidationContext(params: {
|
|
|
|
rootPath?: string,
|
|
|
|
validationErrorModel: ValidationErrorModel
|
|
|
|
}): ValidationContext {
|
|
|
|
const { rootPath = '', validationErrorModel } = params;
|
|
|
|
|
|
|
|
const baseContext: ValidationContext = new ValidationContext();
|
|
|
|
const baseValidationArray: Validation[] = new Array<Validation>();
|
2024-02-07 13:15:25 +01:00
|
|
|
baseValidationArray.push({ key: 'textValue', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}textValue`)] });
|
|
|
|
baseValidationArray.push({ key: 'textListValue', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}textListValue`)] });
|
|
|
|
baseValidationArray.push({ key: 'dateValue', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}dateValue`)] });
|
|
|
|
baseValidationArray.push({ key: 'references', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}references`)] });
|
|
|
|
baseValidationArray.push({ key: 'reference', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}references`)] });
|
2023-12-06 22:10:01 +01:00
|
|
|
baseContext.validation = baseValidationArray;
|
|
|
|
return baseContext;
|
|
|
|
}
|
2024-02-08 16:23:03 +01:00
|
|
|
|
|
|
|
static reapplyValidators(params: {
|
|
|
|
formGroup: UntypedFormGroup,
|
|
|
|
validationErrorModel: ValidationErrorModel,
|
|
|
|
rootPath: string
|
|
|
|
}): void {
|
|
|
|
|
|
|
|
const { formGroup, rootPath, validationErrorModel } = params;
|
|
|
|
const context = DescriptionPropertyDefinitionFieldSetItemEditorModel.createValidationContext({
|
|
|
|
rootPath,
|
|
|
|
validationErrorModel
|
|
|
|
});
|
|
|
|
|
|
|
|
['textValue', 'textListValue', 'dateValue'].forEach(keyField => {
|
|
|
|
const control = formGroup?.get(keyField);
|
|
|
|
control?.clearValidators();
|
|
|
|
control?.addValidators(context.getValidation(keyField).validators);
|
|
|
|
})
|
|
|
|
|
|
|
|
DescriptionExternalIdentifierEditorModel.reapplyValidators({
|
|
|
|
formGroup: formGroup?.get('externalIdentifier') as UntypedFormGroup,
|
|
|
|
rootPath: `${rootPath}externalIdentifier.`,
|
|
|
|
validationErrorModel: validationErrorModel
|
|
|
|
});
|
|
|
|
}
|
2024-02-07 13:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DescriptionExternalIdentifierEditorModel implements DescriptionExternalIdentifierPersist {
|
|
|
|
identifier: string;
|
|
|
|
type: string;
|
|
|
|
|
|
|
|
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
|
|
|
|
) { }
|
|
|
|
|
|
|
|
public fromModel(item: DescriptionExternalIdentifier): DescriptionExternalIdentifierEditorModel {
|
|
|
|
if (item) {
|
|
|
|
this.identifier = item.identifier;
|
|
|
|
this.type = item.type;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(params?: {
|
|
|
|
context?: ValidationContext,
|
|
|
|
disabled?: boolean,
|
|
|
|
rootPath?: string
|
|
|
|
}): UntypedFormGroup {
|
|
|
|
let { context = null, disabled = false, rootPath } = params ?? {}
|
|
|
|
if (context == null) {
|
|
|
|
context = DescriptionExternalIdentifierEditorModel.createValidationContext({
|
|
|
|
validationErrorModel: this.validationErrorModel,
|
|
|
|
rootPath
|
|
|
|
});
|
|
|
|
}
|
2023-12-06 22:10:01 +01:00
|
|
|
|
2024-02-07 13:15:25 +01:00
|
|
|
return this.formBuilder.group({
|
|
|
|
identifier: [{ value: this.identifier, disabled: disabled }, context.getValidation('identifier').validators],
|
|
|
|
type: [{ value: this.type, disabled: disabled }, context.getValidation('type').validators],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static createValidationContext(params: {
|
|
|
|
rootPath?: string,
|
|
|
|
validationErrorModel: ValidationErrorModel
|
|
|
|
}): ValidationContext {
|
|
|
|
const { rootPath = '', validationErrorModel } = params;
|
|
|
|
|
|
|
|
const baseContext: ValidationContext = new ValidationContext();
|
|
|
|
const baseValidationArray: Validation[] = new Array<Validation>();
|
|
|
|
baseValidationArray.push({ key: 'identifier', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}identifier`)] });
|
|
|
|
baseValidationArray.push({ key: 'type', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}type`)] });
|
|
|
|
baseContext.validation = baseValidationArray;
|
|
|
|
return baseContext;
|
|
|
|
}
|
2024-02-08 16:23:03 +01:00
|
|
|
|
|
|
|
static reapplyValidators(params: {
|
|
|
|
formGroup: UntypedFormGroup,
|
|
|
|
validationErrorModel: ValidationErrorModel,
|
|
|
|
rootPath: string
|
|
|
|
}): void {
|
|
|
|
|
|
|
|
const { formGroup, rootPath, validationErrorModel } = params;
|
|
|
|
const context = DescriptionExternalIdentifierEditorModel.createValidationContext({
|
|
|
|
rootPath,
|
|
|
|
validationErrorModel
|
|
|
|
});
|
|
|
|
|
|
|
|
['identifier', 'type'].forEach(keyField => {
|
|
|
|
const control = formGroup?.get(keyField);
|
|
|
|
control?.clearValidators();
|
|
|
|
control?.addValidators(context.getValidation(keyField).validators);
|
|
|
|
});
|
|
|
|
}
|
2023-12-06 22:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DescriptionReferenceEditorModel implements DescriptionReferencePersist {
|
|
|
|
id: Guid;
|
|
|
|
reference?: ReferencePersist;
|
|
|
|
hash: string;
|
|
|
|
|
|
|
|
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
|
|
|
|
) { }
|
|
|
|
|
|
|
|
fromModel(item: DescriptionReference): DescriptionReferenceEditorModel {
|
|
|
|
this.id = item.id;
|
|
|
|
this.reference = item.reference;
|
|
|
|
this.hash = item.hash;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(params?: {
|
|
|
|
context?: ValidationContext,
|
|
|
|
disabled?: boolean,
|
|
|
|
rootPath?: string
|
|
|
|
}): UntypedFormGroup {
|
|
|
|
let { context = null, disabled = false, rootPath } = params ?? {}
|
|
|
|
if (context == null) {
|
|
|
|
context = DescriptionReferenceEditorModel.createValidationContext({
|
|
|
|
validationErrorModel: this.validationErrorModel,
|
|
|
|
rootPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.formBuilder.group({
|
|
|
|
id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators],
|
|
|
|
reference: [{ value: this.reference, disabled: disabled }, context.getValidation('reference').validators],
|
|
|
|
hash: [{ value: this.hash, disabled: disabled }, context.getValidation('hash').validators]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static createValidationContext(params: {
|
|
|
|
rootPath?: string,
|
|
|
|
validationErrorModel: ValidationErrorModel
|
|
|
|
}): ValidationContext {
|
|
|
|
const { rootPath = '', validationErrorModel } = params;
|
|
|
|
|
|
|
|
const baseContext: ValidationContext = new ValidationContext();
|
|
|
|
const baseValidationArray: Validation[] = new Array<Validation>();
|
|
|
|
baseValidationArray.push({ key: 'id', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}id`)] });
|
|
|
|
baseValidationArray.push({ key: 'reference', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}reference`)] });
|
|
|
|
baseValidationArray.push({ key: 'hash', validators: [] });
|
|
|
|
|
|
|
|
baseContext.validation = baseValidationArray;
|
|
|
|
return baseContext;
|
|
|
|
}
|
2024-02-08 16:23:03 +01:00
|
|
|
|
|
|
|
static reapplyValidators(params: {
|
|
|
|
formGroup: UntypedFormGroup,
|
|
|
|
validationErrorModel: ValidationErrorModel,
|
|
|
|
rootPath: string
|
|
|
|
}): void {
|
|
|
|
|
|
|
|
const { formGroup, rootPath, validationErrorModel } = params;
|
|
|
|
const context = DescriptionExternalIdentifierEditorModel.createValidationContext({
|
|
|
|
rootPath,
|
|
|
|
validationErrorModel
|
|
|
|
});
|
|
|
|
|
|
|
|
['id', 'reference'].forEach(keyField => {
|
|
|
|
const control = formGroup?.get(keyField);
|
|
|
|
control?.clearValidators();
|
|
|
|
control?.addValidators(context.getValidation(keyField).validators);
|
|
|
|
});
|
|
|
|
}
|
2024-02-07 13:15:25 +01:00
|
|
|
}
|