Package: MappingUtils
MappingUtils
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
appendField(ObjectNode, String, Object) |
|
|
|
|
|
||||||||||||||||||||
newArrayNode() |
|
|
|
|
|
||||||||||||||||||||
newArrayNode(Collection) |
|
|
|
|
|
||||||||||||||||||||
newArrayNode(Object[]) |
|
|
|
|
|
||||||||||||||||||||
newArrayNodeFromPredicates(Collection) |
|
|
|
|
|
||||||||||||||||||||
newNumericNode(long) |
|
|
|
|
|
||||||||||||||||||||
newObjectNode() |
|
|
|
|
|
||||||||||||||||||||
newObjectNode(KeyValueEntry[]) |
|
|
|
|
|
||||||||||||||||||||
newObjectNode(String, Object) |
|
|
|
|
|
||||||||||||||||||||
newTextNode(String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*******************************************************************************
2: * Copyright (c) 2020, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.service.storable.model.utils;
14:
15: import com.fasterxml.jackson.databind.node.ArrayNode;
16: import com.fasterxml.jackson.databind.node.JsonNodeFactory;
17: import com.fasterxml.jackson.databind.node.NumericNode;
18: import com.fasterxml.jackson.databind.node.ObjectNode;
19: import com.fasterxml.jackson.databind.node.TextNode;
20: import org.eclipse.kapua.commons.util.KapuaDateUtils;
21: import org.eclipse.kapua.model.id.KapuaId;
22: import org.eclipse.kapua.service.storable.exception.InvalidValueMappingException;
23: import org.eclipse.kapua.service.storable.exception.MappingException;
24: import org.eclipse.kapua.service.storable.exception.UnsupportedTypeMappingException;
25: import org.eclipse.kapua.service.storable.model.id.StorableId;
26: import org.eclipse.kapua.service.storable.model.query.predicate.StorablePredicate;
27: import org.slf4j.Logger;
28: import org.slf4j.LoggerFactory;
29:
30: import java.text.ParseException;
31: import java.util.Arrays;
32: import java.util.Collection;
33: import java.util.Date;
34:
35: /**
36: * Utilities to manage the generation of the {@link ObjectNode}
37: *
38: * @since 1.3.0
39: */
40: public class MappingUtils {
41:
42: private final static JsonNodeFactory JSON_NODE_FACTORY = JsonNodeFactory.instance;
43: private static final Logger LOG = LoggerFactory.getLogger(MappingUtils.class);
44:
45: private MappingUtils() {
46: }
47:
48: //
49: // Appends
50: //
51:
52: /**
53: * Appends the provided field/value to the object node
54: *
55: * @param node The {@link ObjectNode} to appends the field to.
56: * @param name The name of the node to append.
57: * @param value The value of the node to append.
58: * @throws UnsupportedTypeMappingException if the type of the given value is not one of the supported ones.
59: * @throws InvalidValueMappingException if the object is a {@link Date} and its value is not compatible with a Date.
60: * @since 1.3.0
61: */
62: public static void appendField(ObjectNode node, String name, Object value) throws MappingException {
63:• if (value instanceof String) {
64: node.set(name, JSON_NODE_FACTORY.textNode((String) value));
65:• } else if (value instanceof Boolean) {
66: node.set(name, JSON_NODE_FACTORY.booleanNode((Boolean) value));
67:• } else if (value instanceof Integer) {
68: node.set(name, JSON_NODE_FACTORY.numberNode((Integer) value));
69:• } else if (value instanceof Long) {
70: node.set(name, JSON_NODE_FACTORY.numberNode((Long) value));
71:• } else if (value instanceof Double) {
72: node.set(name, JSON_NODE_FACTORY.numberNode((Double) value));
73:• } else if (value instanceof Float) {
74: node.set(name, JSON_NODE_FACTORY.numberNode((Float) value));
75:• } else if (value instanceof byte[]) {
76: node.set(name, JSON_NODE_FACTORY.binaryNode((byte[]) value));
77:• } else if (value instanceof Date) {
78: try {
79: node.set(name, JSON_NODE_FACTORY.textNode(KapuaDateUtils.formatDate((Date) value)));
80: } catch (ParseException e) {
81: LOG.warn(
82: "The value of mapping {} of value {} is not compatible with type {}. Error: {}",
83: name,
84: value,
85: Date.class,
86: e.getMessage()
87: );
88: throw new InvalidValueMappingException(e, name, value, Date.class);
89: }
90:• } else if (value instanceof StorableId) {
91: node.set(name, JSON_NODE_FACTORY.textNode(value.toString()));
92:• } else if (value instanceof KapuaId) {
93: node.set(name, JSON_NODE_FACTORY.textNode(value.toString()));
94: } else {
95: throw new UnsupportedTypeMappingException(name, value);
96: }
97: }
98:
99: //
100: // New
101: //
102:
103: /**
104: * Instantiates a new {@link ArrayNode}.
105: *
106: * @return The newly instantiated {@link ArrayNode}.
107: * @since 1.3.0
108: */
109: public static ArrayNode newArrayNode() {
110: return JSON_NODE_FACTORY.arrayNode();
111: }
112:
113: /**
114: * Instantiates a new {@link ArrayNode}.
115: *
116: * @param fields The fields to add to the {@link ArrayNode}.
117: * @return The newly instantiated {@link ArrayNode}.
118: * @since 1.3.0
119: */
120: public static ArrayNode newArrayNode(Object[] fields) {
121: return newArrayNode(Arrays.asList(fields));
122: }
123:
124: /**
125: * Instantiates a new {@link ArrayNode}.
126: *
127: * @param collection The {@link Collection} to add to the {@link ArrayNode}.
128: * @return The newly instantiated {@link ArrayNode}.
129: * @since 1.3.0
130: */
131: public static ArrayNode newArrayNode(Collection<?> collection) {
132: ArrayNode arrayNode = newArrayNode();
133:
134: collection.stream().map(Object::toString).forEach(arrayNode::add);
135:
136: return arrayNode;
137: }
138:
139: /**
140: * Instantiates a new {@link ArrayNode}.
141: *
142: * @param storablePredicates The {@link Collection} of {@link StorablePredicate} to add to the {@link ArrayNode}.
143: * @return The newly instantiated {@link ArrayNode}.
144: * @since 1.3.0
145: */
146: public static ArrayNode newArrayNodeFromPredicates(Collection<StorablePredicate> storablePredicates) throws MappingException {
147: ArrayNode arrayNode = newArrayNode();
148:
149:• for (StorablePredicate predicate : storablePredicates) {
150: arrayNode.add(predicate.toSerializedMap());
151: }
152:
153: return arrayNode;
154: }
155:
156: /**
157: * Instantiates a new {@link ObjectNode}
158: *
159: * @return The newly instantiated {@link ObjectNode}
160: * @since 1.3.0
161: */
162: public static ObjectNode newObjectNode() {
163: return JSON_NODE_FACTORY.objectNode();
164: }
165:
166: /**
167: * Instantiates a new {@link ObjectNode} adding the given {@link KeyValueEntry}es.
168: *
169: * @param entries The {@link KeyValueEntry}es to be added.
170: * @return A newly instantiated {@link ObjectNode} with the {@link KeyValueEntry}es added.
171: * @throws MappingException if {@link #appendField(ObjectNode, String, Object)} {@code throws} {@link MappingException}
172: * @since 1.3.0
173: */
174: public static ObjectNode newObjectNode(KeyValueEntry[] entries) throws MappingException {
175: ObjectNode objectNode = newObjectNode();
176:
177:• for (KeyValueEntry entry : entries) {
178: appendField(objectNode, entry.getKey(), entry.getValue());
179: }
180:
181: return objectNode;
182: }
183:
184: /**
185: * Instantiates a new {@link ObjectNode} with the given name and value added as a field.
186: *
187: * @param name The name of the field to add.
188: * @param value The value of the field to add.
189: * @return A newly instantiated {@link ObjectNode} with the field added.
190: * @throws MappingException if {@link #appendField(ObjectNode, String, Object)} {@code throws} {@link MappingException}
191: * @since 1.3.0
192: */
193: public static ObjectNode newObjectNode(String name, Object value) throws MappingException {
194: ObjectNode objectNode = newObjectNode();
195:
196: appendField(objectNode, name, value);
197:
198: return objectNode;
199: }
200:
201: /**
202: * Instantiates a new {@link NumericNode} with the given value.
203: *
204: * @param number The value to add to the {@link NumericNode}.
205: * @return The The newly instantiated {@link NumericNode}
206: * @since 1.3.0
207: */
208: public static NumericNode newNumericNode(long number) {
209: return JSON_NODE_FACTORY.numberNode(number);
210: }
211:
212: /**
213: * Instantiates a new {@link TextNode} with the given text.
214: *
215: * @param text The text to add to the {@link NumericNode}.
216: * @return The The newly instantiated {@link NumericNode}
217: * @since 1.3.0
218: */
219: public static TextNode newTextNode(String text) {
220: return JSON_NODE_FACTORY.textNode(text);
221: }
222: }