UNPKG

@knora/core

Version:
1,180 lines (1,166 loc) 98.4 kB
import { __decorate, __values, __param, __metadata, __extends } from 'tslib'; import { InjectionToken, NgModule, Inject, ɵɵdefineInjectable, ɵɵinject, Injectable } from '@angular/core'; import { Constants, KnoraApiConnection, KnoraApiConfig } from '@knora/api'; import * as momentImported from 'moment'; import { JsonConvert, OperationMode, ValueCheckingMode } from 'json2typescript'; import { BehaviorSubject, from, Observable, of, forkJoin } from 'rxjs'; import { HttpHeaders, HttpClient } from '@angular/common/http'; import { throwError } from 'rxjs/internal/observable/throwError'; import { map, catchError, mergeMap } from 'rxjs/operators'; // config for knora-api-js-lib (@knora/api) var KnoraApiConfigToken = new InjectionToken('Knora api configuration'); // connection config for knora-api-js-lib (@knora/api) var KnoraApiConnectionToken = new InjectionToken('Knora api connection configuration'); // config for knora-ui (@knora/action, @knora/search, @knora/viewer) var KuiConfigToken = new InjectionToken('Main configuration for knora-ui modules'); var KuiCoreModule = /** @class */ (function () { function KuiCoreModule() { } KuiCoreModule_1 = KuiCoreModule; KuiCoreModule.forRoot = function (kuiConfig) { // get the app environment configuration here // console.log('KuiCoreModule - forRoot - config: ', config); return { ngModule: KuiCoreModule_1, providers: [ { provide: KuiConfigToken, useValue: kuiConfig } ] }; }; var KuiCoreModule_1; KuiCoreModule = KuiCoreModule_1 = __decorate([ NgModule({ imports: [], declarations: [], exports: [] }) ], KuiCoreModule); return KuiCoreModule; }()); var KuiConfig = /** @class */ (function () { function KuiConfig(knora, app) { this.knora = knora; this.app = app; } return KuiConfig; }()); var moment = momentImported; var SessionService = /** @class */ (function () { function SessionService(knoraApiConnection, knoraApiConfig) { this.knoraApiConnection = knoraApiConnection; this.knoraApiConfig = knoraApiConfig; /** * max session time in milliseconds * default value (24h): 86400000 * */ this.MAX_SESSION_TIME = 86400000; // 1d = 24 * 60 * 60 * 1000 } /** * set the session by using the json web token (jwt) and the user object; * it will be used in the login process * * @param jwt * @param username */ SessionService.prototype.setSession = function (jwt, identifier, identifierType) { var _this = this; var session; this.updateKnoraApiConnection(jwt); // get user information this.knoraApiConnection.admin.usersEndpoint.getUser(identifierType, identifier).subscribe(function (response) { var e_1, _a; var sysAdmin = false; var projectAdmin = []; // get permission inforamation: a) is user sysadmin? b) get list of project iris where user is project admin var groupsPerProjectKeys = Object.keys(response.body.user.permissions.groupsPerProject); try { for (var groupsPerProjectKeys_1 = __values(groupsPerProjectKeys), groupsPerProjectKeys_1_1 = groupsPerProjectKeys_1.next(); !groupsPerProjectKeys_1_1.done; groupsPerProjectKeys_1_1 = groupsPerProjectKeys_1.next()) { var key = groupsPerProjectKeys_1_1.value; if (key === Constants.SystemProjectIRI) { sysAdmin = response.body.user.permissions.groupsPerProject[key].indexOf(Constants.SystemAdminGroupIRI) > -1; } if (response.body.user.permissions.groupsPerProject[key].indexOf(Constants.ProjectAdminGroupIRI) > -1) { projectAdmin.push(key); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (groupsPerProjectKeys_1_1 && !groupsPerProjectKeys_1_1.done && (_a = groupsPerProjectKeys_1.return)) _a.call(groupsPerProjectKeys_1); } finally { if (e_1) throw e_1.error; } } // store session information in browser's localstorage session = { id: _this.setTimestamp(), user: { name: response.body.user.username, jwt: jwt, lang: response.body.user.lang, sysAdmin: sysAdmin, projectAdmin: projectAdmin } }; // update localStorage localStorage.setItem('session', JSON.stringify(session)); }, function (error) { localStorage.removeItem('session'); console.error(error); }); }; /** * Validate intern session and check knora api credentials if necessary. * If a json web token exists, it doesn't mean, that the knora api credentials are still valid. * * @returns boolean */ SessionService.prototype.validateSession = function () { var _this = this; // mix of checks with session.validation and this.authenticate var session = JSON.parse(localStorage.getItem('session')); var tsNow = this.setTimestamp(); if (session) { this.updateKnoraApiConnection(session.user.jwt); // check if the session is still valid: if (session.id + this.MAX_SESSION_TIME <= tsNow) { // the internal (knora-ui) session has expired // check if the api credentails are still valid // console.error('session is not valid; check knora api credentials'); this.knoraApiConnection.v2.auth.checkCredentials().subscribe(function (response) { // the knora api credentials are still valid // console.log('knora api credentials', response); // refresh the jwt in @knora/api _this.updateKnoraApiConnection(session.user.jwt); // update the session.id session.id = tsNow; localStorage.setItem('session', JSON.stringify(session)); // console.log('knora api credentials are valid; return', true); return true; }, function (error) { // a user is not authenticated anymore! // console.error('session.service -- validateSession -- authenticate: the session expired on API side'); _this.destroySession(); // console.warn('knora api credentials are not valid; return', false); return false; }); } else { // the internal (knora-ui) session is still valid // console.log('session is valid; return', true); return true; } } else { // no session found; update knora api connection with empty jwt this.updateKnoraApiConnection(); // console.warn('session is not valid; return', false); return false; } }; /** * update the session storage * @param jwt * @param username * * @returns boolean */ SessionService.prototype.updateSession = function (jwt, username) { if (jwt && username) { this.setSession(jwt, username, 'username'); return true; } else { return false; } }; /** * Destroy session by removing the session from local storage * */ SessionService.prototype.destroySession = function () { localStorage.removeItem('session'); }; /** * Update the knora-api-config and knora-api-connection of @knora/api * * @param {string} jwt? */ SessionService.prototype.updateKnoraApiConnection = function (jwt) { this.knoraApiConfig.jsonWebToken = (jwt ? jwt : ''); this.knoraApiConnection = new KnoraApiConnection(this.knoraApiConfig); }; /** * Convert a timestamp in a number * @returns number */ SessionService.prototype.setTimestamp = function () { return (moment().add(0, 'second')).valueOf(); }; SessionService.ctorParameters = function () { return [ { type: KnoraApiConnection, decorators: [{ type: Inject, args: [KnoraApiConnectionToken,] }] }, { type: KnoraApiConfig, decorators: [{ type: Inject, args: [KnoraApiConfigToken,] }] } ]; }; SessionService.ngInjectableDef = ɵɵdefineInjectable({ factory: function SessionService_Factory() { return new SessionService(ɵɵinject(KnoraApiConnectionToken), ɵɵinject(KnoraApiConfigToken)); }, token: SessionService, providedIn: "root" }); SessionService = __decorate([ Injectable({ providedIn: 'root' }), __param(0, Inject(KnoraApiConnectionToken)), __param(1, Inject(KnoraApiConfigToken)), __metadata("design:paramtypes", [KnoraApiConnection, KnoraApiConfig]) ], SessionService); return SessionService; }()); /** * @deprecated Use ApiResponseData from `@knora/api` (github:dasch-swiss/knora-api-js-lib) instead * Result class used as API url response in ApiService */ var ApiServiceResult = /** @class */ (function () { function ApiServiceResult() { /** * Status number */ this.status = 0; /** * Status text */ this.statusText = ''; /** * API url */ this.url = ''; } /** * Gets the result body as instance of classObject. * @param classObject * @returns {any} * @throws */ ApiServiceResult.prototype.getBody = function (classObject) { // console.log(this.body); return ApiServiceResult.jsonConvert.deserialize(this.body, classObject); }; ApiServiceResult.jsonConvert = new JsonConvert(OperationMode.ENABLE, ValueCheckingMode.ALLOW_NULL); return ApiServiceResult; }()); /** * @deprecated Use ApiResponseError from `@knora/api` (github:dasch-swiss/knora-api-js-lib) instead * * Error class used as API response in ApiService */ var ApiServiceError = /** @class */ (function () { function ApiServiceError() { /** * Status number */ this.status = 0; /** * Status text */ this.statusText = ''; /** * API url */ this.url = ''; /** * Additional error info */ this.errorInfo = ''; } return ApiServiceError; }()); var KnoraConstants = /** @class */ (function () { function KnoraConstants() { } // The following version of Knora is needed to work properly with this module KnoraConstants.KnoraVersion = '12.0.0'; KnoraConstants.KnoraApi = 'http://api.knora.org/ontology/knora-api'; KnoraConstants.PathSeparator = '#'; // Simple KnoraConstants.KnoraApiV2SimplePath = KnoraConstants.KnoraApi + '/simple/v2' + KnoraConstants.PathSeparator; KnoraConstants.resourceSimple = KnoraConstants.KnoraApiV2SimplePath + 'Resource'; KnoraConstants.dateSimple = KnoraConstants.KnoraApiV2SimplePath + 'Date'; KnoraConstants.intervalSimple = KnoraConstants.KnoraApiV2SimplePath + 'Interval'; KnoraConstants.geomSimple = KnoraConstants.KnoraApiV2SimplePath + 'Geom'; KnoraConstants.colorSimple = KnoraConstants.KnoraApiV2SimplePath + 'Color'; KnoraConstants.geonameSimple = KnoraConstants.KnoraApiV2SimplePath + 'Geoname'; KnoraConstants.fileSimple = KnoraConstants.KnoraApiV2SimplePath + 'File'; KnoraConstants.listNodeSimple = KnoraConstants.KnoraApiV2SimplePath + 'ListNode'; // comparison operators --> TODO: should be moved to search-module!? KnoraConstants.EqualsComparisonOperator = '='; KnoraConstants.EqualsComparisonLabel = 'is equal to'; KnoraConstants.NotEqualsComparisonOperator = '!='; KnoraConstants.NotEqualsComparisonLabel = 'is not equal to'; KnoraConstants.GreaterThanComparisonOperator = '>'; KnoraConstants.GreaterThanComparisonLabel = 'is greater than'; KnoraConstants.GreaterThanEqualsComparisonOperator = '>='; KnoraConstants.GreaterThanEqualsComparisonLabel = 'is greater than equals to'; KnoraConstants.LessThanComparisonOperator = '<'; KnoraConstants.LessThanComparisonLabel = 'is less than'; KnoraConstants.LessThanEqualsComparisonOperator = '<='; KnoraConstants.LessThanQualsComparisonLabel = 'is less than equals to'; KnoraConstants.ExistsComparisonOperator = 'E'; KnoraConstants.ExistsComparisonLabel = 'exists'; KnoraConstants.LikeComparisonOperator = 'regex'; KnoraConstants.LikeComparisonLabel = 'is like'; KnoraConstants.MatchComparisonOperator = 'contains'; KnoraConstants.MatchComparisonLabel = 'matches'; KnoraConstants.MatchFunction = KnoraConstants.KnoraApi + '/v2' + KnoraConstants.PathSeparator + 'match'; return KnoraConstants; }()); // Knora Schema var KnoraSchema; (function (KnoraSchema) { KnoraSchema[KnoraSchema["complex"] = 0] = "complex"; KnoraSchema[KnoraSchema["simple"] = 1] = "simple"; })(KnoraSchema || (KnoraSchema = {})); /** * @deprecated since v10.0.0 * * Will be replaced by `@knora/api` (github:knora-api-js-lib) * * Represents the parameters of an extended search. */ var ExtendedSearchParams = /** @class */ (function () { /** * * @param generateGravsearch a function that generates a Gravsearch query. * * The function takes the offset * as a parameter and returns a Gravsearch query string. * Returns false if not set correctly (init state). */ function ExtendedSearchParams(generateGravsearch) { this.generateGravsearch = generateGravsearch; } return ExtendedSearchParams; }()); var SearchParamsService = /** @class */ (function () { function SearchParamsService() { // init with a dummy function that returns false // if the application is reloaded, this will be returned this._currentSearchParams = new BehaviorSubject(new ExtendedSearchParams(function (offset) { return false; })); } /** * Updates the parameters of an extended search. * * @param {ExtendedSearchParams} searchParams * @returns void */ SearchParamsService.prototype.changeSearchParamsMsg = function (searchParams) { this._currentSearchParams.next(searchParams); }; /** * Gets the search params of an extended search. * * @returns ExtendedSearchParams - search parameters */ SearchParamsService.prototype.getSearchParams = function () { return this._currentSearchParams.getValue(); }; SearchParamsService.ngInjectableDef = ɵɵdefineInjectable({ factory: function SearchParamsService_Factory() { return new SearchParamsService(); }, token: SearchParamsService, providedIn: "root" }); SearchParamsService = __decorate([ Injectable({ providedIn: 'root' }) /** * @deprecated since v10.0.0 * * Will be replaced by `@knora/api` (github:knora-api-js-lib) * Temporarily stores the parameters of an extended search. */ , __metadata("design:paramtypes", []) ], SearchParamsService); return SearchParamsService; }()); /** * @deprecated Use new service from `@knora/api` (github:dasch-swiss/knora-api-js-lib) instead * * Create GravSearch queries from provided parameters. */ var GravsearchGenerationService = /** @class */ (function () { function GravsearchGenerationService(_searchParamsService) { this._searchParamsService = _searchParamsService; } GravsearchGenerationService_1 = GravsearchGenerationService; /** * @deprecated since v10.0.0 * * Will be replaced by `@knora/api` (github:knora-api-js-lib) * * Generates a Gravsearch query from the provided arguments. * * @param {PropertyWithValue[]} properties the properties specified by the user. * @param {string} [mainResourceClassOption] the class of the main resource, if specified. * @param {number} offset the offset to be used (nth page of results). * @returns string - a KnarQL query string. */ GravsearchGenerationService.prototype.createGravsearchQuery = function (properties, mainResourceClassOption, offset) { if (offset === void 0) { offset = 0; } // class restriction for the resource searched for var mainResourceClass = ''; // if given, create the class restriction for the main resource if (mainResourceClassOption !== undefined) { mainResourceClass = "?mainRes a <" + mainResourceClassOption + "> ."; } // criteria for the order by statement var orderByCriteria = []; // statements to be returned in query results var returnStatements = []; // loop over given properties and create statements and filters from them var props = properties.map(function (propWithVal, index) { // represents the object of a statement var propValue; if (!propWithVal.property.isLinkProperty || propWithVal.valueLiteral.comparisonOperator.getClassName() === 'Exists') { // it is not a linking property, create a variable for the value (to be used by a subsequent FILTER) // OR the comparison operator Exists is used in which case we do not need to specify the object any further propValue = "?propVal" + index; } else { // it is a linking property and the comparison operator is not Exists, use its IRI propValue = propWithVal.valueLiteral.value.toSparql(KnoraSchema.complex); } // generate statement var statement = "?mainRes <" + propWithVal.property.id + "> " + propValue + " ."; // check if it is a linking property that has to be wrapped in a FILTER NOT EXISTS (comparison operator NOT_EQUALS) to negate it if (propWithVal.property.isLinkProperty && propWithVal.valueLiteral.comparisonOperator.getClassName() === 'NotEquals') { // do not include statement in results, because the query checks for the absence of this statement statement = "FILTER NOT EXISTS {\n" + statement + "\n\n\n}"; } else { // TODO: check if statement should be returned returned in results (Boolean flag from checkbox) returnStatements.push(statement); statement = "\n" + statement + "\n\n\n"; } // generate restricting expression (e.g., a FILTER) if comparison operator is not Exists var restriction = ''; // only create a FILTER if the comparison operator is not EXISTS and it is not a linking property if (!propWithVal.property.isLinkProperty && propWithVal.valueLiteral.comparisonOperator.getClassName() !== 'Exists') { // generate variable for value literal var propValueLiteral = propValue + "Literal"; if (propWithVal.valueLiteral.comparisonOperator.getClassName() === 'Like') { // generate statement to value literal restriction = propValue + " <" + GravsearchGenerationService_1.complexTypeToProp[propWithVal.property.objectType] + "> " + propValueLiteral + '\n'; // use regex function for LIKE restriction += "FILTER regex(" + propValueLiteral + ", " + propWithVal.valueLiteral.value.toSparql(KnoraSchema.complex) + ", \"i\")"; } else if (propWithVal.valueLiteral.comparisonOperator.getClassName() === 'Match') { // generate statement to value literal restriction = propValue + " <" + GravsearchGenerationService_1.complexTypeToProp[propWithVal.property.objectType] + "> " + propValueLiteral + '\n'; // use contains function for MATCH restriction += "FILTER <" + KnoraConstants.MatchFunction + ">(" + propValueLiteral + ", " + propWithVal.valueLiteral.value.toSparql(KnoraSchema.complex) + ")"; } else if (propWithVal.property.objectType === Constants.DateValue) { // handle date property restriction = "FILTER(knora-api:toSimpleDate(" + propValue + ") " + propWithVal.valueLiteral.comparisonOperator.type + " " + propWithVal.valueLiteral.value.toSparql(KnoraSchema.complex) + ")"; } else if (propWithVal.property.objectType === Constants.ListValue) { // handle list node restriction = propValue + " <" + GravsearchGenerationService_1.complexTypeToProp[propWithVal.property.objectType] + "> " + propWithVal.valueLiteral.value.toSparql(KnoraSchema.complex) + '\n'; // check for comparison operator "not equals" if (propWithVal.valueLiteral.comparisonOperator.getClassName() === 'NotEquals') { restriction = "FILTER NOT EXISTS {\n " + restriction + "\n }"; } } else { // generate statement to value literal restriction = propValue + " <" + GravsearchGenerationService_1.complexTypeToProp[propWithVal.property.objectType] + "> " + propValueLiteral + '\n'; // generate filter expression restriction += "FILTER(" + propValueLiteral + " " + propWithVal.valueLiteral.comparisonOperator.type + " " + propWithVal.valueLiteral.value.toSparql(KnoraSchema.complex) + ")"; } } // check if current value is a sort criterion if (propWithVal.isSortCriterion) orderByCriteria.push(propValue); return statement + "\n" + restriction + "\n"; }); var orderByStatement = ''; if (orderByCriteria.length > 0) { orderByStatement = "\nORDER BY " + orderByCriteria.join(' ') + "\n"; } // template of the Gravsearch query with dynamic components var gravsearchTemplate = "\nPREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>\nCONSTRUCT {\n\n?mainRes knora-api:isMainResource true .\n\n" + returnStatements.join('\n') + "\n\n} WHERE {\n\n?mainRes a knora-api:Resource .\n\n" + mainResourceClass + "\n\n" + props.join('') + "\n\n}\n" + orderByStatement; // offset component of the Gravsearch query var offsetTemplate = "\nOFFSET " + offset + "\n"; // function that generates the same KnarQL query with the given offset var generateGravsearchQueryWithCustomOffset = function (localOffset) { var offsetCustomTemplate = "\nOFFSET " + localOffset + "\n"; return gravsearchTemplate + offsetCustomTemplate; }; if (offset === 0) { // store the function so another KnarQL query can be created with an increased offset this._searchParamsService.changeSearchParamsMsg(new ExtendedSearchParams(generateGravsearchQueryWithCustomOffset)); } return gravsearchTemplate + offsetTemplate; }; var GravsearchGenerationService_1; /** * @ignore * * Map of complex knora-api value types to simple ones. * Use computed property name: http://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer. */ GravsearchGenerationService.typeConversionComplexToSimple = { 'http://api.knora.org/ontology/knora-api/v2#IntValue': Constants.XsdInteger, 'http://api.knora.org/ontology/knora-api/v2#DecimalValue': Constants.XsdDecimal, 'http://api.knora.org/ontology/knora-api/v2#BooleanValue': Constants.XsdBoolean, 'http://api.knora.org/ontology/knora-api/v2#TextValue': Constants.XsdString, 'http://api.knora.org/ontology/knora-api/v2#UriValue': Constants.XsdAnyUri, 'http://api.knora.org/ontology/knora-api/v2#DateValue': KnoraConstants.dateSimple, 'http://api.knora.org/ontology/knora-api/v2#IntervalValue': KnoraConstants.intervalSimple, 'http://api.knora.org/ontology/knora-api/v2#GeomValue': KnoraConstants.geomSimple, 'http://api.knora.org/ontology/knora-api/v2#ColorValue': KnoraConstants.colorSimple, 'http://api.knora.org/ontology/knora-api/v2#GeonameValue': KnoraConstants.geonameSimple, 'http://api.knora.org/ontology/knora-api/v2#StillImageFileValue': KnoraConstants.fileSimple, 'http://api.knora.org/ontology/knora-api/v2#FileValue': KnoraConstants.fileSimple, 'http://api.knora.org/ontology/knora-api/v2#MovingImageFileValue': KnoraConstants.fileSimple, 'http://api.knora.org/ontology/knora-api/v2#DDDFileValue': KnoraConstants.fileSimple, 'http://api.knora.org/ontology/knora-api/v2#AudioFileValue': KnoraConstants.fileSimple, 'http://api.knora.org/ontology/knora-api/v2#DocumentFileValue': KnoraConstants.fileSimple, 'http://api.knora.org/ontology/knora-api/v2#TextFileValue': KnoraConstants.fileSimple, 'http://api.knora.org/ontology/knora-api/v2#ListValue': KnoraConstants.listNodeSimple }; GravsearchGenerationService.complexTypeToProp = { 'http://api.knora.org/ontology/knora-api/v2#IntValue': Constants.IntValueAsInt, 'http://api.knora.org/ontology/knora-api/v2#DecimalValue': Constants.DecimalValueAsDecimal, 'http://api.knora.org/ontology/knora-api/v2#BooleanValue': Constants.BooleanValueAsBoolean, 'http://api.knora.org/ontology/knora-api/v2#TextValue': Constants.ValueAsString, 'http://api.knora.org/ontology/knora-api/v2#UriValue': Constants.UriValueAsUri, 'http://api.knora.org/ontology/knora-api/v2#ListValue': Constants.ListValueAsListNode }; GravsearchGenerationService.ctorParameters = function () { return [ { type: SearchParamsService } ]; }; GravsearchGenerationService.ngInjectableDef = ɵɵdefineInjectable({ factory: function GravsearchGenerationService_Factory() { return new GravsearchGenerationService(ɵɵinject(SearchParamsService)); }, token: GravsearchGenerationService, providedIn: "root" }); GravsearchGenerationService = GravsearchGenerationService_1 = __decorate([ Injectable({ providedIn: 'root' }), __metadata("design:paramtypes", [SearchParamsService]) ], GravsearchGenerationService); return GravsearchGenerationService; }()); var Equals = /** @class */ (function () { function Equals() { this.type = KnoraConstants.EqualsComparisonOperator; this.label = KnoraConstants.EqualsComparisonLabel; } Equals.prototype.getClassName = function () { return 'Equals'; }; return Equals; }()); var NotEquals = /** @class */ (function () { function NotEquals() { this.type = KnoraConstants.NotEqualsComparisonOperator; this.label = KnoraConstants.NotEqualsComparisonLabel; } NotEquals.prototype.getClassName = function () { return 'NotEquals'; }; return NotEquals; }()); var GreaterThanEquals = /** @class */ (function () { function GreaterThanEquals() { this.type = KnoraConstants.GreaterThanEqualsComparisonOperator; this.label = KnoraConstants.GreaterThanEqualsComparisonLabel; } GreaterThanEquals.prototype.getClassName = function () { return 'GreaterThanEquals'; }; return GreaterThanEquals; }()); var GreaterThan = /** @class */ (function () { function GreaterThan() { this.type = KnoraConstants.GreaterThanComparisonOperator; this.label = KnoraConstants.GreaterThanComparisonLabel; } GreaterThan.prototype.getClassName = function () { return 'GreaterThan'; }; return GreaterThan; }()); var LessThan = /** @class */ (function () { function LessThan() { this.type = KnoraConstants.LessThanComparisonOperator; this.label = KnoraConstants.LessThanComparisonLabel; } LessThan.prototype.getClassName = function () { return 'LessThan'; }; return LessThan; }()); var LessThanEquals = /** @class */ (function () { function LessThanEquals() { this.type = KnoraConstants.LessThanEqualsComparisonOperator; this.label = KnoraConstants.LessThanQualsComparisonLabel; } LessThanEquals.prototype.getClassName = function () { return 'LessThanEquals'; }; return LessThanEquals; }()); var Exists = /** @class */ (function () { function Exists() { this.type = KnoraConstants.ExistsComparisonOperator; this.label = KnoraConstants.ExistsComparisonLabel; } Exists.prototype.getClassName = function () { return 'Exists'; }; return Exists; }()); var Like = /** @class */ (function () { function Like() { this.type = KnoraConstants.LikeComparisonOperator; this.label = KnoraConstants.LikeComparisonLabel; } Like.prototype.getClassName = function () { return 'Like'; }; return Like; }()); var Match = /** @class */ (function () { function Match() { this.type = KnoraConstants.MatchComparisonOperator; this.label = KnoraConstants.MatchComparisonLabel; } Match.prototype.getClassName = function () { return 'Match'; }; return Match; }()); /** * Combination of a comparison operator and a value literal or an IRI. * In case the comparison operator is 'Exists', no value is given. */ var ComparisonOperatorAndValue = /** @class */ (function () { function ComparisonOperatorAndValue(comparisonOperator, value) { this.comparisonOperator = comparisonOperator; this.value = value; } return ComparisonOperatorAndValue; }()); /** * Represents a property's value as a literal with the indication of its type. */ var ValueLiteral = /** @class */ (function () { /** * Constructs a [ValueLiteral]. * * @param {string} value the literal representation of the value. * @param {string} type the type of the value (making use of xsd). */ function ValueLiteral(value, type) { this.value = value; this.type = type; } /** * Creates a type annotated value literal to be used in a SPARQL query. * * @param schema indicates the Knora schema to be used. * @returns {string} */ ValueLiteral.prototype.toSparql = function (schema) { var literalType; // check if a Knora schema conversion is necessary, e.g., knora-api:dateValue (complex) to knora-api:date (simple). // xsd types will remain unchanged if (schema === KnoraSchema.simple && GravsearchGenerationService.typeConversionComplexToSimple[this.type] !== undefined) { // convert to simple schema literalType = GravsearchGenerationService.typeConversionComplexToSimple[this.type]; } else { // do not convert literalType = this.type; } return "\"" + this.value + "\"^^<" + literalType + ">"; }; return ValueLiteral; }()); /** * Represents an IRI. */ var IRI = /** @class */ (function () { /** * Constructs an [IRI]. * * @param {string} iri the IRI of a resource instance. */ function IRI(iri) { this.iri = iri; } /** * Creates a SPARQL representation of the IRI. * * @param schema indicates the Knora schema to be used. * @returns {string} */ IRI.prototype.toSparql = function (schema) { // this is an instance Iri and does not have to be converted. return "<" + this.iri + ">"; }; return IRI; }()); /** * Represents a property, the specified comparison operator, and value. */ var PropertyWithValue = /** @class */ (function () { /** * Constructs a [PropertyWithValue]. * * @param {Property} property the specified property. * @param {ComparisonOperatorAndValue} valueLiteral the specified comparison operator and value. * @param isSortCriterion indicates if the property is used as a sort criterion. */ function PropertyWithValue(property, valueLiteral, isSortCriterion) { this.property = property; this.valueLiteral = valueLiteral; this.isSortCriterion = isSortCriterion; } return PropertyWithValue; }()); /** * Collection of useful utility functions. */ // @dynamic var Utils = /** @class */ (function () { function Utils() { } /** * Given a Knora entity IRI, gets the ontology Iri. * * @param {string} entityIri an entity Iri. * @return {string} the ontology IRI */ Utils.getOntologyIriFromEntityIri = function (entityIri) { // split class Iri on "#" var segments = entityIri.split(KnoraConstants.PathSeparator); if (segments.length !== 2) console.error("Error: " + entityIri + " is not a valid entity IRI."); return segments[0]; }; /** * Converts a complex knora-api entity Iri to a knora-api simple entity Iri. * * @param {string} complexEntityIri * @returns {string} */ Utils.convertComplexKnoraApiEntityIritoSimple = function (complexEntityIri) { // split entity Iri on "#" var segments = complexEntityIri.split('v2' + KnoraConstants.PathSeparator); if (segments.length !== 2) console.error("Error: " + complexEntityIri + " is not a valid entity IRI."); // add 'simple' to base path return segments[0] + 'simple/v2' + KnoraConstants.PathSeparator + segments[1]; }; /** * A regex to validate Email address. * * @type {RegExp} */ Utils.RegexEmail = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; /** * A regex to validate Username. * * @type {RegExp} */ Utils.RegexUsername = /^[a-zA-Z0-9]+$/; /** * A regex to validate URLs. * * @type {RegExp} */ Utils.RegexUrl = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/i; /** * A regex to validate Passwords * * @type {RegExp} */ Utils.RegexPassword = /^(?=.*\d)(?=.*[a-zA-Z]).{8,}$/i; /** * A regex to validate Hexadecimal values * * @type {RegExp} */ Utils.RegexHex = /^[0-9A-Fa-f]+$/; /** * A regex to validate shortname in projects * * @type {RegExp} */ Utils.RegexShortname = /^[a-zA-Z]+\S*$/; /** * Lambda function eliminating duplicates in a collection to be passed to [[filter]]. * * @param elem element of an Array that is currently being looked at. * @param index current elements index. * @param self reference to the whole Array. * @returns {boolean} true if the same element does not already exist in the Array. */ Utils.filterOutDuplicates = function (elem, index, self) { // https://stackoverflow.com/questions/16747798/delete-duplicate-elements-from-an-array // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?v=example // returns true if the element's index equals the index of the leftmost element // -> this means that there is no identical element before this index, hence it is not a duplicate // for all other elements, false is returned return index === self.indexOf(elem); }; return Utils; }()); /** * Represents a region. * Contains a reference to the resource representing the region and its geometries. */ var Region = /** @class */ (function () { /** * * @param {ReadResource} regionResource a resource of type Region */ function Region(regionResource) { this.regionResource = regionResource; } /** * Get all geometry information belonging to this region. * * @returns {ReadGeomValue[]} */ Region.prototype.getGeometries = function () { return this.regionResource.properties[Constants.HasGeometry]; }; return Region; }()); /** * @deprecated since v 10.0.0: replaced by knora-api-js-lib * */ var jsonld = require('jsonld'); var semver = require('semver'); var ApiService = /** @class */ (function () { function ApiService(http, knoraApiConfig) { this.http = http; this.knoraApiConfig = knoraApiConfig; // if is loading, set it true; // it can be used in components // for progress loader element this.loading = false; } /** * @deprecated since v9.5.0 * * GET * * @param {string} path the URL for the GET request. * @param {HttpParams} params the parameters for the GET request. * @returns Observable of any */ ApiService.prototype.httpGet = function (path, params) { var _this = this; this.loading = true; var headers = this.setHeaders(); return this.http.get(this.knoraApiConfig.apiUrl + path, { headers: headers, observe: 'response', params: params }).pipe(map(function (response) { _this.loading = false; var result = new ApiServiceResult(); result.header = { 'server': response.headers.get('Server') }; _this.compareVersion(response.headers.get('Server')); result.status = response.status; result.statusText = response.statusText; result.url = path; result.body = response.body; return result; }), catchError(function (error) { _this.loading = false; return _this.handleRequestError(error); })); }; /** * @deprecated since v9.5.0 * * Processes JSON-LD returned by Knora. * Expands Iris and creates an empty context object. * * @param {ApiServiceResult} resourceResponse */ ApiService.prototype.processJSONLD = function (resourceResponse) { var resPromises = jsonld.promises; // compact JSON-LD using an empty context: expands all Iris var resPromise = resPromises.compact(resourceResponse.body, {}); // convert promise to Observable and return it // https://www.learnrxjs.io/operators/creation/frompromise.html return from(resPromise); }; /** * @deprecated since v9.5.0 * * POST * * @param {string} path * @param {any} body * @returns Observable of any */ ApiService.prototype.httpPost = function (path, body) { var _this = this; this.loading = true; var headers = this.setHeaders(); return this.http.post(this.knoraApiConfig.apiUrl + path, body, { headers: headers, observe: 'response' }).pipe(map(function (response) { _this.loading = false; var result = new ApiServiceResult(); result.header = { 'server': response.headers.get('Server') }; _this.compareVersion(result.header.server); result.status = response.status; result.statusText = response.statusText; result.url = path; result.body = response.body; return result; }), catchError(function (error) { _this.loading = false; // console.error(error); return _this.handleRequestError(error); })); }; /** * @deprecated since v9.5.0 * * PUT * * @param {string} path * @param {any} body * @returns Observable of any */ ApiService.prototype.httpPut = function (path, body) { var _this = this; this.loading = true; var headers = this.setHeaders(); return this.http.put(this.knoraApiConfig.apiUrl + path, body, { headers: headers, observe: 'response' }).pipe(map(function (response) { _this.loading = false; // console.log(response); var result = new ApiServiceResult(); result.header = { 'server': response.headers.get('Server') }; _this.compareVersion(result.header.server); result.status = response.status; result.statusText = response.statusText; result.url = path; result.body = response.body; return result; }), catchError(function (error) { _this.loading = false; // console.error(error); return _this.handleRequestError(error); })); }; /** * @deprecated since v9.5.0 * * DELETE * * @param {string} path * @returns Observable of any */ ApiService.prototype.httpDelete = function (path) { var _this = this; this.loading = true; var headers = this.setHeaders(); return this.http.delete(this.knoraApiConfig.apiUrl + path, { headers: headers, observe: 'response' }).pipe(map(function (response) { _this.loading = false; // console.log(response); var result = new ApiServiceResult(); result.header = { 'server': response.headers.get('Server') }; _this.compareVersion(result.header.server); result.status = response.status; result.statusText = response.statusText; result.url = path; result.body = response.body; return result; }), catchError(function (error) { _this.loading = false; // console.error(error); return _this.handleRequestError(error); })); }; /** * handle request error in case of server error * * @param {HttpErrorResponse} error * @returns Observable of ApiServiceError */ ApiService.prototype.handleRequestError = function (error) { // console.error(error); var serviceError = new ApiServiceError(); serviceError.header = { 'server': error.headers.get('Server') }; serviceError.status = error.status; serviceError.statusText = error.statusText; serviceError.errorInfo = error.message; serviceError.url = error.url; return throwError(serviceError); }; /** * handle json error in case of type error in json response (json2typescript) * * @param {any} error * @returns Observable of ApiServiceError */ ApiService.prototype.handleJsonError = function (error) { if (error instanceof ApiServiceError) return throwError(error); var serviceError = new ApiServiceError(); serviceError.header = { 'server': error.headers.get('Server') }; serviceError.status = -1; serviceError.statusText = 'Invalid JSON'; serviceError.errorInfo = error; serviceError.url = ''; return throwError(serviceError); }; /** * @deprecated since v9.5.0 * * @param {string} server */ ApiService.prototype.compareVersion = function (server) { // expected knora api version var expected = KnoraConstants.KnoraVersion; // existing knora api version if (server) { var versions = server.split(' '); var existing = versions[0].split('/')[1]; // compare the two versions: expected vs existing if (semver.diff(existing, expected) === 'major') { console.warn('The version of the @knora/core module works with Knora v' + expected + ', but you are using it with Knora v' + existing); } } else { // console.warn('No server information from headers response'); } }; /** * Set headers to authorise http requests * * This method was replaced by interceptor. But since the implementation * of new knora-api-js-lib services the interceptor is redundant. * As long we not all services are replaced by knora-api-js-lib * we have to use this setHeaders "hack" for the two services left. * * @returns HttpHeaders */ ApiService.prototype.setHeaders = function () { var session = JSON.parse(localStorage.getItem('session')); if (session && session !== null) { return new HttpHeaders({ 'Authorization': "Bearer " + session.user.jwt }); } else { return new HttpHeaders(); } }; ApiService.ctorParameters = function () { return [ { type: HttpClient }, { type: KnoraApiConfig, decorators: [{ type: Inject, args: [KnoraApiConfigToken,] }] } ]; }; ApiService.ngInjectableDef = ɵɵdefineInjectable({ factory: function ApiService_Factory() { return new ApiService(ɵɵinject(HttpClient), ɵɵinject(KnoraApiConfigToken)); }, token: ApiService, providedIn: "root" }); ApiService = __decorate([ Injectable({ providedIn: 'root', }), __param(1, Inject(KnoraApiConfigToken)), __metadata("design:paramtypes", [HttpClient, KnoraApiConfig]) ], ApiService); return ApiService; }()); /** * @deprecated since v10.0.0 * * Will be replaced by `@knora/api` (github:knora-api-js-lib) * Requests ontology information from Knora. */ var OntologyService = /** @class */ (function (_super) { __extends(OntologyService, _super); function OntologyService() { return _super !== null && _super.apply(this, arguments) || this; } // ------------------------------------------------------------------------ // GET list of ontologies // ------------------------------------------------------------------------ /** * @deprecated: Use **getAllOntologies()** instead * * Requests the metadata about all existing ontologies from Knora's ontologies route. * * @returns Observable<ApiServiceResult> - the metadata of all ontologies. */ OntologyService.prototype.getOntologiesMetadata = function () { return this.httpGet('/v2/ontologies/metadata'); }; /** * Requests the metadata about all existing ontologies from Knora's ontologies route. * * @returns Observable<ApiServiceResult> - the metadata of all ontologies. */ OntologyService.prototype.getAllOntologies = function () { return this.httpGet('/v2/ontologies/metadata'); }; /** * Requests the ontologies of a specific project * * @param projectIri * @returns Observable<ApiServiceResult> - the metadata of project ontologies. */ OntologyService.prototype.getProjectOntologies = function (projectIri) { return this.httpGet('/v2/ontologies/metadata/' + encodeURIComponent(projectIri)); }; // ------------------------------------------------------------------------ // GET ontology // ------------------------------------------------------------------------ /** * Requests all entity definitions for the given ontologies from Knora's ontologies route. * * @param {string} ontologyIri the Iris of the named graphs whose resource classes are to be returned. * @returns Observable<ApiServiceResult> - the requested ontology. */ OntologyService.prototype.getAllEntityDefinitionsForOntologies = function (ontologyIri) { return this.httpGet('/v2/ontologies/allentities/' + encodeURIComponent(ontologyIri)); }; /** * Requests information about the given resource classes from Knora's ontologies route. * * @param {string[]} resourceClassIris the Iris of the resource classes to be queried. * @returns Observable<ApiServiceResult> - the requested resource class definitions. */ OntologyService.prototype.getResourceClasses = function (resourceClassIris) { if (resourceClassIris.length === 0) { // no resource class Iris are given to query for, return a failed Observer return Observable.create(function (observer) { return observer.error('No resource class Iris given for call of OntologyService.getResourceClasses'); }); } var resClassUriEnc = ''; resourceClassIris.forEach(function (resClassIri) { resClassUriEnc = resClassUriEnc + '/' + encodeURIComponent(resClassIri.toString()); }); return this.httpGet('/v2/ontologies/classes' + resClassUriEnc); }; /** * Requests properties from Knora's ontologies route. * * @param {string[]} propertyIris the Iris of the properties to be queried. * @returns Observable<ApiServiceResult> - the requested properties. */ OntologyService.prototype.getProperties = function (propertyIris) { if (propertyIris.length === 0) { // no resource class Iris are given to query for, return a failed Observer return Observable.create(function (observer) { return observer.error('No property Iris given for call of OntologyService.getProperties'); }); } var propertiesUriEnc = ''; propertyIris.forEach(function (resClassIri) { propertiesUriEnc = propertiesUriEnc + '/' + encodeURIComponent(resClassIri.toString()); }); return this.httpGet('/v2/ontologies/properties' + propertiesUriEnc); }; // ------------------------------------------------------------------------ // POST // ------------------------------------------------------------------------ /** * Create new ontology. * * @param {NewOntology} data Data contains: projectIri, name,