This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
/
DataElement.vm
251 lines (224 loc) · 7 KB
/
DataElement.vm
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#parse("templates/common.vm")
#if($package)
package $package;
#end
import java.io.Serializable;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
/**
* This is an implementation of $interface that satisfies the dependencies of
* the @DataElement Annotation and was auto-generated by the ICE Framework.
*/
@Data
@NoArgsConstructor
public class ${class} implements ${interface}, Serializable {
/**
* Logging tool
*/
private static final Logger logger = LoggerFactory.getLogger(${class}.class);
#foreach($field in $fields)
#fielddoc
#nonnull($tab, $newline)
#if(!${field.Getter})
@Getter(AccessLevel.NONE)
#end
#if(!${field.Setter})
@Setter(AccessLevel.NONE)
#end
#fielddecl
#end
#foreach($field in ${fields.Constants})
#fielddoc
#fielddecl
#end
#foreach($field in $fields)
#foreach($alias in ${field.aliases})
#if(${alias.Getter})
/**
* Get ${field.Name} by alias ${alias.Name}.
* @return ${field.Name}
*/
public #fieldtype get${alias.NameForMethod}() {
return ${field.Name};
}
#end
#if(${alias.Setter})
/**
* Set ${field.Name} by alias ${alias.Name}.
*/
public void set${alias.NameForMethod}(#nonnull #fieldtype ${field.Name}) {
this.${field.Name} = ${field.Name};
}
#end
#end
#end
/**
* Copy constructor for $class.
* @param other Instance of $class to copy
* @throws Exception if other is null or not of type $class or other errors.
*/
public $class($class other) throws Exception {
if (other == null) {
throw (new Exception("$class to copy cannot be null."));
}
if (!(other instanceof $class)) {
throw (new Exception("$class can copy only from other instances of $class."));
}
#foreach($field in $fields)
this.${field.Name} = other.${field.Name};
#end
}
/**
* This operation clones the object. Note that it differs from the base class
* implementation in that it will return null if it cannot create the clone to
* promote fast failure. See {@link java.lang.Object#clone()};
*/
@Override
public Object clone() {
try {
// Call the copy constructor to create the clone.
return new $class(this);
} catch (Exception e) {
logger.error("Unable to clone $class!", e);
return null;
}
}
/**
* This function checks deep equality of DataElements to see if all members are
* equal ("match") with the exception of fields with match set to false (such
* as an automatically generated UUID). This is important for checking if two
* objects were generated separately but are otherwise equal.
*
* @param o the other element to compare
* @return true if all members of the element except excluded fields match
* this element.
*/
public boolean matches(Object o) {
boolean retval = false;
// Outer check for null comparisons
if (o != null) {
// Next check for shallow comparison
if (this != o) {
if (o instanceof $class) {
$class other = ($class) o;
// Separate boolean checks used to enable better catching
// by debuggers.
#foreach($field in ${fields.Match})
// Matching ${field.Name}
boolean ${field.Name}Match =
#if(${field.Nullable})
this.${field.Name} == null ?
this.${field.Name} == other.${field.Name} :
this.${field.Name}.equals(other.${field.Name});
#elseif(${field.Primitive})
this.${field.Name} == other.${field.Name};
#else
this.${field.Name}.equals(other.${field.Name});
#end## if nullable
#end## foreach
retval =
#@settab(6)
#foreach($field in ${fields.Match})#@nonewline
${field.Name}Match#if($foreach.hasNext) &&#if($foreach.count % 3 == 0)$newline#else #end#else;$newline#end
#end#end
#end
}
} else {
// This should be true if they are the same because the deep comparison is
// performed otherwise.
retval = true;
}
}
return retval;
}
/**
* This operation serializes the data element to a string in verified JSON.
*
* @return a JSON string describing the element
*/
public String toJSON() {
String value = null;
// Convert to json using Jackson
ObjectMapper mapper = new ObjectMapper();
// Set visibility so that only methods are serialized. This removes duplication
// otherwise produced due to the convenience methods.
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
try {
value = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
} catch (JsonProcessingException e) {
logger.error("Unable to write DataElement to string!", e);
}
return value;
}
/**
* Load DataElement from JsonNode.
* @param rootNode DataElement root as JsonNode
* @param mapper ObjectMapper to convert values
*/
private void fromJsonNode(final JsonNode rootNode, final ObjectMapper mapper) throws JsonProcessingException {
#foreach($field in $fields)
// ${field.Name}
JsonNode ${field.Name}Node = rootNode.get("${field.Name}");
#if(${field.Nullable})
if (rootNode.hasNonNull("${field.Name}")) {
${field.Name} = mapper.treeToValue(${field.Name}Node, ${field.Name}.getClass());
} else {
${field.Name} = null;
}
#else
${field.Name} = mapper.treeToValue(${field.Name}Node, ${field.Type}.class);
#end
#end
}
/**
* This operation deserializes a valid JSON string and tries to load it into the
* object.
*
* @param jsonDataElement the contents of this data element as JSON
*/
public $interface fromJSON(final String jsonDataElement) {
// Load the data from the string with Jackson.
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode rootNode = mapper.readTree(jsonDataElement);
fromJsonNode(rootNode, mapper);
} catch (JsonProcessingException e) {
logger.error("Unable to read DataElement from string!", e);
}
return this;
}
/**
* Load from a String-Object Map, skipping the String parsing step. Structures
* such as <code>org.bson.Document</code> implement Map<String, Object> and
* therefore do not need to be processed from raw String form.
*
* @param jsonDataElement the contents of this data element as a Map<String, Object>
*/
public <T extends Map<String, Object>> $interface fromJSON(final T jsonDataElement) {
// Load the data from the string with Jackson.
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode rootNode = mapper.valueToTree(jsonDataElement);
fromJsonNode(rootNode, mapper);
} catch (JsonProcessingException e) {
logger.error("Unable to read DataElement from string!", e);
}
return this;
}
}