-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource-class-property-info.component.ts
210 lines (178 loc) · 6.82 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
import {
AfterContentInit,
ChangeDetectorRef,
Component,
EventEmitter,
Inject,
Input,
OnChanges,
OnDestroy,
Output,
} from '@angular/core';
import {
CanDoResponse,
Constants,
IHasProperty,
KnoraApiConnection,
ResourcePropertyDefinitionWithAllLanguages,
UpdateOntology,
UpdateResourceClassCardinality,
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from '@dasch-swiss/vre/shared/app-config';
import { DefaultClass, DefaultProperty, OntologyService } from '@dasch-swiss/vre/shared/app-helper-services';
import { ListsSelectors, OntologiesSelectors } from '@dasch-swiss/vre/shared/app-state';
import { Store } from '@ngxs/store';
import { Subject, Subscription } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
// 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, OnDestroy {
@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;
}>();
isDestroyed = new Subject<void>();
propInfo: Property = new Property();
propType: DefaultProperty;
propAttribute: string;
propAttributeComment: string;
propCanBeRemovedFromClass: boolean;
constructor(
@Inject(DspApiConnectionToken)
private _dspApiConnection: KnoraApiConnection,
private _ontoService: OntologyService,
private _store: Store,
private _cd: ChangeDetectorRef
) {}
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;
const currentProjectOntologies = this._store.selectSnapshot(OntologiesSelectors.currentProjectOntologies);
// 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, currentProjectOntologies);
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() {
// get current ontology to get linked res class information
const ontology = this._store.selectSnapshot(OntologiesSelectors.currentOntology);
const currentProjectOntologies = this._store.selectSnapshot(OntologiesSelectors.currentProjectOntologies);
if (ontology && currentProjectOntologies && this.propDef.isLinkProperty) {
// this property is a link property to another resource class
// get the base ontology of object type
const baseOnto = this.propDef.objectType.split('#')[0];
if (baseOnto !== ontology.id) {
// get class info from another ontology
const onto = currentProjectOntologies.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;
}
} else {
this.propAttribute = ontology.classes[this.propDef.objectType].label;
this.propAttributeComment = ontology.classes[this.propDef.objectType].comment;
}
}
// get current ontology lists to get linked list information
this._store
.select(ListsSelectors.listsInProject)
.pipe(takeUntil(this.isDestroyed))
.subscribe(currentOntologyLists => {
if (currentOntologyLists && this.propDef.objectType === Constants.ListValue) {
// this property is a list property
const re = /\<([^)]+)\>/;
const listIri = this.propDef.guiAttributes[0].match(re)[1];
const listUrl = `/project/${this.projectUuid}/list/${listIri.split('/').pop()}`;
const list = currentOntologyLists.find(i => i.id === listIri);
if (list) {
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;
const ontology = this._store.selectSnapshot(OntologiesSelectors.currentOntology);
onto.id = 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;
this._cd.markForCheck();
});
}
ngOnDestroy() {
this.isDestroyed.next();
this.isDestroyed.complete();
}
}