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
10 * ZTE - initial API and implementation and/or initial documentation
11 *******************************************************************************/
12 import { Component, Input, OnInit } from '@angular/core';
13 import { PlanTreeviewItem } from '../../../../model/plan-treeview-item';
14 import { SwaggerBaseParameter } from '../../../../model/workflow/swagger/swagger-base-parameter';
15 import { SwaggerResponse } from '../../../../model/workflow/swagger/swagger-response';
16 import { TreeNode } from 'primeng/components/common/treenode';
17 import { ValueSource } from '../../../../model/value-source.enum';
18 import { SwaggerIn } from '../../../../model/workflow/swagger/swagger-in.enum';
19 import { SwaggerBodyParameter } from '../../../../model/workflow/swagger/swagger-body-parameter';
20 import { SwaggerTreeConverterService } from '../../../../services/swagger-tree-converter.service';
21 import { SwaggerSchema } from '../../../../model/workflow/swagger/swagger-schema';
22 import { WorkflowUtil } from '../../../../util/workflow-util';
23 import { Parameter } from '../../../../model/workflow/parameter';
24 import { SwaggerNormalParameter } from '../../../../model/workflow/swagger/swagger-normal-parameter';
25 import { ValueType } from '../../../../model/value-type.enum';
26 import { ValueObject } from '../../../../model/value-object';
29 selector: 'wfm-rest-parameters',
30 templateUrl: './rest-parameters.component.html',
31 styleUrls: ['./rest-parameters.component.css']
33 export class RestParametersComponent implements OnInit {
34 @Input() public dataTypeInput: SwaggerBaseParameter[];
35 @Input() public dataTypeOutput: SwaggerResponse[];
36 @Input() public definitions: any;
37 @Input() public planItems: PlanTreeviewItem[];
38 public queryParams: Parameter[] = [];
39 public headerParams: Parameter[] = [];
40 public pathParams: Parameter[] = [];
41 public formDataParams: Parameter[] = [];
42 public bodyParams: TreeNode[] = [];
43 public responseParams: TreeNode[] = [];
45 public inputValueSource = [ValueSource.Variable, ValueSource.Plan, ValueSource.Topology];
46 public outputValueSource = [];
50 if (this.dataTypeInput) {
51 this.dataTypeInput.forEach(input => {
53 case SwaggerIn[SwaggerIn.query]:
54 let query = this.normal2Parameter(input as SwaggerNormalParameter);
55 input.value = query.value;
56 input.valueSource = query.valueSource;
57 this.queryParams.push(query);
59 case SwaggerIn[SwaggerIn.header]:
60 let header = this.normal2Parameter(input as SwaggerNormalParameter);
61 input.value = header.value;
62 input.valueSource = header.valueSource;
63 this.headerParams.push(header);
65 case SwaggerIn[SwaggerIn.path]:
66 let path = this.normal2Parameter(input as SwaggerNormalParameter);
67 input.value = path.value;
68 input.valueSource = path.valueSource;
69 this.pathParams.push(path);
71 case SwaggerIn[SwaggerIn.formData]:
72 let formData = this.normal2Parameter(input as SwaggerNormalParameter);
73 input.value = formData.value;
74 input.valueSource = formData.valueSource;
75 this.formDataParams.push(formData);
77 case SwaggerIn[SwaggerIn.body]:
78 let body = this.body2TreeNode(input as SwaggerBodyParameter);
79 input.value = body.value.value;
80 input.valueSource = body.value.valueSource;
81 this.bodyParams.push(body);
84 console.warn(`Not support this parameters in:${input.in}`);
89 this.responseParams.push(this.parameter2TreeNode('status',
90 { type: ValueType[ValueType.number], editable: false }, { value: '', valueSource: ValueSource.string }));
91 if (this.dataTypeOutput) {
92 this.dataTypeOutput.forEach(output => {
93 const treeNode = this.swaggerResponse2TreeNode(output as SwaggerResponse);
95 this.responseParams.push(treeNode);
101 public onParamChange(param: Parameter) {
102 this.dataTypeInput.forEach(input => {
103 if (input.name === param.name) {
104 input.value = param.value;
105 input.valueSource = param.valueSource;
110 private normal2Parameter(normalParam: SwaggerNormalParameter): Parameter {
112 let finalValueSource;
113 if (normalParam.value && normalParam.value && normalParam.valueSource) {
114 finalValue = normalParam.value;
115 finalValueSource = normalParam.valueSource;
117 finalValue = normalParam.default;
118 finalValueSource = ValueSource[ValueSource.string];
120 let parameter = new Parameter(normalParam.name, finalValue, finalValueSource, normalParam.type, normalParam.required, normalParam.show);
124 private body2TreeNode(bodyParam: SwaggerBodyParameter) {
125 return this.swaggerSchema2TreeNode(bodyParam.name, bodyParam.schema, bodyParam);
128 private swaggerResponse2TreeNode(responseParam: SwaggerResponse): TreeNode {
129 if (responseParam.$ref) {
130 return this.swaggerRef2TreeNode(responseParam.name, responseParam.$ref);
131 } else if (responseParam.schema) {
132 return this.swaggerSchema2TreeNode(responseParam.name, responseParam.schema);
134 console.log(`Unsupport response parameter:${responseParam.name}`);
139 private swaggerSchema2TreeNode(name: string | number, schema: SwaggerSchema, value?: any) {
141 return this.swaggerRef2TreeNode(name as string, schema.$ref);
143 value = this.getInitValue4Param(schema, value);
144 return this.parameter2TreeNode(name, schema, value);
148 private swaggerRef2TreeNode(name: string, ref: string, value?: any) {
149 let definition = this.definitions[ref];
151 label: 'Unsupport Ref Parameter',
152 type: this.getTreeNodeType(ref),
153 required: definition.required,
155 definition: definition,
159 // if (value.valueSource === ValueSource[ValueSource.Definition]) {
160 // if (definition.type === 'object') {
161 // refTreeNode.children = this.getPropertyFromObject(definition, value.value);
162 // } else if (definition.type === 'array') {
163 // refTreeNode.children = this.setChildrenForArray(definition, value.value);
169 private getInitValue4Param(schema: SwaggerSchema, value: any) {
170 let definition = schema;
172 definition = this.definitions[schema.$ref];
174 let valueObject: ValueObject = { valueSource: ValueSource[ValueSource.string] };
175 if (undefined == value) {
176 valueObject.value = definition.default;
177 if (ValueType[ValueType.array] === definition.type || ValueType[ValueType.object] === definition.type) {
178 valueObject.valueSource = ValueSource[ValueSource.Definition];
180 valueObject.valueSource = definition.type;
183 if('object' != typeof(value)){
184 console.error('Param value is not object!, param definition is:' + definition);
186 valueObject.valueSource = value.valueSource;
187 valueObject.value = undefined === value.value ? definition.default : value.value;
189 if (ValueType[ValueType.object] === definition.type) {
190 return this.getInitValue4Object(valueObject);
191 } else if (ValueType[ValueType.array] === definition.type) {
192 // if (undefined == value) {
193 // valueObject.value = definition.default;
194 // if (ValueType[ValueType.array] === definition.type || ValueType[ValueType.object] === definition.type) {
195 // valueObject.valueSource = ValueSource[ValueSource.Definition];
197 // valueObject.valueSource = definition.type;
200 // valueObject.valueSource = value.valueSource;
201 // valueObject.value = undefined === value.value ? definition.default : value.value;
203 return this.getInitValue4Array(valueObject);
204 } else { // primary type
205 // valueObject.value = undefined === value ? definition.default : value;
206 // valueObject.valueSource = definition.type;
207 return this.getInitValue4Primary(valueObject);
212 private getInitValue4Object(value: any) {
215 valueSource: ValueSource[ValueSource.Definition]
218 if (!value || '' === value) {
221 if (value.valueSource !== ValueSource[ValueSource.Definition]) {
224 if (typeof value.value !== 'object') {
232 private getInitValue4Array(value: any) {
235 valueSource: ValueSource[ValueSource.Definition]
238 if (!value || '' === value) {
241 if (value.valueSource !== ValueSource[ValueSource.Definition]) {
244 if (!(value.value instanceof Array)) {
252 private getInitValue4Primary(value: any) {
255 valueSource: ValueSource[ValueSource.string]
261 if (typeof value.value === 'object') {
268 private parameter2TreeNode(name: string | number, definition: any, value: any): any {
269 const nodeType = this.getTreeNodeType(definition);
274 required: definition.required,
276 definition: definition,
280 if (value.valueSource === ValueSource[ValueSource.Definition]) {
281 if (ValueType[ValueType.object] === definition.type) {
282 node.children = this.getPropertyFromObject(definition, value.value);
283 } else if (ValueType[ValueType.array] === definition.type) {
284 node.children = this.getItemsFromArray(definition, value.value);
291 private getTreeNodeType(param: any): string {
292 const type = param.type;
293 if (ValueType[ValueType.array] === type) {
295 } else if (ValueType[ValueType.object] === type) {
296 if (param.additionalProperties) {
306 private getPropertyFromObject(schema: SwaggerSchema, value: any): TreeNode[] {
307 if (schema.properties) {
308 const required = schema.required as string[];
309 return this.getPropertyFromSimpleObject(schema.properties, value, required);
310 } else if (schema.additionalProperties) {
311 return this.getPropertyFromMapOrDictionary(schema.additionalProperties, value);
313 console.warn('getPropertyFromObject() return [], param is:' + JSON.stringify(schema));
319 private getPropertyFromSimpleObject(properties: any, objectValue: any, required: string[]): TreeNode[] {
320 const treeNodes: TreeNode[] = [];
321 for (const key in properties) {
322 let property = properties[key];
323 // init required property
324 property.required = false;
325 if (Array.isArray(required)) {
326 for (let index = 0; index < required.length; index++) {
327 if (required[index] === key) {
328 property.required = true;
334 objectValue[key] = this.getInitValue4Param(property, objectValue[key]);
336 const treeNode = this.swaggerSchema2TreeNode(key, property, objectValue[key]);
337 treeNodes.push(treeNode);
342 private getPropertyFromMapOrDictionary(additionalProperties: any, mapOrDictionary: any): TreeNode[] {
343 const treeNodes: TreeNode[] = [];
344 for (const key in mapOrDictionary) {
345 const propertyCopy = WorkflowUtil.deepClone(additionalProperties);
346 propertyCopy.value = mapOrDictionary[key];
347 const treeNode = this.swaggerSchema2TreeNode(key, propertyCopy, propertyCopy.value);
348 treeNode.keyEditable = true;
349 treeNodes.push(treeNode);
350 if (mapOrDictionary[key] !== propertyCopy.value) {
351 mapOrDictionary[key] = propertyCopy.value;
357 private getItemsFromArray(definition: any, value: any[]): any[] {
359 value.forEach((itemValue, index) => {
360 const itemCopy = WorkflowUtil.deepClone(definition.items);
361 children.push(this.swaggerSchema2TreeNode(index, itemCopy, itemValue));