Skip to content

Commit

Permalink
Improved support for models that implement "java.security.Principal",…
Browse files Browse the repository at this point in the history
… switched default JTS to "org.locationtech.jts", and added support for arrays in the Model class
  • Loading branch information
pborissow committed Mar 26, 2024
1 parent 64b95ea commit 9fc7f52
Showing 1 changed file with 91 additions and 35 deletions.
126 changes: 91 additions & 35 deletions src/javaxt/orm/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,34 @@ public String getJavaCode(){
TreeSet<String> includes = new TreeSet<>();



//Special case for models that implement java.security.Principal
boolean hasNameField = false;
String getSecurityPrincipalName = "";
if (implementations.contains("java.security.Principal")){

//Check if fields have a name field
for (Field field : fields){
if (field.getName().equalsIgnoreCase("name")){
hasNameField = true;
break;
}
}

//If no name field is found, we'll need to add a public getName()
if (!hasNameField){
getSecurityPrincipalName += " public String getName(){\r\n";
getSecurityPrincipalName += " return null;\r\n";
getSecurityPrincipalName += " }\r\n\r\n";
}
}



//Loop through all the fields and generate java code
for (int i=0; i<fields.size(); i++){
Field field = fields.get(i);
String fieldName = field.getName();
String fieldName = Utils.underscoreToCamelCase(field.getName());
String fieldType = field.getType();
String methodName = Utils.capitalize(fieldName);
String columnName = field.getColumnName();
Expand All @@ -257,7 +282,7 @@ public String getJavaCode(){
if (fieldType.equals("BigDecimal")) includes.add("java.math.BigDecimal");
if (fieldType.equals("Geometry")){
String jts = options.get("jts");
if (jts==null) jts = "com.vividsolutions.jts";
if (jts==null) jts = "org.locationtech.jts"; //vs "com.vividsolutions.jts";
includes.add(jts+".geom.Geometry");
includes.add(jts+".io.WKTReader");
}
Expand Down Expand Up @@ -338,25 +363,14 @@ public String getJavaCode(){
publicMembers.append(" }\r\n\r\n");
}

//Add extra method if username and implements java.security.Principal
if (fieldName.equals("username") && fieldType.equals("String") &&
implementations.contains("java.security.Principal")){

boolean addMethod = true;
for (Field f : fields){
if (f.getName().equals("name")){
addMethod = false;
break;
}
}

if (addMethod){
publicMembers.append(" public String getName(){\r\n");
publicMembers.append(" return ");
publicMembers.append(fieldName);
publicMembers.append(";\r\n");
publicMembers.append(" }\r\n\r\n");
}
//Special case for models that implement java.security.Principal
//If there's no name field, use the username as a return value
//for the getName() method
if (fieldName.equals("username") && fieldType.equals("String") &&
implementations.contains("java.security.Principal") &&
!hasNameField){
getSecurityPrincipalName = getSecurityPrincipalName.replace("null", fieldName);
}

}
Expand Down Expand Up @@ -466,13 +480,33 @@ else if (fieldType.equals("Geometry")){
getValues.append("\").toString());}catch(Exception e){}\r\n");
}
else{
getValues.append(" this.");
getValues.append(fieldName);
getValues.append(" = getValue(rs, \"");
getValues.append(columnName);
getValues.append("\").to");
getValues.append(password ? "String" : fieldType);
getValues.append("();\r\n");

if (fieldType.endsWith("[]")){ //e.g. String[]
getValues.append(" {");

//Object[] v = (Object[])getValue(rs, "recent_customers").toArray();
getValues.append("Object[] v = (Object[]) getValue(rs, \"");
getValues.append(columnName);
getValues.append("\").toArray();\r\n");

//this.recentCustomers = java.util.Arrays.copyOf(v, v.length, String[].class);
getValues.append(" this.");
getValues.append(fieldName);
getValues.append(" = v==null ? null : java.util.Arrays.copyOf(v, v.length, ");
getValues.append(fieldType);
getValues.append(".class);");

getValues.append("}\r\n");
}
else{
getValues.append(" this.");
getValues.append(fieldName);
getValues.append(" = getValue(rs, \"");
getValues.append(columnName);
getValues.append("\").to");
getValues.append(password ? "String" : fieldType);
getValues.append("();\r\n");
}
}
}
else{
Expand Down Expand Up @@ -571,13 +605,35 @@ else if (fieldType.equals("byte[]")){
getJson.append("\").toByteArray();\r\n");
}
else{
getJson.append(" this.");
getJson.append(fieldName);
getJson.append(" = json.get(\"");
getJson.append(fieldName);
getJson.append("\").to");
getJson.append(fieldType);
getJson.append("();\r\n");

if (fieldType.endsWith("[]")){ //e.g. String[]
getJson.append(" {");

//Object[] v = json.has("recentCustomers") ? json.get("recentCustomers").toJSONArray().toArray() : null;
getJson.append("Object[] v = json.has(\"");
getJson.append(fieldName);
getJson.append("\") ? json.get(\"");
getJson.append(fieldName);
getJson.append("\").toJSONArray().toArray() : null;\r\n");

//this.recentCustomers = java.util.Arrays.copyOf(v, v.length, String[].class);
getJson.append(" this.");
getJson.append(fieldName);
getJson.append(" = v==null ? null : java.util.Arrays.copyOf(v, v.length, ");
getJson.append(fieldType);
getJson.append(".class);");

getJson.append("}\r\n");
}
else{
getJson.append(" this.");
getJson.append(fieldName);
getJson.append(" = json.get(\"");
getJson.append(fieldName);
getJson.append("\").to");
getJson.append(fieldType);
getJson.append("();\r\n");
}
}
}
else{
Expand Down Expand Up @@ -689,7 +745,7 @@ else if (fieldType.equals("byte[]")){
str = str.replace("${field[0]}", fields.get(0).getColumnName());
str = str.replace("${initArrays}", initArrays.toString().trim());
str = str.replace("${privateFields}", privateFields.toString().trim());
str = str.replace("${publicMembers}", publicMembers.toString().trim());
str = str.replace("${publicMembers}", (getSecurityPrincipalName + publicMembers.toString()).trim());
str = str.replace("${getModels}", getModels.toString());
str = str.replace("${getValues}", getValues.toString());
str = str.replace("${getJson}", getJson.toString().trim());
Expand Down

0 comments on commit 9fc7f52

Please sign in to comment.