2 * Copyright (c) 2017 ZTE Corporation.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * and the Apache License 2.0 which both accompany this distribution,
\r
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
\r
7 * and http://www.apache.org/licenses/LICENSE-2.0
\r
10 * ZTE - initial API and implementation and/or initial documentation
\r
13 import { Injectable } from '@angular/core';
\r
14 import { WorkflowNode } from "../model/workflow/workflow-node";
\r
15 import { DataAccessService } from "./data-access/data-access.service";
\r
16 import { Observable } from "rxjs/Observable";
\r
17 import { Workflow } from "../model/workflow/workflow";
\r
18 import { Position } from "../model/workflow/position";
\r
19 import { NodeType } from "../model/workflow/node-type.enum";
\r
20 import { StartEvent } from "../model/workflow/start-event";
\r
21 import { SequenceFlow } from "../model/workflow/sequence-flow";
\r
22 import { RestTask } from "../model/workflow/rest-task";
\r
26 * provides all of the operations about workflow operations.
\r
29 export class WorkflowService {
\r
31 public workflow: Workflow;
\r
33 constructor(private dataAccessService: DataAccessService) {
\r
37 public save(): Observable<boolean> {
\r
38 console.log(this.workflow);
\r
39 return this.dataAccessService.catalogService.saveWorkflow(this.workflow);
\r
42 public addNode(name: string, type: string, top: number, left: number): WorkflowNode {
\r
43 let node: WorkflowNode;
\r
45 case NodeType[NodeType.startEvent]:
\r
46 node = new StartEvent(this.createId(), name, type, new Position(top, left), []);
\r
48 case NodeType[NodeType.restTask]:
\r
49 node = new RestTask(this.createId(), name, type, new Position(top, left), []);
\r
52 node = new WorkflowNode(this.createId(), name, type, new Position(top, left), []);
\r
56 this.workflow.nodes.push(node);
\r
60 public deleteNode(nodeId: string): WorkflowNode {
\r
61 // delete related connections
\r
62 this.workflow.nodes.forEach(node => this.deleteSequenceFlow(node.id, nodeId));
\r
64 // delete current node
\r
65 const index = this.workflow.nodes.findIndex(node => node.id === nodeId);
\r
67 const node = this.workflow.nodes.splice(index, 1)[0];
\r
68 node.sequenceFlows = [];
\r
75 public addSequenceFlow(sourceId: string, targetId: string) {
\r
76 const node = this.getNodeById(sourceId);
\r
78 const index = node.sequenceFlows.findIndex(sequenceFlow => sequenceFlow.targetRef === targetId);
\r
80 node.sequenceFlows.push(new SequenceFlow(sourceId, targetId));
\r
85 public deleteSequenceFlow(sourceId: string, targetId: string) {
\r
86 const node = this.getNodeById(sourceId);
\r
88 const index = node.sequenceFlows.findIndex(sequenceFlow => sequenceFlow.targetRef === targetId);
\r
90 node.sequenceFlows.splice(index, 1);
\r
95 public getNodeById(sourceId: string): WorkflowNode {
\r
96 return this.workflow.nodes.find(node => node.id === sourceId);
\r
99 private createId() {
\r
100 const idSet = new Set();
\r
101 this.workflow.nodes.forEach(node => idSet.add(node.id));
\r
103 for (let i = 0; i < idSet.size; i++) {
\r
104 if (!idSet.has('node' + i)) {
\r
109 return 'node' + idSet.size;
\r