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 { Swagger } from "../model/swagger";
19 import { WorkflowConfigService } from "./workflow-config.service";
22 export class SwaggerTreeConverterService {
24 private swagger: Swagger;
26 constructor(private workflowConfigService: WorkflowConfigService) {
30 public schema2TreeNode(swagger: Swagger, key: string | number, schema: any, value?: any): any {
31 this.swagger = swagger;
33 const treeNode = this.getTreeNodeBySwaggerDefinition(key, schema, value);
36 value = this.getInitValue4Param(schema, value);
37 return this.parameter2TreeNode(key, schema, value);
41 private getTreeNodeBySwaggerDefinition(key: string | number, schema: any, value: any): TreeNode {
42 const swaggerDefinition = this.workflowConfigService.getDefinition(this.swagger, schema.$ref);
44 const definitionCopy = WorkflowUtil.deepClone(swaggerDefinition);
46 value = this.getInitValue4Param(definitionCopy, value);
48 return this.schema2TreeNode(this.swagger, key, definitionCopy, value);
51 private getInitValue4Param(definition: any, value: any) {
52 if (definition.$ref) {
53 definition = this.workflowConfigService.getDefinition(this.swagger, definition.$ref);
55 if (definition.type === 'object') {
56 return this.getInitValue4Object(value);
57 } else if (definition.type === 'array') {
58 return this.getInitValue4Array(value);
59 } else { // primary type
60 return this.getInitValue4Primate(value);
64 private getInitValue4Object(value: any) {
67 valueSource: ValueSource[ValueSource.Definition]
73 if(value.valueSource !== ValueSource[ValueSource.Definition]) {
76 if(!value.value || typeof value.value !== 'object') {
84 private getInitValue4Array(value: any) {
87 valueSource: ValueSource[ValueSource.Definition]
93 if(value.valueSource !== ValueSource[ValueSource.Definition]) {
96 if(!(value.value instanceof Array)) {
104 private getInitValue4Primate(value: any) {
107 valueSource: ValueSource[ValueSource.String]
113 if(typeof value.value === 'object') {
120 private parameter2TreeNode(name: string | number, definition: any, value: any): any {
121 const nodeType = this.getTreeNodeType(definition);
126 required: definition.required,
128 definition: definition,
132 if(value.valueSource === ValueSource[ValueSource.Definition]) {
133 if (definition.type === 'object') {
134 node.children = this.getPropertyFromObject(definition, value.value);
135 } else if (definition.type === 'array') {
136 this.setChildrenForArray(definition, value.value);
143 private getTreeNodeType(param: any): string {
144 const type = param.type;
145 if (type === 'array') {
147 } else if (type === 'object') {
148 if (param.additionalProperties) {
158 private setChildrenForArray(definition: any, value: any[]): any[] {
160 value.forEach((itemValue, index) => {
161 const itemCopy = WorkflowUtil.deepClone(definition.items);
162 children.push(this.schema2TreeNode(this.swagger, index, itemCopy, itemValue));
168 private getPropertyFromObject(definition: any, value: any): TreeNode[] {
169 if (definition.properties) {
170 return this.getPropertyFromSimpleObject(definition.properties, value, definition.required);
171 } else if (definition.additionalProperties) {
172 return this.getPropertyFromMapOrDictionary(definition.additionalProperties, value);
174 console.log('getPropertyFromObject() return [], param is:' + JSON.stringify(definition));
180 private getPropertyFromSimpleObject(properties: any, objectValue: any, required: string[]): TreeNode[] {
181 const treeNodes: TreeNode[] = [];
182 for (const key in properties) {
183 let property = properties[key];
184 // init required property
185 property.required = false;
186 if (Array.isArray(required)) {
187 for (let index = 0; index < required.length; index++) {
188 if (required[index] === key) {
189 property.required = true;
195 objectValue[key] = this.getInitValue4Param(property, objectValue[key]);
197 const treeNode = this.schema2TreeNode(this.swagger, key, property, objectValue[key]);
198 treeNodes.push(treeNode);
203 private getPropertyFromMapOrDictionary(additionalProperties: any, mapOrDictionary: any): TreeNode[] {
204 const treeNodes: TreeNode[] = [];
205 for (const key in mapOrDictionary) {
206 const propertyCopy = WorkflowUtil.deepClone(additionalProperties);
207 propertyCopy.value = mapOrDictionary[key];
209 const treeNode = this.schema2TreeNode(this.swagger, key, propertyCopy, propertyCopy.value);
210 treeNode.keyEditable = true;
211 treeNodes.push(treeNode);
213 if (mapOrDictionary[key] !== propertyCopy.value) {
214 mapOrDictionary[key] = propertyCopy.value;