From b18e6ecba2afb3059ef75fb20c3d5699468e8335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Kilchenmann?= Date: Wed, 30 Jun 2021 13:15:46 +0200 Subject: [PATCH] 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 --- src/app/main/dialog/dialog.component.html | 6 +- src/app/main/dialog/dialog.component.ts | 1 + .../property-form.component.spec.ts | 23 +++++- .../property-form/property-form.component.ts | 13 +++- .../resource-class-info.component.spec.ts | 15 +++- .../resource-class-info.component.ts | 73 +++++++------------ 6 files changed, 76 insertions(+), 55 deletions(-) diff --git a/src/app/main/dialog/dialog.component.html b/src/app/main/dialog/dialog.component.html index e5049173df..741fa760ab 100644 --- a/src/app/main/dialog/dialog.component.html +++ b/src/app/main/dialog/dialog.component.html @@ -281,21 +281,21 @@
- +
- +
- +
diff --git a/src/app/main/dialog/dialog.component.ts b/src/app/main/dialog/dialog.component.ts index 73d19590e2..5bfa2ad601 100644 --- a/src/app/main/dialog/dialog.component.ts +++ b/src/app/main/dialog/dialog.component.ts @@ -12,6 +12,7 @@ export interface DialogData { name?: string; existing?: string[]; propInfo?: PropertyInfoObject; + canBeUpdated?: boolean; position?: number; parentIri?: string; projectCode?: string; diff --git a/src/app/project/ontology/property-form/property-form.component.spec.ts b/src/app/project/ontology/property-form/property-form.component.spec.ts index b7668b237e..3afff3bb55 100644 --- a/src/app/project/ontology/property-form/property-form.component.spec.ts +++ b/src/app/project/ontology/property-form/property-form.component.spec.ts @@ -25,7 +25,7 @@ import { PropertyFormComponent } from './property-form.component'; * Property is of type simple text */ @Component({ - template: '' + template: '' }) class SimpleTextHostComponent { @@ -256,7 +256,6 @@ describe('PropertyFormComponent', () => { }); - it('expect link to other resource called "Thing"', () => { expect(linkHostComponent.propertyFormComponent).toBeTruthy(); expect(linkHostComponent.propertyFormComponent.propertyInfo.propDef).toBeDefined(); @@ -267,4 +266,24 @@ describe('PropertyFormComponent', () => { expect(form.controls['guiAttr'].value).toEqual('http://0.0.0.0:3333/ontology/0001/anything/v2#Thing'); }); + + it('expect "required" toggle switch (cardinality) to be disabled', () => { + expect(simpleTextHostComponent.propertyFormComponent).toBeTruthy(); + expect(simpleTextHostComponent.propertyFormComponent.propertyInfo.propDef).toBeDefined(); + expect(simpleTextHostComponent.propertyFormComponent.propertyInfo.propType).toBeDefined(); + + const form = simpleTextHostComponent.propertyFormComponent.propertyForm; + expect(form.controls['required'].disabled).toBeTruthy(); + + }); + + it('expect "required" toggle switch (cardinality) to be enabled', () => { + expect(linkHostComponent.propertyFormComponent).toBeTruthy(); + expect(linkHostComponent.propertyFormComponent.propertyInfo.propDef).toBeDefined(); + expect(linkHostComponent.propertyFormComponent.propertyInfo.propType).toBeDefined(); + + const form = linkHostComponent.propertyFormComponent.propertyForm; + expect(form.controls['required'].disabled).toBeFalsy(); + + }); }); diff --git a/src/app/project/ontology/property-form/property-form.component.ts b/src/app/project/ontology/property-form/property-form.component.ts index d6974d6538..e585005b0e 100644 --- a/src/app/project/ontology/property-form/property-form.component.ts +++ b/src/app/project/ontology/property-form/property-form.component.ts @@ -41,6 +41,12 @@ export class PropertyFormComponent implements OnInit { */ @Input() resClassIri?: string; + /** + * info if the cardinality can be fully set or not + * (depending on canReplaceCardinalityOfResourceClass request) + */ + @Input() canSetFullCardinality?: boolean = true; + /** * position of property in case of cardinality update */ @@ -148,8 +154,11 @@ export class PropertyFormComponent implements OnInit { 'guiAttr': new FormControl({ value: this.guiAttributes }), - 'multiple': new FormControl(), - 'required': new FormControl() + 'multiple': new FormControl(), // --> TODO: here we will check, if it can be updated; case updateCardinality: disabled if !canSetFullCardinality && multiple.value !== true + 'required': new FormControl({ + value: '', + disabled: !this.canSetFullCardinality + }) }); this.updateAttributeField(this.propertyInfo.propType); diff --git a/src/app/project/ontology/resource-class-info/resource-class-info.component.spec.ts b/src/app/project/ontology/resource-class-info/resource-class-info.component.spec.ts index acea358c76..a852b3b408 100644 --- a/src/app/project/ontology/resource-class-info/resource-class-info.component.spec.ts +++ b/src/app/project/ontology/resource-class-info/resource-class-info.component.spec.ts @@ -6,10 +6,9 @@ import { MatMenuModule } from '@angular/material/menu'; import { MatTooltipModule } from '@angular/material/tooltip'; import { By } from '@angular/platform-browser'; import { CanDoResponse, ClassDefinition, Constants, MockOntology, OntologiesEndpointV2, ReadOntology } from '@dasch-swiss/dsp-js'; -import { AppInitService, DspActionModule, DspApiConfigToken, DspApiConnectionToken, SortingService } from '@dasch-swiss/dsp-ui'; +import { DspActionModule, DspApiConnectionToken, SortingService } from '@dasch-swiss/dsp-ui'; import { of } from 'rxjs'; import { CacheService } from 'src/app/main/cache/cache.service'; -import { TestConfig } from 'test.config'; import { ResourceClassInfoComponent } from './resource-class-info.component'; /** @@ -70,7 +69,7 @@ describe('ResourceClassInfoComponent', () => { beforeEach(waitForAsync(() => { const ontologyEndpointSpyObj = { v2: { - onto: jasmine.createSpyObj('onto', ['replaceGuiOrderOfCardinalities', 'canDeleteResourceClass']) + onto: jasmine.createSpyObj('onto', ['replaceGuiOrderOfCardinalities', 'canDeleteResourceClass', 'canReplaceCardinalityOfResourceClass']) } }; @@ -125,6 +124,16 @@ describe('ResourceClassInfoComponent', () => { } ); + (dspConnSpy.v2.onto as jasmine.SpyObj).canReplaceCardinalityOfResourceClass.and.callFake( + () => { + const replaceCardinalityOfResClass: CanDoResponse = { + 'canDo': false + }; + + return of(replaceCardinalityOfResClass); + } + ); + hostFixture = TestBed.createComponent(HostComponent); hostComponent = hostFixture.componentInstance; hostFixture.detectChanges(); diff --git a/src/app/project/ontology/resource-class-info/resource-class-info.component.ts b/src/app/project/ontology/resource-class-info/resource-class-info.component.ts index 9e1660a5e7..e0c4eccf55 100644 --- a/src/app/project/ontology/resource-class-info/resource-class-info.component.ts +++ b/src/app/project/ontology/resource-class-info/resource-class-info.component.ts @@ -60,6 +60,8 @@ export class ResourceClassInfoComponent implements OnInit { classCanBeDeleted: boolean; + classCanReplaceCardinality: boolean; + // list of properties that can be displayed (not all of the props should be displayed) propsToDisplay: IHasProperty[] = []; @@ -88,17 +90,14 @@ export class ResourceClassInfoComponent implements OnInit { (response: ReadOntology) => { this.ontology = response; this.lastModificationDate = this.ontology.lastModificationDate; + // translate iris from "sub class of" array this.translateSubClassOfIri(this.resourceClass.subClassOf); + // prepare list of properties to display this.preparePropsToDisplay(this.resourceClass.propertiesList); // check if the class can be deleted - this._dspApiConnection.v2.onto.canDeleteResourceClass(this.resourceClass.id).subscribe( - (res: CanDoResponse) => { - this.classCanBeDeleted = res.canDo; - }, - (error: ApiResponseError) => { - this._errorHandler.showMessage(error); - } - ); + this.canBeDeleted(); + // check if the cardinalities can be changed + this.canReplaceCardinality(); }, (error: ApiResponseError) => { this._errorHandler.showMessage(error); @@ -108,7 +107,7 @@ export class ResourceClassInfoComponent implements OnInit { } /** - * translates iri from "sub class of" array + * translates iris from "sub class of" array * - display label from default resource classes (as part of DSP System Project) * - in case the class is a subclass of another class in the same ontology: display this class label * - in none of those cases display the name from the class IRI @@ -230,6 +229,17 @@ export class ResourceClassInfoComponent implements OnInit { ); } + canReplaceCardinality() { + this._dspApiConnection.v2.onto.canReplaceCardinalityOfResourceClass(this.resourceClass.id).subscribe( + (response: CanDoResponse) => { + this.classCanReplaceCardinality = response.canDo; + }, + (error: ApiResponseError) => { + this._errorHandler.showMessage(error); + } + ); + } + addNewProperty(propType: DefaultProperty) { const cardinality: CardinalityInfo = { resClass: this.resourceClass, @@ -329,7 +339,15 @@ export class ResourceClassInfoComponent implements OnInit { position: { top: '112px' }, - data: { propInfo: card.property, title: title, subtitle: 'Customize property and cardinality', mode: mode, parentIri: card.resClass.id, position: this.propsToDisplay.length } + data: { + propInfo: card.property, + title: title, + subtitle: 'Customize property and cardinality', + mode: mode, + parentIri: card.resClass.id, + position: this.propsToDisplay.length, + canBeUpdated: this.classCanReplaceCardinality + } }; const dialogRef = this._dialog.open( @@ -341,45 +359,10 @@ export class ResourceClassInfoComponent implements OnInit { // update the view: list of properties in resource class this.updateCardinality.emit(this.ontology.id); }); - } else { - } } - /** - * opens property form - * @param mode - * @param propertyInfo (could be subClassOf (create mode) or resource class itself (edit mode)) - */ - openPropertyForm(mode: 'createProperty' | 'editProperty', propertyInfo: PropertyInfoObject): void { - - const title = (propertyInfo.propDef ? propertyInfo.propDef.label : propertyInfo.propType.group + ': ' + propertyInfo.propType.label); - - const dialogConfig: MatDialogConfig = { - width: '640px', - maxHeight: '80vh', - position: { - top: '112px' - }, - data: { propInfo: propertyInfo, title: title, subtitle: 'Customize property as part of ' + this.resourceClass.label, mode: mode, parentIri: this.resourceClass.id } - }; - - const dialogRef = this._dialog.open( - DialogComponent, - dialogConfig - ); - - dialogRef.afterClosed().subscribe(result => { - // update the view - // this.initOntologiesList(); - this.ngOnInit(); - }); - } - // open dialog box with property-form - // create new property or add existing property - // form includes cardinality and gui-attribute - /** * drag and drop property line