argos/dmp-frontend/src/app/ui/dmp/dmp-editor-blueprint/dmp-editor.model.ts

664 lines
26 KiB
TypeScript
Raw Normal View History

2024-01-23 13:12:50 +01:00
import { FormArray, FormControl, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms";
2023-12-28 16:18:49 +01:00
import { DmpAccessType } from "@app/core/common/enum/dmp-access-type";
2024-01-31 10:27:36 +01:00
import { DmpContactType } from "@app/core/common/enum/dmp-contact-type";
2023-12-28 16:18:49 +01:00
import { DmpStatus } from "@app/core/common/enum/dmp-status";
2024-02-20 16:00:20 +01:00
import { DmpUserRole } from "@app/core/common/enum/dmp-user-role";
2024-01-19 17:28:53 +01:00
import { IsActive } from "@app/core/common/enum/is-active.enum";
2024-01-15 17:47:34 +01:00
import { DmpBlueprint } from "@app/core/model/dmp-blueprint/dmp-blueprint";
2024-02-20 16:00:20 +01:00
import { Dmp, DmpBlueprintValue, DmpBlueprintValuePersist, DmpContact, DmpContactPersist, DmpDescriptionTemplate, DmpDescriptionTemplatePersist, DmpPersist, DmpProperties, DmpPropertiesPersist, DmpReferenceDataPersist, DmpReferencePersist, DmpUser, DmpUserPersist } from "@app/core/model/dmp/dmp";
2023-12-28 16:18:49 +01:00
import { DmpReference } from "@app/core/model/dmp/dmp-reference";
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 DmpEditorModel extends BaseEditorModel implements DmpPersist {
label: string;
status: DmpStatus;
2024-01-31 10:27:36 +01:00
properties: DmpPropertiesEditorModel = new DmpPropertiesEditorModel(this.validationErrorModel);
2023-12-28 16:18:49 +01:00
description: String;
language: String;
blueprint: Guid;
accessType: DmpAccessType;
descriptionTemplates: DmpDescriptionTemplateEditorModel[] = [];
2024-02-20 16:00:20 +01:00
users: DmpUserEditorModel[] = [];
2023-12-28 16:18:49 +01:00
permissions: string[];
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel();
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor() { super(); }
public fromModel(item: Dmp): DmpEditorModel {
if (item) {
super.fromModel(item);
this.label = item.label;
this.status = item.status;
2024-01-29 14:25:59 +01:00
this.properties = new DmpPropertiesEditorModel(this.validationErrorModel).fromModel(item.properties, item.dmpReferences?.filter(x => x.isActive === IsActive.Active), item.blueprint);
2023-12-28 16:18:49 +01:00
this.description = item.description;
this.language = item.language;
this.blueprint = item.blueprint?.id;
this.accessType = item.accessType;
2024-02-20 16:00:20 +01:00
if (item?.dmpUsers) { item.dmpUsers.map(x => this.users.push(new DmpUserEditorModel(this.validationErrorModel).fromModel(x))); }
2024-01-18 13:23:20 +01:00
item.blueprint.definition.sections.forEach(section => {
if (section.hasTemplates) {
const sectionTempaltesFromDmp = item.dmpDescriptionTemplates?.filter(x => x.sectionId == section.id) || [];
if (sectionTempaltesFromDmp.length > 0) {
item.dmpDescriptionTemplates?.filter(x => x.sectionId == section.id).forEach(dmpDescriptionTemplate => {
2024-01-23 13:12:50 +01:00
this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel(this.validationErrorModel).fromModel(
2024-01-18 13:23:20 +01:00
{
sectionId: section.id,
descriptionTemplateGroupId: dmpDescriptionTemplate?.descriptionTemplateGroupId,
}));
});
} else if (section.descriptionTemplates?.length > 0) {
section.descriptionTemplates.forEach(blueprintDefinedDescriptionTemplate => {
2024-01-23 13:12:50 +01:00
this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel(this.validationErrorModel).fromModel(
2024-01-18 13:23:20 +01:00
{
sectionId: section.id,
descriptionTemplateGroupId: blueprintDefinedDescriptionTemplate?.descriptionTemplateGroupId,
2024-01-18 13:23:20 +01:00
}));
});
} else {
2024-01-23 13:12:50 +01:00
this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel(this.validationErrorModel).fromModel(
2024-01-18 13:23:20 +01:00
{
sectionId: section.id,
}));
}
}
});
2023-12-28 16:18:49 +01:00
}
return this;
}
buildForm(context: ValidationContext = null, disabled: boolean = false): UntypedFormGroup {
if (context == null) { context = this.createValidationContext(); }
const formGroup = this.formBuilder.group({
id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators],
label: [{ value: this.label, disabled: disabled }, context.getValidation('label').validators],
status: [{ value: this.status, disabled: disabled }, context.getValidation('status').validators],
2024-01-15 17:47:34 +01:00
properties: this.properties.buildForm({
rootPath: `properties.`
}),
2024-02-20 16:00:20 +01:00
users: this.formBuilder.array(
(this.users ?? []).map(
(item, index) => item.buildForm({
rootPath: `users[${index}].`
})
), context.getValidation('users').validators
),
2023-12-28 16:18:49 +01:00
description: [{ value: this.description, disabled: disabled }, context.getValidation('description').validators],
language: [{ value: this.language, disabled: disabled }, context.getValidation('language').validators],
blueprint: [{ value: this.blueprint, disabled: disabled }, context.getValidation('blueprint').validators],
accessType: [{ value: this.accessType, disabled: disabled }, context.getValidation('accessType').validators],
hash: [{ value: this.hash, disabled: disabled }, context.getValidation('hash').validators]
});
2024-01-15 17:47:34 +01:00
const descriptionTemplatesFormGroup = this.formBuilder.group({});
(this.descriptionTemplates ?? []).filter(x => x?.sectionId).map(x => x.sectionId).map(
2024-01-18 13:23:20 +01:00
(item, index) => descriptionTemplatesFormGroup.addControl(item.toString(),
new FormControl(this.descriptionTemplates?.filter(x => x.sectionId === item)?.filter(x => x.descriptionTemplateGroupId).map(x => x.descriptionTemplateGroupId) || [], context.getValidation('descriptionTemplates').validators))
);
// // buildForm({
// // rootPath: `descriptionTemplates[${index}].`
// })), //context.getValidation('descriptionTemplates')
// ))
2024-01-15 17:47:34 +01:00
formGroup.addControl('descriptionTemplates', descriptionTemplatesFormGroup);
2023-12-28 16:18:49 +01:00
return formGroup;
}
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: 'status', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'status')] });
baseValidationArray.push({ key: 'properties', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'properties')] });
baseValidationArray.push({ key: 'description', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'description')] });
baseValidationArray.push({ key: 'language', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'language')] });
baseValidationArray.push({ key: 'blueprint', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'blueprint')] });
baseValidationArray.push({ key: 'accessType', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'accessType')] });
baseValidationArray.push({ key: 'descriptionTemplates', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'descriptionTemplates')] });
2024-02-20 16:00:20 +01:00
baseValidationArray.push({ key: 'users', validators: [BackendErrorValidator(this.validationErrorModel, `users`)] });
2023-12-28 16:18:49 +01:00
baseValidationArray.push({ key: 'hash', validators: [] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-01-23 13:12:50 +01:00
2024-01-31 10:27:36 +01:00
createChildContact(index: number): UntypedFormGroup {
const contact: DmpContactEditorModel = new DmpContactEditorModel(this.validationErrorModel);
return contact.buildForm({ rootPath: 'properties.contacts[' + index + '].' });
}
2024-02-20 16:00:20 +01:00
createChildUser(index: number): UntypedFormGroup {
const user: DmpUserEditorModel = new DmpUserEditorModel(this.validationErrorModel);
return user.buildForm({ rootPath: 'users[' + index + '].' });
}
2024-01-23 13:12:50 +01:00
static reApplyPropertiesValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
}): void {
const { formGroup, validationErrorModel } = params;
const control = formGroup?.get('properties');
DmpPropertiesEditorModel.reapplyValidators({
formGroup: control as UntypedFormGroup,
rootPath: `properties.`,
validationErrorModel: validationErrorModel
});
2024-02-20 16:00:20 +01:00
const descriptionTemplates = formGroup?.get('descriptionTemplates') as UntypedFormGroup;
const keys = Object.keys(descriptionTemplates.value as Object);
keys.forEach((key) => {
const control = descriptionTemplates?.get(key);
DmpBlueprintValueEditorModel.reapplyValidators({
formGroup: control as UntypedFormGroup,
rootPath: `descriptionTemplates[${key}].`,
validationErrorModel: validationErrorModel
})
});
(formGroup.get('users') as FormArray).controls?.forEach(
(control, index) => DmpUserEditorModel.reapplyValidators({
2024-01-23 13:12:50 +01:00
formGroup: control as UntypedFormGroup,
2024-02-20 16:00:20 +01:00
rootPath: `users[${index}].`,
2024-01-23 13:12:50 +01:00
validationErrorModel: validationErrorModel
})
);
}
2023-12-28 16:18:49 +01:00
}
2024-01-15 17:47:34 +01:00
export class DmpPropertiesEditorModel implements DmpPropertiesPersist {
2024-01-19 17:28:53 +01:00
dmpBlueprintValues: Map<Guid, DmpBlueprintValueEditorModel> = new Map<Guid, DmpBlueprintValueEditorModel>;
2024-01-15 17:47:34 +01:00
contacts: DmpContactEditorModel[] = [];
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DmpProperties, dmpReferences: DmpReference[], dmpBlueprint: DmpBlueprint): DmpPropertiesEditorModel {
2024-01-19 17:28:53 +01:00
2024-01-15 17:47:34 +01:00
dmpBlueprint.definition.sections.forEach(section => {
2024-01-29 10:47:12 +01:00
section.fields?.forEach(field => {
2024-01-23 13:12:50 +01:00
this.dmpBlueprintValues.set(field.id, new DmpBlueprintValueEditorModel(this.validationErrorModel).fromModel(
2024-01-15 17:47:34 +01:00
{
fieldId: field.id,
2024-01-18 13:23:20 +01:00
fieldValue: item?.dmpBlueprintValues?.find(x => x.fieldId == field.id)?.fieldValue,
2024-01-15 17:47:34 +01:00
}, dmpReferences));
});
});
2024-01-23 13:12:50 +01:00
if (item?.contacts) { item.contacts.map(x => this.contacts.push(new DmpContactEditorModel(this.validationErrorModel).fromModel(x))); }
2024-01-15 17:47:34 +01:00
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
2024-01-19 17:28:53 +01:00
}, dmpReferences?: DmpReference[], dmpBlueprint?: DmpBlueprint): UntypedFormGroup {
2024-01-15 17:47:34 +01:00
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DmpPropertiesEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
const formGroup = this.formBuilder.group({
contacts: this.formBuilder.array(
(this.contacts ?? []).map(
2024-02-01 10:36:55 +01:00
(item, index) => item.buildForm({
2024-01-23 13:12:50 +01:00
rootPath: `${rootPath}contacts[${index}].`
2024-01-31 10:27:36 +01:00
})
), context.getValidation('contacts').validators
2024-01-15 17:47:34 +01:00
),
});
2024-01-18 13:23:20 +01:00
const dmpBlueprintValuesFormGroup = this.formBuilder.group({});
2024-01-19 17:28:53 +01:00
this.dmpBlueprintValues.forEach((value, key) => dmpBlueprintValuesFormGroup.addControl(key.toString(), value.buildForm({
2024-01-23 13:12:50 +01:00
rootPath: `${rootPath}dmpBlueprintValues[${key}].`
2024-01-15 17:47:34 +01:00
})), context.getValidation('dmpBlueprintValues')
)
2024-01-18 13:23:20 +01:00
formGroup.addControl('dmpBlueprintValues', dmpBlueprintValuesFormGroup);
2024-01-15 17:47:34 +01:00
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: 'dmpBlueprintValues', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}dmpBlueprintValues`)] });
baseValidationArray.push({ key: 'contacts', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}contacts`)] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-01-23 13:12:50 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
2024-02-09 15:16:46 +01:00
const dmpBlueprintValues = formGroup?.get('dmpBlueprintValues') as UntypedFormGroup;
const keys = Object.keys(dmpBlueprintValues.value as Object);
keys.forEach((key) => {
const control = dmpBlueprintValues?.get(key);
DmpBlueprintValueEditorModel.reapplyValidators({
formGroup: control as UntypedFormGroup,
rootPath: `${rootPath}dmpBlueprintValues[${key}].`,
validationErrorModel: validationErrorModel
})
});
2024-01-23 13:12:50 +01:00
(formGroup.get('contacts') as FormArray).controls?.forEach(
(control, index) => DmpContactEditorModel.reapplyValidators({
formGroup: control as UntypedFormGroup,
rootPath: `${rootPath}contacts[${index}].`,
validationErrorModel: validationErrorModel
})
);
}
2024-01-15 17:47:34 +01:00
}
export class DmpBlueprintValueEditorModel implements DmpBlueprintValuePersist {
fieldId: Guid;
fieldValue: string;
2024-02-21 10:03:11 +01:00
references: DmpReferencePersist[] = [];
2024-01-15 17:47:34 +01:00
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DmpBlueprintValue, dmpReferences: DmpReference[]): DmpBlueprintValueEditorModel {
this.fieldId = item.fieldId;
this.fieldValue = item.fieldValue;
2024-02-21 10:03:11 +01:00
// TODO check typeId field
dmpReferences.forEach(dmpReference => {
if(dmpReference.data.blueprintFieldId == this.fieldId){
this.references.push({
data: dmpReference.data,
reference: {
id: dmpReference.reference.id,
label: dmpReference.reference.label,
reference: dmpReference.reference.reference,
source: dmpReference.reference.source,
typeId: null,
description: dmpReference.reference.source,
definition: dmpReference.reference.definition,
abbreviation: dmpReference.reference.abbreviation,
sourceType: dmpReference.reference.sourceType
}
})
}
})
2024-01-15 17:47:34 +01:00
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DmpBlueprintValueEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
return this.formBuilder.group({
fieldId: [{ value: this.fieldId, disabled: disabled }, context.getValidation('fieldId').validators],
fieldValue: [{ value: this.fieldValue, disabled: disabled }, context.getValidation('fieldValue').validators],
2024-01-18 13:23:20 +01:00
references: [{ value: this.references?.map(x => x.reference), disabled: disabled }, context.getValidation('references').validators],
2024-01-15 17:47:34 +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: 'fieldId', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}fieldId`)] });
baseValidationArray.push({ key: 'fieldValue', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}fieldValue`)] });
baseValidationArray.push({ key: 'references', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}references`)] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-01-23 13:12:50 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DmpBlueprintValueEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
['fieldId', 'fieldValue', 'references'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
})
}
2024-01-15 17:47:34 +01:00
}
export class DmpContactEditorModel implements DmpContactPersist {
userId: Guid;
firstName: string;
lastName: string;
email: string;
2024-01-31 10:41:11 +01:00
contactType: DmpContactType= DmpContactType.Internal;
2024-01-15 17:47:34 +01:00
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DmpContact): DmpContactEditorModel {
2024-01-31 10:27:36 +01:00
if(item?.user?.id) this.userId = item.user.id;
2024-01-15 17:47:34 +01:00
this.firstName = item.firstName;
this.lastName = item.lastName;
2024-01-31 10:27:36 +01:00
this.email = item.email;
2024-02-01 10:36:55 +01:00
this.contactType = (item == null || this.userId != null) ? DmpContactType.Internal : DmpContactType.External;
2024-01-15 17:47:34 +01:00
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
2024-01-31 10:27:36 +01:00
context = DmpContactEditorModel.createValidationContext({
2024-01-15 17:47:34 +01:00
validationErrorModel: this.validationErrorModel,
rootPath
});
}
return this.formBuilder.group({
userId: [{ value: this.userId, disabled: disabled }, context.getValidation('userId').validators],
firstName: [{ value: this.firstName, disabled: disabled }, context.getValidation('firstName').validators],
lastName: [{ value: this.lastName, disabled: disabled }, context.getValidation('lastName').validators],
email: [{ value: this.email, disabled: disabled }, context.getValidation('email').validators],
2024-01-31 10:27:36 +01:00
contactType: [{ value: this.contactType, disabled: disabled }, context.getValidation('contactType').validators],
2024-01-15 17:47:34 +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: 'userId', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}userId`)] });
baseValidationArray.push({ key: 'firstName', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}firstName`)] });
baseValidationArray.push({ key: 'lastName', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}lastName`)] });
baseValidationArray.push({ key: 'email', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}email`)] });
2024-01-31 10:27:36 +01:00
baseValidationArray.push({ key: 'contactType', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}contactType`)] });
2024-01-15 17:47:34 +01:00
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-01-23 13:12:50 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DmpContactEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
['userId', 'firstName', 'lastName', 'email'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
})
}
2024-01-15 17:47:34 +01:00
}
2024-02-20 16:00:20 +01:00
export class DmpUserEditorModel implements DmpUserPersist {
user: Guid;
role: DmpUserRole;
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DmpUser): DmpUserEditorModel {
if(item?.user?.id) this.user = item.user.id;
this.role = item.role;
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DmpUserEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
return this.formBuilder.group({
user: [{ value: this.user, disabled: disabled }, context.getValidation('user').validators],
role: [{ value: this.role, disabled: disabled }, context.getValidation('role').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: 'user', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}user`)] });
baseValidationArray.push({ key: 'role', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}role`)] });
baseContext.validation = baseValidationArray;
return baseContext;
}
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DmpUserEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
['user', 'role'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
})
}
}
2023-12-28 16:18:49 +01:00
export class DmpReferenceEditorModel implements DmpReferencePersist {
id: Guid;
reference: ReferencePersist;
2024-01-15 17:47:34 +01:00
referenceId: Guid;
data: DmpReferenceDataPersist;
2023-12-28 16:18:49 +01:00
hash: string;
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DmpReference): DmpReferenceEditorModel {
this.id = item.id;
// this.reference = item.reference; //TODO: refactor reference type
2023-12-28 16:18:49 +01:00
this.data = item.data;
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 = DmpReferenceEditorModel.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],
data: [{ value: this.data, disabled: disabled }, context.getValidation('data').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: 'data', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}data`)] });
baseValidationArray.push({ key: 'hash', validators: [] });
baseContext.validation = baseValidationArray;
return baseContext;
}
}
export class DmpDescriptionTemplateEditorModel implements DmpDescriptionTemplatePersist {
descriptionTemplateGroupId: Guid;
sectionId: Guid;
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DmpDescriptionTemplate): DmpDescriptionTemplateEditorModel {
this.descriptionTemplateGroupId = item.descriptionTemplateGroupId;
this.sectionId = item.sectionId;
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DmpDescriptionTemplateEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
return this.formBuilder.group({
2024-01-15 17:47:34 +01:00
descriptionTemplateGroupId: [{ value: this.descriptionTemplateGroupId, disabled: disabled }, context.getValidation('descriptionTemplateGroupId').validators],
2023-12-28 16:18:49 +01:00
sectionId: [{ value: this.sectionId, disabled: disabled }, context.getValidation('sectionId').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: 'descriptionTemplateGroupId', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}descriptionTemplateGroupId`)] });
baseValidationArray.push({ key: 'sectionId', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}sectionId`)] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-01-23 13:12:50 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DmpDescriptionTemplateEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
['descriptionTemplateGroupId', 'sectionId'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
})
}
2023-12-28 16:18:49 +01:00
}