11package io .zentity .model ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
34import com .fasterxml .jackson .databind .JsonNode ;
4- import com .fasterxml .jackson .databind .ObjectMapper ;
5+ import io .zentity .common .Json ;
6+ import io .zentity .common .Patterns ;
57
68import java .io .IOException ;
7- import java .util .*;
8- import java .util .regex .Pattern ;
9+ import java .util .Arrays ;
10+ import java .util .Iterator ;
11+ import java .util .Map ;
12+ import java .util .Set ;
13+ import java .util .TreeMap ;
14+ import java .util .TreeSet ;
915
1016public class Attribute {
1117
12- public static final Set <String > VALID_TYPES = new HashSet <>(
18+ public static final Set <String > VALID_TYPES = new TreeSet <>(
1319 Arrays .asList ("string" , "number" , "boolean" )
1420 );
15- private static final Pattern REGEX_EMPTY = Pattern .compile ("^\\ s*$" );
1621
1722 private final String name ;
23+ private Map <String , String > params = new TreeMap <>();
1824 private String type = "string" ;
1925
20- public Attribute (String name , JsonNode json ) throws ValidationException {
26+ public Attribute (String name , JsonNode json ) throws ValidationException , JsonProcessingException {
2127 validateName (name );
2228 this .name = name ;
2329 this .deserialize (json );
@@ -29,89 +35,14 @@ public Attribute(String name, String json) throws ValidationException, IOExcepti
2935 this .deserialize (json );
3036 }
3137
32- public static Boolean convertTypeBoolean (JsonNode value ) {
33- if (value .isNull ())
34- return null ;
35- return value .booleanValue ();
36- }
37-
38- public static Number convertTypeNumber (JsonNode value ) {
39- if (value .isNull ())
40- return null ;
41- else if (value .isIntegralNumber ())
42- return value .bigIntegerValue ();
43- else if (value .isFloatingPointNumber ())
44- return value .doubleValue ();
45- else
46- return value .numberValue ();
47- }
48-
49- public static String convertTypeString (JsonNode value ) {
50- if (value .isNull ())
51- return null ;
52- return value .textValue ();
53- }
54-
55- public static Object convertType (String targetType , JsonNode value ) throws ValidationException {
56- switch (targetType ) {
57- case "boolean" :
58- return convertTypeBoolean (value );
59- case "number" :
60- return convertTypeNumber (value );
61- case "string" :
62- return convertTypeString (value );
63- default :
64- throw new ValidationException ("'" + targetType + " is not a recognized attribute data type." );
65- }
66- }
67-
68- public static boolean isTypeBoolean (JsonNode value ) {
69- return value .isBoolean ();
70- }
71-
72- public static boolean isTypeNumber (JsonNode value ) {
73- return value .isNumber ();
74- }
75-
76- public static boolean isTypeString (JsonNode value ) {
77- return value .isTextual ();
78- }
79-
80- public static void validateTypeBoolean (JsonNode value ) throws ValidationException {
81- if (!isTypeBoolean (value ) && !value .isNull ())
82- throw new ValidationException ("Expected 'boolean' attribute data type." );
83- }
84-
85- public static void validateTypeNumber (JsonNode value ) throws ValidationException {
86- if (!isTypeNumber (value ) && !value .isNull ())
87- throw new ValidationException ("Expected 'number' attribute data type." );
88- }
89-
90- public static void validateTypeString (JsonNode value ) throws ValidationException {
91- if (!isTypeString (value ) && !value .isNull ())
92- throw new ValidationException ("Expected 'string' attribute data type." );
93- }
94-
95- public static void validateType (String expectedType , JsonNode value ) throws ValidationException {
96- switch (expectedType ) {
97- case "boolean" :
98- validateTypeBoolean (value );
99- break ;
100- case "number" :
101- validateTypeNumber (value );
102- break ;
103- case "string" :
104- validateTypeString (value );
105- break ;
106- default :
107- throw new ValidationException ("'" + expectedType + " is not a recognized attribute data type." );
108- }
109- }
110-
11138 public String name () {
11239 return this .name ;
11340 }
11441
42+ public Map <String , String > params () {
43+ return this .params ;
44+ }
45+
11546 public String type () {
11647 return this .type ;
11748 }
@@ -122,19 +53,45 @@ public void type(JsonNode value) throws ValidationException {
12253 }
12354
12455 private void validateName (String value ) throws ValidationException {
125- if (REGEX_EMPTY .matcher (value ).matches ())
56+ if (Patterns . EMPTY_STRING .matcher (value ).matches ())
12657 throw new ValidationException ("'attributes' has an attribute with empty name." );
12758 }
12859
60+ /**
61+ * Validate the value of "attributes".ATTRIBUTE_NAME."type".
62+ * Must be a non-empty string containing a recognized type.
63+ *
64+ * @param value The value of "attributes".ATTRIBUTE_NAME."type".
65+ * @throws ValidationException
66+ */
12967 private void validateType (JsonNode value ) throws ValidationException {
13068 if (!value .isTextual ())
13169 throw new ValidationException ("'attributes." + this .name + ".type' must be a string." );
132- if (REGEX_EMPTY .matcher (value .textValue ()).matches ())
70+ if (Patterns . EMPTY_STRING .matcher (value .textValue ()).matches ())
13371 throw new ValidationException ("'attributes." + this .name + ".type'' must not be empty." );
13472 if (!VALID_TYPES .contains (value .textValue ()))
135- throw new ValidationException ("'attributes." + this .name + ".type' has an unrecognized data type '" + value .textValue () + "'." );
73+ throw new ValidationException ("'attributes." + this .name + ".type' has an unrecognized type '" + value .textValue () + "'." );
74+ }
75+
76+ /**
77+ * Validate the value of "attributes".ATTRIBUTE_NAME."params".
78+ * Must be an object.
79+ *
80+ * @param value The value of "attributes".ATTRIBUTE_NAME."params".
81+ * @throws ValidationException
82+ */
83+ private void validateParams (JsonNode value ) throws ValidationException {
84+ if (!value .isObject ())
85+ throw new ValidationException ("'attributes." + this .name + ".params' must be an object." );
13686 }
13787
88+ /**
89+ * Validate the value of "attributes".ATTRIBUTE_NAME.
90+ * Must be an object.
91+ *
92+ * @param object The value of "attributes".ATTRIBUTE_NAME.
93+ * @throws ValidationException
94+ */
13895 private void validateObject (JsonNode object ) throws ValidationException {
13996 if (!object .isObject ())
14097 throw new ValidationException ("'attributes." + this .name + "' must be an object." );
@@ -151,8 +108,9 @@ private void validateObject(JsonNode object) throws ValidationException {
151108 *
152109 * @param json Attribute object of an entity model.
153110 * @throws ValidationException
111+ * @throws JsonProcessingException
154112 */
155- public void deserialize (JsonNode json ) throws ValidationException {
113+ public void deserialize (JsonNode json ) throws ValidationException , JsonProcessingException {
156114 validateObject (json );
157115
158116 // Validate and hold the state of fields.
@@ -165,14 +123,31 @@ public void deserialize(JsonNode json) throws ValidationException {
165123 case "type" :
166124 this .type (value );
167125 break ;
126+ case "params" :
127+ // Set any params that were specified in the input, with the values serialized as strings.
128+ if (!value .isObject ())
129+ throw new ValidationException ("'attributes." + this .name + ".params' must be an object." );
130+ Iterator <Map .Entry <String , JsonNode >> paramsNode = value .fields ();
131+ while (paramsNode .hasNext ()) {
132+ Map .Entry <String , JsonNode > paramNode = paramsNode .next ();
133+ String paramField = "params." + paramNode .getKey ();
134+ JsonNode paramValue = paramNode .getValue ();
135+ if (paramValue .isObject () || paramValue .isArray ())
136+ this .params ().put (paramField , Json .MAPPER .writeValueAsString (paramValue ));
137+ else if (paramValue .isNull ())
138+ this .params ().put (paramField , "null" );
139+ else
140+ this .params ().put (paramField , paramValue .asText ());
141+ }
142+ break ;
168143 default :
169144 throw new ValidationException ("'attributes." + this .name + "." + name + "' is not a recognized field." );
170145 }
171146 }
172147 }
173148
174149 public void deserialize (String json ) throws ValidationException , IOException {
175- deserialize (new ObjectMapper () .readTree (json ));
150+ deserialize (Json . MAPPER .readTree (json ));
176151 }
177152
178153}
0 commit comments