1717import com .blobcity .db .exceptions .InternalDbException ;
1818import com .blobcity .db .search .Query ;
1919import com .blobcity .db .search .StringUtil ;
20- import com .google .gson .Gson ;
21- import com .google .gson .JsonArray ;
22- import com .google .gson .JsonElement ;
23- import com .google .gson .JsonObject ;
24- import com .google .gson .JsonParser ;
20+ import com .google .gson .*;
2521
2622import java .lang .annotation .Annotation ;
2723import java .lang .reflect .Field ;
@@ -45,6 +41,7 @@ public abstract class Db {
4541
4642 private String collection = null ;
4743 private String ds = null ;
44+ private String _id = null ; //primary key value holder, in case the sub class does not have this property
4845
4946 public Db () {
5047 for (Annotation annotation : this .getClass ().getAnnotations ()) {
@@ -57,10 +54,6 @@ public Db() {
5754 }
5855 else {
5956 ds = blobCityEntity .ds ();
60- String dbName = Credentials .getInstance ().getDb ();
61- if (dbName .equals ("dummy" )) {
62- Credentials .getInstance ().setDb (ds );
63- }
6457 }
6558
6659 if (StringUtil .isEmpty (collection )) {
@@ -334,23 +327,136 @@ public static <T extends Db> Object execute(final Credentials credentials, final
334327 throw new DbOperationException (response .getErrorCode (), response .getErrorCause ());
335328 }
336329
337- public static void insertJsonData (final String collection , final JsonObject insertJson ){
338- insertJsonData (Credentials .getInstance (), collection , insertJson );
330+ public static void insertJson (final String collection , final JsonObject insertJson ){
331+ insertJson (Credentials .getInstance (), collection , Arrays .asList (new JsonObject []{insertJson }));
332+ }
333+
334+ public static void insertJson (final Credentials credentials , final String collection , final JsonObject insertJson ){
335+ insertJson (credentials , collection , Arrays .asList (new JsonObject []{insertJson }));
336+ }
337+
338+ public static void insertJson (final String collection , final List <JsonObject > jsonList ) {
339+ insertJson (Credentials .getInstance (), collection , jsonList );
340+ }
341+
342+ public static void insertJson (final Credentials credentials , final String collection , final List <JsonObject > jsonList ) {
343+ if (collection == null || collection .isEmpty ()) {
344+ throw new InternalAdapterException ("collection name must be specified" );
345+ }
346+
347+ if (jsonList == null || jsonList .isEmpty ()) {
348+ throw new InternalAdapterException ("At-least one json required for a json insert operation" );
349+ }
350+
351+ JsonObject payloadJsonObject = new JsonObject ();
352+ JsonArray dataArray = new JsonArray ();
353+ for (JsonObject element : jsonList ) {
354+ dataArray .add (element );
355+ }
356+ payloadJsonObject .add (QueryConstants .DATA , dataArray );
357+ payloadJsonObject .addProperty (QueryConstants .TYPE , "json" );
358+
359+ final DbQueryResponse response = postStaticRequest (credentials , QueryType .INSERT , collection , payloadJsonObject );
360+ reportIfError (response );
361+ }
362+
363+ public static void insertCsv (final String collection , final String csvString ) {
364+ throw new UnsupportedOperationException ("Not supported yet." );
365+ }
366+
367+ public static void insertCsv (final Credentials credentials , final String collection , final String csvString ) {
368+ throw new UnsupportedOperationException ("Not supported yet." );
369+ }
370+
371+ public static void insertCsv (final String collection , final List <String > csvList ) {
372+ throw new UnsupportedOperationException ("Not supported yet." );
373+ }
374+
375+ public static void insertCsv (final Credentials credentials , final String collection , final List <String > csvList ) {
376+ throw new UnsupportedOperationException ("Not supported yet." );
377+ }
378+
379+ public static void insertXml (final String collection , final String xmlString ) {
380+ insertXml (Credentials .getInstance (), collection , Arrays .asList (new String []{xmlString }));
381+ }
382+
383+ public static void insertXml (final Credentials credentials , final String collection , final String xmlString ) {
384+ insertXml (credentials , collection , Arrays .asList (new String []{xmlString }));
339385 }
340386
341- public static void insertJsonData (final Credentials credentials , final String collection , final JsonObject insertJson ){
387+ public static void insertXml (final String collection , final List <String > xmlList ) {
388+ insertXml (Credentials .getInstance (), collection , xmlList );
389+ }
390+
391+ public static void insertXml (final Credentials credentials , final String collection , final List <String > xmlList ) {
342392 if (collection == null || collection .isEmpty ()) {
343393 throw new InternalAdapterException ("collection name must be specified" );
344394 }
345395
346- if (insertJson == null ) {
347- throw new InternalAdapterException ("json data to insert cannot be null" );
396+ if (xmlList == null || xmlList .isEmpty ()) {
397+ throw new InternalAdapterException ("At-least one xml required for a xml insert operation" );
398+ }
399+
400+ JsonObject payloadJsonObject = new JsonObject ();
401+ JsonArray dataArray = new JsonArray ();
402+ for (String element : xmlList ) {
403+ dataArray .add (new JsonPrimitive (element ));
348404 }
405+ payloadJsonObject .add (QueryConstants .DATA , dataArray );
406+ payloadJsonObject .addProperty (QueryConstants .TYPE , "xml" );
349407
350- final DbQueryResponse response = postStaticRequest (credentials , QueryType .INSERT , collection , insertJson );
408+ final DbQueryResponse response = postStaticRequest (credentials , QueryType .INSERT , collection , payloadJsonObject );
351409 reportIfError (response );
352410 }
353411
412+ public static void insertSql (final String collection , final String sqlString ) {
413+ throw new UnsupportedOperationException ("Not supported yet." );
414+ }
415+
416+ public static void insertSql (final Credentials credentials , final String collection , final String sqlString ) {
417+ throw new UnsupportedOperationException ("Not supported yet." );
418+ }
419+
420+ public static void insertSql (final String collection , final List <String > sqlList ) {
421+ throw new UnsupportedOperationException ("Not supported yet." );
422+ }
423+
424+ public static void insertSql (final Credentials credentials , final String collection , final List <String > sqlList ) {
425+ throw new UnsupportedOperationException ("Not supported yet." );
426+ }
427+
428+ public static void insertText (final String collection , final String text ) {
429+ throw new UnsupportedOperationException ("Not supported yet." );
430+ }
431+
432+ public static void insertText (final Credentials credentials , final String collection , final String text ) {
433+ throw new UnsupportedOperationException ("Not supported yet." );
434+ }
435+
436+ public static void insertText (final String collection , final List <String > text ) {
437+ throw new UnsupportedOperationException ("Not supported yet." );
438+ }
439+
440+ public static void insertText (final Credentials credentials , final String collection , final List <String > text ) {
441+ throw new UnsupportedOperationException ("Not supported yet." );
442+ }
443+
444+ public static void insert (final String collection , final String insertString ) {
445+ throw new UnsupportedOperationException ("Not supported yet." );
446+ }
447+
448+ public static void insert (final Credentials credentials , final String collection , final String insertString ) {
449+ throw new UnsupportedOperationException ("Not supported yet." );
450+ }
451+
452+ public static void insert (final String collection , final List <String > insertList ) {
453+ throw new UnsupportedOperationException ("Not supported yet." );
454+ }
455+
456+ public static void insert (final Credentials credentials , final List <String > insertList ) {
457+ throw new UnsupportedOperationException ("Not supported yet." );
458+ }
459+
354460 //TODO: Add support for inserting other data formats
355461
356462 /**
@@ -1193,7 +1299,12 @@ private DbQueryResponse postRequest(final Credentials credentials, QueryType que
11931299 break ;
11941300 case INSERT :
11951301 case SAVE :
1196- queryJson .add (QueryConstants .PAYLOAD , toJson ());
1302+ JsonObject payloadJson = new JsonObject ();
1303+ payloadJson .addProperty (QueryConstants .TYPE ,"json" );
1304+ JsonArray jsonArray = new JsonArray ();
1305+ jsonArray .add (toJson ());
1306+ payloadJson .add (QueryConstants .DATA , jsonArray );
1307+ queryJson .add (QueryConstants .PAYLOAD , payloadJson );
11971308 break ;
11981309 default :
11991310 throw new InternalDbException ("Attempting to executed unknown or unidentifed query" );
@@ -1315,8 +1426,8 @@ public void save(final Credentials credentials) {
13151426 public boolean insert (final Credentials credentials ) {
13161427 final DbQueryResponse response = postRequest (credentials , QueryType .INSERT );
13171428 if (response .isSuccessful ()) {
1318- final JsonElement payloadJson = response .getPayload ();
1319- fromJson (payloadJson .getAsJsonObject ());
1429+ // final JsonElement payloadJson = response.getPayload();
1430+ // fromJson(payloadJson.getAsJsonObject());
13201431 return true ;
13211432 }
13221433
0 commit comments