Fixes the issue that the DVS does not send an object to the server if no
setters have been called on it. Added tests for the issue. Finally, removed the
unnecessary operations.size() restriction in DeltaValueStoreJsonImpl.

Patch by: amitmanjhi
Review by: rjrjr, cromwellian


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@8582 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImpl.java b/user/src/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImpl.java
index 8aec549..24d9529 100644
--- a/user/src/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImpl.java
+++ b/user/src/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImpl.java
@@ -194,7 +194,7 @@
           returnedJso, WriteOperation.DELETE.name());
       Map<Long, Map<String, String>> violationsMap = getViolationsMap(deletedRecords);
       int length = deletedRecords.length();
-      // for (Map.Entry<RecordKey, RecordJsoImpl> entry : deletes.entrySet()) {
+
       for (int i = 0; i < length; i++) {
         final RecordKey key = new RecordKey(deletedRecords.get(i).getId(),
             requestFactory.getSchema(deletedRecords.get(i).getSchema()),
@@ -300,27 +300,18 @@
   }
 
   String toJson() {
-    if (operations.size() > 1) {
-      throw new UnsupportedOperationException(
-          "Currently, only one entity can be saved/persisted at a time");
-      /*
-       * TODO: Short-term todo is to allow multiple entities belonging to the
-       * same class to be persisted at the same time. The client side support
-       * for this operation is already in place. On the server side, this will
-       * entail persisting all entities as part of a single transaction. In
-       * particular, the transaction should fail if the validation check on any
-       * of the entities fail.
-       * 
-       * Multiple entities belonging to different records can not be persisted
-       * at present due to the appEngine limitation of a transaction not being
-       * allowed to span multiple entity groups.
-       */
-    }
-    return toJsonWithoutChecks();
-  }
-
-  String toJsonWithoutChecks() {
     used = true;
+
+    /*
+     * pull the creates that only the requestFactory knows about.
+     */
+    for (RecordKey recordKey : requestFactory.creates.keySet()) {
+      RecordJsoImpl oldRecord = requestFactory.creates.remove(recordKey);
+      assert oldRecord != null;
+      operations.put(recordKey, WriteOperation.CREATE);
+      creates.put(recordKey, oldRecord);
+    }
+
     StringBuffer jsonData = new StringBuffer("{");
     for (WriteOperation writeOperation : new WriteOperation[] {
         WriteOperation.CREATE, WriteOperation.UPDATE}) {
diff --git a/user/test/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImplTest.java b/user/test/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImplTest.java
index 7753b41..32dfdd3 100644
--- a/user/test/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImplTest.java
+++ b/user/test/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImplTest.java
@@ -79,6 +79,7 @@
         return create(token, typeMap);
       }
 
+      @Override
       public RecordSchema getSchema(String token) {
         return typeMap.getType(token);
       }
@@ -124,6 +125,17 @@
 
     DeltaValueStoreJsonImpl deltaValueStore = new DeltaValueStoreJsonImpl(
         valueStore, requestFactory);
+    String json = deltaValueStore.toJson();
+    testAndGetChangeRecord(json, WriteOperation.CREATE);
+  }
+
+  public void testCreateWithSet() {
+    Record created = requestFactory.create(SimpleFooRecord.class);
+    assertNotNull(created.getId());
+    assertNotNull(created.getVersion());
+
+    DeltaValueStoreJsonImpl deltaValueStore = new DeltaValueStoreJsonImpl(
+        valueStore, requestFactory);
     // DVS does not know about the created entity.
     assertFalse(deltaValueStore.isChanged());
     deltaValueStore.set(SimpleFooRecord.userName, created, "harry");
@@ -182,7 +194,7 @@
     deltaValueStore.set(SimpleFooRecord.userName, createRecord, "harry");
     deltaValueStore.set(SimpleFooRecord.userName, mockRecord, "bovik");
     assertTrue(deltaValueStore.isChanged());
-    String jsonString = deltaValueStore.toJsonWithoutChecks();
+    String jsonString = deltaValueStore.toJson();
     JSONObject jsonObject = (JSONObject) JSONParser.parse(jsonString);
     assertFalse(jsonObject.containsKey(WriteOperation.DELETE.name()));
     assertTrue(jsonObject.containsKey(WriteOperation.CREATE.name()));