import { IParserLine } from "../models/IParserLine"; import { observable } from "rxjs/symbol/observable"; import { Observable } from "rxjs"; import { IExamImportParser } from "../models/IExamImportParser"; import { IParserResult } from "../models/IParserResult"; import { mySqlConnection } from "@rolesvillesoftware/tiberhealthmodel/repositories/mySqlConnection"; export type importerFileType = "ExamResultSummary"; export type examVendor = "ExamSoft" export class Importer, P extends IExamImportParser> { private result: R; private parser: P; constructor( dbConnection: mySqlConnection, private resultClass: new () => TResult, private parserFactory?: new () => P, concreteFactory?: new (dbConnection: mySqlConnection) => R, result?: R, parser?: P) { this.parser = parser || new parserFactory(); this.result = result || new concreteFactory(dbConnection); } /** * Parse the results of the import file * * @param observable Observable to watch for data stream */ private parseResults(observable: Observable): Promise> { this.result.initializeImport(); return new Promise>( (response, reject) => { observable .subscribe( data => { this.result.parse(data); }, error => { reject(error); }, () => { this.result.onImportComplete() .then(data => { this.result.dispose(); if (data != null) { response(data); } else { reject("No data parsed"); } }) .catch(error => { this.result.onImportFailed(error) .then(_ => { reject(error); }) .catch(_ => { reject(error); }) }); }); } ) } /** * Imports an exam exported file * * @param filename The path and file name of the CS */ importFile(filename: string): Promise> { return this.parseResults(this.parser.open(filename)); } /** * Parses a file based on the string contents * * @param fileContents Contents of the file to parse (string) */ importContents(fileContents: string): Promise> { return this.parseResults(this.parser.fromString(fileContents)); } }