From c7abd712f9fbdb219a073e0265fc10eeaa36154e Mon Sep 17 00:00:00 2001 From: synapticloop Date: Sun, 19 Apr 2020 12:20:44 +1000 Subject: [PATCH] reformatted code, fixed H2Zero finder error --- build.gradle | 2 +- build.h2zero.mysql.gradle | 2 +- build.h2zero.sqlite3.gradle | 2 +- .../h2zero/base/model/ModelBase.java | 10 +++++++ .../h2zero/base/model/ModelBaseHelper.java | 26 +++++++++++++++++++ src/main/resources/java-create-model.templar | 3 +-- .../snippet/finder/user-finders.templar | 2 +- 7 files changed, 41 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 508f9f11..3513a2ac 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ plugins { group = 'synapticloop' archivesBaseName = 'h2zero' description = """lightweight ORM generator for mysql/sqlite, java with extensions for taglibs and routemaster""" -version = '4.2.5' +version = '4.2.6' tasks.withType(Javadoc).all { enabled = false } diff --git a/build.h2zero.mysql.gradle b/build.h2zero.mysql.gradle index 081c5738..e0a10e63 100644 --- a/build.h2zero.mysql.gradle +++ b/build.h2zero.mysql.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { - classpath 'synapticloop:h2zero:4.2.5' + classpath 'synapticloop:h2zero:4.2.6' // classpath 'synapticloop:h2zero-extension-taglibs:1.0.0' } } diff --git a/build.h2zero.sqlite3.gradle b/build.h2zero.sqlite3.gradle index 30d8f379..69df0cb1 100644 --- a/build.h2zero.sqlite3.gradle +++ b/build.h2zero.sqlite3.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { - classpath 'synapticloop:h2zero:4.1.1' + classpath 'synapticloop:h2zero:4.2.6' classpath 'synapticloop:h2zero-extension-taglibs:1.0.0' } } diff --git a/src/main/java/synapticloop/h2zero/base/model/ModelBase.java b/src/main/java/synapticloop/h2zero/base/model/ModelBase.java index 49f47938..655b2727 100755 --- a/src/main/java/synapticloop/h2zero/base/model/ModelBase.java +++ b/src/main/java/synapticloop/h2zero/base/model/ModelBase.java @@ -608,4 +608,14 @@ protected boolean isDifferent(Object from, Object to) { protected void addtoJSONObject(JSONObject jsonObject, String key, Object object) { jsonObject.put(key, object); } + + public JSONObject getToJSON() { + return(toJSON()); + } + + public abstract JSONObject toJSON(); + + public abstract String toJsonString(); + + public abstract String getJsonString(); } diff --git a/src/main/java/synapticloop/h2zero/base/model/ModelBaseHelper.java b/src/main/java/synapticloop/h2zero/base/model/ModelBaseHelper.java index ec16087d..d9544b7e 100644 --- a/src/main/java/synapticloop/h2zero/base/model/ModelBaseHelper.java +++ b/src/main/java/synapticloop/h2zero/base/model/ModelBaseHelper.java @@ -1,9 +1,35 @@ package synapticloop.h2zero.base.model; +import java.util.List; + +import org.json.JSONArray; import org.json.JSONObject; public class ModelBaseHelper { + /** + * Add an object to the JSON object + * + * @param jsonObject The JSON object to add the object to + * @param key the key to add to the JSON object + * @param object the object to add + */ public static void addtoJSONObject(JSONObject jsonObject, String key, Object object) { jsonObject.put(key, object); } + + public static JSONArray getJSONArrayResponse(List list) { + JSONArray jsonArray = new JSONArray(); + for (Object object : list) { + jsonArray.put(((ModelBase)object).toJSON()); + } + return(jsonArray); + } + + public static JSONObject getJSONResponse(Object object) { + if(object instanceof List) { + getJSONArrayResponse((List)object); + } + return(((ModelBase)object).getToJSON()); + } + } diff --git a/src/main/resources/java-create-model.templar b/src/main/resources/java-create-model.templar index ed3890ca..737de7e6 100755 --- a/src/main/resources/java-create-model.templar +++ b/src/main/resources/java-create-model.templar @@ -168,7 +168,6 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase{endif {\n}// Static lookups for fields in the hit counter.{\n} {\t}public static final int HIT_TOTAL = 0;{\n} - {\t}public static final int HIT_PRIMARY_KEY = 0;{\n} {loop table.fields as field} {\t}public static final int HIT_{field.upperName} = {fieldStatus.index};{\n} {endloop}{\n} @@ -447,7 +446,7 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase{endif {\t}{\t}} catch (SQLException sqlex) {{{\n} {\t}{\t}{\t}throw sqlex;{\n} {\t}{\t}} finally {{{\n} - {\t}{\t}this.isHydrated = true; + {\t}{\t}this.isHydrated = true;{\n} {\t}{\t}{\t}ConnectionManager.closeAll(resultSet, preparedStatement);{\n} {\t}{\t}}{\n} {\t}}{\n} diff --git a/src/main/resources/snippet/finder/user-finders.templar b/src/main/resources/snippet/finder/user-finders.templar index 696ecf4e..145f324d 100644 --- a/src/main/resources/snippet/finder/user-finders.templar +++ b/src/main/resources/snippet/finder/user-finders.templar @@ -188,7 +188,7 @@ {\t}{\t}}{\n} {\n} {\n} - {\t}{\t}if(null == result{if !finder.unique}s{endif}) {{{\n} + {\t}{\t}if(null == result{if !finder.unique}s || results.size() == 0{endif}) {{{\n} {\t}{\t}{\t}throw new H2ZeroFinderException("Could not find result.");{\n} {\t}{\t}}{\n} {\t}{\t}return(result{if !finder.unique}s{endif});{\n}