d9a949eaef062b6bfa89ad7beba55f2ee66b31cb
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-ui / src / app / services / workflow.service.ts
1 /**\r
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
8  *\r
9  * Contributors:\r
10  *     ZTE - initial API and implementation and/or initial documentation\r
11  */\r
12 \r
13 import { Injectable } from '@angular/core';\r
14 import { WorkflowNode } from "../model/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";\r
18 \r
19 /**\r
20  * WorkflowService\r
21  * provides all of the operations about workflow operations.\r
22  */\r
23 @Injectable()\r
24 export class WorkflowService {\r
25 \r
26     public workflow: Workflow;\r
27 \r
28     constructor(private dataAccessService: DataAccessService) {\r
29 \r
30     }\r
31 \r
32     public save(): Observable<boolean> {\r
33         return this.dataAccessService.catalogService.saveWorkflow(this.workflow);\r
34     }\r
35 \r
36     public addNode(name: string, type: string, top: number, left: number): WorkflowNode {\r
37         const node = new WorkflowNode(this.createId(), name, type, top, left);\r
38         this.workflow.nodes.push(node);\r
39         return node;\r
40     }\r
41 \r
42     public deleteNode(nodeId: string): WorkflowNode {\r
43         // delete current node\r
44         const index = this.workflow.nodes.findIndex(node => node.id === nodeId);\r
45         if (index !== -1) {\r
46             const node = this.workflow.nodes.splice(index, 1)[0];\r
47             return node;\r
48         }\r
49 \r
50         return undefined;\r
51     }\r
52 \r
53     public getNodeById(sourceId: string): WorkflowNode {\r
54         return this.workflow.nodes.find(node => node.id === sourceId);\r
55     }\r
56 \r
57     private createId() {\r
58         const idSet = new Set();\r
59         this.workflow.nodes.forEach(node => idSet.add(node.id));\r
60 \r
61         for (let i = 0; i < idSet.size; i++) {\r
62             if (!idSet.has('node' + i)) {\r
63                 return 'node' + i;\r
64             }\r
65         }\r
66 \r
67         return 'node' + idSet.size;\r
68     }\r
69 }\r