argos/dmp-frontend/src/app/models/projects/ProjectModel.ts

81 lines
4.0 KiB
TypeScript

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 { Status } from '../Status';
import { ContentFile } from '../../models/files/ContentFile';
import { JsonSerializer } from '../../utilities/JsonSerializer';
export enum ProjectType {
External = 0,
Internal = 1
}
export class ProjectModel implements Serializable<ProjectModel> {
public id: string;
public label: string;
public abbreviation: string;
public reference: string;
public type: ProjectType = ProjectType.Internal;
public uri: String;
public status: Status = Status.Active;
public startDate: Date;
public endDate: Date;
public description: String;
public contentUrl: string;
public files: ContentFile[];
public errorModel: BaseErrorModel = new BaseErrorModel();
fromJSONObject(item: any): ProjectModel {
this.id = item.id;
this.label = item.label;
this.type = item.type;
this.abbreviation = item.abbreviation;
this.reference = item.reference;
this.uri = item.uri;
this.status = item.status;
this.startDate = item.startDate ? new Date(item.startDate) : null;
this.endDate = item.endDate ? new Date(item.endDate) : null;
this.description = item.description;
this.contentUrl = item.contentUrl;
this.files = JsonSerializer.fromJSONArray(item.files, ContentFile);
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],
abbreviation: [{ value: this.abbreviation, disabled: disabled }, context.getValidation('abbreviation').validators],
uri: [{ value: this.uri, disabled: disabled }, context.getValidation('uri').validators],
status: [{ value: this.status, disabled: disabled }, context.getValidation('status').validators],
type: [{ value: this.type, disabled: disabled }, context.getValidation('type').validators],
description: [{ value: this.description, disabled: disabled }, context.getValidation('description').validators],
startDate: [{ value: this.startDate, disabled: disabled }, context.getValidation('startDate').validators],
endDate: [{ value: this.endDate, disabled: disabled }, context.getValidation('endDate').validators],
files: [{ value: this.files, disabled: disabled }, context.getValidation('files').validators]
});
return formGroup;
}
createValidationContext(): ValidationContext {
const baseContext: ValidationContext = new ValidationContext();
baseContext.validation.push({ key: 'id', validators: [] });
baseContext.validation.push({ key: 'label', validators: [Validators.required, BackendErrorValidator(this.errorModel, 'label')] });
baseContext.validation.push({ key: 'abbreviation', validators: [BackendErrorValidator(this.errorModel, 'abbreviation')] });
baseContext.validation.push({ key: 'uri', validators: [BackendErrorValidator(this.errorModel, 'uri')] });
baseContext.validation.push({ key: 'type', validators: [BackendErrorValidator(this.errorModel, 'type')] });
baseContext.validation.push({ key: 'status', validators: [] });
baseContext.validation.push({ key: 'description', validators: [Validators.required, BackendErrorValidator(this.errorModel, 'description')] });
baseContext.validation.push({ key: 'startDate', validators: [BackendErrorValidator(this.errorModel, 'startDate')] });
baseContext.validation.push({ key: 'endDate', validators: [BackendErrorValidator(this.errorModel, 'endDate')] });
baseContext.validation.push({ key: 'files', validators: [BackendErrorValidator(this.errorModel, 'files')] });
return baseContext;
}
}