@@ -61,17 +61,19 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
61
61
62
62
//Parse fields
63
63
JSONArray arr = modelInfo .get ("fields" ).toJSONArray ();
64
- if (arr !=null )
65
- for (int i =0 ; i <arr .length (); i ++){
66
- JSONObject json = arr .get (i ).toJSONObject ();
67
- Field field = new Field (json .get ("name" ).toString (), json .get ("type" ).toString ());
68
-
69
-
70
- //Don't add ID field. It is added automatically.
71
- if (field .getName ().equalsIgnoreCase ("id" )) continue ;
72
-
73
-
74
- fields .add (field );
64
+ if (arr !=null ){
65
+ for (JSONValue f : arr ){
66
+ String name = f .get ("name" ).toString ();
67
+ String type = f .get ("type" ).toString ();
68
+
69
+ //Don't add ID field. It is added automatically.
70
+ if (name .equalsIgnoreCase ("id" )) continue ;
71
+
72
+ //Create field and update the fields array
73
+ Field field = new Field (name , type );
74
+ addConstraints (field , f .toJSONObject ());
75
+ this .fields .add (field );
76
+ }
75
77
}
76
78
77
79
@@ -85,58 +87,86 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
85
87
}
86
88
87
89
88
-
89
90
//Parse constraints
90
91
JSONArray constraints = modelInfo .get ("constraints" ).toJSONArray ();
91
- if (constraints !=null )
92
- for (int i =0 ; i <constraints .length (); i ++){
93
- JSONObject constraint = constraints .get (i ).toJSONObject ();
94
- String fieldName = constraint .get ("name" ).toString ();
95
- Boolean isRequired = constraint .get ("required" ).toBoolean ();
96
- if (isRequired ==null ) isRequired = constraint .get ("nullable" ).toBoolean ();
97
- Boolean isUnique = constraint .get ("unique" ).toBoolean ();
98
- Integer length = constraint .get ("length" ).toInteger ();
99
- if (length ==null ) length = constraint .get ("size" ).toInteger ();
100
- Integer srid = constraint .get ("srid" ).toInteger ();
101
-
102
- for (Field field : fields ){
103
- if (field .getName ().equals (fieldName )){
104
- if (isRequired !=null ) field .isRequired (isRequired );
105
- if (isUnique !=null ) field .isUnique (isUnique );
106
- if (length !=null ) field .setLength (length );
107
- if (srid !=null ) field .setSRID (srid );
108
-
109
- ForeignKey foreignKey = field .getForeignKey ();
110
- if (foreignKey !=null ){
111
- if (constraint .has ("onDelete" )){
112
- foreignKey .onDelete (constraint .get ("onDelete" ).toString ());
113
- }
92
+ if (constraints !=null ){
93
+ for (JSONValue constraint : constraints ){
94
+ String fieldName = constraint .get ("name" ).toString ();
95
+
96
+ for (Field field : fields ){
97
+ if (field .getName ().equals (fieldName )){
98
+ addConstraints (field , constraint .toJSONObject ());
99
+ break ;
114
100
}
115
-
116
- break ;
117
101
}
118
102
}
119
103
}
120
104
121
105
122
106
//Parse default values
123
107
JSONArray defaultValues = modelInfo .get ("defaults" ).toJSONArray ();
124
- if (defaultValues !=null )
125
- for (int i =0 ; i <defaultValues .length (); i ++){
126
- JSONObject json = defaultValues .get (i ).toJSONObject ();
127
- String fieldName = json .get ("name" ).toString ();
128
- Object val = json .get ("value" ).toObject ();
129
-
130
- for (Field field : fields ){
131
- if (field .getName ().equals (fieldName )){
132
- field .setDefaultValue (val );
133
- break ;
108
+ if (defaultValues !=null ){
109
+ for (JSONValue d : defaultValues ){
110
+ String fieldName = d .get ("name" ).toString ();
111
+
112
+ for (Field field : fields ){
113
+ if (field .getName ().equals (fieldName )){
114
+ field .setDefaultValue (d .get ("value" ).toObject ());
115
+ break ;
116
+ }
134
117
}
135
118
}
136
119
}
137
120
}
138
121
139
122
123
+ //**************************************************************************
124
+ //** addConstraints
125
+ //**************************************************************************
126
+ /** Used to add constraints to a given field. Constraints can be defined in
127
+ * a "constraints" array or inline with the field definition. If both are
128
+ * present, the constraints in the "constraints" array may override
129
+ * constraints defined inline.
130
+ */
131
+ private void addConstraints (Field field , JSONObject constraint ){
132
+
133
+ Boolean isRequired = constraint .get ("required" ).toBoolean ();
134
+ if (isRequired ==null ) isRequired = constraint .get ("nullable" ).toBoolean ();
135
+ Boolean isUnique = constraint .get ("unique" ).toBoolean ();
136
+ Integer length = constraint .get ("length" ).toInteger ();
137
+ if (length ==null ) length = constraint .get ("size" ).toInteger ();
138
+ Integer srid = constraint .get ("srid" ).toInteger ();
139
+
140
+
141
+ if (isRequired !=null ) field .isRequired (isRequired );
142
+ if (isUnique !=null ) field .isUnique (isUnique );
143
+ if (length !=null ) field .setLength (length );
144
+ if (srid !=null ) field .setSRID (srid );
145
+
146
+
147
+ ForeignKey foreignKey = field .getForeignKey ();
148
+ if (foreignKey !=null ){
149
+ String onDelete = constraint .get ("onDelete" ).toString ();
150
+
151
+ //Special case for models. Cascade delete by default
152
+ if (onDelete ==null && field .isModel ()){
153
+ onDelete = "cascade" ;
154
+ }
155
+
156
+ if (onDelete !=null ){
157
+ foreignKey .onDelete (onDelete );
158
+ }
159
+ }
160
+
161
+
162
+ if (constraint .has ("default" )){
163
+ Object defaultValue = constraint .get ("default" ).toObject ();
164
+ field .setDefaultValue (defaultValue );
165
+ }
166
+
167
+ }
168
+
169
+
140
170
//**************************************************************************
141
171
//** getName
142
172
//**************************************************************************
0 commit comments