Skip to content

Commit 9fc7f52

Browse files
committed
Improved support for models that implement "java.security.Principal", switched default JTS to "org.locationtech.jts", and added support for arrays in the Model class
1 parent 64b95ea commit 9fc7f52

File tree

1 file changed

+91
-35
lines changed

1 file changed

+91
-35
lines changed

Diff for: src/javaxt/orm/Model.java

+91-35
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,34 @@ public String getJavaCode(){
240240
TreeSet<String> includes = new TreeSet<>();
241241

242242

243+
244+
//Special case for models that implement java.security.Principal
245+
boolean hasNameField = false;
246+
String getSecurityPrincipalName = "";
247+
if (implementations.contains("java.security.Principal")){
248+
249+
//Check if fields have a name field
250+
for (Field field : fields){
251+
if (field.getName().equalsIgnoreCase("name")){
252+
hasNameField = true;
253+
break;
254+
}
255+
}
256+
257+
//If no name field is found, we'll need to add a public getName()
258+
if (!hasNameField){
259+
getSecurityPrincipalName += " public String getName(){\r\n";
260+
getSecurityPrincipalName += " return null;\r\n";
261+
getSecurityPrincipalName += " }\r\n\r\n";
262+
}
263+
}
264+
265+
266+
267+
//Loop through all the fields and generate java code
243268
for (int i=0; i<fields.size(); i++){
244269
Field field = fields.get(i);
245-
String fieldName = field.getName();
270+
String fieldName = Utils.underscoreToCamelCase(field.getName());
246271
String fieldType = field.getType();
247272
String methodName = Utils.capitalize(fieldName);
248273
String columnName = field.getColumnName();
@@ -257,7 +282,7 @@ public String getJavaCode(){
257282
if (fieldType.equals("BigDecimal")) includes.add("java.math.BigDecimal");
258283
if (fieldType.equals("Geometry")){
259284
String jts = options.get("jts");
260-
if (jts==null) jts = "com.vividsolutions.jts";
285+
if (jts==null) jts = "org.locationtech.jts"; //vs "com.vividsolutions.jts";
261286
includes.add(jts+".geom.Geometry");
262287
includes.add(jts+".io.WKTReader");
263288
}
@@ -338,25 +363,14 @@ public String getJavaCode(){
338363
publicMembers.append(" }\r\n\r\n");
339364
}
340365

341-
//Add extra method if username and implements java.security.Principal
342-
if (fieldName.equals("username") && fieldType.equals("String") &&
343-
implementations.contains("java.security.Principal")){
344-
345-
boolean addMethod = true;
346-
for (Field f : fields){
347-
if (f.getName().equals("name")){
348-
addMethod = false;
349-
break;
350-
}
351-
}
352366

353-
if (addMethod){
354-
publicMembers.append(" public String getName(){\r\n");
355-
publicMembers.append(" return ");
356-
publicMembers.append(fieldName);
357-
publicMembers.append(";\r\n");
358-
publicMembers.append(" }\r\n\r\n");
359-
}
367+
//Special case for models that implement java.security.Principal
368+
//If there's no name field, use the username as a return value
369+
//for the getName() method
370+
if (fieldName.equals("username") && fieldType.equals("String") &&
371+
implementations.contains("java.security.Principal") &&
372+
!hasNameField){
373+
getSecurityPrincipalName = getSecurityPrincipalName.replace("null", fieldName);
360374
}
361375

362376
}
@@ -466,13 +480,33 @@ else if (fieldType.equals("Geometry")){
466480
getValues.append("\").toString());}catch(Exception e){}\r\n");
467481
}
468482
else{
469-
getValues.append(" this.");
470-
getValues.append(fieldName);
471-
getValues.append(" = getValue(rs, \"");
472-
getValues.append(columnName);
473-
getValues.append("\").to");
474-
getValues.append(password ? "String" : fieldType);
475-
getValues.append("();\r\n");
483+
484+
if (fieldType.endsWith("[]")){ //e.g. String[]
485+
getValues.append(" {");
486+
487+
//Object[] v = (Object[])getValue(rs, "recent_customers").toArray();
488+
getValues.append("Object[] v = (Object[]) getValue(rs, \"");
489+
getValues.append(columnName);
490+
getValues.append("\").toArray();\r\n");
491+
492+
//this.recentCustomers = java.util.Arrays.copyOf(v, v.length, String[].class);
493+
getValues.append(" this.");
494+
getValues.append(fieldName);
495+
getValues.append(" = v==null ? null : java.util.Arrays.copyOf(v, v.length, ");
496+
getValues.append(fieldType);
497+
getValues.append(".class);");
498+
499+
getValues.append("}\r\n");
500+
}
501+
else{
502+
getValues.append(" this.");
503+
getValues.append(fieldName);
504+
getValues.append(" = getValue(rs, \"");
505+
getValues.append(columnName);
506+
getValues.append("\").to");
507+
getValues.append(password ? "String" : fieldType);
508+
getValues.append("();\r\n");
509+
}
476510
}
477511
}
478512
else{
@@ -571,13 +605,35 @@ else if (fieldType.equals("byte[]")){
571605
getJson.append("\").toByteArray();\r\n");
572606
}
573607
else{
574-
getJson.append(" this.");
575-
getJson.append(fieldName);
576-
getJson.append(" = json.get(\"");
577-
getJson.append(fieldName);
578-
getJson.append("\").to");
579-
getJson.append(fieldType);
580-
getJson.append("();\r\n");
608+
609+
if (fieldType.endsWith("[]")){ //e.g. String[]
610+
getJson.append(" {");
611+
612+
//Object[] v = json.has("recentCustomers") ? json.get("recentCustomers").toJSONArray().toArray() : null;
613+
getJson.append("Object[] v = json.has(\"");
614+
getJson.append(fieldName);
615+
getJson.append("\") ? json.get(\"");
616+
getJson.append(fieldName);
617+
getJson.append("\").toJSONArray().toArray() : null;\r\n");
618+
619+
//this.recentCustomers = java.util.Arrays.copyOf(v, v.length, String[].class);
620+
getJson.append(" this.");
621+
getJson.append(fieldName);
622+
getJson.append(" = v==null ? null : java.util.Arrays.copyOf(v, v.length, ");
623+
getJson.append(fieldType);
624+
getJson.append(".class);");
625+
626+
getJson.append("}\r\n");
627+
}
628+
else{
629+
getJson.append(" this.");
630+
getJson.append(fieldName);
631+
getJson.append(" = json.get(\"");
632+
getJson.append(fieldName);
633+
getJson.append("\").to");
634+
getJson.append(fieldType);
635+
getJson.append("();\r\n");
636+
}
581637
}
582638
}
583639
else{
@@ -689,7 +745,7 @@ else if (fieldType.equals("byte[]")){
689745
str = str.replace("${field[0]}", fields.get(0).getColumnName());
690746
str = str.replace("${initArrays}", initArrays.toString().trim());
691747
str = str.replace("${privateFields}", privateFields.toString().trim());
692-
str = str.replace("${publicMembers}", publicMembers.toString().trim());
748+
str = str.replace("${publicMembers}", (getSecurityPrincipalName + publicMembers.toString()).trim());
693749
str = str.replace("${getModels}", getModels.toString());
694750
str = str.replace("${getValues}", getValues.toString());
695751
str = str.replace("${getJson}", getJson.toString().trim());

0 commit comments

Comments
 (0)