Skip to content

Commit aa60c94

Browse files
committed
- Added support for "geometry" types (vs "geo")
- Added ability to specify SRID for both geometry and geo types - Added option to specify JTS version (e.g. "com.vividsolutions.jts" vs "org.locationtech.jts") - Added try/catch block around the WKT Parser - Updated default name of diamond tables git-svn-id: svn://192.168.0.80/JavaXT/javaxt-orm@1329 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 9b4abf1 commit aa60c94

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

src/javaxt/orm/Field.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ else if (type.equalsIgnoreCase("json")){
6868
}
6969
else if (type.equalsIgnoreCase("geo")){
7070
type = "Geometry";
71-
columnType = "geometry(Geometry,4326)"; //PostgreSQL specific
71+
columnType = "geometry(Geometry,4326)";
72+
}
73+
else if (type.equalsIgnoreCase("geometry")){
74+
type = "Geometry";
75+
columnType = "geometry(GeometryZ)";
7276
}
7377
else if (type.equalsIgnoreCase("password")){
7478
//type = "String";
@@ -185,4 +189,15 @@ public void setLength(int length){
185189
}
186190
}
187191
}
192+
193+
194+
public void setSRID(Integer srid){
195+
if (type.equalsIgnoreCase("Geometry")){
196+
boolean hasZ = columnType.contains("Z");
197+
columnType = "geometry(Geometry";
198+
if (hasZ) columnType+="Z";
199+
if (srid!=null) columnType+=","+srid;
200+
columnType+=")";
201+
}
202+
}
188203
}

src/javaxt/orm/Model.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javaxt.orm;
22
import javaxt.json.*;
3+
import java.util.HashMap;
34

45
//******************************************************************************
56
//** Model Class
@@ -19,20 +20,21 @@ public class Model {
1920
private String packageName;
2021
private String schemaName;
2122
private String escapedSchemaName;
23+
private HashMap<String, String> options;
2224

2325

2426
//**************************************************************************
2527
//** Constructor
2628
//**************************************************************************
2729
/** Creates a new instance of this class.
2830
*/
29-
protected Model(String modelName, String packageName, String schemaName, JSONObject modelInfo){
31+
protected Model(String modelName, JSONObject modelInfo, String packageName, HashMap<String, String> options){
3032
this.name = modelName;
3133
this.fields = new java.util.ArrayList<Field>();
32-
34+
this.options = options;
3335
this.packageName = packageName;
3436
this.tableName = Utils.camelCaseToUnderScore(name).toLowerCase();
35-
this.schemaName = schemaName;
37+
this.schemaName = options.get("schema");
3638

3739

3840
if (schemaName==null){
@@ -83,13 +85,14 @@ protected Model(String modelName, String packageName, String schemaName, JSONObj
8385
Boolean isUnique = constraint.get("unique").toBoolean();
8486
Integer length = constraint.get("length").toInteger();
8587
if (length==null) length = constraint.get("size").toInteger();
88+
Integer srid = constraint.get("srid").toInteger();
8689

8790
for (Field field : fields){
8891
if (field.getName().equals(fieldName)){
8992
if (isRequired!=null) field.isRequired(isRequired);
9093
if (isUnique!=null) field.isUnique(isUnique);
9194
if (length!=null) field.setLength(length);
92-
95+
if (srid!=null) field.setSRID(srid);
9396

9497
ForeignKey foreignKey = field.getForeignKey();
9598
if (foreignKey!=null){
@@ -207,8 +210,10 @@ public String getJavaCode(){
207210
if (fieldType.equals("Date")) includes.add("javaxt.utils.Date");
208211
if (fieldType.equals("BigDecimal")) includes.add("java.math.BigDecimal");
209212
if (fieldType.equals("Geometry")){
210-
includes.add("com.vividsolutions.jts.geom.Geometry");
211-
includes.add("com.vividsolutions.jts.io.WKTReader");
213+
String jts = options.get("jts");
214+
if (jts==null) jts = "com.vividsolutions.jts";
215+
includes.add(jts+".geom.Geometry");
216+
includes.add(jts+".io.WKTReader");
212217
}
213218

214219

@@ -348,11 +353,11 @@ else if (fieldType.equals("byte[]")){
348353
getValues.append("\").toByteArray();\r\n");
349354
}
350355
else if (fieldType.equals("Geometry")){
351-
getValues.append(" this.");
356+
getValues.append(" try{this.");
352357
getValues.append(fieldName);
353358
getValues.append(" = new WKTReader().read(getValue(rs, \"");
354359
getValues.append(columnName);
355-
getValues.append("\").toString());\r\n");
360+
getValues.append("\").toString());}catch(Exception e){}\r\n");
356361
}
357362
else{
358363
getValues.append(" this.");
@@ -773,8 +778,8 @@ public String getDiamondTableSQL(){
773778
if (leftTable.equals(rightTable)) rightColumn = rightTable + "_ID2";
774779

775780

776-
//String tableName = leftTable + "_" + rightTable;
777-
String tableName = leftTable + "_" + field.getColumnName().toUpperCase();
781+
String tableName = leftTable + "_" + rightTable;
782+
//String tableName = leftTable + "_" + field.getColumnName().toUpperCase();
778783

779784

780785
String foreignKey = "FK_" + tableName;

src/javaxt/orm/Parser.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import javax.script.*;
33
import jdk.nashorn.api.scripting.ScriptObjectMirror;
44
import javaxt.json.*;
5+
import java.util.HashMap;
56

67
//******************************************************************************
78
//** Parser Class
@@ -13,9 +14,8 @@
1314

1415
public class Parser {
1516

16-
private String packageName;
17-
private String schemaName;
1817
private Model[] models;
18+
private static String[] optionalVars = new String[]{"schema", "jts"};
1919

2020

2121
//**************************************************************************
@@ -65,13 +65,17 @@ public Model[] getModels(){
6565
/** Used to parse a json document containing models and a package name.
6666
*/
6767
private void init(JSONObject json){
68-
packageName = json.get("package").toString();
69-
schemaName = json.get("schema").toString();
68+
String packageName = json.get("package").toString();
7069
JSONObject models = json.get("models").toJSONObject();
70+
HashMap<String, String> options = new HashMap<String, String>();
71+
for (String key : optionalVars){
72+
String val = json.get(key).toString();
73+
if (val!=null) options.put(key, val);
74+
}
7175

7276
java.util.ArrayList<Model> arr = new java.util.ArrayList<Model>();
7377
for (String modelName : models.keySet()){
74-
Model model = new Model(modelName, packageName, schemaName, models.get(modelName).toJSONObject());
78+
Model model = new Model(modelName, models.get(modelName).toJSONObject(), packageName, options);
7579
arr.add(model);
7680
}
7781

@@ -98,25 +102,28 @@ private JSONObject parseJavaScript(String js) throws Exception {
98102
ScriptContext ctx = new SimpleScriptContext();
99103
ctx.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
100104
engine.eval(js, ctx);
101-
Object packageName = ctx.getAttribute("package");
102-
Object schemaName = ctx.getAttribute("schema");
103-
Object models = ctx.getAttribute("models");
104105

105106

106-
//Add package to output
107+
//Add package name to output
108+
Object packageName = ctx.getAttribute("package");
107109
output.set("package", packageName.toString());
108110

109111

110-
//Add package to output
111-
if (schemaName!=null) output.set("schema", schemaName.toString());
112-
112+
//Add options to output
113+
for (String key : optionalVars){
114+
Object schemaName = ctx.getAttribute(key);
115+
if (schemaName!=null) output.set(key, schemaName.toString());
116+
}
117+
113118

114119
//Stringify models and convert to json
120+
Object models = ctx.getAttribute("models");
115121
ScriptObjectMirror json = (ScriptObjectMirror) engine.eval("JSON");
116122
String str = json.callMember("stringify", models).toString();
117123
output.set("models", new JSONObject(str));
118124

119125

126+
120127
//Return JSON
121128
return output;
122129
}

0 commit comments

Comments
 (0)