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

432 lines
18 KiB
TypeScript
Raw Normal View History

2024-01-18 13:23:20 +01:00
import { FormControl, UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms";
2023-12-28 16:18:49 +01:00
import { DmpAccessType } from "@app/core/common/enum/dmp-access-type";
import { DmpStatus } from "@app/core/common/enum/dmp-status";
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";
import { Dmp, DmpBlueprintValue, DmpBlueprintValuePersist, DmpContact, DmpContactPersist, DmpDescriptionTemplate, DmpDescriptionTemplatePersist, DmpPersist, DmpProperties, DmpPropertiesPersist, DmpReferenceDataPersist, DmpReferencePersist } 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-18 13:23:20 +01:00
properties: DmpPropertiesEditorModel = new DmpPropertiesEditorModel();
2023-12-28 16:18:49 +01:00
description: String;
language: String;
blueprint: Guid;
accessType: DmpAccessType;
descriptionTemplates: DmpDescriptionTemplateEditorModel[] = [];
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-19 17:28:53 +01:00
this.properties = new DmpPropertiesEditorModel().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-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 => {
this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel().fromModel(
{
sectionId: section.id,
descriptionTemplateGroupId: dmpDescriptionTemplate?.descriptionTemplateGroupId,
}));
});
} else if (section.descriptionTemplates?.length > 0) {
section.descriptionTemplates.forEach(blueprintDefinedDescriptionTemplate => {
this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel().fromModel(
{
sectionId: section.id,
descriptionTemplateGroupId: blueprintDefinedDescriptionTemplate?.descriptionTemplateId,
}));
});
} else {
this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel().fromModel(
{
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.`
}),
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')] });
baseValidationArray.push({ key: 'hash', validators: [] });
baseContext.validation = baseValidationArray;
return baseContext;
}
}
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 => {
section.fields.forEach(field => {
2024-01-19 17:28:53 +01:00
this.dmpBlueprintValues.set(field.id, new DmpBlueprintValueEditorModel().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));
});
});
if (item?.contacts) { item.contacts.map(x => this.contacts.push(new DmpContactEditorModel().fromModel(x))); }
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(
(item, index) => new DmpContactEditorModel(
this.validationErrorModel
).fromModel(item).buildForm({
rootPath: `contacts[${index}].`
}), context.getValidation('contacts')
)
),
});
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({
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;
}
}
export class DmpBlueprintValueEditorModel implements DmpBlueprintValuePersist {
fieldId: Guid;
fieldValue: string;
references: DmpReferencePersist[];
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-01-18 13:23:20 +01:00
this.references = dmpReferences?.filter(x => x.data?.blueprintFieldId === this.fieldId) || [];
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;
}
}
export class DmpContactEditorModel implements DmpContactPersist {
userId: Guid;
firstName: string;
lastName: string;
email: string;
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DmpContact): DmpContactEditorModel {
this.userId = item.userId;
this.firstName = item.firstName;
this.lastName = item.lastName;
this.email = item.email;
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({
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],
});
}
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`)] });
baseContext.validation = baseValidationArray;
return baseContext;
}
}
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;
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;
}
}