X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Ffactories%2Fbpmn-factory.service.ts;fp=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Ffactories%2Fbpmn-factory.service.ts;h=1a928544b50ae6dce1445191040ae136e89afd20;hp=0000000000000000000000000000000000000000;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b diff --git a/otf-frontend/client/src/app/shared/factories/bpmn-factory.service.ts b/otf-frontend/client/src/app/shared/factories/bpmn-factory.service.ts new file mode 100644 index 0000000..1a92854 --- /dev/null +++ b/otf-frontend/client/src/app/shared/factories/bpmn-factory.service.ts @@ -0,0 +1,132 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + +import { Injectable } from '@angular/core'; +import { FileTransferService } from '../services/file-transfer.service'; +import { TestDefinitionService } from '../services/test-definition.service'; +import { Observable } from 'rxjs'; +import { Buffer } from 'buffer'; +import { BpmnOptions, Bpmn } from '../models/bpmn.model'; + +interface BpmnFactoryOptions extends BpmnOptions { + fileId?: String, + testDefinitionId?: String, + version?: String, + xml?: String +} + +@Injectable({ + providedIn: 'root' +}) +export class BpmnFactoryService { + + constructor( + private filesTransfer: FileTransferService, + private testDefinition: TestDefinitionService + ) { } + + public async setup(options: BpmnFactoryOptions): Promise { + return new Promise(async (resolve, reject) => { + //check for required options + if (!options.mode) { + console.error('Bpmn options require: mode'); + reject('Bpmn options require: mode') + } + + let xml = await this.getXml(options); + + let instance = new Bpmn(xml, { + mode: options.mode, + options: options.options + }) + + resolve(instance); + }); + + } + + public async getXml(options): Promise { + return new Promise(async (resolve, reject) => { + let xml; + + //handle the way to retrieve bpmn xml + if (options.xml) { + xml = options.xml + } else if (options.fileId) { + xml = await this.loadFile(options.fileId); + } else if (options.testDefinitionId && options.version) { + let fileId = await this.getFileId(options.testDefinitionId, options.version); + xml = await this.loadFile(fileId); + } else if (options.testDefinitionId) { + let fileId = await this.getFileId(options.testDefinitionId); + xml = await this.loadFile(fileId); + } else { + console.warn('Either xml, fileId, testDefinitionId and version, or testDefinitionId is required to render the bpmn'); + } + + resolve(xml); + + }); + + } + + private getFileId(id, version?): Observable { + return new Observable(observer => { + this.testDefinition.get(id).subscribe( + data => { + if (data['bpmnInstances']) { + if (version) { + let index; + for (let i = 0; i < data['bpmnInstances'].length; i++) { + if (version == data['bpmnInstances'][i].version) { + index = i; + break; + } + } + if (index) { + observer.next(data['bpmnInstances'][index].bpmnFileId); + } else { + observer.error('No bpmn file'); + } + + } else { + if (data['bpmnInstances'][data['bpmnInstances'].length - 1].bpmnFileId) { + observer.next(data['bpmnInstances'][data['bpmnInstances'].length - 1].bpmnFileId); + } else { + observer.error('No bpmn file'); + } + } + } else { + observer.error('No bpmn instances'); + } + }, + err => { + observer.error('No test definition found'); + } + ) + }) + } + + public loadFile(bpmnFileId) { + return new Promise((resolve, reject) => { + this.filesTransfer.get(bpmnFileId).subscribe(content => { + resolve(new Buffer(content as Buffer).toString()); + }, err => { + reject(err); + }); + }); + } +}