UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

205 lines 9.41 kB
"use strict"; 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.RelationManager = void 0; const base_types_1 = require("../../base-types"); 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 load_options_1 = require("../querying/load-options"); const property_load_option_1 = require("../querying/property-load-option"); const relation_load_option_1 = require("../querying/relation-load-option"); const relation_specification_1 = require("../querying/relation-specification"); const lazy_loading_manager_1 = require("./lazy-loading-manager"); const relation_1 = require("./relation"); const relation_container_1 = require("./relation-container"); class RelationManager { get count() { return this._count; } constructor(client, relations, entity) { guard_1.default.notNullOrUndefined(client); guard_1.default.notNullOrUndefined(relations); guard_1.default.notNullOrUndefined(entity); this._client = client; this._entity = entity; this._relations = new base_types_1.CaseInsensitiveStringMap(); for (const relation of relations) { this.addRelation(relation); } } /** * Gets the relation for the specified name. If role is null and the relation is self-referencing, it will throw. * Returns null if there are no loaded relations. * @param relationName - The name of the relation * @param role - The role of the relation * @returns A relation or null. */ getRelation(relationName, role) { guard_1.default.stringNotNullOrEmpty(relationName); const container = this._relations[relationName]; if (!container) { return null; } return container.getRelation(role); } /** * Gets a list of all loaded relations. */ getRelations() { const relations = []; for (const container of Object.values(this._relations)) { if (container.isSelfRelation) { Array.prototype.push.apply(relations, container.getRelations()); } else { relations.push(container.getRelation()); } } return relations; } /** * Lazy loads a relation if it is not loaded yet, adds it to the loaded relations and returns it. * If the relation does not exist, null will be returned. * @param relationName - The name of the relation * @param role - The role of the relation * @param forceLoad - Forces the relation to be loaded even though it has already been loaded before * @returns A promise resolving to the relation or null. */ loadRelationAsync(relationName_1, role_1) { return __awaiter(this, arguments, void 0, function* (relationName, role, forceLoad = false) { guard_1.default.stringNotNullOrEmpty(relationName); lazy_loading_manager_1.LazyLoadingManager.ensureLazyLoadingIsPossible(this._entity); let relation = this.getRelation(relationName, role); if (relation != null && !forceLoad) { return relation; } relation = yield this.fetchRelationAsync(relationName, role); if (relation != null) { this.addRelation(relation); } return relation; }); } /** * Loads the specified relations. * It will only load if it is actually missing relations and does not overwrite existing relations. * @param relationLoadOption - Option specifying which relations to load * @returns True when something was loaded. */ loadRelationsAsync(relationLoadOption) { return __awaiter(this, void 0, void 0, function* () { guard_1.default.notNullOrUndefined(relationLoadOption); lazy_loading_manager_1.LazyLoadingManager.ensureLazyLoadingIsPossible(this._entity); if (!this.hasMissingRelations(relationLoadOption)) { return false; } const loadConfig = new entity_load_configuration_1.EntityLoadConfiguration(culture_load_option_1.CultureLoadOption.None, property_load_option_1.PropertyLoadOption.None, relationLoadOption); const tempEntity = yield this._client.entities.getAsync(this._entity.id, loadConfig); this.importMissingRelations(tempEntity); return true; }); } /** * Checks if the entity is missing relations that are specified in the {@link IRelationLoadOption}. * @param relationLoadOption - Option specifying which relations to load * @returns Boolean indicating if something is missing. */ hasMissingRelations(relationLoadOption) { if (relationLoadOption == null || relationLoadOption.loadOption === load_options_1.LoadOption.None || relationLoadOption.relations == null || relationLoadOption.relations.length === 0) { return false; } if (relationLoadOption.loadOption === load_options_1.LoadOption.All) { // We don't have any info about the definition here. (Same logic as in the C# SDK) return true; } return relationLoadOption.relations.some(requestedRelation => !this.exists(requestedRelation.name, requestedRelation.role)); } /** * Checks if the specified relation is loaded. If role is null, any loaded role will return true. * @param relationName - The name of the relation * @param role - The role of the relation * @returns Boolean indicating if the specified relation exists. */ exists(relationName, role) { guard_1.default.stringNotNullOrEmpty(relationName); let exists = false; const container = this._relations[relationName]; if (container) { if (role == null) { exists = container.any(); } else { exists = container.relationExists(role); } } return exists; } /** * Imports missing relations from the entity. * @param tempEntity - An entity */ importMissingRelations(tempEntity) { guard_1.default.notNullOrUndefined(tempEntity); for (const relation of tempEntity.relations) { if (!this.exists(relation.name, relation.role)) { this.addRelation(relation); } } } /** * Fetches the relation from the server. * @param relationName - The name of the relation * @param role - The role of the relation */ fetchRelationAsync(relationName, role) { return __awaiter(this, void 0, void 0, function* () { const relationLoadOption = new relation_load_option_1.RelationLoadOption([new relation_specification_1.RelationSpecification(relationName, role)]); const loadConfig = new entity_load_configuration_1.EntityLoadConfiguration(new culture_load_option_1.CultureLoadOption([...this._entity.cultures]), property_load_option_1.PropertyLoadOption.None, relationLoadOption); const tempEntity = yield this._client.entities.getAsync(this._entity.id, loadConfig); if (tempEntity == null) { return null; } const relation = tempEntity.getRelation(relationName, role); return relation; }); } /** * Adds the relation to the loaded relations and updates the count (when the relation doesn't exist yet). * @param relation - Relation to add */ addRelation(relation) { let container = this._relations[relation.name]; if (!container) { container = new relation_container_1.RelationContainer(relation.name); this._relations[relation.name] = container; } if (container.relationExists(relation.role)) { const relationInRole = container.getRelation(relation.role); if ((relation_1.RelationBase.isChildToManyParentsRelation(relationInRole) || relation_1.RelationBase.isParentToManyChildrenRelation(relationInRole)) && !relationInRole.isFullyLoaded) { container.setRelation(relation); } return; } container.setRelation(relation); this._count++; } } exports.RelationManager = RelationManager; //# sourceMappingURL=relation-manager.js.map