09e0e55eb380a3d97e8acb0aa92c5d38ca28ad69
[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/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 \r
23 /**\r
24  * WorkflowService\r
25  * provides all of the operations about workflow operations.\r
26  */\r
27 @Injectable()\r
28 export class WorkflowService {\r
29 \r
30     public workflow: Workflow;\r
31 \r
32     constructor(private dataAccessService: DataAccessService) {\r
33 \r
34     }\r
35 \r
36     public save(): Observable<boolean> {\r
37         console.log(this.workflow);\r
38         return this.dataAccessService.catalogService.saveWorkflow(this.workflow);\r
39     }\r
40 \r
41     public addNode(name: string, type: string, top: number, left: number): WorkflowNode {\r
42         let node: WorkflowNode;\r
43         switch (type) {\r
44             case NodeType[NodeType.startEvent]:\r
45                 node = new StartEvent(this.createId(), name, type, new Position(top, left), []);\r
46                 break;\r
47             default:\r
48                 node = new WorkflowNode(this.createId(), name, type, new Position(top, left), []);\r
49                 break;\r
50         }\r
51 \r
52         this.workflow.nodes.push(node);\r
53         return node;\r
54     }\r
55 \r
56     public deleteNode(nodeId: string): WorkflowNode {\r
57         // delete related connections\r
58         this.workflow.nodes.forEach(node => this.deleteSequenceFlow(node.id, nodeId));\r
59 \r
60         // delete current node\r
61         const index = this.workflow.nodes.findIndex(node => node.id === nodeId);\r
62         if (index !== -1) {\r
63             const node = this.workflow.nodes.splice(index, 1)[0];\r
64             node.sequenceFlows = [];\r
65             return node;\r
66         }\r
67 \r
68         return undefined;\r
69     }\r
70 \r
71     public addSequenceFlow(sourceId: string, targetId: string) {\r
72         const node = this.getNodeById(sourceId);\r
73         if (node) {\r
74             const index = node.sequenceFlows.findIndex(sequenceFlow => sequenceFlow.targetRef === targetId);\r
75             if (index === -1) {\r
76                 node.sequenceFlows.push(new SequenceFlow(sourceId, targetId));\r
77             }\r
78         }\r
79     }\r
80 \r
81     public deleteSequenceFlow(sourceId: string, targetId: string) {\r
82         const node = this.getNodeById(sourceId);\r
83         if (node) {\r
84             const index = node.sequenceFlows.findIndex(sequenceFlow => sequenceFlow.targetRef === targetId);\r
85             if (index !== -1) {\r
86                 node.sequenceFlows.splice(index, 1);\r
87             }\r
88         }\r
89     }\r
90 \r
91     public getNodeById(sourceId: string): WorkflowNode {\r
92         return this.workflow.nodes.find(node => node.id === sourceId);\r
93     }\r
94 \r
95     private createId() {\r
96         const idSet = new Set();\r
97         this.workflow.nodes.forEach(node => idSet.add(node.id));\r
98 \r
99         for (let i = 0; i < idSet.size; i++) {\r
100             if (!idSet.has('node' + i)) {\r
101                 return 'node' + i;\r
102             }\r
103         }\r
104 \r
105         return 'node' + idSet.size;\r
106     }\r
107 }\r