import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { BehaviorSubject , Observable , of } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; /**
 * Metadata service which converts and returns product metadata to json format
 */ @ Injectable ( ) export class MetadataService { error : any = null ; metadata$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ; constructor ( private httpClient ) { } param multigmpe fixMultiGmpeFormat ( multigmpe , weight = null ) { if ( typeof multigmpe === 'string' ) { multigmpe = { name : multigmpe , gmpes : [ ] } ; } multigmpe . weight = weight ; const newGmpes = [ ] ; let subGmpeWeight ; multigmpe . gmpes . forEach ( ( gmpe , index ) => { subGmpeWeight = multigmpe . weights [ index ] ; newGmpes . push ( this . fixMultiGmpeFormat ( gmpe , subGmpeWeight ) ) ; } ) ; multigmpe . gmpes = newGmpes ; return multigmpe ; } param product getMetadata ( product ) { if ( product === null || ! product . contents [ 'download/info.json' ] ) { this . onMetadata ( null ) ; return ; } const metadata = product . contents [ 'download/info.json' ] ; this . httpClient . get ( metadata . url ) . pipe ( catchError ( this . handleError ( ) ) ) . subscribe ( ( data ) => { try { this . onMetadata ( data ) ; } catch ( e ) { this . error = e ; this . metadata$ . next ( null ) ; } } ) ; } param obj returns obj2Arr ( obj ) { const arr = [ ] ; for ( const itemId of Object . keys ( obj ) ) { const item = obj [ itemId ] ; item . type = itemId ; arr . push ( item ) ; } return arr ; } param metadata returns onMetadata ( metadata ) { metadata = this . translate ( metadata ) ; this . metadata$ . next ( metadata ) ; } param metadata returns translate ( metadata ) { const needsTrans = { output : [ 'ground_motions' , 'map_information' ] , processing : [ 'ground_motion_modules' , 'roi' ] } ; for ( const dataType of Object . keys ( needsTrans ) ) { for ( const each of needsTrans [ dataType ] ) { if ( metadata && metadata [ dataType ] && metadata [ dataType ] [ each ] && ! ( metadata [ dataType ] [ each ] instanceof Array ) ) { metadata [ dataType ] [ each ] = this . obj2Arr ( metadata [ dataType ] [ each ] ) ; } } } const modifiedGmpes = { } ; if ( metadata && metadata . multigmpe ) { Object . keys ( metadata . multigmpe ) . forEach ( key => { const fixedGmpes = this . fixMultiGmpeFormat ( metadata . multigmpe [ key ] ) ; modifiedGmpes [ key ] = fixedGmpes ; } ) ; metadata . multigmpe . modified = modifiedGmpes ; } return metadata ; } returns { any } private handleError ( ) { return ( error ) : Observable < any > => { this . error = error ; return of ( null ) ; } ; } }