/**
 * 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 { Component , OnInit , ElementRef } from '@angular/core' ; import { Cohort } from '../../../../models/cohort-models/cohort' ; import { CohortService } from '../../../../services/cohort.service' ; import { CohortDiffRecord } from '../../../../models/cohort-models/cohort-diff-record' ; import { DownloadHelper } from '../../../../utilities/download-helper' ; import { ConfirmationService } from 'primeng' ; import { UIHelper } from '../../../../utilities/ui-helper' ; import { MessageHelper } from '../../../../utilities/message-helper' ; import { FormatHelper } from '../../../../utilities/format-helper' ; import { FileImportHelper } from '../../../../utilities/file-import-helper' ; import { SubjectSetConstraint } from '../../../../models/constraint-models/subject-set-constraint' ; import { CountService } from '../../../../services/count.service' ; import { IconHelper } from '../../../../utilities/icon-helper' ; import { CohortMapper } from '../../../../utilities/cohort-utilities/cohort-mapper' ; import { CohortRepresentation } from '../../../../models/gb-backend-models/cohort-representation' ; @ Component ( { selector : 'gb-cohorts' , templateUrl : './gb-cohorts.component.html' , styleUrls : [ './gb-cohorts.component.css' ] } ) export class GbCohortsComponent implements OnInit { public readonly fileElementId : string = 'cohortFileUpload' ; searchTerm = '' ; isUploadListenerNotAdded : boolean ; file : File ; constructor ( private cohortService , private countService , private element , private confirmationService ) { this . isUploadListenerNotAdded = true ; } ngOnInit ( ) { } importCohort ( ) { let reader = new FileReader ( ) ; reader . onload = this . handleCohortImport . bind ( this ) ; FileImportHelper . importCriteria ( this . fileElementId , reader , this . isUploadListenerNotAdded ) ; this . isUploadListenerNotAdded = false ; } handleCohortImport ( e ) { MessageHelper . alert ( 'info' , 'File upload successful!' ) ; this . file = FileImportHelper . getFile ( this . fileElementId ) ; const data = e . target [ 'result' ] ; if ( FileImportHelper . isTextFile ( this . file ) ) { this . processSubjectIdsUpload ( data as string , this . file . name ) ; } else if ( FileImportHelper . isJsonFile ( this . file ) ) { let name = this . file . name . substr ( 0 , this . file . name . indexOf ( '.' ) ) ; this . processCohortUpload ( data , name ) ; } else { MessageHelper . alert ( 'error' , 'Invalid file for cohort import.' ) ; return ; } } param { string } fileContents param { string } name private processSubjectIdsUpload ( fileContents , name ) { let subjectIds = fileContents . split ( /[\r\n]+/ ) . map ( id => id . trim ( ) ) . filter ( id => id . length > 0 ) ; let cohort = new Cohort ( null , name ) ; let subjectSetConstraint = new SubjectSetConstraint ( ) ; subjectSetConstraint . subjectIds = subjectIds ; cohort . constraint = subjectSetConstraint ; this . cohortService . restoreCohort ( cohort ) ; } private processCohortUpload ( data , name ) { let cohortRepresentation = JSON . parse ( data ) as CohortRepresentation ; if ( cohortRepresentation && cohortRepresentation . queryConstraint ) { let cohort = CohortMapper . deserialise ( cohortRepresentation ) ; if ( cohort && ! cohort . name ) { cohort . name = name ; } this . cohortService . restoreCohort ( cohort ) ; } else { MessageHelper . alert ( 'error' , 'Invalid json format for cohort import.' ) ; } } getCohortTypeIcon ( cohortType ) { return IconHelper . getDimensionIcon ( cohortType ) ; } toggleSubscription ( event , target ) { event . stopPropagation ( ) ; this . cohortService . toggleCohortSubscription ( target ) ; } getSubscriptionButtonIcon ( target ) { return target . subscribed ? 'fa fa-rss-square' : 'fa fa-rss' ; } toggleBookmark ( event , target ) { event . stopPropagation ( ) ; this . cohortService . toggleCohortBookmark ( target ) ; } getBookmarkButtonIcon ( target ) { return target . bookmarked ? 'fa fa-star' : 'fa fa-star-o' ; } restoreCohort ( event , selected ) { event . stopPropagation ( ) ; this . cohortService . restoreCohort ( selected ) ; } toggleSubscriptionPanel ( target ) { target . subscriptionCollapsed = ! target . subscriptionCollapsed ; } toggleSubscriptionRecordPanel ( record ) { record . showCompleteRepresentation = ! record . showCompleteRepresentation ; } removeCohort ( event , target ) { event . stopPropagation ( ) ; this . cohortService . deleteCohort ( target ) ; } confirmRemoval ( event , target ) { event . stopPropagation ( ) ; this . confirmationService . confirm ( { message : 'Are you sure you want to remove the cohort?' , header : 'Delete Confirmation' , icon : 'fa fa-trash' , accept : ( ) => { this . removeCohort ( event , target ) ; } , reject : ( ) => { MessageHelper . alert ( 'error' , `Cannot remove the cohort ${ target . name }` ) ; } } ) ; } downloadCohort ( event , target ) { event . stopPropagation ( ) ; DownloadHelper . downloadJSON ( CohortMapper . serialise ( target ) , target . name ) ; } radioCheckSubscriptionFrequency ( event , target ) { event . stopPropagation ( ) ; event . preventDefault ( ) ; let obj = { subscriptionFreq : target . subscriptionFreq } ; this . cohortService . editCohort ( target , obj ) ; } downloadSubscriptionRecord ( target , record ) { const filename = target . name + '-record-' + record . createDate ; DownloadHelper . downloadJSON ( record . completeRepresentation , filename ) ; } onFiltering ( event ) { let filterWord = this . searchTerm . trim ( ) . toLowerCase ( ) ; for ( let c of this . cohortService . cohorts ) { if ( c . name . toLowerCase ( ) . indexOf ( filterWord ) === - 1 ) { c . visible = false ; } else { c . visible = true ; } } UIHelper . removePrimeNgLoaderIcon ( this . element , 500 ) ; } clearFilter ( ) { this . searchTerm = '' ; for ( let c of this . cohortService . cohorts ) { c . visible = true ; } UIHelper . removePrimeNgLoaderIcon ( this . element , 500 ) ; } get cohorts ( ) : Cohort [ ] { return this . cohortService . cohorts ; } sortByName ( ) { this . cohorts . sort ( ( q1 , q2 ) => { if ( q1 . name > q2 . name ) { return 1 ; } else if ( q1 [ 'name' ] < q2 [ 'name' ] ) { return - 1 ; } else { return 0 ; } } ) ; } sortBySubscription ( ) { this . cohorts . sort ( ( q1 , q2 ) => { if ( ! q1 . subscribed && q2 . subscribed ) { return 1 ; } else if ( q1 . subscribed && ! q2 . subscribed ) { return - 1 ; } else { return 0 ; } } ) ; } sortByDate ( ) { this . cohorts . sort ( ( q1 , q2 ) => { if ( q1 . updateDate > q2 . updateDate ) { return 1 ; } else if ( q1 . updateDate < q2 . updateDate ) { return - 1 ; } else { return 0 ; } } ) ; } sortByBookmark ( ) { this . cohorts . sort ( ( q1 , q2 ) => { if ( q1 . bookmarked && ! q2 . bookmarked ) { return - 1 ; } else if ( ! q1 . bookmarked && q2 . bookmarked ) { return 1 ; } else { return 0 ; } } ) ; } onCohortCheckClick ( e ) { e . stopPropagation ( ) ; this . cohortService . updateCountsWithAllCohorts ( ) ; } get isCohortSubscriptionIncluded ( ) : boolean { return this . cohortService . isCohortSubscriptionIncluded ; } get subjectCountText ( ) : string { const count = this . countService . allCohortsCount . subjectCount ; const countString = ( this . cohortService . isUpdatingCurrent || this . cohortService . isUpdatingAll ) ? '...' : FormatHelper . formatCountNumber ( count ) ; let noun = count === 1 ? 'subject' : 'subjects' ; return 'Total ' + countString + ' ' + noun + ' in selected cohort(s)' ; } }