import { ODataClient } from "./ODataClient" ; import { ICacheableQuery } from "./ICacheableQuery" ; /**
 * Expresses mapping relationship between underlying Field Names and Analytics Queryable name, where such a relation exists.
 * Does not currently cover any custom mappings on NavigationProperty and Key fields
 */ export interface FieldMapping { referenceName : string ; queryableName : string ; } export interface MetadataInformation { fieldMappings : FieldMapping [ ] ; } param referenceName param mapping export function mapReferenceNameForQuery ( referenceName , mapping ) { let match = mapping . fieldMappings . find ( o => o . referenceName === referenceName ) ; return match != null ? match . queryableName : referenceName ; } /**
 * Indicates if the specified name has a mapping available.
 */ export function hasMetadataMapping ( referenceName , mapping ) { return mapping . fieldMappings . find ( o => o . referenceName === referenceName ) != null ; } /** Encapsulates minimal metadata-Load/parsing logic.
 *  For more advanced/general OData scenarios - OLingo V4 library is intended to provide similar, broader metadata facilities.
 * */ export class MetadataQuery implements ICacheableQuery < MetadataInformation > { public static WorkItemSnapshot = "WorkItemSnapshot" ; private entity : string ; private projectId : string ; public constructor ( project , entity ) { this . entity = entity ; this . projectId = project ; } public getKey ( ) { return `MetadataQuery(${ this . entity })` ; } public runQuery ( ) { return ODataClient . getInstance ( ) . then ( ( client ) => { return client . runMetadataQuery ( this . projectId , `$metadata#${ this . entity }` ) . then ( ( results ) => { let entities = results . getElementsByTagName ( "EntityType" ) ; var mappings = [ ] ; for ( let i = 0 ; i < entities . length ; i ++ ) { let entity = entities . item ( i ) ; let entityName = entities . item ( i ) . getAttribute ( "Name" ) ; if ( entityName === this . entity ) { mappings = this . extractMappingsFromEntity ( entity ) ; } } return { fieldMappings : mappings } ; } ) ; } ) ; } private extractMappingsFromEntity ( entity ) { let entityProperties = entity . getElementsByTagName ( "Property" ) ; let fieldMappings = [ ] ; for ( let i = 0 ; i < entityProperties . length ; i ++ ) { let property = entityProperties . item ( i ) ; let fieldQueryingName = property . getAttribute ( "Name" ) ; let fieldreferenceName = fieldQueryingName ; let annotations = property . getElementsByTagName ( "Annotation" ) ; let fieldReferenceName = this . extractRefName ( annotations ) ; fieldMappings . push ( { queryableName : fieldQueryingName , referenceName : fieldReferenceName ? fieldReferenceName : fieldQueryingName } ) ; } return fieldMappings ; } private extractRefName ( annotations ) { let refName = "Ref.ReferenceName" ; for ( let i = 0 ; i < annotations . length ; i ++ ) { let annotation = annotations . item ( i ) ; let term = annotation . getAttribute ( "Term" ) ; let value = annotation . getAttribute ( "String" ) ; if ( term == "Ref.ReferenceName" && value ) { return value ; } } return null ; } }