-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource-class-property-info.component.ts
241 lines (208 loc) · 7.99 KB
/
resource-class-property-info.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
import {
AfterContentInit,
Component,
EventEmitter,
Inject,
Input,
OnChanges,
Output,
} from '@angular/core';
import {
ApiResponseError,
CanDoResponse,
Constants,
IHasProperty,
KnoraApiConnection,
ListNodeInfo,
ReadOntology,
ResourcePropertyDefinitionWithAllLanguages,
UpdateOntology,
UpdateResourceClassCardinality,
} from '@dasch-swiss/dsp-js';
import { ApplicationStateService } from '@dasch-swiss/vre/shared/app-state-service';
import { DspApiConnectionToken } from '@dasch-swiss/vre/shared/app-config';
import { AppErrorHandler } from '@dasch-swiss/vre/shared/app-error-handler';
import { DefaultProperty } from '../../default-data/default-properties';
import { DefaultClass } from '../../default-data/default-resource-classes';
import { OntologyService } from '../../ontology.service';
// property data structure
export class Property {
iri: string;
label: string;
type: DefaultProperty;
multiple: boolean;
required: boolean;
guiAttr: string;
constructor(
iri?: string,
label?: string,
type?: any,
multiple?: boolean,
required?: boolean,
guiAttr?: string
) {
this.iri = iri;
this.label = label;
this.type = type;
this.multiple = multiple;
this.required = required;
this.guiAttr = guiAttr;
}
}
export interface GuiCardinality {
key: CardinalityKey;
value: boolean;
}
type CardinalityKey = 'multiple' | 'required';
@Component({
selector: 'app-resource-class-property-info',
templateUrl: './resource-class-property-info.component.html',
styleUrls: ['./resource-class-property-info.component.scss'],
})
export class ResourceClassPropertyInfoComponent
implements OnChanges, AfterContentInit
{
@Input() propDef: ResourcePropertyDefinitionWithAllLanguages;
@Input() propCard: IHasProperty;
@Input() resourceIri?: string;
@Input() projectUuid: string;
@Input() lastModificationDate?: string;
@Input() userCanEdit: boolean; // is user a project admin or sys admin?
@Output() removePropertyFromClass: EventEmitter<DefaultClass> =
new EventEmitter<DefaultClass>();
@Output() changeCardinalities: EventEmitter<{
prop: IHasProperty;
propType: DefaultProperty;
targetCardinality: GuiCardinality;
}> = new EventEmitter<{
prop: IHasProperty;
propType: DefaultProperty;
targetCardinality: GuiCardinality;
}>();
propInfo: Property = new Property();
propType: DefaultProperty;
propAttribute: string;
propAttributeComment: string;
propCanBeRemovedFromClass: boolean;
ontology: ReadOntology;
constructor(
@Inject(DspApiConnectionToken)
private _dspApiConnection: KnoraApiConnection,
private _applicationStateService: ApplicationStateService,
private _errorHandler: AppErrorHandler,
private _ontoService: OntologyService
) {
this._applicationStateService
.get('currentOntology')
.subscribe((response: ReadOntology) => {
this.ontology = response;
});
}
ngOnChanges(): void {
// set the cardinality values in the class view
const cards = this._ontoService.getCardinalityGuiValues(
this.propCard.cardinality
);
this.propInfo.multiple = cards.multiple;
this.propInfo.required = cards.required;
// get info about subproperties, if they are not a subproperty of knora base ontology
// in this case add it to the list of subproperty iris
const superProp = this._ontoService.getSuperProperty(this.propDef);
if (superProp) {
if (this.propDef.subPropertyOf.indexOf(superProp) === -1) {
this.propDef.subPropertyOf.push(superProp);
}
}
// get the default property type for this property
this._ontoService
.getDefaultPropType(this.propDef)
.subscribe((prop: DefaultProperty) => {
this.propType = prop;
});
}
ngAfterContentInit() {
if (this.propDef.isLinkProperty) {
// this property is a link property to another resource class
// get current ontology to get linked res class information
// get the base ontology of object type
const baseOnto = this.propDef.objectType.split('#')[0];
if (baseOnto !== this.ontology.id) {
// get class info from another ontology
this._applicationStateService.get('currentProjectOntologies').subscribe(
(ontologies: ReadOntology[]) => {
const onto = ontologies.find((i) => i.id === baseOnto);
if ( !onto ) {
if (this.propDef.objectType === Constants.Region) {
this.propAttribute = 'Region';
} // else no ontology found
} else {
this.propAttribute =
onto.classes[this.propDef.objectType].label;
this.propAttributeComment =
onto.classes[this.propDef.objectType].comment;
}
},
() => {} // don't log error to rollbar if 'currentProjectOntologies' does not exist in the application state
);
} else {
this.propAttribute =
this.ontology.classes[this.propDef.objectType].label;
this.propAttributeComment =
this.ontology.classes[this.propDef.objectType].comment;
}
}
if (this.propDef.objectType === Constants.ListValue) {
// this property is a list property
// get current ontology lists to get linked list information
this._applicationStateService
.get('currentOntologyLists')
.subscribe((response: ListNodeInfo[]) => {
const re = /\<([^)]+)\>/;
const listIri = this.propDef.guiAttributes[0].match(re)[1];
const listUrl = `/project/${
this.projectUuid
}/lists/${encodeURIComponent(listIri)}`;
const list = response.find((i) => i.id === listIri);
this.propAttribute = `<a href="${listUrl}">${list.labels[0].value}</a>`;
this.propAttributeComment = list.comments.length
? list.comments[0].value
: null;
});
}
}
/**
* determines whether a property can be removed from a class or not
*/
canBeRemovedFromClass(): void {
// check if the property can be removed from res class
if (!this.lastModificationDate) {
// guard
return;
}
// property can only be removed from class if it's not inherited from another prop or class
if (this.propCard.isInherited) {
// other guard
this.propCanBeRemovedFromClass = false;
return;
}
const onto = new UpdateOntology<UpdateResourceClassCardinality>();
onto.lastModificationDate = this.lastModificationDate;
onto.id = this.ontology.id;
const delCard = new UpdateResourceClassCardinality();
delCard.id = this.resourceIri;
delCard.cardinalities = [];
delCard.cardinalities = [this.propCard];
onto.entity = delCard;
this._dspApiConnection.v2.onto
.canDeleteCardinalityFromResourceClass(onto)
.subscribe(
(canDoRes: CanDoResponse) => {
this.propCanBeRemovedFromClass = canDoRes.canDo;
},
// open snackbar displaying the error
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
}
}