Name

sn_entitlement.UnallocatedEntityCalculation_ContextFactory

Description

No description available

Script

var UnallocatedEntityCalculation_ContextFactory = Class.create();
UnallocatedEntityCalculation_ContextFactory.prototype = {
  initialize: function(oobRolesOnly) {
      this._oobRolesOnly = oobRolesOnly;

      this._logSourceName = `sn_entitlement.${this.type}`;

      this._v2SchemaIsInUseResult = null;
  },

  /**
   * Creates a group context object based on the current state of the schema
   *
   * @param {guid} groupId The sys_user_group.sys_id to use for the context
   * @return {GroupContext} The correct group context object for the schema
   */
  createGroupContext: function(groupId, subscriptionContext = null) {
      subscriptionContext = subscriptionContext ?
          subscriptionContext :
          this.createSubscriptionContext();

      const groupHasRoleInMemoryCache = this._getGroupHasRoleInMemoryCache();

      return this.v2SchemaIsInUse() ?
          new sn_entitlement.UnallocatedEntityCalculation_GroupContextV2(groupId, this._oobRolesOnly, subscriptionContext) :
          new sn_entitlement.UnallocatedEntityCalculation_GroupContextV1(groupId, this._oobRolesOnly, subscriptionContext, groupHasRoleInMemoryCache);
  },

  /**
   * Creates a user context object based on the current state of the schema
   *
   * @param {guid} userId The sys_user.sys_id to use for the context
   * @return {UserContext} The correct user context object for the schema
   */
  createUserContext: function(userId, subscriptionContext = null) {
      subscriptionContext = subscriptionContext ?
          subscriptionContext :
          this.createSubscriptionContext();

      const userHasRoleInMemoryCache = this._getUserHasRoleInMemoryCache();
      const userHasLicenseInMemoryCache = this._getUserHasLicenseInMemoryCache();

      return this.v2SchemaIsInUse() ?
          new sn_entitlement.UnallocatedEntityCalculation_UserContextV2(userId, this._oobRolesOnly, subscriptionContext) :
          new sn_entitlement.UnallocatedEntityCalculation_UserContextV1(userId, this._oobRolesOnly, subscriptionContext, userHasRoleInMemoryCache, userHasLicenseInMemoryCache);
  },

  /**
   * Creates a subscription context object based on the current state of the schema
   *
   * @return {SubscriptionContext} The correct subscription context object for the schema
   */
  createSubscriptionContext: function() {
      return this.v2SchemaIsInUse() ?
          new sn_entitlement.UnallocatedEntityCalculation_SubscriptionsContextV2(this._oobRolesOnly) :
          new sn_entitlement.UnallocatedEntityCalculation_SubscriptionsContextV1(this._oobRolesOnly);
  },

  /**
   * Determines if the V2 schema is in use
   *
   * @return {bool} Returns true if the V2 schema is being used
   */
  v2SchemaIsInUse: function() {
      // Store the result a prevent re-calculating this for the lifetime of this object.
      if (this._v2SchemaIsInUseResult === null)
          this._v2SchemaIsInUseResult = this._v2SchemaIsInUseDetectViaFlags();
      return this._v2SchemaIsInUseResult;
  },

  /**
   * Determines if the v2 schema is in use based on glide properties
   *
   * @returns {bool} True if the flags are set to enable the V2 schema
   */
  _v2SchemaIsInUseDetectViaFlags: function() {
      // Default to V1 behavior
      const surfRouting = gs.getProperty('glide.entitlement.surf.routing', true).toLowerCase() === 'true';
      const emsDataAvailable = gs.getProperty('glide.entitlement.ems.data.available', false).toLowerCase() === 'true';
      const isV2SchemaInUse = !surfRouting && emsDataAvailable;
      gs.info(`${this._logSourceName}: Detected schema ${isV2SchemaInUse ? 'V2' : 'V1'} by checking glide.entitlement.surf.routing and glide.entitlement.ems.data.available`);
      return isV2SchemaInUse;
  },

  /**
   * Creates or retrieves an existing UserHasRoleInMemoryCache object
   */
  _getUserHasRoleInMemoryCache: function() {
      if (!this._userHasRoleInMemoryCache) {
          const meteredRoleIds = this._getMeteredRoleIds();
          this._userHasRoleInMemoryCache = new sn_entitlement.UserHasRoleInMemoryCache(meteredRoleIds);
      }
      return this._userHasRoleInMemoryCache;
  },

  /**
   * Creates or retrieves an existing GroupHasRoleInMemoryCache object
   */
  _getGroupHasRoleInMemoryCache: function() {
      if (!this._groupHasRoleInMemoryCache) {
          const meteredRoleIds = this._getMeteredRoleIds();
          this._groupHasRoleInMemoryCache = new sn_entitlement.GroupHasRoleInMemoryCache(meteredRoleIds);
      }
      return this._groupHasRoleInMemoryCache;
  },

  /**
   * Creates or retrieves an existing UserHasLicenseInMemoryCache object
   */
  _getUserHasLicenseInMemoryCache: function() {
      if (!this._userHasLicenseInMemoryCache)
          this._userHasLicenseInMemoryCache = new sn_entitlement.UserHasLicenseInMemoryCache();
      return this._userHasLicenseInMemoryCache;
  },

  /**
   * Retrieves an array of metered role ids
   */
  _getMeteredRoleIds: function() {
      return this.createSubscriptionContext().getRoleIdsByRequiresASubscription();
  },

  type: 'UnallocatedEntityCalculation_ContextFactory'
};

Sys ID

6ea4f504ff312110468365d7d3b8fef6

Offical Documentation

Official Docs: