import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import { HttpClient } from '@angular/common/http' ; import { ConfigurationService } from '../configuration/configuration.service' ; import 'rxjs/add/operator/map' ; import { AnalyticsService } from '../analytics/analytics.service' ; import { LookupModel } from '../../models/lookup.model' ; import { ConfigModel } from '../../models/config.model' ; import { DatabaseType } from '../../models/database-type.enum' ; import { LookupApiResponseModel } from '../../models/lookup-api-response.model' ; import { LookupSource } from '../../models/lookup-source.enum' ; import 'rxjs/add/operator/timeout' ; @ Injectable ( ) export class DefinitionService { glossaryFileName = 'glossary.json' ; previousSearchTerm : string ; config : ConfigModel ; constructor ( private configurationService , private analyticsService , private http ) { this . populateConfiguration ( ) ; } /**
     * Populates the configuration
     */ populateConfiguration ( ) { this . configurationService . getConfiguration ( ) . subscribe ( ( config ) => { this . config = config ; } ) ; } param { string } searchTerm param { LookupSource } source returns { Observable < LookupModel [ ] > } lookupTerm ( searchTerm , source ) { console . log ( 'Acronym Decoder is looking up: ' + searchTerm ) ; if ( this . config . enableRemoteLookup ) { return this . lookupTermRemotely ( searchTerm , source ) ; } else { return this . lookupTermLocally ( searchTerm , source ) ; } } param { string } searchTerm param { LookupSource } source returns { Observable < LookupModel [ ] > } lookupTermLocally ( searchTerm , source ) { return Observable . create ( observer => { this . configurationService . getJsonFileContent ( this . glossaryFileName ) . subscribe ( glossary => { this . previousSearchTerm = searchTerm ; const definitions = glossary . filter ( termObj => termObj . acronym . toLowerCase ( ) === searchTerm . toLowerCase ( ) ) ; this . gaLookupEvent ( source , DatabaseType . local , searchTerm , definitions . length ) ; console . log ( 'Search results (locally): ' , definitions ) ; observer . next ( definitions ) ; } ) ; } ) ; } param { string } searchTerm param { LookupSource } source returns { Observable < LookupModel [ ] > } lookupTermRemotely ( searchTerm , source ) { const lookupUrl = this . config . lookupApiUrl + searchTerm ; return Observable . create ( observer => { this . http . get < LookupApiResponseModel > ( lookupUrl ) . timeout ( 3000 ) . subscribe ( ( res ) => { console . log ( 'Search results (remotely): ' , res ) ; ConfigurationService . isBackendOnline = true ; this . gaLookupEvent ( source , DatabaseType . server , searchTerm , res . data . length ) ; observer . next ( res . data ) ; } , error => { console . log ( 'Http Client error: ' , error ) ; ConfigurationService . isBackendOnline = false ; this . lookupTermLocally ( searchTerm , source ) . subscribe ( localRes => { observer . next ( localRes ) ; } ) ; } ) ; } ) ; } param { LookupSource } lookupSource param { DatabaseType } databaseType param { string } term param { number } numResults gaLookupEvent ( lookupSource , databaseType , term , numResults ) { if ( this . config . googleAnalyticsEnabled ) { this . analyticsService . getVisitor ( ) . event ( lookupSource , databaseType , term , numResults ) . send ( ) ; } } }