import { AfterViewInit , Component , OnDestroy , OnInit } from '@angular/core' ; import { CmdbType } from '../../framework/models/cmdb-type' ; import { ImporterConfig , ImporterFile , ImportResponse } from './import-object.models' ; import { ImportService } from '../import.service' ; import { Subscription } from 'rxjs' ; import { SidebarService } from '../../layout/services/sidebar.service' ; import { ProgressSpinnerService } from '../../layout/progress/progress-spinner.service' ; @ Component ( { selector : 'cmdb-import-objects' , templateUrl : './import-objects.component.html' , styleUrls : [ './import-objects.component.scss' ] } ) export class ImportObjectsComponent implements OnInit , AfterViewInit , OnDestroy { private fileReader : FileReader ; public typeInstance : CmdbType = undefined ; private importerSubscription : Subscription ; public importerFile : ImporterFile = { } as ImporterFile ; public importerConfig : ImporterConfig = { } as ImporterConfig ; public defaultImporterConfig : any ; private parseDataSubscription : Subscription ; public parserConfig : any = undefined ; public parsedData : any = undefined ; public mapping : [ ] = undefined ; public importResponse : ImportResponse = undefined ; public constructor ( private importService , private spinner , public sidebarService ) { this . fileReader = new FileReader ( ) ; this . importerSubscription = new Subscription ( ) ; this . parseDataSubscription = new Subscription ( ) ; } public ngOnInit ( ) { this . spinner . show ( ) ; this . fileReader . onload = ( ) => { this . importerFile . fileContent = this . fileReader . result ; } ; } public ngAfterViewInit ( ) { this . spinner . hide ( ) ; } public ngOnDestroy ( ) { if ( this . fileReader . readyState !== FileReader . DONE ) { this . fileReader . abort ( ) ; } this . importerSubscription . unsubscribe ( ) ; this . parseDataSubscription . unsubscribe ( ) ; } public formatChange ( format ) { this . importerFile . fileFormat = format ; const defaultImporterConfig = this . importService . getObjectImporterDefaultConfig ( this . importerFile . fileFormat ) . subscribe ( ( defaultConfig ) => { this . defaultImporterConfig = defaultConfig ; } , ( error ) => console . error ( error ) , ( ) => defaultImporterConfig . unsubscribe ( ) ) ; } public fileChange ( file ) { this . importerFile . file = file ; this . importerFile . fileName = file . name ; this . fileReader . readAsText ( file ) ; } public parserConfigChange ( config ) { this . parserConfig = config ; } public importConfigChange ( config ) { if ( config !== undefined ) { this . importerConfig = config as ImporterConfig ; if ( this . typeInstance !== undefined ) { this . importerConfig . type_id = this . typeInstance . public_id as number ; } } } public typeChange ( change ) { this . importerConfig . type_id = change . typeID as number ; this . typeInstance = change . typeInstance as CmdbType ; } public mappingChange ( change ) { this . mapping = change . filter ( value => Object . keys ( value ) . length !== 0 ) ; } public onParseData ( ) { this . spinner . show ( ) ; this . parseDataSubscription = this . importService . postObjectParser ( this . importerFile . file , this . importerFile . fileFormat , this . parserConfig ) . subscribe ( ( parsedData ) => { this . parsedData = parsedData ; } , ( e ) => console . error ( e ) , ( ) => { this . spinner . hide ( ) ; } ) ; } public startImport ( ) { this . spinner . show ( ) ; const runtimeConfig = this . importerConfig ; if ( this . defaultImporterConfig . manually_mapping ) { runtimeConfig . mapping = this . mapping ; } this . importService . importObjects ( this . importerFile . file , this . importerFile . fileFormat , this . parserConfig , runtimeConfig ) . subscribe ( ( importResponse ) => { this . importResponse = importResponse ; } , ( error ) => { console . error ( error ) ; this . spinner . hide ( ) ; } , ( ) => { this . spinner . hide ( ) ; this . sidebarService . updateTypeCounter ( this . typeInstance . public_id ) ; } ) ; } }