8e8cc94fecf9e481c74d2ef1ec906a29062fbd44
[sdc/sdc-workflow-designer.git] /
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 import { PlanTreeviewItem } from "../../../../model/plan-treeview-item";
23 import { WorkflowConfigService } from "../../../../services/workflow-config.service";
24 import { RestService } from '../../../../services/rest.service';
25
26 /**
27  * property component presents information of a workflow node.
28  * the presented information can be edit in this component.
29  * it may load information dynamically. the content may be different for different node type.
30  */
31 @Component({
32     selector: 'b4t-rest-task-parameters',
33     templateUrl: 'rest-task-parameters.component.html',
34 })
35 export class RestTaskParametersComponent implements OnInit {
36     @Input() public task: RestTask;
37     @Input() public planItems: PlanTreeviewItem[];
38
39     public inputSources: ValueSource[] = [ValueSource.String, ValueSource.Variable, ValueSource.Plan];
40     public requestParameters: RestParameter[] = []; // not include body parameter
41     public bodyParameter: TreeNode[] = [];
42     public responseParameter: TreeNode[] = [];
43     public valueSource = ValueSource;
44
45     private index = 1;
46
47     constructor(private broadcastService: BroadcastService,
48         private restService: RestService,
49         private swaggerTreeConverterService: SwaggerTreeConverterService) {
50     }
51
52     public ngOnInit() {
53         this.broadcastService.nodeTaskChange$.subscribe(() => {
54             this.resetRequestParams();
55             this.resetResponseParams();
56         });
57     }
58
59     public resetRequestParams() {
60         this.requestParameters = [];
61         this.bodyParameter = [];
62
63         this.task.parameters.forEach(param => {
64             if (param.position === 'body') {
65                 const requestTreeNode = this.swaggerTreeConverterService
66                     .schema2TreeNode(this.restService.getSwaggerInfo(this.task.restConfigId), 'Request Param', param.schema, param.value);
67                 param.value = requestTreeNode.value;
68                 param.value = param.schema.value;
69                 this.bodyParameter.push(requestTreeNode);
70             } else {
71                 this.requestParameters.push(param);
72             }
73         });
74     }
75
76     public resetResponseParams() {
77         // TODO add response body handler
78     }
79 }