/**
 * Copyright 2017 - 2018  The Hyve B.V.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */ import { Observable , of as observableOf } from 'rxjs' ; import { Injectable } from '@angular/core' ; import { Constraint } from '../../models/constraint-models/constraint' ; import { ExportJob } from '../../models/export-models/export-job' ; import { AppConfig } from '../../config/app.config' ; import { TransmartConstraintMapper } from '../../utilities/transmart-utilities/transmart-constraint-mapper' ; import { ExportDataType } from '../../models/export-models/export-data-type' ; import { ExportFileFormat } from '../../models/export-models/export-file-format' ; import { TransmartPackerJob } from '../../models/transmart-packer-models/transmart-packer-job' ; import { HttpHelper } from '../../utilities/http-helper' ; import { HttpClient } from '@angular/common/http' ; import { TransmartExportJob } from '../../models/transmart-models/transmart-export-job' ; import { TransmartPackerJobParameters } from '../../models/transmart-packer-models/transmart-packer-job-parameters' ; import { ConstraintHelper } from '../../utilities/constraint-utilities/constraint-helper' ; @ Injectable ( { providedIn : 'root' , } ) export class TransmartPackerHttpService { private _customExportJobName : string ; private _endpointUrl : string ; private httpHelper : HttpHelper ; constructor ( private appConfig , private httpClient ) { if ( this . appConfig . isLoaded ) { this . customExportJobName = this . appConfig . getConfig ( 'export-mode' ) [ 'data-view' ] ; this . endpointUrl = this . appConfig . getConfig ( 'export-mode' ) [ 'export-url' ] ; this . httpHelper = new HttpHelper ( this . endpointUrl , httpClient ) ; } } get customExportJobName ( ) : string { return this . _customExportJobName ; } set customExportJobName ( value ) { this . _customExportJobName = value ; } get endpointUrl ( ) : string { return this . _endpointUrl ; } set endpointUrl ( value ) { this . _endpointUrl = value ; } returns { Observable < any [ ] > } getAllJobs ( ) { const urlPart = 'jobs' ; const responseField = 'jobs' ; return this . httpHelper . getCall ( urlPart , responseField ) ; } param jobId returns { Observable < ExportJob > } getJobStatus ( jobId ) { const urlPart = `jobs/status/${ jobId }` ; const responseField = 'exportJob' ; return this . httpHelper . getCall ( urlPart , responseField ) ; } param { string } jobName param { Constraint } targetConstraint param { Constraint } rowFilterConstraint returns { Observable < ExportJob > } runJob ( jobName , targetConstraint , rowFilterConstraint ) { const urlPart = `jobs/create` ; const responseField = 'job' ; let packerJobParameters = new TransmartPackerJobParameters ( ) ; packerJobParameters . constraint = TransmartConstraintMapper . mapConstraint ( ConstraintHelper . ensurePatientLevelConstraint ( targetConstraint ) ) ; packerJobParameters . custom_name = jobName ; packerJobParameters . row_filter = TransmartConstraintMapper . mapConstraint ( rowFilterConstraint ) ; let packerJob = new TransmartPackerJob ( ) ; packerJob . job_type = this . customExportJobName ; packerJob . job_parameters = packerJobParameters ; return this . httpHelper . postCall ( urlPart , packerJob , responseField ) ; } param jobId returns { Observable < blob > } downloadJobData ( jobId ) { let url = `jobs/data/${ jobId }` ; return this . httpHelper . downloadData ( url ) ; } param jobId returns { Observable < blob > } cancelJob ( jobId ) { const urlPart = `jobs/cancel/${ jobId }` ; return this . httpHelper . getCall ( urlPart , null ) ; } param { string } name returns { Observable < TransmartExportJob > } createExportJob ( name ) { let newExportJob = new TransmartExportJob ( ) ; newExportJob . id = name ; newExportJob . jobName = name ; return observableOf ( newExportJob ) ; } returns { Observable < ExportJob > } getExportDataTypes ( ) { let dataTypes = [ ] ; let dataType = new ExportDataType ( 'clinical' , true ) ; dataType . fileFormats . push ( new ExportFileFormat ( 'TSV' , true ) ) ; dataTypes . push ( dataType ) ; return observableOf ( dataTypes ) ; } param { string } jobID returns { Observable < { } > } archiveJob ( jobID ) { return observableOf ( { } ) ; } }