UNPKG

@dasch-swiss/dsp-js

Version:

JavaScript library that handles API requests to Knora

215 lines 9.28 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; 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 */ var ReadResource = /** @class */ (function (_super) { __extends(ReadResource, _super); function ReadResource() { var _this = _super !== null && _super.apply(this, arguments) || this; _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 = []; return _this; } /** * 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). */ ReadResource.prototype.getNumberOfProperties = function () { return Object.keys(this.properties).length; }; /** * Gets the number of values for a given resource property. * * @param property the IRI of the property. */ ReadResource.prototype.getNumberOfValues = function (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. */ ReadResource.prototype.getValueType = function (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. */ ReadResource.prototype.getLinkPropertyIriFromLinkValuePropertyIri = function (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 var 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 ".concat(linkValueProperty)); } } else { throw new Error("".concat(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. */ ReadResource.prototype.getValues = function (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. */ ReadResource.prototype.getValuesAsStringArray = function (property, defaultStr) { if (defaultStr === void 0) { defaultStr = "?"; } var vals = this.getValues(property); return vals.map(function (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. */ ReadResource.prototype.getValuesAs = function (property, valueType) { return this.getValues(property).map(function (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", String) ], ReadResource.prototype, "label", void 0); __decorate([ JsonProperty(Constants.AttachedToProject, IdConverter), __metadata("design:type", String) ], ReadResource.prototype, "attachedToProject", void 0); __decorate([ JsonProperty(Constants.AttachedToUser, IdConverter), __metadata("design:type", String) ], ReadResource.prototype, "attachedToUser", void 0); __decorate([ JsonProperty(Constants.HasPermissions, String), __metadata("design:type", String) ], ReadResource.prototype, "hasPermissions", void 0); __decorate([ JsonProperty(Constants.UserHasPermission, String), __metadata("design:type", String) ], ReadResource.prototype, "userHasPermission", void 0); __decorate([ JsonProperty(Constants.ArkUrl, UriConverter), __metadata("design:type", String) ], ReadResource.prototype, "arkUrl", void 0); __decorate([ JsonProperty(Constants.VersionArkUrl, UriConverter), __metadata("design:type", String) ], ReadResource.prototype, "versionArkUrl", void 0); __decorate([ JsonProperty(Constants.CreationDate, DateTimeStampConverter), __metadata("design:type", String) ], 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); return ReadResource; }(ReadWriteResource)); export { ReadResource }; //# sourceMappingURL=read-resource.js.map