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 *******************************************************************************/
13 import { Injectable } from '@angular/core';
14 import { TreeNode } from 'primeng/primeng';
16 import { ValueSource } from '../model/value-source.enum';
17 import { WorkflowUtil } from '../util/workflow-util';
18 import { WorkflowConfigService } from "./workflow-config.service";
21 export class SwaggerTreeConverterService {
22 private serviceName: string;
23 private serviceVersion: string;
25 constructor(private configService: WorkflowConfigService) {
29 public schema2TreeNode(key: string | number, serviceName: string, serviceVersion: string, schema: any): any {
30 this.serviceName = serviceName;
31 this.serviceVersion = serviceVersion;
34 const treeNode = this.getTreeNodeBySwaggerDefinition(key, schema);
37 this.setInitValue4Param(schema.value, schema);
38 return this.parameter2TreeNode(key, schema);
42 private getTreeNodeBySwaggerDefinition(key: string | number, schema: any): TreeNode {
43 const swagger = this.configService.getSwaggerInfo(this.serviceName, this.serviceVersion);
44 if(swagger === undefined) {
45 console.log(`swagger definition not exist, [${this.serviceName}].[${this.serviceVersion}]`);
48 const swaggerDefinition = this.configService.getDefinition(swagger, schema.$ref);
50 const definitionCopy = WorkflowUtil.deepClone(swaggerDefinition);
52 this.setInitValue4Param(schema.value, definitionCopy);
53 if (schema.value !== definitionCopy.value) {
54 schema.value = definitionCopy.value;
57 return this.schema2TreeNode(key, this.serviceName, this.serviceVersion, definitionCopy);
60 private setInitValue4Param(value: any | any[], param: any) {
63 if (param.type === 'object') {
65 } else if (param.type === 'array') {
67 } else { // primary type
68 param.valueSource = ValueSource[ValueSource.String];
73 private parameter2TreeNode(name: string | number, paramDefinition: any): any {
74 const nodeType = this.getTreeNodeType(paramDefinition);
80 parameter: paramDefinition,
83 if (paramDefinition.type === 'object') {
84 node.children = this.getPropertyFromObject(paramDefinition.value, paramDefinition);
85 } else if (paramDefinition.type === 'array') {
86 this.setChildrenForArray(node, paramDefinition);
92 private getTreeNodeType(param: any): string {
93 const type = param.type;
94 if (type === 'array') {
96 } else if (type === 'object') {
97 if (param.additionalProperties) {
107 private setChildrenForArray(node: any, param: any) {
108 param.value.forEach((itemValue, index) => {
109 const itemCopy = WorkflowUtil.deepClone(param.items);
110 itemCopy.value = itemValue;
111 node.children.push(this.schema2TreeNode(index, this.serviceName, this.serviceVersion, itemCopy));
115 private getPropertyFromObject(objectValue: any, param: any): TreeNode[] {
116 if (param.properties) {
117 return this.getPropertyFromSimpleObject(objectValue, param.properties);
118 } else if (param.additionalProperties) {
119 return this.getPropertyFromMapOrDictionary(objectValue, param.additionalProperties);
126 private getPropertyFromSimpleObject(objectValue: any, properties: any): TreeNode[] {
127 const treeNodes: TreeNode[] = [];
128 for (const key in properties) {
129 const property = properties[key];
130 this.setInitValue4Param(objectValue[key], property);
132 if (property.value !== objectValue[key]) {
133 objectValue[key] = property.value;
136 treeNodes.push(this.schema2TreeNode(key, this.serviceName, this.serviceVersion, property));
141 private getPropertyFromMapOrDictionary(mapOrDictionary: any, additionalProperties: any): TreeNode[] {
142 const treeNodes: TreeNode[] = [];
143 for (const key in mapOrDictionary) {
144 const propertyCopy = WorkflowUtil.deepClone(additionalProperties);
145 propertyCopy.value = mapOrDictionary[key];
147 const treeNode = this.schema2TreeNode(key, this.serviceName, this.serviceVersion, propertyCopy);
148 treeNode.keyEditable = true;
149 treeNodes.push(treeNode);
151 if (mapOrDictionary[key] !== propertyCopy.value) {
152 mapOrDictionary[key] = propertyCopy.value;