Skip to content

Commit 1a20320

Browse files
author
synapticloop
committed
removed secure fields, refactored JSON
1 parent 7cc178b commit 1a20320

File tree

13 files changed

+90
-305
lines changed

13 files changed

+90
-305
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ plugins {
2121
group = 'synapticloop'
2222
archivesBaseName = 'h2zero'
2323
description = """lightweight ORM generator for mysql/sqlite, java with extensions for taglibs and routemaster"""
24-
version = '4.2.4'
24+
version = '4.2.5'
2525

2626
tasks.withType(Javadoc).all { enabled = false }
2727

build.h2zero.mysql.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
}
1010

1111
dependencies {
12-
classpath 'synapticloop:h2zero:4.2.3'
12+
classpath 'synapticloop:h2zero:4.2.5'
1313
// classpath 'synapticloop:h2zero-extension-taglibs:1.0.0'
1414
}
1515
}

src/main/java/synapticloop/h2zero/base/model/ModelBase.java

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.sql.Connection;
2121
import java.sql.SQLException;
2222

23+
import org.json.JSONObject;
2324
import org.slf4j.Logger;
2425
import org.slf4j.LoggerFactory;
2526

@@ -603,4 +604,8 @@ protected boolean isDifferent(Object from, Object to) {
603604
}
604605
}
605606
}
607+
608+
protected void addtoJSONObject(JSONObject jsonObject, String key, Object object) {
609+
jsonObject.put(key, object);
610+
}
606611
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package synapticloop.h2zero.base.model;
2+
3+
import org.json.JSONObject;
4+
5+
public class ModelBaseHelper {
6+
public static void addtoJSONObject(JSONObject jsonObject, String key, Object object) {
7+
jsonObject.put(key, object);
8+
}
9+
}

src/main/resources/java-create-model.templar

+10-22
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ package {database.package}.model;{\n}{\n}
3131
import java.sql.Statement;{\n}
3232
import java.sql.PreparedStatement;{\n}
3333
import java.sql.SQLException;{\n}
34-
{\n}
3534
{else}
3635
import java.util.HashMap;{\n}{\n}
3736
import java.util.Map;{\n}{\n}
37+
3838
{endif}
3939

4040
{\n}
4141
import org.json.JSONObject;{\n}
4242
{\n}
4343

44+
import synapticloop.h2zero.base.model.ModelBaseHelper;{\n}
4445
import {database.package}.model.util.Constants;{\n}
4546

4647
{if !table.isConstant}
@@ -62,7 +63,7 @@ import {database.package}.model.util.Constants;{\n}
6263

6364
{\n}{\n}
6465

65-
public class {table.javaClassName} {if !table.isConstant}extends ModelBase {endif}{{{\n}
66+
public class {table.javaClassName} {if !table.isConstant}extends ModelBase{endif} {{{\n}
6667
{\t}// the binder is unused in code, but will generate compile problems if this {\n}
6768
{\t}// class is no longer referenced in the h2zero file. Just a nicety for{\n}
6869
{\t}// removing dead code{\n}
@@ -615,19 +616,17 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase {endi
615616
{\t}{\t}return(toJSON());{\n}
616617
{\t}}{\n}
617618

618-
{-- Now we need the toJsonString Method--}
619+
{-- Now we need the toJson Method--}
619620
{\n}
620621
{\t}public JSONObject toJSON() {{{\n}
621-
{\t}{\t}String nullString = null;{\n}{\n}
622-
623622
{\t}{\t}JSONObject jsonObject = new JSONObject();{\n}
623+
{\n}
624624
{\t}{\t}jsonObject.put("type", "{table.javaClassName}");{\n}
625+
{\n}
625626
{loop table.fields as field}
626-
{\t}{\t}if(null == this.{field.javaName}) {{{\n}
627-
{\t}{\t}{\t}jsonObject.put("{field.javaName}", nullString);{\n}
628-
{\t}{\t}} else {{{\n}
629-
{\t}{\t}{\t}jsonObject.put("{field.javaName}", this.{field.javaName});{\n}
630-
{\t}{\t}}{\n}
627+
{if !field.isSecure}
628+
{\t}{\t}ModelBaseHelper.addtoJSONObject(jsonObject, "{field.javaName}", this.get{field.javaAccessorName}());{\n}
629+
{endif}
631630
{endloop}
632631
{\t}{\t}return(jsonObject);{\n}
633632
{\t}}{\n}
@@ -637,18 +636,7 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase {endi
637636
{-- Now we need the toJsonString Method--}
638637
{\n}
639638
{\t}public String toJsonString() {{{\n}
640-
{\t}{\t}String nullString = null;{\n}{\n}
641-
642-
{\t}{\t}JSONObject jsonObject = new JSONObject();{\n}
643-
{\t}{\t}jsonObject.put("type", "{table.javaClassName}");{\n}
644-
{loop table.fields as field}
645-
{\t}{\t}if(null == this.{field.javaName}) {{{\n}
646-
{\t}{\t}{\t}jsonObject.put("{field.javaName}", nullString);{\n}
647-
{\t}{\t}} else {{{\n}
648-
{\t}{\t}{\t}jsonObject.put("{field.javaName}", this.{field.javaName});{\n}
649-
{\t}{\t}}{\n}
650-
{endloop}
651-
{\t}{\t}return(jsonObject.toString());{\n}
639+
{\t}{\t}return(toJSON().toString());{\n}
652640
{\t}}{\n}
653641
{\n}
654642

src/test/java/synapticloop/sample/h2zero/mysql/finder/PetFinder.java

+15-18
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ public static List<Pet> findAllSilent() {
293293
* @param nmPet
294294
* @param numAge
295295
*
296-
* @return the unique result of Pet found
296+
* @return the list of Pet results found
297297
*
298298
* @throws H2ZeroFinderException if no results could be found
299299
* @throws SQLException if there was an error in the SQL statement
300300
*/
301-
public static Pet findByNmPetNumAge(Connection connection, String nmPet, Integer numAge, Integer limit, Integer offset) throws H2ZeroFinderException, SQLException {
301+
public static List<Pet> findByNmPetNumAge(Connection connection, String nmPet, Integer numAge, Integer limit, Integer offset) throws H2ZeroFinderException, SQLException {
302302
boolean hasConnection = (null != connection);
303303
String statement = null;
304304

@@ -328,7 +328,7 @@ public static Pet findByNmPetNumAge(Connection connection, String nmPet, Integer
328328

329329
PreparedStatement preparedStatement = null;
330330
ResultSet resultSet = null;
331-
Pet result = null;
331+
List<Pet> results = null;
332332
try {
333333
if(!hasConnection) {
334334
connection = ConnectionManager.getConnection();
@@ -338,12 +338,9 @@ public static Pet findByNmPetNumAge(Connection connection, String nmPet, Integer
338338
ConnectionManager.setInt(preparedStatement, 2, numAge);
339339

340340
resultSet = preparedStatement.executeQuery();
341-
result = uniqueResult(resultSet);
342-
ConnectionManager.closeAll(resultSet, preparedStatement);
341+
results = list(resultSet);
343342
} catch (SQLException sqlex) {
344343
throw sqlex;
345-
} catch (H2ZeroFinderException h2zfex) {
346-
throw new H2ZeroFinderException(h2zfex.getMessage() + " Additionally, the parameters were " + "[nmPet:" + nmPet + "], " + "[numAge:" + numAge + "].");
347344
} finally {
348345
if(hasConnection) {
349346
ConnectionManager.closeAll(resultSet, preparedStatement, null);
@@ -353,26 +350,26 @@ public static Pet findByNmPetNumAge(Connection connection, String nmPet, Integer
353350
}
354351

355352

356-
if(null == result) {
353+
if(null == results) {
357354
throw new H2ZeroFinderException("Could not find result.");
358355
}
359-
return(result);
356+
return(results);
360357
}
361358

362-
public static Pet findByNmPetNumAge(Connection connection, String nmPet, Integer numAge) throws H2ZeroFinderException, SQLException {
359+
public static List<Pet> findByNmPetNumAge(Connection connection, String nmPet, Integer numAge) throws H2ZeroFinderException, SQLException {
363360
return(findByNmPetNumAge(connection, nmPet, numAge, null, null));
364361
}
365362

366-
public static Pet findByNmPetNumAge(String nmPet, Integer numAge, Integer limit, Integer offset) throws H2ZeroFinderException, SQLException {
363+
public static List<Pet> findByNmPetNumAge(String nmPet, Integer numAge, Integer limit, Integer offset) throws H2ZeroFinderException, SQLException {
367364
return(findByNmPetNumAge(null, nmPet, numAge, limit, offset));
368365
}
369366

370-
public static Pet findByNmPetNumAge(String nmPet, Integer numAge) throws H2ZeroFinderException, SQLException {
367+
public static List<Pet> findByNmPetNumAge(String nmPet, Integer numAge) throws H2ZeroFinderException, SQLException {
371368
return(findByNmPetNumAge(null, nmPet, numAge, null, null));
372369
}
373370

374371
// silent connection, params..., limit, offset
375-
public static Pet findByNmPetNumAgeSilent(Connection connection, String nmPet, Integer numAge, Integer limit, Integer offset) {
372+
public static List<Pet> findByNmPetNumAgeSilent(Connection connection, String nmPet, Integer numAge, Integer limit, Integer offset) {
376373
try {
377374
return(findByNmPetNumAge(connection, nmPet, numAge, limit, offset));
378375
} catch(H2ZeroFinderException h2zfex) {
@@ -382,29 +379,29 @@ public static Pet findByNmPetNumAgeSilent(Connection connection, String nmPet, I
382379
h2zfex.printStackTrace();
383380
}
384381
}
385-
return(null);
382+
return(new ArrayList<Pet>());
386383
} catch(SQLException sqlex) {
387384
if(LOGGER.isWarnEnabled()) {
388385
LOGGER.warn("SQLException findByNmPetNumAgeSilent(connection: " + connection + ", " + nmPet + ", " + numAge + ", limit: " + limit + ", offset: " + offset + "): " + sqlex.getMessage());
389386
if(LOGGER.isDebugEnabled()) {
390387
sqlex.printStackTrace();
391388
}
392389
}
393-
return(null);
390+
return(new ArrayList<Pet>());
394391
}
395392
}
396393

397394
// silent connection, params...
398-
public static Pet findByNmPetNumAgeSilent(Connection connection, String nmPet, Integer numAge) {
395+
public static List<Pet> findByNmPetNumAgeSilent(Connection connection, String nmPet, Integer numAge) {
399396
return(findByNmPetNumAgeSilent(connection, nmPet, numAge, null, null));
400397
}
401398

402399
// silent params..., limit, offset
403-
public static Pet findByNmPetNumAgeSilent(String nmPet, Integer numAge, Integer limit, Integer offset) {
400+
public static List<Pet> findByNmPetNumAgeSilent(String nmPet, Integer numAge, Integer limit, Integer offset) {
404401
return(findByNmPetNumAgeSilent(null , nmPet, numAge, limit, offset));
405402
}
406403

407-
public static Pet findByNmPetNumAgeSilent(String nmPet, Integer numAge) {
404+
public static List<Pet> findByNmPetNumAgeSilent(String nmPet, Integer numAge) {
408405
return(findByNmPetNumAgeSilent(null, nmPet, numAge, null, null));
409406
}
410407

src/test/java/synapticloop/sample/h2zero/mysql/model/Pet.java

+11-68
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
import java.sql.Statement;
1818
import java.sql.PreparedStatement;
1919
import java.sql.SQLException;
20-
20+
import java.sql.SQLException;
2121

2222
import org.json.JSONObject;
2323

24+
import synapticloop.h2zero.base.model.ModelBaseHelper;
2425
import synapticloop.sample.h2zero.mysql.model.util.Constants;
2526

2627
import synapticloop.sample.h2zero.mysql.finder.PetFinder;
@@ -233,80 +234,22 @@ public JSONObject getToJSON() {
233234
}
234235

235236
public JSONObject toJSON() {
236-
String nullString = null;
237-
238237
JSONObject jsonObject = new JSONObject();
238+
239239
jsonObject.put("type", "Pet");
240-
if(null == this.idPet) {
241-
jsonObject.put("idPet", nullString);
242-
} else {
243-
jsonObject.put("idPet", this.idPet);
244-
}
245-
if(null == this.nmPet) {
246-
jsonObject.put("nmPet", nullString);
247-
} else {
248-
jsonObject.put("nmPet", this.nmPet);
249-
}
250-
if(null == this.numAge) {
251-
jsonObject.put("numAge", nullString);
252-
} else {
253-
jsonObject.put("numAge", this.numAge);
254-
}
255-
if(null == this.fltWeight) {
256-
jsonObject.put("fltWeight", nullString);
257-
} else {
258-
jsonObject.put("fltWeight", this.fltWeight);
259-
}
260-
if(null == this.dtBirthday) {
261-
jsonObject.put("dtBirthday", nullString);
262-
} else {
263-
jsonObject.put("dtBirthday", this.dtBirthday);
264-
}
265-
if(null == this.imgPhoto) {
266-
jsonObject.put("imgPhoto", nullString);
267-
} else {
268-
jsonObject.put("imgPhoto", this.imgPhoto);
269-
}
240+
241+
ModelBaseHelper.addtoJSONObject(jsonObject, "idPet", this.getIdPet());
242+
ModelBaseHelper.addtoJSONObject(jsonObject, "nmPet", this.getNmPet());
243+
ModelBaseHelper.addtoJSONObject(jsonObject, "numAge", this.getNumAge());
244+
ModelBaseHelper.addtoJSONObject(jsonObject, "fltWeight", this.getFltWeight());
245+
ModelBaseHelper.addtoJSONObject(jsonObject, "dtBirthday", this.getDtBirthday());
246+
ModelBaseHelper.addtoJSONObject(jsonObject, "imgPhoto", this.getImgPhoto());
270247
return(jsonObject);
271248
}
272249

273250

274251
public String toJsonString() {
275-
String nullString = null;
276-
277-
JSONObject jsonObject = new JSONObject();
278-
jsonObject.put("type", "Pet");
279-
if(null == this.idPet) {
280-
jsonObject.put("idPet", nullString);
281-
} else {
282-
jsonObject.put("idPet", this.idPet);
283-
}
284-
if(null == this.nmPet) {
285-
jsonObject.put("nmPet", nullString);
286-
} else {
287-
jsonObject.put("nmPet", this.nmPet);
288-
}
289-
if(null == this.numAge) {
290-
jsonObject.put("numAge", nullString);
291-
} else {
292-
jsonObject.put("numAge", this.numAge);
293-
}
294-
if(null == this.fltWeight) {
295-
jsonObject.put("fltWeight", nullString);
296-
} else {
297-
jsonObject.put("fltWeight", this.fltWeight);
298-
}
299-
if(null == this.dtBirthday) {
300-
jsonObject.put("dtBirthday", nullString);
301-
} else {
302-
jsonObject.put("dtBirthday", this.dtBirthday);
303-
}
304-
if(null == this.imgPhoto) {
305-
jsonObject.put("imgPhoto", nullString);
306-
} else {
307-
jsonObject.put("imgPhoto", this.imgPhoto);
308-
}
309-
return(jsonObject.toString());
252+
return(toJSON().toString());
310253
}
311254

312255
public String getJsonString() {

0 commit comments

Comments
 (0)