@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
410 lines • 17.4 kB
JavaScript
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);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
import format from "string-format";
import { JsonElementType, JsonObject, JsonProperty } from "ta-json";
import ErrorMessages from "../../error-messages";
import { ArgumentError } from "../../errors/argument-error";
import { NotLoadedError } from "../../errors/not-loaded-error";
import { SchemaError } from "../../errors/schema-error";
import Guard from "../../guard";
import { CultureLoadOption } from "../querying/culture-load-option";
import { EntityLoadConfiguration } from "../querying/entity-load-configuration";
import { MemberLoadOption } from "../querying/member-load-option";
import { EntityConstructionArgs } from "./entity-construction-args";
import { MemberDefinitionType } from "./member-definition-type";
import { PropertyBase } from "./property";
import { PropertyManager } from "./property-manager";
import { RelationBase } from "./relation";
import { RelationManager } from "./relation-manager";
let EntityBase = class EntityBase {
get properties() {
return this._propertyManager.getProperties();
}
get relations() {
return this._relationManager.getRelations();
}
get canDoLazyLoading() {
return !this.isNew && !!this.id;
}
get isDirty() {
if (!this.isTracking) {
return false;
}
for (const dirtyTracker of this.getDirtyTrackingMembers()) {
if (dirtyTracker.isDirty) {
return true;
}
}
return false;
}
get isNew() {
return !this.id || this.id <= 0;
}
constructor(client, args) {
this._members = {};
Guard.notNullOrUndefined(client);
Guard.notNullOrUndefined(args);
Guard.arrayNoneNullOrInvariantCulture(args.cultures);
Guard.arrayNoneNullOrUndefined(args.properties);
Guard.arrayNoneNullOrUndefined(args.relations);
this._client = client;
this.definitionName = args.definitionName;
this.id = args.id || undefined;
this.identifier = args.identifier;
this.cultures = args.cultures;
this._propertyManager = new PropertyManager(client, args.properties, this);
this._relationManager = new RelationManager(client, args.relations, this);
}
isProperty(member) {
return member && member.definitionType === MemberDefinitionType.Property;
}
getProperty(name) {
Guard.stringNotNullOrEmpty(name);
const property = this._propertyManager.getProperty(name);
if (property == null) {
const errorMessage = format(ErrorMessages.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName);
throw new NotLoadedError(errorMessage);
}
return property;
}
getPropertyAsync(name_1) {
return __awaiter(this, arguments, void 0, function* (name, loadOption = MemberLoadOption.LazyLoading) {
Guard.stringNotNullOrEmpty(name);
Guard.notNullOrUndefined(loadOption);
let property = this._propertyManager.getProperty(name);
if (property == null && loadOption == MemberLoadOption.LazyLoading) {
property = yield this._propertyManager.loadPropertyAsync(name);
if (property == null) {
const errorMessage = format(ErrorMessages.Entity.PropertyDoesNotExist, name, this.definitionName);
throw new SchemaError(errorMessage);
}
}
if (property == null) {
const errorMessage = format(ErrorMessages.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName);
throw new SchemaError(errorMessage);
}
return property;
});
}
getPropertyValue(name, culture) {
Guard.stringNotNullOrEmpty(name);
const property = this.getProperty(name);
if (property == null) {
const errorMessage = format(ErrorMessages.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName);
throw new NotLoadedError(errorMessage);
}
else if (property.isMultiLanguage && culture == null) {
throw new ArgumentError(ErrorMessages.Entity.CultureRequired);
}
else if (culture != null && !property.isMultiLanguage) {
throw new ArgumentError(ErrorMessages.Entity.CultureNotSupported);
}
if (property.isMultiLanguage) {
const cultureSensitiveProp = property;
return cultureSensitiveProp.getValue(culture);
}
else {
const cultureInsensitiveProp = property;
return cultureInsensitiveProp.getValue();
}
}
getPropertyValueAsync(name_1) {
return __awaiter(this, arguments, void 0, function* (name, loadOption = MemberLoadOption.LazyLoading, culture) {
Guard.stringNotNullOrEmpty(name);
Guard.notNullOrUndefined(loadOption);
const property = yield this.getPropertyAsync(name, loadOption);
if (property == null) {
const errorMessage = format(ErrorMessages.Entity.PropertyDoesNotExist, name, this.definitionName);
throw new SchemaError(errorMessage);
}
else if (property.isMultiLanguage && culture == null) {
throw new ArgumentError(ErrorMessages.Entity.CultureRequired);
}
else if (culture != null && !property.isMultiLanguage) {
throw new ArgumentError(ErrorMessages.Entity.CultureNotSupported);
}
if (property.isMultiLanguage) {
const cultureSensitiveProp = property;
return cultureSensitiveProp.getValue(culture);
}
else {
const cultureInsensitiveProp = property;
return cultureInsensitiveProp.getValue();
}
});
}
setPropertyValue(name, value, culture) {
Guard.stringNotNullOrEmpty(name);
const property = this._propertyManager.getProperty(name);
if (property == null) {
const errorMessage = format(ErrorMessages.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName);
throw new NotLoadedError(errorMessage);
}
else if (property.isMultiLanguage && culture == null) {
throw new ArgumentError(ErrorMessages.Entity.CultureRequired);
}
else if (culture != null && !property.isMultiLanguage) {
throw new ArgumentError(ErrorMessages.Entity.CultureRequired);
}
if (property.isMultiLanguage) {
property.setValue(culture, value);
}
else {
property.setValue(value);
}
}
getRelation(name, role, _returnNull = false) {
Guard.stringNotNullOrEmpty(name);
const relation = this._relationManager.getRelation(name, role);
if (relation == null) {
if (_returnNull) {
return null;
}
const errorMessage = format(ErrorMessages.Entity.RelationNotFound, name, this.id.toString(), this.definitionName);
throw new NotLoadedError(errorMessage);
}
else if (RelationBase.isChildToManyParentsRelation(relation)) {
return relation;
}
else if (RelationBase.isChildToOneParentRelation(relation)) {
return relation;
}
else if (RelationBase.isParentToManyChildrenRelation(relation)) {
return relation;
}
else if (RelationBase.isParentToOneChildRelation(relation)) {
return relation;
}
return null;
}
getRelationAsync(name, role, loadOption) {
return __awaiter(this, void 0, void 0, function* () {
Guard.stringNotNullOrEmpty(name);
const relation = this.getRelation(name, role, true);
if (relation) {
if ((RelationBase.isChildToManyParentsRelation(relation) ||
RelationBase.isParentToManyChildrenRelation(relation)) &&
!relation.isFullyLoaded) {
return (yield this._relationManager.loadRelationAsync(name, role, true));
}
}
else {
if (loadOption == MemberLoadOption.LazyLoading) {
// Scoped to be able to redefine relation as type RelationType does not match IRelation. //TODO generics?
const relation = yield this._relationManager.loadRelationAsync(name, role);
if (relation == null) {
const errorMessage = format(ErrorMessages.Entity.RelationDoesNotExist, name, this.definitionName);
throw new SchemaError(errorMessage);
}
if (RelationBase.isChildToManyParentsRelation(relation)) {
return relation;
}
else if (RelationBase.isChildToOneParentRelation(relation)) {
return relation;
}
else if (RelationBase.isParentToManyChildrenRelation(relation)) {
return relation;
}
else if (RelationBase.isParentToOneChildRelation(relation)) {
return relation;
}
}
}
return relation;
});
}
loadPropertiesAsync(propertyLoadOption) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.canDoLazyLoading || propertyLoadOption == null) {
return false;
}
return this._propertyManager.loadPropertiesAsync(propertyLoadOption);
});
}
loadRelationsAsync(relationLoadOption) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.canDoLazyLoading || relationLoadOption == null) {
return false;
}
return this._relationManager.loadRelationsAsync(relationLoadOption);
});
}
loadMembersAsync(propertyLoadOption, relationLoadOption) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.canDoLazyLoading) {
return false;
}
const hasMissingProperties = this._propertyManager.hasMissingProperties(propertyLoadOption);
const hasMissingRelations = this._relationManager.hasMissingRelations(relationLoadOption);
if (!hasMissingProperties && !hasMissingRelations) {
return false;
}
const cultureLoadOption = new CultureLoadOption([...this.cultures]);
const loadConfiguration = new EntityLoadConfiguration(cultureLoadOption, propertyLoadOption, relationLoadOption);
const tempEntity = yield this._client.entities.getAsync(this.id, loadConfiguration);
// Import missing members
this._propertyManager.importMissingProperties(tempEntity);
this._relationManager.importMissingRelations(tempEntity);
return true;
});
}
getEntityDefinitionAsync() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this._client.entityDefinitions.getCachedAsync(this.definitionName));
});
}
startTracking() {
if (this.isTracking)
return;
this.isTracking = true;
for (const member of this.getDirtyTrackingMembers()) {
member.startTracking();
}
}
markClean() {
for (const member of this.getDirtyTrackingMembers()) {
member.markClean();
}
}
getDirtyTrackingMembers() {
const properties = this._propertyManager.getProperties();
const relations = this._relationManager.getRelations();
const list = [...properties, ...relations];
return list;
}
};
__decorate([
JsonProperty("id"),
__metadata("design:type", Number)
], EntityBase.prototype, "id", void 0);
__decorate([
JsonProperty("identifier"),
__metadata("design:type", Object)
], EntityBase.prototype, "identifier", void 0);
__decorate([
JsonProperty("hasPublicLink"),
__metadata("design:type", Boolean)
], EntityBase.prototype, "hasPublicLink", void 0);
__decorate([
JsonProperty("annotationCount"),
__metadata("design:type", Number)
], EntityBase.prototype, "annotationCount", void 0);
__decorate([
JsonProperty("masterFileModifiedOn"),
__metadata("design:type", Date)
], EntityBase.prototype, "masterFileModifiedOn", void 0);
__decorate([
JsonProperty("gatewayLinks"),
__metadata("design:type", Object)
], EntityBase.prototype, "gatewayLinks", void 0);
__decorate([
JsonProperty("publicLink"),
__metadata("design:type", String)
], EntityBase.prototype, "publicLink", void 0);
__decorate([
JsonProperty("publicCollectionLink"),
__metadata("design:type", String)
], EntityBase.prototype, "publicCollectionLink", void 0);
__decorate([
JsonProperty("locked_by"),
__metadata("design:type", Object)
], EntityBase.prototype, "lockedBy", void 0);
__decorate([
JsonProperty("locked_on"),
__metadata("design:type", Object)
], EntityBase.prototype, "lockedOn", void 0);
__decorate([
JsonProperty("is_root_taxonomy_item"),
__metadata("design:type", Boolean)
], EntityBase.prototype, "isRootTaxonomyItem", void 0);
__decorate([
JsonProperty("is_path_root"),
__metadata("design:type", Boolean)
], EntityBase.prototype, "isPathRoot", void 0);
__decorate([
JsonProperty("inherits_security"),
__metadata("design:type", Boolean)
], EntityBase.prototype, "inheritsSecurity", void 0);
__decorate([
JsonProperty("is_system_owned"),
__metadata("design:type", Boolean)
], EntityBase.prototype, "isSystemOwned", void 0);
__decorate([
JsonProperty("version"),
__metadata("design:type", Number)
], EntityBase.prototype, "version", void 0);
__decorate([
JsonProperty("cultures"),
JsonElementType(String),
__metadata("design:type", Array)
], EntityBase.prototype, "cultures", void 0);
__decorate([
JsonProperty("is_current_user_default"),
__metadata("design:type", Boolean)
], EntityBase.prototype, "isCurrentUserDefault", void 0);
__decorate([
JsonProperty("is_enabled"),
__metadata("design:type", Boolean)
], EntityBase.prototype, "isEnabled", void 0);
__decorate([
JsonProperty("modules"),
JsonElementType(String),
__metadata("design:type", Array)
], EntityBase.prototype, "modules", void 0);
__decorate([
JsonProperty("combinedPublishStatus"),
__metadata("design:type", String)
], EntityBase.prototype, "combinedPublishStatus", void 0);
__decorate([
JsonProperty("combinedPublishStatusDetails"),
__metadata("design:type", String)
], EntityBase.prototype, "combinedPublishStatusDetails", void 0);
__decorate([
JsonProperty("properties"),
JsonElementType(PropertyBase),
__metadata("design:type", Array)
], EntityBase.prototype, "_properties", void 0);
__decorate([
JsonProperty("relations"),
JsonElementType(RelationBase),
__metadata("design:type", Array)
], EntityBase.prototype, "_relations", void 0);
__decorate([
JsonProperty("created_on"),
__metadata("design:type", Date)
], EntityBase.prototype, "createdOn", void 0);
__decorate([
JsonProperty("created_by"),
__metadata("design:type", Number)
], EntityBase.prototype, "createdBy", void 0);
__decorate([
JsonProperty("modified_on"),
__metadata("design:type", Date)
], EntityBase.prototype, "modifiedOn", void 0);
__decorate([
JsonProperty("modified_by"),
__metadata("design:type", Number)
], EntityBase.prototype, "modifiedBy", void 0);
EntityBase = __decorate([
JsonObject(),
__metadata("design:paramtypes", [Object, EntityConstructionArgs])
], EntityBase);
export { EntityBase };
//# sourceMappingURL=entity-base.js.map