/
resource-class-form.service.ts
278 lines (226 loc) · 8.63 KB
/
resource-class-form.service.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
import { Injectable } from '@angular/core';
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Cardinality, Constants, IHasProperty, PropertyDefinition, ResourceClassDefinition, ResourcePropertyDefinition, StringLiteral } from '@dasch-swiss/dsp-js';
import { BehaviorSubject, Observable } from 'rxjs';
import { DefaultProperties, PropertyType } from '../default-data/default-properties';
// property data structure
export class Property {
iri: string;
label: string;
type: PropertyType;
multiple: boolean;
required: boolean;
guiAttr: string;
// permission: string;
constructor(
iri?: string,
label?: string,
type?: any,
multiple?: boolean,
required?: boolean,
guiAttr?: string
// permission?: string
) {
this.iri = iri;
this.label = label;
this.type = type;
this.multiple = multiple;
this.required = required;
this.guiAttr = guiAttr;
// this.permission = permission;
}
}
// property form controls
export class PropertyForm {
iri = new FormControl();
label = new FormControl();
type = new FormControl();
multiple = new FormControl();
required = new FormControl();
guiAttr = new FormControl();
// permission = new FormControl();
constructor(
property: Property
) {
this.iri.setValue(property.iri);
this.label.setValue(property.label);
this.label.setValidators([Validators.required]);
this.type.setValue(property.type);
this.type.setValidators([Validators.required]);
this.multiple.setValue(property.multiple);
this.required.setValue(property.required);
this.guiAttr.setValue(property.guiAttr);
// this.permission.setValue(property.permission);
// TODO: permission is not implemented yet
// this.permission.setValidators([Validators.required]);
}
}
// resource class data structure
export class ResourceClass {
language: string;
properties: Property[];
constructor(language = 'en', properties?: Property[]) {
this.language = language;
this.properties = properties;
}
}
// resource class form controls
export class ResourceClassForm {
language = new FormControl();
properties = new FormArray([]);
constructor(resourceClass: ResourceClass) {
this.language.setValue('en');
if (resourceClass.properties) {
let i = 0;
this.properties.setControl;
resourceClass.properties.forEach(prop => {
this.properties[i] = new FormControl(prop);
i++;
});
}
}
}
@Injectable({
providedIn: 'root'
})
export class ResourceClassFormService {
private resourceClassForm: BehaviorSubject<FormGroup | undefined> = new BehaviorSubject(this._fb.group(
new ResourceClassForm(new ResourceClass())
));
resourceClassForm$: Observable<FormGroup> = this.resourceClassForm.asObservable();
constructor(private _fb: FormBuilder) { }
/**
* reset all properties
*/
resetProperties() {
const currentResourceClass = this._fb.group(
new ResourceClassForm(new ResourceClass())
);
this.resourceClassForm.next(currentResourceClass);
}
/**
* Sets properties in case of update resource class' cardinalities
* @param resClass
*/
setProperties(resClass: ResourceClassDefinition, ontoProperties: PropertyDefinition[]) {
const updateResClass = new ResourceClass();
updateResClass.properties = [];
// get cardinality and gui order and grab property definition
resClass.propertiesList.forEach((prop: IHasProperty) => {
if (prop.guiOrder >= 0) {
// get property definition
Object.keys(ontoProperties).forEach(key => {
if (ontoProperties[key].id === prop.propertyIndex && !ontoProperties[key].isLinkValueProperty) {
const propDef: ResourcePropertyDefinition = ontoProperties[key];
const property: Property = new Property();
// property.propDef = ontoProperties[key];
property.label = propDef.label;
if(ontoProperties[key].isLinkProperty) {
property.guiAttr = propDef.objectType;
} else {
property.guiAttr = propDef.guiAttributes[0];
}
property.iri = prop.propertyIndex;
// convert cardinality
switch (prop.cardinality) {
case 0:
property.multiple = false;
property.required = true;
break;
case 1:
property.multiple = false;
property.required = false;
break;
case 2:
property.multiple = true;
property.required = false;
break;
case 3:
property.multiple = true;
property.required = true;
break;
}
// find property type in list of default properties
// just a test
// property.type = DefaultProperties.data[0].elements[0];
this.addProperty(property);
}
});
}
});
}
/**
* add new property line
*/
addProperty(prop?: Property) {
const currentResourceClass = this.resourceClassForm.getValue();
const currentProperties = currentResourceClass.get('properties') as FormArray;
currentProperties.push(
this._fb.group(
new PropertyForm((prop ? prop : new Property('', '', {}, false, false)))
)
);
this.resourceClassForm.next(currentResourceClass);
}
/**
* delete property line by index i
*
* @param {number} i
*/
removeProperty(i: number) {
const currentResourceClass = this.resourceClassForm.getValue();
const currentProperties = currentResourceClass.get('properties') as FormArray;
currentProperties.removeAt(i);
this.resourceClassForm.next(currentResourceClass);
}
/**
* Create a unique name (id) for resource classes or properties;
*
* @param ontologyIri
* @param [label]
* @returns unique name
*/
setUniqueName(ontologyIri: string, label?: string, type?: 'class' | 'prop'): string {
if (label && type) {
// build name from label
// normalize and replace spaces and special chars
return type + '-' + label.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[\u00a0-\u024f]/g, '').replace(/[\])}[{(]/g, '').replace(/\s+/g, '-').replace(/\//g, '-').toLowerCase();
} else {
// build randomized name
// The name starts with the three first character of ontology iri to avoid a start with a number followed by randomized string
return this.getOntologyName(ontologyIri).substring(0, 3) + Math.random().toString(36).substring(2, 5) + Math.random().toString(36).substring(2, 5);
}
}
/**
* Get the ontolgoy name from ontology iri
*
* @param {string} ontologyIri
* @returns string
*/
getOntologyName(ontologyIri: string): string {
const array = ontologyIri.split('/');
const pos = array.length - 2;
return array[pos].toLowerCase();
}
/**
* Convert cardinality values (multiple? & required?) from form to DSP-JS cardinality enum 1-n, 0-n, 1, 0-1
* @param {boolean} multiple
* @param {boolean} required
* @returns Cardinality
*/
translateCardinality(multiple: boolean, required: boolean): Cardinality {
if (multiple && required) {
// Cardinality 1-n (at least one)
return Cardinality._1_n;
} else if (multiple && !required) {
// Cardinality 0-n (may have many)
return Cardinality._0_n;
} else if (!multiple && required) {
// Cardinality 1 (required)
return Cardinality._1;
} else {
// Cardinality 0-1 (optional)
return Cardinality._0_1;
}
}
}