-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMisc.java
More file actions
173 lines (151 loc) · 5.63 KB
/
Misc.java
File metadata and controls
173 lines (151 loc) · 5.63 KB
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
package jsontotree;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.thirdparty.org.json.JSONObject;
import jsontotree.proxies.JSONNode;
public class Misc {
/**
* Traverse a JSON Tree
*
* @param ctx Context for the current Java Action
* @param rootObject Root object that binds all Node objects
* @param rootNodes Array/Single Tree object
* @param childKey Key that represents the array with child nodes
*/
public static void traverseRootObjects(
IContext ctx,
IMendixObject rootObject,
JsonNode rootNodes,
String childKey
) {
traverse(rootObject, ctx, rootNodes, childKey, null);
}
/**
* Root traverse mode, will traverse through an array or single object
*
* @param rootObject Root object that binds all Node objects
* @param ctx Context for the current Java Action
* @param node current node
* @param childKey Key that represents the array with child nodes
* @param parentMXObject Mendix JSONNode object that is the parent
*/
private static void traverse(IMendixObject rootObject, IContext ctx, JsonNode node, String childKey, IMendixObject parentMXObject) {
if (node.getNodeType() == JsonNodeType.ARRAY) {
traverseArray(rootObject, ctx, node, childKey, parentMXObject);
} else if (node.getNodeType() == JsonNodeType.OBJECT) {
traverseObject(rootObject, ctx, node, childKey, parentMXObject);
} else {
throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Problem with traversing JSON, node type not implemented yet: " + node.getNodeType().toString());
}
}
/**
* Traverse through single node
*
* @param rootObject Root object that binds all Node objects
* @param ctx Context for the current Java Action
* @param node current node
* @param childKey Key that represents the array with child nodes
* @param parentMXObject Mendix JSONNode object that is the parent
*/
private static void traverseObject(IMendixObject rootObject, IContext ctx, JsonNode node, String childKey, IMendixObject parentMXObject) {
JsonNode child = node.get(childKey);
boolean hasChild;
if (child == null){
hasChild = false;
} else {
hasChild = traversable(child);
}
JSONObject copy = createJSONObject(node, childKey);
String jsonContent = copy.toString();
IMendixObject jsonObj = createNodeObject(ctx, rootObject, parentMXObject, jsonContent);
if (hasChild) {
traverse(rootObject, ctx, child, childKey, jsonObj);
}
}
/**
* Traverse through array of nodes
*
* @param rootObject Root object that binds all Node objects
* @param ctx Context for the current Java Action
* @param node current node
* @param childKey Key that represents the array with child nodes
* @param parentMXObject Mendix JSONNode object that is the parent
*/
private static void traverseArray(IMendixObject rootObject, IContext ctx, JsonNode node, String childKey, IMendixObject parentMXObject) {
for (JsonNode jsonArrayNode : node) {
if (traversable(jsonArrayNode)) {
traverse(rootObject, ctx, jsonArrayNode, childKey, parentMXObject);
}
}
}
/**
* Check if the node is not empty, not an object (we don't allow that), but an array
*
* @param node
* @return Is this a traversable array?
*/
private static boolean traversable(JsonNode node) {
return !node.isEmpty()
&& (node.getNodeType() == JsonNodeType.OBJECT || node.getNodeType() == JsonNodeType.ARRAY);
}
/**
* Create a JSON object that holds all the fields from the node, with the exception of the children
*
* @param node
* @param childKey
* @return
*/
private static JSONObject createJSONObject(JsonNode node, String childKey) {
JSONObject copy = new JSONObject();
node.fieldNames().forEachRemaining((String fieldName) -> {
JsonNode childNode = node.get(fieldName);
if (!fieldName.equalsIgnoreCase(childKey)) {
Object value = null;
if (childNode.isTextual()) {
value = childNode.textValue();
} else if (childNode.isNumber()) {
value = childNode.numberValue();
} else if (childNode.isDouble()) {
value = childNode.doubleValue();
} else if (childNode.isLong()) {
value = childNode.asLong();
} else if (childNode.isBoolean()) {
value = childNode.asBoolean();
} else {
value = childNode.asText();
}
copy.put(fieldName, value);
}
});
return copy;
}
/**
* Create a Mendix JSONNode object
*
* @param ctx
* @param rootObject
* @param parentMXObject
* @param jsonContent
* @return
*/
private static IMendixObject createNodeObject(IContext ctx, IMendixObject rootObject, IMendixObject parentMXObject, String jsonContent) {
IMendixObject JSONRepresentationObject = Core.instantiate(ctx, JSONNode.getType());
JSONRepresentationObject.setValue(ctx, JSONNode.MemberNames.Content.toString(),
jsonContent);
JSONRepresentationObject.setValue(ctx, JSONNode.MemberNames.JSONNode_Root.toString(),
rootObject.getId());
if (parentMXObject != null) {
JSONRepresentationObject.setValue(ctx, JSONNode.MemberNames.Parent.toString(), parentMXObject.getId());
}
try {
Core.commit(ctx, JSONRepresentationObject);
} catch (CoreException e) {
throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Issue with committing temporary JSON Object");
}
return JSONRepresentationObject;
}
}