import { Serializable } from "../Serializable"; import { ValidationContext } from "../../utilities/validators/ValidationContext"; import { FormGroup, FormBuilder, FormControl, Validators } from "@angular/forms"; import { BackendErrorValidator } from "../../utilities/validators/BackendErrorValidator"; import { BaseErrorModel } from "../error/BaseErrorModel"; import { ExternalSourcesItemModel } from "../external-sources/ExternalSourcesItemModel"; import { ProjectModel } from "../projects/ProjectModel"; import { OrganisationModel } from "../organisation/OrganisationModel"; import { ResearcherModel } from "../researcher/ResearcherModel"; import { JsonSerializer } from "../../utilities/JsonSerializer"; import { ProfileModel } from "../profile/ProfileModel"; import { Status } from "../Status"; import { DmpUsersModel } from "@app/models/dmpUsers/DmpUsersModel"; export class DataManagementPlanModel implements Serializable { public id: String; public label: String; public previous: String; public version: number; public status: Status = Status.Active; public description: String; public project: ProjectModel; public organisations: OrganisationModel[] = []; public researchers: ResearcherModel[] = []; public profiles: ProfileModel[] = []; public associatedUsers: DmpUsersModel[] = []; public errorModel: BaseErrorModel = new BaseErrorModel(); fromJSONObject(item: any): DataManagementPlanModel { this.id = item.id; this.label = item.label; this.previous = item.previous; this.version = item.version; this.status = item.status; this.description = item.description; this.project = JsonSerializer.fromJSONObject(item.project, ProjectModel); this.organisations = JsonSerializer.fromJSONArray(item.organisations, OrganisationModel); this.researchers = JsonSerializer.fromJSONArray(item.researchers, ResearcherModel); this.profiles = JsonSerializer.fromJSONArray(item.profiles, ProfileModel); this.associatedUsers = JsonSerializer.fromJSONArray(item.associatedUsers, DmpUsersModel); return this; } buildForm(context: ValidationContext = null, disabled: boolean = false): FormGroup { if (context == null) { context = this.createValidationContext(); } const formGroup = new FormBuilder().group({ id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators], label: [{ value: this.label, disabled: disabled }, context.getValidation('label').validators], previous: [{ value: this.previous, disabled: disabled }, context.getValidation('previous').validators], version: [{ value: this.version, disabled: disabled }, context.getValidation('version').validators], status: [{ value: this.status, disabled: disabled }, context.getValidation('status').validators], description: [{ value: this.description, disabled: disabled }, context.getValidation('description').validators], project: [{ value: this.project, disabled: disabled }, context.getValidation('project').validators], organisations: [{ value: this.organisations, disabled: disabled }, context.getValidation('organisations').validators], researchers: [{ value: this.researchers, disabled: disabled }, context.getValidation('researchers').validators], profiles: [{ value: this.profiles, disabled: disabled },{validators:Validators.required}, context.getValidation('profiles').validators], associatedUsers: [{ value: this.associatedUsers, disabled: disabled }, context.getValidation('associatedUsers').validators] }); return formGroup; } createValidationContext(): ValidationContext { const baseContext: ValidationContext = new ValidationContext(); baseContext.validation.push({ key: 'id', validators: [ BackendErrorValidator(this.errorModel, 'id')] }); baseContext.validation.push({ key: 'label', validators: [Validators.required, BackendErrorValidator(this.errorModel, 'label')] }); baseContext.validation.push({ key: 'previous', validators: [BackendErrorValidator(this.errorModel, 'previous')] }); baseContext.validation.push({ key: 'version', validators: [BackendErrorValidator(this.errorModel, 'version')] }); baseContext.validation.push({ key: 'status', validators: [Validators.required, BackendErrorValidator(this.errorModel, 'status')] }); baseContext.validation.push({ key: 'description', validators: [Validators.required, BackendErrorValidator(this.errorModel, 'description')] }); baseContext.validation.push({ key: 'project', validators: [Validators.required, BackendErrorValidator(this.errorModel, 'project')] }); baseContext.validation.push({ key: 'organisations', validators: [BackendErrorValidator(this.errorModel, 'organisations')] }); baseContext.validation.push({ key: 'researchers', validators: [BackendErrorValidator(this.errorModel, 'researchers')] }); baseContext.validation.push({ key: 'profiles', validators: [BackendErrorValidator(this.errorModel, 'profiles')] }); baseContext.validation.push({ key: 'associatedUsers', validators: [BackendErrorValidator(this.errorModel, 'associatedUsers')] }); return baseContext; } }