1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*******************************************************************************
* Copyright (c) 2017 ZTE Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and the Apache License 2.0 which both accompany this distribution,
* and are available at http://www.eclipse.org/legal/epl-v10.html
* and http://www.apache.org/licenses/LICENSE-2.0
*
* Contributors:
* ZTE - initial API and implementation and/or initial documentation
*******************************************************************************/
import { Injectable } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { ValueSource } from '../model/value-source.enum';
import { WorkflowUtil } from '../util/workflow-util';
import { Swagger } from "../model/swagger";
import { WorkflowConfigService } from "./workflow-config.service";
@Injectable()
export class SwaggerTreeConverterService {
private swagger: Swagger;
constructor(private workflowConfigService: WorkflowConfigService) {
}
public schema2TreeNode(swagger: Swagger, key: string | number, schema: any, value?: any): any {
this.swagger = swagger;
if (schema.$ref) {
const treeNode = this.getTreeNodeBySwaggerDefinition(key, schema, value);
return treeNode;
} else {
value = this.getInitValue4Param(schema, value);
return this.parameter2TreeNode(key, schema, value);
}
}
private getTreeNodeBySwaggerDefinition(key: string | number, schema: any, value: any): TreeNode {
const swaggerDefinition = this.workflowConfigService.getDefinition(this.swagger, schema.$ref);
const definitionCopy = WorkflowUtil.deepClone(swaggerDefinition);
value = this.getInitValue4Param(definitionCopy, value);
return this.schema2TreeNode(this.swagger, key, definitionCopy, value);
}
private getInitValue4Param(definition: any, value: any) {
if (definition.$ref) {
definition = this.workflowConfigService.getDefinition(this.swagger, definition.$ref);
}
if (definition.type === 'object') {
return this.getInitValue4Object(value);
} else if (definition.type === 'array') {
return this.getInitValue4Array(value);
} else { // primary type
return this.getInitValue4Primate(value);
}
}
private getInitValue4Object(value: any) {
const newValue = {
value: {},
valueSource: ValueSource[ValueSource.Definition]
};
if(!value) {
return newValue;
} else {
if(value.valueSource !== ValueSource[ValueSource.Definition]) {
return value;
} else {
if(!value.value || typeof value.value !== 'object') {
value.value = {};
}
return value;
}
}
}
private getInitValue4Array(value: any) {
const newValue = {
value: [],
valueSource: ValueSource[ValueSource.Definition]
};
if(!value) {
return newValue;
} else {
if(value.valueSource !== ValueSource[ValueSource.Definition]) {
return value;
} else {
if(!(value.value instanceof Array)) {
value.value = [];
}
return value;
}
}
}
private getInitValue4Primate(value: any) {
const newValue = {
value: '',
valueSource: ValueSource[ValueSource.String]
};
if(!value) {
return newValue;
} else {
if(typeof value.value === 'object') {
value.value = '';
}
return value;
}
}
private parameter2TreeNode(name: string | number, definition: any, value: any): any {
const nodeType = this.getTreeNodeType(definition);
const node = {
label: name,
type: nodeType,
required: definition.required,
children: [],
definition: definition,
value: value,
};
if(value.valueSource === ValueSource[ValueSource.Definition]) {
if (definition.type === 'object') {
node.children = this.getPropertyFromObject(definition, value.value);
} else if (definition.type === 'array') {
this.setChildrenForArray(definition, value.value);
}
}
return node;
}
private getTreeNodeType(param: any): string {
const type = param.type;
if (type === 'array') {
return 'array';
} else if (type === 'object') {
if (param.additionalProperties) {
return 'map';
} else {
return 'object';
}
} else {
return 'default';
}
}
private setChildrenForArray(definition: any, value: any[]): any[] {
const children = [];
value.forEach((itemValue, index) => {
const itemCopy = WorkflowUtil.deepClone(definition.items);
children.push(this.schema2TreeNode(this.swagger, index, itemCopy, itemValue));
});
return children;
}
private getPropertyFromObject(definition: any, value: any): TreeNode[] {
if (definition.properties) {
return this.getPropertyFromSimpleObject(definition.properties, value, definition.required);
} else if (definition.additionalProperties) {
return this.getPropertyFromMapOrDictionary(definition.additionalProperties, value);
} else {
console.log('getPropertyFromObject() return [], param is:' + JSON.stringify(definition));
return [];
}
}
private getPropertyFromSimpleObject(properties: any, objectValue: any, required: string[]): TreeNode[] {
const treeNodes: TreeNode[] = [];
for (const key in properties) {
let property = properties[key];
// init required property
property.required = false;
if (Array.isArray(required)) {
for (let index = 0; index < required.length; index++) {
if (required[index] === key) {
property.required = true;
break;
}
}
}
objectValue[key] = this.getInitValue4Param(property, objectValue[key]);
const treeNode = this.schema2TreeNode(this.swagger, key, property, objectValue[key]);
treeNodes.push(treeNode);
}
return treeNodes;
}
private getPropertyFromMapOrDictionary(additionalProperties: any, mapOrDictionary: any): TreeNode[] {
const treeNodes: TreeNode[] = [];
for (const key in mapOrDictionary) {
const propertyCopy = WorkflowUtil.deepClone(additionalProperties);
propertyCopy.value = mapOrDictionary[key];
const treeNode = this.schema2TreeNode(this.swagger, key, propertyCopy, propertyCopy.value);
treeNode.keyEditable = true;
treeNodes.push(treeNode);
if (mapOrDictionary[key] !== propertyCopy.value) {
mapOrDictionary[key] = propertyCopy.value;
}
}
return treeNodes;
}
}
|