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 { RestService } from './rest.service';
19 import { Swagger } from "../model/swagger";
20 import { ValueObject } from '../model/value-object';
21 import { ValueType } from '../model/value-type.enum';
24 export class SwaggerTreeConverterService {
26 private swagger: Swagger;
28 constructor(private restService: RestService) { }
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.restService.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.restService.getDefinition(this.swagger, definition.$ref);
55 let valueObject: ValueObject = { valueSource: ValueSource[ValueSource.string] };
56 if (undefined == value) {
57 valueObject.value = definition.default;
58 if (ValueType[ValueType.array] === definition.type || ValueType[ValueType.object] === definition.type) {
59 valueObject.valueSource = ValueSource[ValueSource.Definition];
61 valueObject.valueSource = definition.type;
64 valueObject.valueSource = value.valueSource;
65 valueObject.value = undefined === value.value ? definition.default : value.value;
67 if (definition.type === 'object') {
68 // if (undefined == value) {
69 // valueObject.value = definition.default;
70 // if (ValueType[ValueType.array] === definition.type || ValueType[ValueType.object] === definition.type) {
71 // valueObject.valueSource = ValueSource[ValueSource.Definition];
73 // valueObject.valueSource = definition.type;
76 // valueObject.valueSource = value.valueSource;
77 // valueObject.value = undefined === value.value ? definition.default : value.value;
79 return this.getInitValue4Object(valueObject);
80 } else if (definition.type === 'array') {
81 return this.getInitValue4Array(valueObject);
82 } else { // primary type
83 // valueObject.value = undefined === value ? definition.default : value;
84 // valueObject.valueSource = definition.type;
85 return this.getInitValue4Primary(valueObject);
89 private getInitValue4Object(value: any) {
92 valueSource: ValueSource[ValueSource.Definition]
95 if (!value || '' === value) {
98 if (value.valueSource !== ValueSource[ValueSource.Definition]) {
101 if (typeof value.value !== 'object') {
109 private getInitValue4Array(value: any) {
112 valueSource: ValueSource[ValueSource.Definition]
115 if (!value || '' === value) {
118 if (value.valueSource !== ValueSource[ValueSource.Definition]) {
121 if (!(value.value instanceof Array)) {
129 private getInitValue4Primary(value: any) {
132 valueSource: ValueSource[ValueSource.string]
138 if (typeof value.value === 'object') {
145 private parameter2TreeNode(name: string | number, definition: any, value: any): any {
146 const nodeType = this.getTreeNodeType(definition);
151 required: definition.required,
153 definition: definition,
157 if (value.valueSource === ValueSource[ValueSource.Definition]) {
158 if (definition.type === 'object') {
159 node.children = this.getPropertyFromObject(definition, value.value);
160 } else if (definition.type === 'array') {
161 node.children = this.setChildrenForArray(definition, value.value);
168 private getTreeNodeType(param: any): string {
169 const type = param.type;
170 if (type === 'array') {
172 } else if (type === 'object') {
173 if (param.additionalProperties) {
183 private setChildrenForArray(definition: any, value: any[]): any[] {
185 value.forEach((itemValue, index) => {
186 const itemCopy = WorkflowUtil.deepClone(definition.items);
187 children.push(this.schema2TreeNode(this.swagger, index, itemCopy, itemValue));
193 private getPropertyFromObject(definition: any, value: any): TreeNode[] {
194 if (definition.properties) {
195 return this.getPropertyFromSimpleObject(definition.properties, value, definition.required);
196 } else if (definition.additionalProperties) {
197 return this.getPropertyFromMapOrDictionary(definition.additionalProperties, value);
199 console.log('getPropertyFromObject() return [], param is:' + JSON.stringify(definition));
205 private getPropertyFromSimpleObject(properties: any, objectValue: any, required: string[]): TreeNode[] {
206 const treeNodes: TreeNode[] = [];
207 for (const key in properties) {
208 let property = properties[key];
209 // init required property
210 property.required = false;
211 if (Array.isArray(required)) {
212 for (let index = 0; index < required.length; index++) {
213 if (required[index] === key) {
214 property.required = true;
220 objectValue[key] = this.getInitValue4Param(property, objectValue[key]);
222 const treeNode = this.schema2TreeNode(this.swagger, key, property, objectValue[key]);
223 treeNodes.push(treeNode);
228 private getPropertyFromMapOrDictionary(additionalProperties: any, mapOrDictionary: any): TreeNode[] {
229 const treeNodes: TreeNode[] = [];
230 for (const key in mapOrDictionary) {
231 const propertyCopy = WorkflowUtil.deepClone(additionalProperties);
232 propertyCopy.value = mapOrDictionary[key];
234 const treeNode = this.schema2TreeNode(this.swagger, key, propertyCopy, propertyCopy.value);
235 treeNode.keyEditable = true;
236 treeNodes.push(treeNode);
238 if (mapOrDictionary[key] !== propertyCopy.value) {
239 mapOrDictionary[key] = propertyCopy.value;