UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

420 lines 19.6 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityBase = void 0; /* eslint-disable @typescript-eslint/no-redundant-type-constituents */ const string_format_1 = __importDefault(require("string-format")); const ta_json_1 = require("ta-json"); const error_messages_1 = __importDefault(require("../../error-messages")); const argument_error_1 = require("../../errors/argument-error"); const not_loaded_error_1 = require("../../errors/not-loaded-error"); const schema_error_1 = require("../../errors/schema-error"); const guard_1 = __importDefault(require("../../guard")); const culture_load_option_1 = require("../querying/culture-load-option"); const entity_load_configuration_1 = require("../querying/entity-load-configuration"); const member_load_option_1 = require("../querying/member-load-option"); const entity_construction_args_1 = require("./entity-construction-args"); const member_definition_type_1 = require("./member-definition-type"); const property_1 = require("./property"); const property_manager_1 = require("./property-manager"); const relation_1 = require("./relation"); const relation_manager_1 = require("./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_1.default.notNullOrUndefined(client); guard_1.default.notNullOrUndefined(args); guard_1.default.arrayNoneNullOrInvariantCulture(args.cultures); guard_1.default.arrayNoneNullOrUndefined(args.properties); guard_1.default.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 property_manager_1.PropertyManager(client, args.properties, this); this._relationManager = new relation_manager_1.RelationManager(client, args.relations, this); } isProperty(member) { return member && member.definitionType === member_definition_type_1.MemberDefinitionType.Property; } getProperty(name) { guard_1.default.stringNotNullOrEmpty(name); const property = this._propertyManager.getProperty(name); if (property == null) { const errorMessage = (0, string_format_1.default)(error_messages_1.default.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName); throw new not_loaded_error_1.NotLoadedError(errorMessage); } return property; } getPropertyAsync(name_1) { return __awaiter(this, arguments, void 0, function* (name, loadOption = member_load_option_1.MemberLoadOption.LazyLoading, culture) { guard_1.default.stringNotNullOrEmpty(name); guard_1.default.notNullOrUndefined(loadOption); let property = this._propertyManager.getProperty(name); if (property == null && loadOption == member_load_option_1.MemberLoadOption.LazyLoading) { property = yield this._propertyManager.loadPropertyAsync(name, culture); if (property == null) { const errorMessage = (0, string_format_1.default)(error_messages_1.default.Entity.PropertyDoesNotExist, name, this.definitionName); throw new schema_error_1.SchemaError(errorMessage); } } if (property == null) { const errorMessage = (0, string_format_1.default)(error_messages_1.default.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName); throw new schema_error_1.SchemaError(errorMessage); } return property; }); } getPropertyValue(name, culture) { guard_1.default.stringNotNullOrEmpty(name); const property = this.getProperty(name); if (property == null) { const errorMessage = (0, string_format_1.default)(error_messages_1.default.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName); throw new not_loaded_error_1.NotLoadedError(errorMessage); } else if (property.isMultiLanguage && culture == null) { throw new argument_error_1.ArgumentError(error_messages_1.default.Entity.CultureRequired); } else if (culture != null && !property.isMultiLanguage) { throw new argument_error_1.ArgumentError(error_messages_1.default.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 = member_load_option_1.MemberLoadOption.LazyLoading, culture) { guard_1.default.stringNotNullOrEmpty(name); guard_1.default.notNullOrUndefined(loadOption); const property = yield this.getPropertyAsync(name, loadOption, culture); if (property == null) { const errorMessage = (0, string_format_1.default)(error_messages_1.default.Entity.PropertyDoesNotExist, name, this.definitionName); throw new schema_error_1.SchemaError(errorMessage); } else if (property.isMultiLanguage && culture == null) { throw new argument_error_1.ArgumentError(error_messages_1.default.Entity.CultureRequired); } else if (culture != null && !property.isMultiLanguage) { throw new argument_error_1.ArgumentError(error_messages_1.default.Entity.CultureNotSupported); } if (property.isMultiLanguage) { const cultureSensitiveProp = property; return cultureSensitiveProp.getValue(culture); } else { const cultureInsensitiveProp = property; return cultureInsensitiveProp.getValue(); } }); } setPropertyValue(name, value, culture) { guard_1.default.stringNotNullOrEmpty(name); const property = this._propertyManager.getProperty(name); if (property == null) { const errorMessage = (0, string_format_1.default)(error_messages_1.default.Entity.PropertyNotFound, name, this.id.toString(), this.definitionName); throw new not_loaded_error_1.NotLoadedError(errorMessage); } else if (property.isMultiLanguage && culture == null) { throw new argument_error_1.ArgumentError(error_messages_1.default.Entity.CultureRequired); } else if (culture != null && !property.isMultiLanguage) { throw new argument_error_1.ArgumentError(error_messages_1.default.Entity.CultureRequired); } if (property.isMultiLanguage) { property.setValue(culture, value); } else { property.setValue(value); } } getRelation(name, role, _returnNull = false) { guard_1.default.stringNotNullOrEmpty(name); const relation = this._relationManager.getRelation(name, role); if (relation == null) { if (_returnNull) { return null; } const errorMessage = (0, string_format_1.default)(error_messages_1.default.Entity.RelationNotFound, name, this.id.toString(), this.definitionName); throw new not_loaded_error_1.NotLoadedError(errorMessage); } else if (relation_1.RelationBase.isChildToManyParentsRelation(relation)) { return relation; } else if (relation_1.RelationBase.isChildToOneParentRelation(relation)) { return relation; } else if (relation_1.RelationBase.isParentToManyChildrenRelation(relation)) { return relation; } else if (relation_1.RelationBase.isParentToOneChildRelation(relation)) { return relation; } return null; } getRelationAsync(name, role, loadOption) { return __awaiter(this, void 0, void 0, function* () { guard_1.default.stringNotNullOrEmpty(name); const relation = this.getRelation(name, role, true); if (relation) { if ((relation_1.RelationBase.isChildToManyParentsRelation(relation) || relation_1.RelationBase.isParentToManyChildrenRelation(relation)) && !relation.isFullyLoaded) { return (yield this._relationManager.loadRelationAsync(name, role, true)); } } else { if (loadOption == member_load_option_1.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 = (0, string_format_1.default)(error_messages_1.default.Entity.RelationDoesNotExist, name, this.definitionName); throw new schema_error_1.SchemaError(errorMessage); } if (relation_1.RelationBase.isChildToManyParentsRelation(relation)) { return relation; } else if (relation_1.RelationBase.isChildToOneParentRelation(relation)) { return relation; } else if (relation_1.RelationBase.isParentToManyChildrenRelation(relation)) { return relation; } else if (relation_1.RelationBase.isParentToOneChildRelation(relation)) { return relation; } } } return relation; }); } loadPropertiesAsync(propertyLoadOption, culture) { return __awaiter(this, void 0, void 0, function* () { if (!this.canDoLazyLoading || propertyLoadOption == null) { return false; } return this._propertyManager.loadPropertiesAsync(propertyLoadOption, culture); }); } 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 culture_load_option_1.CultureLoadOption([...this.cultures]); const loadConfiguration = new entity_load_configuration_1.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; } }; exports.EntityBase = EntityBase; __decorate([ (0, ta_json_1.JsonProperty)("id"), __metadata("design:type", Number) ], EntityBase.prototype, "id", void 0); __decorate([ (0, ta_json_1.JsonProperty)("identifier"), __metadata("design:type", Object) ], EntityBase.prototype, "identifier", void 0); __decorate([ (0, ta_json_1.JsonProperty)("hasPublicLink"), __metadata("design:type", Boolean) ], EntityBase.prototype, "hasPublicLink", void 0); __decorate([ (0, ta_json_1.JsonProperty)("annotationCount"), __metadata("design:type", Number) ], EntityBase.prototype, "annotationCount", void 0); __decorate([ (0, ta_json_1.JsonProperty)("masterFileModifiedOn"), __metadata("design:type", Date) ], EntityBase.prototype, "masterFileModifiedOn", void 0); __decorate([ (0, ta_json_1.JsonProperty)("gatewayLinks"), __metadata("design:type", Object) ], EntityBase.prototype, "gatewayLinks", void 0); __decorate([ (0, ta_json_1.JsonProperty)("publicLink"), __metadata("design:type", String) ], EntityBase.prototype, "publicLink", void 0); __decorate([ (0, ta_json_1.JsonProperty)("publicCollectionLink"), __metadata("design:type", String) ], EntityBase.prototype, "publicCollectionLink", void 0); __decorate([ (0, ta_json_1.JsonProperty)("locked_by"), __metadata("design:type", Object) ], EntityBase.prototype, "lockedBy", void 0); __decorate([ (0, ta_json_1.JsonProperty)("locked_on"), __metadata("design:type", Object) ], EntityBase.prototype, "lockedOn", void 0); __decorate([ (0, ta_json_1.JsonProperty)("is_root_taxonomy_item"), __metadata("design:type", Boolean) ], EntityBase.prototype, "isRootTaxonomyItem", void 0); __decorate([ (0, ta_json_1.JsonProperty)("is_path_root"), __metadata("design:type", Boolean) ], EntityBase.prototype, "isPathRoot", void 0); __decorate([ (0, ta_json_1.JsonProperty)("inherits_security"), __metadata("design:type", Boolean) ], EntityBase.prototype, "inheritsSecurity", void 0); __decorate([ (0, ta_json_1.JsonProperty)("is_system_owned"), __metadata("design:type", Boolean) ], EntityBase.prototype, "isSystemOwned", void 0); __decorate([ (0, ta_json_1.JsonProperty)("version"), __metadata("design:type", Number) ], EntityBase.prototype, "version", void 0); __decorate([ (0, ta_json_1.JsonProperty)("cultures"), (0, ta_json_1.JsonElementType)(String), __metadata("design:type", Array) ], EntityBase.prototype, "cultures", void 0); __decorate([ (0, ta_json_1.JsonProperty)("is_current_user_default"), __metadata("design:type", Boolean) ], EntityBase.prototype, "isCurrentUserDefault", void 0); __decorate([ (0, ta_json_1.JsonProperty)("is_enabled"), __metadata("design:type", Boolean) ], EntityBase.prototype, "isEnabled", void 0); __decorate([ (0, ta_json_1.JsonProperty)("modules"), (0, ta_json_1.JsonElementType)(String), __metadata("design:type", Array) ], EntityBase.prototype, "modules", void 0); __decorate([ (0, ta_json_1.JsonProperty)("combinedPublishStatus"), __metadata("design:type", String) ], EntityBase.prototype, "combinedPublishStatus", void 0); __decorate([ (0, ta_json_1.JsonProperty)("combinedPublishStatusDetails"), __metadata("design:type", String) ], EntityBase.prototype, "combinedPublishStatusDetails", void 0); __decorate([ (0, ta_json_1.JsonProperty)("properties"), (0, ta_json_1.JsonElementType)(property_1.PropertyBase), __metadata("design:type", Array) ], EntityBase.prototype, "_properties", void 0); __decorate([ (0, ta_json_1.JsonProperty)("relations"), (0, ta_json_1.JsonElementType)(relation_1.RelationBase), __metadata("design:type", Array) ], EntityBase.prototype, "_relations", void 0); __decorate([ (0, ta_json_1.JsonProperty)("created_on"), __metadata("design:type", Date) ], EntityBase.prototype, "createdOn", void 0); __decorate([ (0, ta_json_1.JsonProperty)("created_by"), __metadata("design:type", Number) ], EntityBase.prototype, "createdBy", void 0); __decorate([ (0, ta_json_1.JsonProperty)("modified_on"), __metadata("design:type", Date) ], EntityBase.prototype, "modifiedOn", void 0); __decorate([ (0, ta_json_1.JsonProperty)("modified_by"), __metadata("design:type", Number) ], EntityBase.prototype, "modifiedBy", void 0); __decorate([ (0, ta_json_1.JsonProperty)("external_data_identifier"), __metadata("design:type", String) ], EntityBase.prototype, "externalDataIdentifier", void 0); exports.EntityBase = EntityBase = __decorate([ (0, ta_json_1.JsonObject)(), __metadata("design:paramtypes", [Object, entity_construction_args_1.EntityConstructionArgs]) ], EntityBase); //# sourceMappingURL=entity-base.js.map