support set body parameter by json
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-ui / src / app / components / property / rest-task / rest-task-parameters / rest-task-parameters.component.ts
1 /**
2  * Copyright (c) 2017 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
14 import { TreeNode } from 'primeng/primeng';
15
16 import { Swagger } from '../../../../model/swagger';
17 import { RestTask } from '../../../../model/workflow/rest-task';
18 import { BroadcastService } from '../../../../services/broadcast.service';
19 import { RestParameter } from "../../../../model/workflow/rest-parameter";
20 import { ValueSource } from "../../../../model/value-source.enum";
21 import { SwaggerTreeConverterService } from "../../../../services/swagger-tree-converter.service";
22
23 /**
24  * property component presents information of a workflow node.
25  * the presented information can be edit in this component.
26  * it may load information dynamically. the content may be different for different node type.
27  */
28 @Component({
29     selector: 'b4t-rest-task-parameters',
30     templateUrl: 'rest-task-parameters.component.html',
31 })
32 export class RestTaskParametersComponent implements OnInit {
33     @Input() public task: RestTask;
34
35     public inputSources: ValueSource[] = [ValueSource.String];
36     public requestParameters: RestParameter[] = []; // not include body parameter
37     public bodyParameter: TreeNode[] = [];
38     public responseParameter: TreeNode[] = [];
39     public valueSource = ValueSource;
40
41     private index = 1;
42
43     constructor(private broadcastService: BroadcastService, private swaggerTreeConverterService: SwaggerTreeConverterService) {
44     }
45
46     public ngOnInit() {
47         this.broadcastService.nodeTaskChange$.subscribe(() => {
48             this.resetRequestParams();
49             this.resetResponseParams();
50         });
51     }
52
53     public resetRequestParams() {
54         this.requestParameters = [];
55         this.bodyParameter = [];
56
57         this.task.parameters.forEach(param => {
58             if (param.position === 'body') {
59                 const requestTreeNode = this.swaggerTreeConverterService
60                 .schema2TreeNode('Request Param', this.task.serviceName, this.task.serviceVersion, param.schema);
61                 param.value = param.schema.value;
62                 this.bodyParameter.push(requestTreeNode);
63             } else {
64                 this.requestParameters.push(param);
65             }
66         });
67     }
68
69     public resetResponseParams() {
70         // TODO add response body handler
71     }
72 }