/**
 * Copyright 2017 - 2019  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 { AsyncSubject , Observable , of as observableOf } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { Injectable } from '@angular/core' ; import { Study } from '../models/constraint-models/study' ; import { Constraint } from '../models/constraint-models/constraint' ; import { TrialVisit } from '../models/constraint-models/trial-visit' ; import { ExportJob } from '../models/export-models/export-job' ; import { Cohort } from '../models/cohort-models/cohort' ; import { SubjectSet } from '../models/constraint-models/subject-set' ; import { Pedigree } from '../models/constraint-models/pedigree' ; import { CohortRepresentation } from '../models/gb-backend-models/cohort-representation' ; import { DataTable } from '../models/table-models/data-table' ; import { TransmartMapper } from '../utilities/transmart-utilities/transmart-mapper' ; import { ExportDataType } from '../models/export-models/export-data-type' ; import { ConceptConstraint } from '../models/constraint-models/concept-constraint' ; import { Aggregate } from '../models/aggregate-models/aggregate' ; import { CrossTable } from '../models/table-models/cross-table' ; import { TransmartCrossTable } from '../models/transmart-models/transmart-cross-table' ; import { ConstraintHelper } from '../utilities/constraint-utilities/constraint-helper' ; import { CountItem } from '../models/aggregate-models/count-item' ; import { TransmartCrossTableMapper } from '../utilities/transmart-utilities/transmart-cross-table-mapper' ; import { TransmartCountItem } from '../models/transmart-models/transmart-count-item' ; import { TransmartTrialVisit } from '../models/transmart-models/transmart-trial-visit' ; import { CategoricalAggregate } from '../models/aggregate-models/categorical-aggregate' ; import { TransmartResourceService } from './transmart-resource.service' ; import { TransmartExportJob } from '../models/transmart-models/transmart-export-job' ; import { TransmartPatient } from '../models/transmart-models/transmart-patient' ; import { GbBackendHttpService } from './http/gb-backend-http.service' ; import { CohortMapper } from '../utilities/cohort-utilities/cohort-mapper' ; import { Dimension } from '../models/constraint-models/dimension' ; import { TransmartDimension } from '../models/transmart-models/transmart-dimension' ; import { ServerStatus } from '../models/server-status' ; @ Injectable ( { providedIn : 'root' , } ) export class ResourceService { constructor ( private transmartResourceService , private gbBackendHttpService ) { } init ( ) { this . transmartResourceService . init ( ) ; } get status ( ) : AsyncSubject < ServerStatus > { return this . transmartResourceService . status ; } returns { Observable < Study [ ] > } getStudies ( ) { return this . transmartResourceService . getStudies ( ) ; } param { string } root param { number } depth param { boolean } hasCounts param { boolean } hasTags returns { Observable < Object > } getTreeNodes ( root , depth , hasCounts , hasTags ) { return this . transmartResourceService . getTreeNodes ( root , depth , hasCounts , hasTags ) ; } updateCohortSelectionCounts ( constraint ) { return new Promise < any > ( ( resolve , reject ) => { this . transmartResourceService . updateCohortSelectionCounts ( constraint ) . then ( ( ) => { resolve ( TransmartMapper . mapTransmartCountItem ( this . transmartResourceService . counts ) ) ; } ) . catch ( err => { reject ( err ) ; } ) ; } ) ; } param { Constraint } constraint returns { Observable < Object > } getCountsPerStudyAndConcept ( constraint ) { return this . transmartResourceService . getCountsPerStudyAndConcept ( constraint ) . pipe ( map ( ( response ) => { return TransmartMapper . mapStudyConceptCountObject ( response ) ; } ) ) ; } param { Constraint } constraint returns { Observable < Map < string , CountItem > > } getCountsPerStudy ( constraint ) { return this . transmartResourceService . getCountsPerStudy ( constraint ) . pipe ( map ( ( response ) => { return TransmartMapper . mapStudyCountObject ( response ) ; } ) ) ; } param { Constraint } constraint returns { Observable < Map < string , CountItem > > } getCountsPerConcept ( constraint ) { return this . transmartResourceService . getCountsPerConcept ( constraint ) . pipe ( map ( ( response ) => { return TransmartMapper . mapConceptCountObject ( response ) ; } ) ) ; } param { Constraint } constraint returns { Observable < Object > } getCounts ( constraint ) { return this . transmartResourceService . getCounts ( constraint ) . pipe ( map ( ( tmCountItem ) => { return TransmartMapper . mapTransmartCountItem ( tmCountItem ) ; } ) ) ; } param { Constraint } constraint returns { Observable < object > } getAggregate ( constraint ) { return this . transmartResourceService . getAggregate ( constraint ) . pipe ( map ( ( tmConceptAggregate ) => { return TransmartMapper . mapTransmartConceptAggregate ( tmConceptAggregate , constraint . concept . code ) ; } ) ) ; } getCategoricalAggregate ( constraint ) { return this . transmartResourceService . getCategoricalAggregate ( constraint ) . pipe ( map ( ( tmConceptAggregate ) => { return TransmartMapper . mapTransmartCategoricalConceptAggregate ( tmConceptAggregate , constraint . concept . code ) ; } ) ) ; } param constraint returns { Observable < R | T > } getTrialVisits ( constraint ) { return this . transmartResourceService . getTrialVisits ( constraint ) . pipe ( map ( ( tmTrialVisits ) => { return TransmartMapper . mapTransmartTrialVisits ( tmTrialVisits ) ; } ) ) ; } returns { Observable < Object [ ] > } getPedigrees ( ) { return this . transmartResourceService . getPedigrees ( ) ; } getExportDataTypes ( constraint ) { return this . transmartResourceService . getExportDataTypes ( constraint ) ; } returns { Observable < ExportJob [ ] > } getExportJobs ( ) { return this . transmartResourceService . getExportJobs ( ) . pipe ( map ( ( tmExportJobs ) => { return TransmartMapper . mapTransmartExportJobs ( tmExportJobs ) ; } ) ) ; } param name returns { Observable < ExportJob > } createExportJob ( name ) { return this . transmartResourceService . createExportJob ( name ) . pipe ( map ( ( tmExportJob ) => { return TransmartMapper . mapTransmartExportJob ( tmExportJob ) ; } ) ) ; } param { ExportJob } job param { ExportDataType [ ] } dataTypes param { Constraint } subjectConstraint param { Constraint } variableConstraint param { DataTable } dataTable param { boolean } dateColumnsIncluded returns { Observable < ExportJob > } runExportJob ( job , dataTypes , subjectConstraint , variableConstraint , dataTable , dateColumnsIncluded ) { let hasSelectedFormat = false ; for ( let dataType of dataTypes ) { if ( dataType . checked ) { for ( let fileFormat of dataType . fileFormats ) { if ( fileFormat . checked ) { hasSelectedFormat = true ; } } } } if ( hasSelectedFormat ) { return this . transmartResourceService . runExportJob ( job . id , job . name , subjectConstraint , variableConstraint , dataTypes , dataTable , dateColumnsIncluded ) . pipe ( map ( ( tmExportJob ) => { return TransmartMapper . mapTransmartExportJob ( tmExportJob ) ; } ) ) ; } else { return observableOf ( null ) ; } } param jobId returns { Observable < blob > } downloadExportJob ( jobId ) { return this . transmartResourceService . downloadExportJob ( jobId ) ; } param jobId returns { Observable < blob > } cancelExportJob ( jobId ) { return this . transmartResourceService . cancelExportJob ( jobId ) ; } param jobId returns { Observable < blob > } archiveExportJob ( jobId ) { return this . transmartResourceService . archiveExportJob ( jobId ) ; } returns { Observable < CohortRepresentation [ ] > } getCohorts ( ) { return this . gbBackendHttpService . getQueries ( ) . pipe ( map ( ( gbBackendQueries ) => { return CohortMapper . deserialiseList ( gbBackendQueries ) ; } ) ) ; } param { Cohort } cohort returns { Observable < Cohort > } saveCohort ( cohort ) { let gbBackendQuery = CohortMapper . serialise ( cohort ) ; return this . gbBackendHttpService . saveQuery ( gbBackendQuery ) . pipe ( map ( ( savedGbBackendQuery ) => { return CohortMapper . deserialise ( savedGbBackendQuery ) ; } ) ) ; } param { string } id param { Object } body returns { Observable < { } > } editCohort ( id , body ) { return this . gbBackendHttpService . updateQuery ( id , body ) ; } param { string } id returns { Observable < any > } deleteCohort ( id ) { return this . gbBackendHttpService . deleteQuery ( id ) ; } diffCohort ( id ) { return this . gbBackendHttpService . diffQuery ( id ) ; } saveSubjectSet ( name , constraint ) { return this . transmartResourceService . savePatientSet ( name , constraint ) ; } getSubjects ( constraint ) { return this . transmartResourceService . getPatients ( constraint ) ; } getDataTable ( dataTable ) { return this . transmartResourceService . getDataTable ( dataTable ) ; } get sortableDimensions ( ) : Set < string > { return this . transmartResourceService . sortableDimensions ; } get subjectDimensions ( ) : Observable < Dimension [ ] > { return this . transmartResourceService . getSubjectDimensions ( ) . pipe ( map ( ( transmartDimensions ) => TransmartMapper . mapDimensions ( transmartDimensions ) ) ) ; } public getCrossTable ( crossTable ) { return this . transmartResourceService . getCrossTable ( crossTable . constraint , crossTable . rowHeaderConstraints . map ( constraints => ConstraintHelper . combineSubjectLevelConstraints ( constraints ) ) , crossTable . columnHeaderConstraints . map ( constraints => ConstraintHelper . combineSubjectLevelConstraints ( constraints ) ) ) . pipe ( map ( ( tmCrossTable ) => { return TransmartCrossTableMapper . mapTransmartCrossTable ( tmCrossTable , crossTable ) ; } ) ) ; } }