Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Ontologies Endpoint #239

Merged
merged 15 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ e2e-tests: ## runs the e2e tests
sudo npm install yalc -g
npm run prepare-dev-publication
npm run yalc-publish
cd test-framework && yalc remove --all && yalc add @dasch-swiss/dsp-js && npm install && npm run webdriver-update && npm run e2e && npm run build-app && docker build .
cd test-framework && yalc add @dasch-swiss/dsp-js && npm install && npm run webdriver-update && npm run e2e && npm run build-app && docker build .

.PHONY: build
build: ## builds the lib
Expand Down
110 changes: 48 additions & 62 deletions src/api/v2/ontology/ontologies-endpoint-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ import { AjaxResponse } from "rxjs/ajax";
import { catchError, map, mergeMap } from "rxjs/operators";
import { ApiResponseError } from "../../../models/api-response-error";
import { Constants } from "../../../models/v2/Constants";
import { AddCardinalityToResourceClass } from "../../../models/v2/ontologies/create/add-cardinality-to-resource-class";
import { CreateOntology } from "../../../models/v2/ontologies/create/create-ontology";
import { CreateResourceClass } from "../../../models/v2/ontologies/create/create-resource-class";
import {
CreateResourceClassPayload,
NewResourceClass
} from "../../../models/v2/ontologies/create/create-resource-class-payload";
import { CreateResourceProperty } from "../../../models/v2/ontologies/create/create-resource-property";
CreateResourceClass,
CreateResourceClassPayload
} from "../../../models/v2/ontologies/create/create-resource-class";
import {
CreateResourcePropertyPayload,
NewResourcePropertyPayload
} from "../../../models/v2/ontologies/create/create-resource-property-payload";
CreateResourceProperty,
CreateResourcePropertyPayload
} from "../../../models/v2/ontologies/create/create-resource-property";
import { DeleteOntology } from "../../../models/v2/ontologies/delete/delete-ontology";
import { DeleteOntologyResponse } from "../../../models/v2/ontologies/delete/delete-ontology-response";
import { DeleteResourceClass } from "../../../models/v2/ontologies/delete/delete-resource-class";
import { DeleteResourceProperty } from "../../../models/v2/ontologies/delete/delete-resource-property";
import { OntologiesMetadata, OntologyMetadata } from "../../../models/v2/ontologies/ontology-metadata";
import { OntologyConversionUtil } from "../../../models/v2/ontologies/OntologyConversionUtil";
import { ReadOntology } from "../../../models/v2/ontologies/read/read-ontology";
import { ResourceClassDefinitionWithAllLanguages } from "../../../models/v2/ontologies/resource-class-definition";
import { ResourcePropertyDefinitionWithAllLanguages } from "../../../models/v2/ontologies/resource-property-definition";
import { UpdateOntology } from "../../../models/v2/ontologies/update-ontology";
import { UpdateOntology } from "../../../models/v2/ontologies/update/update-ontology";
import { UpdateOntologyResourceClassCardinality } from "../../../models/v2/ontologies/update/update-ontology-resource-class-cardinality";
import { Endpoint } from "../../endpoint";

declare let require: any; // http://stackoverflow.com/questions/34730010/angular2-5-minute-install-bug-require-is-not-defined
Expand All @@ -37,7 +38,6 @@ export class OntologiesEndpointV2 extends Endpoint {
*/
getOntologiesMetadata(): Observable<OntologiesMetadata | ApiResponseError> {

// TODO: Do not hard-code the URL and http call params, generate this from Knora
return this.httpGet("/metadata").pipe(
mergeMap((ajaxResponse: AjaxResponse) => {
// TODO: @rosenth Adapt context object
Expand Down Expand Up @@ -124,7 +124,7 @@ export class OntologiesEndpointV2 extends Endpoint {
*
* @param ontology the ontology to be deleted.
*/
deleteOntology(ontology: UpdateOntology): Observable<DeleteOntologyResponse | ApiResponseError> {
deleteOntology(ontology: DeleteOntology): Observable<DeleteOntologyResponse | ApiResponseError> {

const path = "/" + encodeURIComponent(ontology.id) + "?lastModificationDate=" + encodeURIComponent(ontology.lastModificationDate);

Expand All @@ -145,29 +145,23 @@ export class OntologiesEndpointV2 extends Endpoint {
/**
* Creates a resource class without cardinalities.
*
* @param resClass The resource class to be created.
* @param resourceClasses The resource class to be created.
*/
createResourceClass(resClass: CreateResourceClass): Observable<ResourceClassDefinitionWithAllLanguages | ApiResponseError> {

const resClassPayload = new CreateResourceClassPayload();
createResourceClass(resourceClasses: UpdateOntology<CreateResourceClass>): Observable<ResourceClassDefinitionWithAllLanguages | ApiResponseError> {

// prepare ontology data for payload
resClassPayload.id = resClass.ontology.id;
resClassPayload.lastModificationDate = resClass.ontology.lastModificationDate;
const resClassPay = new CreateResourceClassPayload();

// prepare new res class object for payload
const newResClass = new NewResourceClass();
newResClass.id = resClass.ontology.id + Constants.Delimiter + resClass.name;
newResClass.label = resClass.labels;
newResClass.comment = (resClass.comments.length ? resClass.comments : resClass.labels);
newResClass.subClassOf = resClass.subClassOf;
newResClass.type = Constants.Class;
resClassPay.id = resourceClasses.id + Constants.Delimiter + resourceClasses.entity.name;
resClassPay.label = resourceClasses.entity.label;
resClassPay.comment = (resourceClasses.entity.comment.length ? resourceClasses.entity.comment : resourceClasses.entity.label);
resClassPay.subClassOf = resourceClasses.entity.subClassOf;
resClassPay.type = Constants.Class;

resClassPayload.resClass = [newResClass];
const ontoPayload = this.jsonConvert.serializeObject(resourceClasses);

const payload = this.jsonConvert.serializeObject(resClassPayload);
ontoPayload["@graph"] = [this.jsonConvert.serializeObject(resClassPay)];

return this.httpPost("/classes", payload).pipe(
return this.httpPost("/classes", ontoPayload).pipe(
mergeMap((ajaxResponse: AjaxResponse) => {
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
Expand All @@ -182,13 +176,13 @@ export class OntologiesEndpointV2 extends Endpoint {
}

/**
* Deletes a resource class
* Deletes a resource class.
*
* @param updateOntology with class IRI.
* @param deleteResourceClass with class IRI.
*/
deleteResourceClass(updateOntology: UpdateOntology): Observable<OntologyMetadata | ApiResponseError> {
deleteResourceClass(deleteResourceClass: DeleteResourceClass): Observable<OntologyMetadata | ApiResponseError> {

const path = "/classes/" + encodeURIComponent(updateOntology.id) + "?lastModificationDate=" + encodeURIComponent(updateOntology.lastModificationDate);
const path = "/classes/" + encodeURIComponent(deleteResourceClass.id) + "?lastModificationDate=" + encodeURIComponent(deleteResourceClass.lastModificationDate);

return this.httpDelete(path).pipe(
mergeMap((ajaxResponse: AjaxResponse) => {
Expand All @@ -205,43 +199,35 @@ export class OntologiesEndpointV2 extends Endpoint {
}

/**
* Creates a resource property
* Creates a resource property.
*
* @param resProp the resource property to be created.
* @param resourceProperties the resource property to be created.
*/
createResourceProperty(resProp: CreateResourceProperty): Observable<ResourcePropertyDefinitionWithAllLanguages | ApiResponseError> {

const resPropPayload = new CreateResourcePropertyPayload();

// prepare ontology data for payload
resPropPayload.id = resProp.ontology.id;
resPropPayload.lastModificationDate = resProp.ontology.lastModificationDate;
createResourceProperty(resourceProperties: UpdateOntology<CreateResourceProperty>): Observable<ResourcePropertyDefinitionWithAllLanguages | ApiResponseError> {

// prepare new res class object for payload
const newResProperty = new NewResourcePropertyPayload();
const resPropPay = new CreateResourcePropertyPayload();

newResProperty.id = resProp.ontology.id + Constants.Delimiter + resProp.name;
resPropPay.id = resourceProperties.id + Constants.Delimiter + resourceProperties.entity.name;

newResProperty.label = resProp.labels;
newResProperty.comment = (resProp.comments.length ? resProp.comments : resProp.labels);
newResProperty.subPropertyOf = resProp.subPropertyOf;
newResProperty.type = Constants.ObjectProperty;
resPropPay.label = resourceProperties.entity.label;
resPropPay.comment = (resourceProperties.entity.comment.length ? resourceProperties.entity.comment : resourceProperties.entity.label);
resPropPay.subPropertyOf = resourceProperties.entity.subPropertyOf;

newResProperty.subjectType = resProp.subjectType;
newResProperty.objectType = resProp.objectType;
resPropPay.subjectType = resourceProperties.entity.subjectType;
resPropPay.objectType = resourceProperties.entity.objectType;

if (resProp.guiElement) {
newResProperty.guiElement = resProp.guiElement;
if (resourceProperties.entity.guiElement) {
resPropPay.guiElement = resourceProperties.entity.guiElement;
}
if (resProp.guiAttributes) {
newResProperty.guiAttributes = resProp.guiAttributes;
if (resourceProperties.entity.guiAttributes) {
resPropPay.guiAttributes = resourceProperties.entity.guiAttributes;
}

resPropPayload.resProperty = [newResProperty];
const ontoPayload = this.jsonConvert.serializeObject(resourceProperties);

const payload = this.jsonConvert.serializeObject(resPropPayload);
ontoPayload["@graph"] = [this.jsonConvert.serializeObject(resPropPay)];

return this.httpPost("/properties", payload).pipe(
return this.httpPost("/properties", ontoPayload).pipe(
mergeMap((ajaxResponse: AjaxResponse) => {
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
Expand All @@ -258,11 +244,11 @@ export class OntologiesEndpointV2 extends Endpoint {
/**
* Deletes a resource property.
*
* @param updateOntology with property IRI.
* @param deleteResourceProperty with property IRI.
*/
deleteResourceProperty(updateOntology: UpdateOntology): Observable<OntologyMetadata | ApiResponseError> {
deleteResourceProperty(deleteResourceProperty: DeleteResourceProperty): Observable<OntologyMetadata | ApiResponseError> {

const path = "/properties/" + encodeURIComponent(updateOntology.id) + "?lastModificationDate=" + encodeURIComponent(updateOntology.lastModificationDate);
const path = "/properties/" + encodeURIComponent(deleteResourceProperty.id) + "?lastModificationDate=" + encodeURIComponent(deleteResourceProperty.lastModificationDate);

return this.httpDelete(path).pipe(
mergeMap((ajaxResponse: AjaxResponse) => {
Expand All @@ -282,7 +268,7 @@ export class OntologiesEndpointV2 extends Endpoint {
*
* @param addCardinalityToResourceClass the cardinailities to be added.
*/
addCardinalityToResourceClass(addCardinalityToResourceClass: AddCardinalityToResourceClass): Observable<ResourceClassDefinitionWithAllLanguages | ApiResponseError> {
addCardinalityToResourceClass(addCardinalityToResourceClass: UpdateOntologyResourceClassCardinality): Observable<ResourceClassDefinitionWithAllLanguages | ApiResponseError> {

return this.httpPost("/cardinalities", this.jsonConvert.serializeObject(addCardinalityToResourceClass)).pipe(
mergeMap((ajaxResponse: AjaxResponse) => {
Expand Down
Loading