All files / lib/model/oasis/annotations AnnotationTarget.js

100% Statements 23/23
100% Branches 2/2
100% Functions 12/12
100% Lines 21/21

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    1x                                     666x       666x   666x 1599x     666x 1012x     666x 666x 54x     666x 14x                               46x     46x       43x                   8x                   26x       1x       26x   26x       1x  
"use strict";
 
const ExpressionBuilder = require("./ExpressionBuilder");
 
/**
 * Envelopes an annotation target:
 *
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_Target
 *
 * @class AnnotationTarget
 */
class AnnotationTarget {
  /**
   * Creates an instance of AnnotationTarget.
   *
   * @param {Object} rawMetadata raw metadata object for annotation target object
   * @param {Object} model reference to model which owns the annotation target
   *
   * @memberof AnnotationTarget
   */
  constructor(rawMetadata, model) {
    this.annotations = [];
 
    // almost all annotation target types must have uniquie name (in some scope)
    // there are 2 exceptions (Return Type, Annotation) that do not have name (in OASIS-CSDL)
    let name = rawMetadata.$.Name ? rawMetadata.$.Name : "";
 
    Object.defineProperty(this, "raw", {
      get: () => rawMetadata,
    });
 
    Object.defineProperty(this, "name", {
      get: () => name,
    });
 
    let extensions = [];
    Object.defineProperty(this, "extensions", {
      get: () => extensions,
    });
 
    Object.defineProperty(this, "model", {
      get: () => model,
    });
  }
 
  /**
   * Applies external target annoations.
   *
   * see https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/9fb2fa3c-5aac-4430-87c6-6786314b1588
   * see http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_AnnotationswithExternalTargeting
   *
   * @param {Object[]} [annotations] Annotations (container) elements
   */
  applyAnnotations(annotations) {
    // 'Annotations' element may have a qualifier, but there is no more addition info for it and sap doesn't use it, so it is ignored
    // "'Annotations' element MUST contain at least one edm:Annotation element." but ODATA.publish=true services do not comply with this
 
    let content = [].concat
      .apply(
        [],
        annotations.map((a) => a.Annotation)
      )
      .filter(Boolean)
      .map(ExpressionBuilder.buildAnnotation);
    this.annotations.push.apply(this.annotations, content);
  }
 
  /**
   * Checks if annotations contains specific term.
   *
   * @param {string} term annotation term to search for.
   * @returns {boolean} true if term is contained in annotations.
   */
  hasTerm(term) {
    return !!this.annotations.find((a) => a.term === term);
  }
 
  /**
   * Gets legacy api object. (XML casing, maybe some other changes.)
   *
   * @returns {Object} legacy api object
   * @memberof AnnotationTarget
   */
  getLegacyApiObject() {
    let api = {
      Name: this.name,
      Annotations: {
        raw: this.raw,
        hasTerm: (term) => this.hasTerm(term),
      },
    };
 
    this.extensions.forEach((e) => e.extendLegacyApiObject(api));
 
    return api;
  }
}
 
module.exports = AnnotationTarget;