@dasch-swiss/dsp-js
Version:
TypeScript client library for DSP-API
189 lines • 7.13 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { JsonObject, JsonProperty } from 'json2typescript';
import { Constants } from '../../Constants';
import { DateTimeStampConverter } from '../../custom-converters/date-time-stamp-converter';
import { IdConverter } from '../../custom-converters/id-converter';
import { UriConverter } from '../../custom-converters/uri-converter';
import { ResourcePropertyDefinition } from '../../ontologies/resource-property-definition';
import { ReadWriteResource } from '../read-write-resource';
import { TypeGuard } from '../type-guard';
/**
* @category Model V2
*/
let ReadResource = class ReadResource extends ReadWriteResource {
constructor() {
super(...arguments);
this.label = '';
this.attachedToProject = '';
this.attachedToUser = '';
this.hasPermissions = '';
this.userHasPermission = '';
this.arkUrl = '';
this.versionArkUrl = '';
this.creationDate = '';
this.lastModificationDate = undefined;
this.deleteDate = undefined;
this.deleteComment = '';
this.isDeleted = false;
this.properties = {};
this.incomingReferences = [];
this.outgoingReferences = [];
}
/**
* Gets number of resource properties for this resource.
* Each resource property is counted once,
* regardless of how many values it has (if number of values > 1).
*/
getNumberOfProperties() {
return Object.keys(this.properties).length;
}
/**
* Gets the number of values for a given resource property.
*
* @param property the IRI of the property.
*/
getNumberOfValues(property) {
if (this.properties.hasOwnProperty(property)) {
return this.properties[property].length;
}
else {
return 0;
}
}
/**
* Gets the value type of a given resource property,
* or `false` if cannot be determined.
*
* @param property the IRI of the property.
*/
getValueType(property) {
if (this.entityInfo.properties.hasOwnProperty(property) &&
this.entityInfo.properties[property].objectType !== undefined) {
return this.entityInfo.properties[property].objectType;
}
else {
return false;
}
}
/**
* Given the IRI of a property pointing to a link value,
* returns the corresponding link property IRI.
*
* @param linkValueProperty IRI of the link value property.
*/
getLinkPropertyIriFromLinkValuePropertyIri(linkValueProperty) {
if (this.entityInfo.properties[linkValueProperty] !== undefined &&
this.entityInfo.properties[linkValueProperty] instanceof ResourcePropertyDefinition &&
this.entityInfo.properties[linkValueProperty].isLinkValueProperty &&
linkValueProperty.endsWith('Value')) {
// remove "Value" from the end of the string and return the Iri
const linkPropIri = linkValueProperty.substring(0, linkValueProperty.length - 5);
if (this.entityInfo.properties[linkPropIri] !== undefined &&
this.entityInfo.properties[linkPropIri] instanceof ResourcePropertyDefinition &&
this.entityInfo.properties[linkPropIri].isLinkProperty) {
return linkPropIri;
}
else {
throw new Error(`Could not determine link property IRI for ${linkValueProperty}`);
}
}
else {
throw new Error(`${linkValueProperty} is not a valid link value property IRI`);
}
}
/**
* Gets all the values for a given resource property.
*
* @param property the IRI of the property.
*/
getValues(property) {
if (this.properties.hasOwnProperty(property)) {
return this.properties[property];
}
else {
return [];
}
}
/**
* Gets an array of string values representing the values of a property.
*
* @param property the IRI of the property.
* @param defaultStr placeholder if there is no string representation of a value.
*/
getValuesAsStringArray(property, defaultStr = '?') {
const vals = this.getValues(property);
return vals.map((val) => {
return val.strval === undefined ? defaultStr : val.strval;
});
}
/**
* Gets all the values for a given resource property as instances of the requested type.
* If the value cannot be casted to the requested type, an error is thrown.
*
* @param property the IRI of the property.
* @param valueType the requested type of the value.
*/
getValuesAs(property, valueType) {
return this.getValues(property).map(val => {
if (TypeGuard.typeGuard(val, valueType)) {
return val;
}
else {
throw new Error(`Cannot cast to type ${valueType}`);
}
});
}
};
__decorate([
JsonProperty(Constants.Label, String),
__metadata("design:type", Object)
], ReadResource.prototype, "label", void 0);
__decorate([
JsonProperty(Constants.AttachedToProject, IdConverter),
__metadata("design:type", Object)
], ReadResource.prototype, "attachedToProject", void 0);
__decorate([
JsonProperty(Constants.AttachedToUser, IdConverter),
__metadata("design:type", Object)
], ReadResource.prototype, "attachedToUser", void 0);
__decorate([
JsonProperty(Constants.HasPermissions, String),
__metadata("design:type", Object)
], ReadResource.prototype, "hasPermissions", void 0);
__decorate([
JsonProperty(Constants.UserHasPermission, String),
__metadata("design:type", Object)
], ReadResource.prototype, "userHasPermission", void 0);
__decorate([
JsonProperty(Constants.ArkUrl, UriConverter),
__metadata("design:type", Object)
], ReadResource.prototype, "arkUrl", void 0);
__decorate([
JsonProperty(Constants.VersionArkUrl, UriConverter),
__metadata("design:type", Object)
], ReadResource.prototype, "versionArkUrl", void 0);
__decorate([
JsonProperty(Constants.CreationDate, DateTimeStampConverter),
__metadata("design:type", Object)
], ReadResource.prototype, "creationDate", void 0);
__decorate([
JsonProperty(Constants.LastModificationDate, DateTimeStampConverter, true),
__metadata("design:type", String)
], ReadResource.prototype, "lastModificationDate", void 0);
__decorate([
JsonProperty(Constants.DeleteDate, DateTimeStampConverter, true),
__metadata("design:type", String)
], ReadResource.prototype, "deleteDate", void 0);
__decorate([
JsonProperty(Constants.DeleteComment, String, true),
__metadata("design:type", String)
], ReadResource.prototype, "deleteComment", void 0);
__decorate([
JsonProperty(Constants.IsDeleted, Boolean, true),
__metadata("design:type", Boolean)
], ReadResource.prototype, "isDeleted", void 0);
ReadResource = __decorate([
JsonObject('ReadResource')
], ReadResource);
export { ReadResource };
//# sourceMappingURL=read-resource.js.map