/
ontology-form.component.ts
280 lines (229 loc) · 9.19 KB
/
ontology-form.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { Component, EventEmitter, Inject, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import {
ApiResponseError,
CreateOntology,
KnoraApiConnection,
OntologyMetadata,
ReadOntology,
ReadProject,
UpdateOntologyMetadata
} from '@dasch-swiss/dsp-js';
import { CacheService } from 'src/app/main/cache/cache.service';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { existingNamesValidator } from 'src/app/main/directive/existing-name/existing-name.directive';
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service';
import { CustomRegex } from 'src/app/workspace/resource/values/custom-regex';
import { OntologyService } from '../ontology.service';
export interface NewOntology {
projectIri: string;
name: string;
label: string;
}
@Component({
selector: 'app-ontology-form',
templateUrl: './ontology-form.component.html',
styleUrls: ['./ontology-form.component.scss']
})
export class OntologyFormComponent implements OnInit {
// project short code
@Input() projectCode: string;
// ontology iri in case of edit
@Input() iri: string;
// existing ontology names; name has to be unique
@Input() existingOntologyNames: string[];
@Output() closeDialog: EventEmitter<any> = new EventEmitter<any>();
@Output() updateParent: EventEmitter<string> = new EventEmitter<string>();
loading: boolean;
project: ReadProject;
ontologyForm: FormGroup;
ontologyLabel: string;
ontologyComment: string;
lastModificationDate: string;
// ontology name must not contain one of the following words
forbiddenNames: string[] = [
'knora',
'salsah',
'standoff',
'ontology',
'simple',
'shared'
];
existingNames: [RegExp];
nameMinLength = 3;
nameMaxLength = 16;
formErrors = {
'name': '',
'label': ''
};
validationMessages = {
'name': {
'required': 'Name is required.',
'minlength': 'Name must be at least ' + this.nameMinLength + ' characters long.',
'maxlength': 'Name cannot be more than ' + this.nameMaxLength + ' characters long.',
'pattern': 'Name shouldn\'t start with a number or v + number and spaces or special characters (except dash, dot and underscore) are not allowed.',
'existingName': 'This name is not allowed or exists already.'
},
'label': {
'required': 'Label is required.',
'minlength': 'Label must be at least ' + this.nameMinLength + ' characters long.',
}
};
ontologyNameInfo = `
The unique name
- must be at least 3 characters long
- shouldn't begin with a number
- shouldn't begin with the letter v and a number
- spaces or special characters (except dash, dot and underscore) are not allowed
- may not contain these reserved words: knora, ontology, salsah, shared, simple or standoff
- can't be changed afterwards
`;
error = false;
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _cache: CacheService,
private _errorHandler: ErrorHandlerService,
private _fb: FormBuilder,
private _ontologyService: OntologyService
) { }
ngOnInit() {
this.loading = true;
this._cache.get(this.projectCode).subscribe(
(response: ReadProject) => {
this.project = response;
this.buildForm();
this.loading = false;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
this.loading = false;
}
);
if (this.iri) {
// edit mode: get current ontology
this._cache.get('currentOntology').subscribe(
(response: ReadOntology) => {
// add values to the ontology form
this.ontologyForm.controls['name'].disable();
const name = this._ontologyService.getOntologyName(this.iri);
this.ontologyForm.controls['name'].setValue(name);
this.ontologyForm.controls['label'].setValue(response.label);
this.ontologyForm.controls['label'].setValidators(
[Validators.required]
);
this.ontologyForm.controls['comment'].setValue(response.comment);
// disable name input
this.lastModificationDate = response.lastModificationDate;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
}
buildForm() {
// reset existing names
this.existingNames = [
new RegExp('anEmptyRegularExpressionWasntPossible')
];
// get all project ontologies and read the name
// name has to be unique; if it already exists
// show an error message
for (const name of this.existingOntologyNames) {
this.existingNames.push(
new RegExp('(?:^|W)' + name + '(?:$|W)')
);
}
for (const name of this.forbiddenNames) {
this.existingNames.push(
new RegExp(name)
);
}
this.ontologyLabel = '';
this.ontologyForm = this._fb.group({
name: new FormControl({
value: '', disabled: false
}, [
Validators.required,
Validators.minLength(this.nameMinLength),
Validators.maxLength(this.nameMaxLength),
existingNamesValidator(this.existingNames),
Validators.pattern(CustomRegex.ID_NAME_REGEX)
]),
label: new FormControl({
value: this.ontologyLabel, disabled: false
}, [
Validators.minLength(this.nameMinLength)
]),
comment: new FormControl({
value: this.ontologyComment, disabled: false
})
});
this.ontologyForm.valueChanges.subscribe(data => this.onValueChanged(data));
}
onValueChanged(data?: any) {
if (!this.ontologyForm) {
return;
}
if (!this.iri) {
this.ontologyLabel = this.capitalizeFirstLetter(data.name);
}
Object.keys(this.formErrors).map(field => {
this.formErrors[field] = '';
const control = this.ontologyForm.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
Object.keys(control.errors).map(key => {
this.formErrors[field] += messages[key] + ' ';
});
}
});
}
submitData() {
this.loading = true;
if (this.iri) {
// edit mode
const ontologyData = new UpdateOntologyMetadata();
ontologyData.id = this.iri;
ontologyData.lastModificationDate = this.lastModificationDate;
ontologyData.label = this.ontologyForm.controls['label'].value;
ontologyData.comment = this.ontologyForm.controls['comment'].value;
this._dspApiConnection.v2.onto.updateOntology(ontologyData).subscribe(
(response: OntologyMetadata) => {
this.updateParent.emit(response.id);
this.loading = false;
this.closeDialog.emit(response.id);
},
(error: ApiResponseError) => {
// in case of an error
this.loading = false;
this.error = true;
this._errorHandler.showMessage(error);
}
);
} else {
// create mode
const ontologyData = new CreateOntology();
ontologyData.label = this.project.shortname + ': ' + this.ontologyForm.controls['label'].value;
ontologyData.name = this.ontologyForm.controls['name'].value;
ontologyData.comment = this.ontologyForm.controls['comment'].value;
ontologyData.attachedToProject = this.project.id;
this._dspApiConnection.v2.onto.createOntology(ontologyData).subscribe(
(response: OntologyMetadata) => {
this.updateParent.emit(response.id);
this.loading = false;
this.closeDialog.emit(response.id);
},
(error: ApiResponseError) => {
// in case of an error... e.g. because the ontolog iri is not unique, rebuild the form including the error message
this.formErrors['name'] += this.validationMessages['name']['existingName'] + ' ';
this.loading = false;
this._errorHandler.showMessage(error);
}
);
}
}
capitalizeFirstLetter(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1);
}
}