Skip to content

Commit b18e6ec

Browse files
authored
feat(ontology): add property to res class that is in use (DSP-1631) (#477)
* feat(ontology): add property to res class that is in use (DSP-1631) * refactor(ontology): clean up imports * test(ontology): bug fix in tests * test(ontology): update test for new implementation
1 parent 8be7e55 commit b18e6ec

6 files changed

Lines changed: 76 additions & 55 deletions

File tree

‎src/app/main/dialog/dialog.component.html‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,21 @@
281281
<div *ngSwitchCase="'updateCardinality'">
282282
<app-dialog-header [title]="data.title" [subtitle]="data.subtitle">
283283
</app-dialog-header>
284-
<app-property-form [propertyInfo]="data.propInfo" [resClassIri]="data.parentIri" [guiOrder]="data.position" (closeDialog)="dialogRef.close()">
284+
<app-property-form [propertyInfo]="data.propInfo" [resClassIri]="data.parentIri" [guiOrder]="data.position" [canSetFullCardinality]="data.canBeUpdated" (closeDialog)="dialogRef.close()">
285285
</app-property-form>
286286
</div>
287287

288288
<div *ngSwitchCase="'createProperty'">
289289
<app-dialog-header [title]="data.title" [subtitle]="data.subtitle">
290290
</app-dialog-header>
291-
<app-property-form [propertyInfo]="data.propInfo" [resClassIri]="data.parentIri" [guiOrder]="data.position" (closeDialog)="dialogRef.close()">
291+
<app-property-form [propertyInfo]="data.propInfo" [resClassIri]="data.parentIri" [guiOrder]="data.position" [canSetFullCardinality]="data.canBeUpdated" (closeDialog)="dialogRef.close()">
292292
</app-property-form>
293293
</div>
294294

295295
<div *ngSwitchCase="'editProperty'">
296296
<app-dialog-header [title]="data.title" [subtitle]="data.subtitle">
297297
</app-dialog-header>
298-
<app-property-form [propertyInfo]="data.propInfo" [resClassIri]="data.parentIri" [guiOrder]="data.position" (closeDialog)="dialogRef.close()">
298+
<app-property-form [propertyInfo]="data.propInfo" [resClassIri]="data.parentIri" [guiOrder]="data.position" [canSetFullCardinality]="data.canBeUpdated" (closeDialog)="dialogRef.close()">
299299
</app-property-form>
300300
</div>
301301

‎src/app/main/dialog/dialog.component.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface DialogData {
1212
name?: string;
1313
existing?: string[];
1414
propInfo?: PropertyInfoObject;
15+
canBeUpdated?: boolean;
1516
position?: number;
1617
parentIri?: string;
1718
projectCode?: string;

‎src/app/project/ontology/property-form/property-form.component.spec.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { PropertyFormComponent } from './property-form.component';
2525
* Property is of type simple text
2626
*/
2727
@Component({
28-
template: '<app-property-form #propertyForm [propertyInfo]="propertyInfo"></app-property-form>'
28+
template: '<app-property-form #propertyForm [propertyInfo]="propertyInfo" [canSetFullCardinality]="false"></app-property-form>'
2929
})
3030
class SimpleTextHostComponent {
3131

@@ -256,7 +256,6 @@ describe('PropertyFormComponent', () => {
256256

257257
});
258258

259-
260259
it('expect link to other resource called "Thing"', () => {
261260
expect(linkHostComponent.propertyFormComponent).toBeTruthy();
262261
expect(linkHostComponent.propertyFormComponent.propertyInfo.propDef).toBeDefined();
@@ -267,4 +266,24 @@ describe('PropertyFormComponent', () => {
267266
expect(form.controls['guiAttr'].value).toEqual('http://0.0.0.0:3333/ontology/0001/anything/v2#Thing');
268267

269268
});
269+
270+
it('expect "required" toggle switch (cardinality) to be disabled', () => {
271+
expect(simpleTextHostComponent.propertyFormComponent).toBeTruthy();
272+
expect(simpleTextHostComponent.propertyFormComponent.propertyInfo.propDef).toBeDefined();
273+
expect(simpleTextHostComponent.propertyFormComponent.propertyInfo.propType).toBeDefined();
274+
275+
const form = simpleTextHostComponent.propertyFormComponent.propertyForm;
276+
expect(form.controls['required'].disabled).toBeTruthy();
277+
278+
});
279+
280+
it('expect "required" toggle switch (cardinality) to be enabled', () => {
281+
expect(linkHostComponent.propertyFormComponent).toBeTruthy();
282+
expect(linkHostComponent.propertyFormComponent.propertyInfo.propDef).toBeDefined();
283+
expect(linkHostComponent.propertyFormComponent.propertyInfo.propType).toBeDefined();
284+
285+
const form = linkHostComponent.propertyFormComponent.propertyForm;
286+
expect(form.controls['required'].disabled).toBeFalsy();
287+
288+
});
270289
});

‎src/app/project/ontology/property-form/property-form.component.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export class PropertyFormComponent implements OnInit {
4141
*/
4242
@Input() resClassIri?: string;
4343

44+
/**
45+
* info if the cardinality can be fully set or not
46+
* (depending on canReplaceCardinalityOfResourceClass request)
47+
*/
48+
@Input() canSetFullCardinality?: boolean = true;
49+
4450
/**
4551
* position of property in case of cardinality update
4652
*/
@@ -148,8 +154,11 @@ export class PropertyFormComponent implements OnInit {
148154
'guiAttr': new FormControl({
149155
value: this.guiAttributes
150156
}),
151-
'multiple': new FormControl(),
152-
'required': new FormControl()
157+
'multiple': new FormControl(), // --> TODO: here we will check, if it can be updated; case updateCardinality: disabled if !canSetFullCardinality && multiple.value !== true
158+
'required': new FormControl({
159+
value: '',
160+
disabled: !this.canSetFullCardinality
161+
})
153162
});
154163

155164
this.updateAttributeField(this.propertyInfo.propType);

‎src/app/project/ontology/resource-class-info/resource-class-info.component.spec.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import { MatMenuModule } from '@angular/material/menu';
66
import { MatTooltipModule } from '@angular/material/tooltip';
77
import { By } from '@angular/platform-browser';
88
import { CanDoResponse, ClassDefinition, Constants, MockOntology, OntologiesEndpointV2, ReadOntology } from '@dasch-swiss/dsp-js';
9-
import { AppInitService, DspActionModule, DspApiConfigToken, DspApiConnectionToken, SortingService } from '@dasch-swiss/dsp-ui';
9+
import { DspActionModule, DspApiConnectionToken, SortingService } from '@dasch-swiss/dsp-ui';
1010
import { of } from 'rxjs';
1111
import { CacheService } from 'src/app/main/cache/cache.service';
12-
import { TestConfig } from 'test.config';
1312
import { ResourceClassInfoComponent } from './resource-class-info.component';
1413

1514
/**
@@ -70,7 +69,7 @@ describe('ResourceClassInfoComponent', () => {
7069
beforeEach(waitForAsync(() => {
7170
const ontologyEndpointSpyObj = {
7271
v2: {
73-
onto: jasmine.createSpyObj('onto', ['replaceGuiOrderOfCardinalities', 'canDeleteResourceClass'])
72+
onto: jasmine.createSpyObj('onto', ['replaceGuiOrderOfCardinalities', 'canDeleteResourceClass', 'canReplaceCardinalityOfResourceClass'])
7473
}
7574
};
7675

@@ -125,6 +124,16 @@ describe('ResourceClassInfoComponent', () => {
125124
}
126125
);
127126

127+
(dspConnSpy.v2.onto as jasmine.SpyObj<OntologiesEndpointV2>).canReplaceCardinalityOfResourceClass.and.callFake(
128+
() => {
129+
const replaceCardinalityOfResClass: CanDoResponse = {
130+
'canDo': false
131+
};
132+
133+
return of(replaceCardinalityOfResClass);
134+
}
135+
);
136+
128137
hostFixture = TestBed.createComponent(HostComponent);
129138
hostComponent = hostFixture.componentInstance;
130139
hostFixture.detectChanges();

‎src/app/project/ontology/resource-class-info/resource-class-info.component.ts‎

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export class ResourceClassInfoComponent implements OnInit {
6060

6161
classCanBeDeleted: boolean;
6262

63+
classCanReplaceCardinality: boolean;
64+
6365
// list of properties that can be displayed (not all of the props should be displayed)
6466
propsToDisplay: IHasProperty[] = [];
6567

@@ -88,17 +90,14 @@ export class ResourceClassInfoComponent implements OnInit {
8890
(response: ReadOntology) => {
8991
this.ontology = response;
9092
this.lastModificationDate = this.ontology.lastModificationDate;
93+
// translate iris from "sub class of" array
9194
this.translateSubClassOfIri(this.resourceClass.subClassOf);
95+
// prepare list of properties to display
9296
this.preparePropsToDisplay(this.resourceClass.propertiesList);
9397
// check if the class can be deleted
94-
this._dspApiConnection.v2.onto.canDeleteResourceClass(this.resourceClass.id).subscribe(
95-
(res: CanDoResponse) => {
96-
this.classCanBeDeleted = res.canDo;
97-
},
98-
(error: ApiResponseError) => {
99-
this._errorHandler.showMessage(error);
100-
}
101-
);
98+
this.canBeDeleted();
99+
// check if the cardinalities can be changed
100+
this.canReplaceCardinality();
102101
},
103102
(error: ApiResponseError) => {
104103
this._errorHandler.showMessage(error);
@@ -108,7 +107,7 @@ export class ResourceClassInfoComponent implements OnInit {
108107
}
109108

110109
/**
111-
* translates iri from "sub class of" array
110+
* translates iris from "sub class of" array
112111
* - display label from default resource classes (as part of DSP System Project)
113112
* - in case the class is a subclass of another class in the same ontology: display this class label
114113
* - in none of those cases display the name from the class IRI
@@ -230,6 +229,17 @@ export class ResourceClassInfoComponent implements OnInit {
230229
);
231230
}
232231

232+
canReplaceCardinality() {
233+
this._dspApiConnection.v2.onto.canReplaceCardinalityOfResourceClass(this.resourceClass.id).subscribe(
234+
(response: CanDoResponse) => {
235+
this.classCanReplaceCardinality = response.canDo;
236+
},
237+
(error: ApiResponseError) => {
238+
this._errorHandler.showMessage(error);
239+
}
240+
);
241+
}
242+
233243
addNewProperty(propType: DefaultProperty) {
234244
const cardinality: CardinalityInfo = {
235245
resClass: this.resourceClass,
@@ -329,7 +339,15 @@ export class ResourceClassInfoComponent implements OnInit {
329339
position: {
330340
top: '112px'
331341
},
332-
data: { propInfo: card.property, title: title, subtitle: 'Customize property and cardinality', mode: mode, parentIri: card.resClass.id, position: this.propsToDisplay.length }
342+
data: {
343+
propInfo: card.property,
344+
title: title,
345+
subtitle: 'Customize property and cardinality',
346+
mode: mode,
347+
parentIri: card.resClass.id,
348+
position: this.propsToDisplay.length,
349+
canBeUpdated: this.classCanReplaceCardinality
350+
}
333351
};
334352

335353
const dialogRef = this._dialog.open(
@@ -341,45 +359,10 @@ export class ResourceClassInfoComponent implements OnInit {
341359
// update the view: list of properties in resource class
342360
this.updateCardinality.emit(this.ontology.id);
343361
});
344-
} else {
345-
346362
}
347363

348364
}
349365

350-
/**
351-
* opens property form
352-
* @param mode
353-
* @param propertyInfo (could be subClassOf (create mode) or resource class itself (edit mode))
354-
*/
355-
openPropertyForm(mode: 'createProperty' | 'editProperty', propertyInfo: PropertyInfoObject): void {
356-
357-
const title = (propertyInfo.propDef ? propertyInfo.propDef.label : propertyInfo.propType.group + ': ' + propertyInfo.propType.label);
358-
359-
const dialogConfig: MatDialogConfig = {
360-
width: '640px',
361-
maxHeight: '80vh',
362-
position: {
363-
top: '112px'
364-
},
365-
data: { propInfo: propertyInfo, title: title, subtitle: 'Customize property as part of ' + this.resourceClass.label, mode: mode, parentIri: this.resourceClass.id }
366-
};
367-
368-
const dialogRef = this._dialog.open(
369-
DialogComponent,
370-
dialogConfig
371-
);
372-
373-
dialogRef.afterClosed().subscribe(result => {
374-
// update the view
375-
// this.initOntologiesList();
376-
this.ngOnInit();
377-
});
378-
}
379-
// open dialog box with property-form
380-
// create new property or add existing property
381-
// form includes cardinality and gui-attribute
382-
383366

384367
/**
385368
* drag and drop property line

0 commit comments

Comments
 (0)