@@ -315,11 +315,20 @@ else if (method.startsWith("delete")){
315315 //**************************************************************************
316316 //** getRecordset
317317 //**************************************************************************
318- /** Protected method that subclasses can override to apply filters or add
319- * constraints when retrieving objects from the database. This method is
320- * called before "get", "create", "update", "delete" requests.
318+ /** Returns a Recordset that is used fetch records from the database and
319+ * support CRUD operations. This is a protected method that extending
320+ * classes can override to apply custom filters or add constraints when
321+ * retrieving objects from the database. This method is called whenever an
322+ * HTTP GET, POST, or DELETE request is made for a Model.
323+ * @param op Operation that is requesting the Recordset. Options include
324+ * "list, "get", "save", and "delete".
325+ * @param c The Model (Java class) associated with the request.
326+ * @param sql The default SQL statement generated for the request.
327+ * @param conn A database connection used to open the Recordset.
321328 */
322- protected Recordset getRecordset (ServiceRequest request , String op , Class c , String sql , Connection conn ) throws Exception {
329+ protected Recordset getRecordset (ServiceRequest request , String op , Class c ,
330+ String sql , Connection conn ) throws Exception {
331+
323332 Recordset rs = new Recordset ();
324333 if (op .equals ("list" )) rs .setFetchSize (1000 );
325334 rs .open (sql , conn );
@@ -359,10 +368,11 @@ private ServiceResponse get(Class c, ServiceRequest request, Database database)
359368
360369 //Apply filter
361370 id = null ;
362- Recordset rs = getRecordset (request , "get" , c , "select id from " +
363- getTableName (obj ) + " where id=" + getMethod ("getID" , c ).invoke (obj ), conn );
364- if (!rs .EOF ) id = rs .getValue (0 ).toLong ();
365- rs .close ();
371+ try (Recordset rs = getRecordset (request , "get" , c ,
372+ "select id from " + getTableName (obj ) +
373+ " where id=" + getMethod ("getID" , c ).invoke (obj ), conn )){
374+ if (!rs .EOF ) id = rs .getValue (0 ).toLong ();
375+ }
366376 if (id ==null ) return new ServiceResponse (404 );
367377
368378
@@ -489,108 +499,107 @@ private ServiceResponse list(Class c, ServiceRequest request, Database database)
489499
490500 //Excute query and generate response
491501 try (Connection conn = database .getConnection ()){
492- Recordset rs = getRecordset (request , "list" , c , sql .toString (), conn );
502+ try ( Recordset rs = getRecordset (request , "list" , c , sql .toString (), conn )){
493503
494504
495- ServiceResponse response ;
496- if (format .equals ("csv" )){
505+ ServiceResponse response ;
506+ if (format .equals ("csv" )){
497507
498- StringBuilder csv = new StringBuilder ();
499- long x = 0 ;
500- while (rs .next ()){
501- if (x >0 ) csv .append ("\r \n " );
508+ StringBuilder csv = new StringBuilder ();
509+ long x = 0 ;
510+ while (rs .next ()){
511+ if (x >0 ) csv .append ("\r \n " );
512+
513+ if (x ==0 ){
514+ int i = 0 ;
515+ for (javaxt .sql .Field field : rs .getFields ()){
516+ if (i >0 ) csv .append ("," );
517+ csv .append (field .getName ());
518+ i ++;
519+ }
520+ }
502521
503- if (x ==0 ){
504522 int i = 0 ;
505523 for (javaxt .sql .Field field : rs .getFields ()){
506524 if (i >0 ) csv .append ("," );
507- csv .append (field .getName ());
525+ javaxt .sql .Value value = field .getValue ();
526+ if (!value .isNull ()){
527+ String val = value .toString ();
528+ if (val .contains ("\" " )) val = "\" " + val + "\" " ;
529+ csv .append (val );
530+ }
508531 i ++;
509532 }
510- }
511533
512- int i = 0 ;
513- for (javaxt .sql .Field field : rs .getFields ()){
514- if (i >0 ) csv .append ("," );
515- javaxt .sql .Value value = field .getValue ();
516- if (!value .isNull ()){
517- String val = value .toString ();
518- if (val .contains ("\" " )) val = "\" " + val + "\" " ;
519- csv .append (val );
520- }
521- i ++;
534+
535+ x ++;
522536 }
523537
524538
525- x ++ ;
526- }
539+ response = new ServiceResponse ( csv ) ;
540+ response . setContentType ( "text/csv" );
527541
542+ }
543+ else if (format .equals ("json" )){
528544
529- response = new ServiceResponse (csv );
530- response .setContentType ("text/csv" );
545+ StringBuilder json = new StringBuilder ("[" );
531546
532- }
533- else if (format .equals ("json" )){
547+ long x = 0 ;
548+ while (rs .next ()){
549+ if (x >0 ) json .append ("," );
550+ json .append (DbUtils .getJson (rs ));
551+ x ++;
552+ }
553+ json .append ("]" );
534554
535- StringBuilder json = new StringBuilder ("[" );
555+ response = new ServiceResponse (json .toString ());
556+ response .setContentType ("application/json" );
536557
537- long x = 0 ;
538- while (rs .next ()){
539- if (x >0 ) json .append ("," );
540- json .append (DbUtils .getJson (rs ));
541- x ++;
542558 }
543- json . append ( "]" );
559+ else {
544560
545- response = new ServiceResponse (json .toString ());
546- response .setContentType ("application/json" );
561+ long x = 0 ;
562+ JSONArray cols = new JSONArray ();
563+ StringBuilder json = new StringBuilder ("{\" rows\" :[" );
547564
548- }
549- else {
550-
551- long x = 0 ;
552- JSONArray cols = new JSONArray ();
553- StringBuilder json = new StringBuilder ("{\" rows\" :[" );
554-
555- while (rs .next ()){
556- JSONArray row = new JSONArray ();
557-
558- JSONObject record = DbUtils .getJson (rs );
559- for (javaxt .sql .Field field : rs .getFields ()){
560- String fieldName = field .getName ().toLowerCase ();
561- fieldName = StringUtils .underscoreToCamelCase (fieldName );
562- if (x ==0 ) cols .add (fieldName );
563-
564- JSONValue val = record .get (fieldName );
565- if (!val .isNull ()){
566- if (spatialFields .contains (fieldName )){
567- if (database .getDriver ().equals ("PostgreSQL" )){
568- val = new JSONValue (createGeom (val .toString ()));
565+ while (rs .next ()){
566+ JSONArray row = new JSONArray ();
567+
568+ JSONObject record = DbUtils .getJson (rs );
569+ for (javaxt .sql .Field field : rs .getFields ()){
570+ String fieldName = field .getName ().toLowerCase ();
571+ fieldName = StringUtils .underscoreToCamelCase (fieldName );
572+ if (x ==0 ) cols .add (fieldName );
573+
574+ JSONValue val = record .get (fieldName );
575+ if (!val .isNull ()){
576+ if (spatialFields .contains (fieldName )){
577+ if (database .getDriver ().equals ("PostgreSQL" )){
578+ val = new JSONValue (createGeom (val .toString ()));
579+ }
569580 }
570581 }
582+ row .add (val );
571583 }
572- row .add (val );
573- }
574584
575- if (x >0 ) json .append ("," );
576- json .append (row .toString ());
577- x ++;
578- }
579- rs .close ();
580- json .append ("]" );
585+ if (x >0 ) json .append ("," );
586+ json .append (row .toString ());
587+ x ++;
588+ }
581589
590+ json .append ("]" );
582591
583- json .append (",\" cols\" :" );
584- json .append (cols .toString ());
585- json .append ("}" );
586- response = new ServiceResponse (json .toString ());
587- response .setContentType ("application/json" );
588592
589- }
593+ json .append (",\" cols\" :" );
594+ json .append (cols .toString ());
595+ json .append ("}" );
596+ response = new ServiceResponse (json .toString ());
597+ response .setContentType ("application/json" );
590598
591- rs . close ();
599+ }
592600
593- return response ;
601+ return response ;
602+ }
594603 }
595604 catch (Exception e ){
596605 return getServiceResponse (e );
@@ -605,10 +614,9 @@ else if (format.equals("json")){
605614 * ID.
606615 */
607616 private ServiceResponse save (Class c , ServiceRequest request , Database database ) {
608- Connection conn = null ;
609617 try {
610- JSONObject json = new JSONObject ( new String ( request .getPayload (), "UTF-8" ) );
611- if (json .isEmpty ()) throw new Exception ("JSON is empty." );
618+ JSONObject json = request .getJson ( );
619+ if (json == null || json .isEmpty ()) throw new Exception ("JSON is empty." );
612620
613621
614622 //Create new instance of the class
@@ -628,12 +636,13 @@ private ServiceResponse save(Class c, ServiceRequest request, Database database)
628636
629637 //Apply filter
630638 if (!isNew ){
631- conn = database .getConnection ();
632- Recordset rs = getRecordset (request , "save" , c , "select id from " +
633- getTableName (obj ) + " where id=" + getMethod ("getID" , c ).invoke (obj ), conn );
634- if (!rs .EOF ) id = rs .getValue (0 ).toLong ();
635- rs .close ();
636- conn .close ();
639+ try (Connection conn = database .getConnection ()){
640+ try (Recordset rs = getRecordset (request , "save" , c ,
641+ "select id from " + getTableName (obj ) +
642+ " where id=" + getMethod ("getID" , c ).invoke (obj ), conn )){
643+ if (!rs .EOF ) id = rs .getValue (0 ).toLong ();
644+ }
645+ }
637646 if (id ==null ) return new ServiceResponse (404 );
638647 }
639648
@@ -646,6 +655,7 @@ private ServiceResponse save(Class c, ServiceRequest request, Database database)
646655 //Get id
647656 Method getID = getMethod ("getID" , c );
648657 id = (Long ) getID .invoke (obj );
658+ if (id ==null ) throw new Exception ("Save failed" );
649659
650660
651661 //Fire event
@@ -656,7 +666,6 @@ private ServiceResponse save(Class c, ServiceRequest request, Database database)
656666 return new ServiceResponse (id +"" );
657667 }
658668 catch (Exception e ){
659- if (conn !=null ) conn .close ();
660669 return getServiceResponse (e );
661670 }
662671 }
@@ -673,10 +682,11 @@ private ServiceResponse delete(Class c, ServiceRequest request, Database databas
673682
674683 //Apply filter
675684 Long id = null ;
676- Recordset rs = getRecordset (request , "delete" , c , "select id from " +
677- getTableName (c .newInstance ()) + " where id=" + request .getID (), conn );
678- if (!rs .EOF ) id = rs .getValue (0 ).toLong ();
679- rs .close ();
685+ try (Recordset rs = getRecordset (request , "delete" , c ,
686+ "select id from " + getTableName (c .newInstance ()) +
687+ " where id=" + request .getID (), conn )){
688+ if (!rs .EOF ) id = rs .getValue (0 ).toLong ();
689+ }
680690 if (id ==null ) return new ServiceResponse (404 );
681691
682692
0 commit comments