All files / lib/model/nw/dataSource EntitySet.js

100% Statements 26/26
92.3% Branches 12/13
100% Functions 12/12
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122    1x     1x 1x                         3x 2x   2x 2x                                 2x   1x         1x           3x                                             60x   60x 60x 13x                       3x       3x 3x 3x 2x 2x       3x                   3x 3x 3x       1x  
"use strict";
 
const AnnotationTarget = require("../../oasis/annotations/AnnotationTarget");
 
function areNamesCorrelated(srcEntity, targetEntity) {
  let nameCore = /^(.*)Parameters$/.exec(srcEntity.name)[1];
  return nameCore && targetEntity && targetEntity.name === `${nameCore}Type`;
}
 
/**
 * Finds Determine if navigation property is values association by target 'sap:semantics' attribute.
 * Workd in analytical services.
 *
 * @param {EntityType} entityType parametric entity type
 * @param {CsdlSchema} schema schema for resolving references
 *
 * @returns {NavigationProperty} values association navigation property, if found
 */
function findValuesAssociationBySemantics(entityType, schema) {
  return entityType.navigationProperties.find((navigationProperty) => {
    let target = schema
      .resolveModelPath(navigationProperty.relationship)
      .ends.find((end) => end.type !== entityType);
    return target && target.type.sap.semantics === "aggregate";
  });
}
 
/**
 * Finds values association by name.
 *
 * If parametrized entity is used in transactional OData service, then the results entity type
 * doesn't have sap:semantics attribute but the association is named 'Set' and the results type
 * is named 'xxxType' for 'xxxParameters' type.
 *
 * @param {EntityType} entityType parametric entity type
 * @param {CsdlSchema} schema schema for resolving references
 *
 * @returns {NavigationProperty} values association navigation property, if found
 */
function findValueAssociationByName(entityType, schema) {
  return entityType.navigationProperties.find(
    (navigationProperty) =>
      navigationProperty.name === "Set" &&
      areNamesCorrelated(
        entityType,
        schema
          .resolveModelPath(navigationProperty.relationship)
          .ends.find((end) => end.type !== entityType).type
      )
  );
}
 
function findValueAssociation(entityType, schema) {
  return (
    findValuesAssociationBySemantics(entityType, schema) ||
    findValueAssociationByName(entityType, schema)
  );
}
 
/**
 * Envelops an entity set.
 *
 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/4a09a48c-1da3-4d84-87b4-2b6c46731470
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_EntitySet
 *
 * @class EntitySet
 * @extends {AnnotationTarget}
 */
class EntitySet extends AnnotationTarget {
  /**
   * Creates an instance of AssociationEnd.
   * @param {Object} rawMetadata raw metadata object for the association end
   * @param {CsdlSchema} schema to resolve association reference
   * @memberof EntitySet
   */
  constructor(rawMetadata, schema) {
    super(rawMetadata);
 
    let entityType = schema.resolveModelPath(rawMetadata.$.EntityType);
    Object.defineProperty(this, "entityType", {
      get: () => entityType,
    });
  }
 
  /**
   * Gets info on parameterization of the entity set
   *
   * @param {CsdlSchema} schema to resolve association reference
   * @returns {Object} info with {Bool} isParameterized and {NavigationProperty} valuesAssociation, if isParameterized is true
   * @memberof EntitySet
   */
  getParameterizationInfo(schema) {
    var info = {
      isParameterized: false,
    };
 
    Eif (this.entityType.sap.semantics === "parameters") {
      let valuesAssociation = findValueAssociation(this.entityType, schema);
      if (valuesAssociation) {
        info.isParameterized = true;
        info.valuesAssociation = valuesAssociation;
      }
    }
 
    return info;
  }
 
  /**
   * Gets legacy api object. (XML casing, maybe some other changes.)
   *
   * @returns {Object} legacy api object
   * @memberof EntityType
   */
  getLegacyApiObject() {
    let api = super.getLegacyApiObject();
    api.EntityType = this.entityType.name;
    return api;
  }
}
 
module.exports = EntitySet;