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