Revision: 8063
Author: [email protected]
Date: Thu May  6 12:47:56 2010
Log: Updates the RequestFactory generator to build a class that can map the String tokens on Record classes to internal 'schemas'.

Patch by: amitmanjhi
Review by: rjrjr (desk review)

Review at http://gwt-code-reviews.appspot.com/488801

http://code.google.com/p/google-web-toolkit/source/detail?r=8063

Added:
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/client/impl/RecordToTypeMap.java
Modified:
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/rebind/RequestFactoryGenerator.java /branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/DeltaValueStoreJsonImpl.java /branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/ValueStoreJsonImpl.java

=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/client/impl/RecordToTypeMap.java Thu May 6 12:47:56 2010
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.requestfactory.client.impl;
+
+import com.google.gwt.valuestore.shared.Record;
+import com.google.gwt.valuestore.shared.impl.RecordSchema;
+
+/**
+ * A class that can map the "TOKEN" generated by a JPA-savvy tool in every
+ * Record class to its internal "type" representation.
+ */
+public interface RecordToTypeMap {
+  RecordSchema<? extends Record> getType(String token);
+}
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java Mon May 3 13:28:30 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java Thu May 6 12:47:56 2010
@@ -35,15 +35,15 @@
 /**
  * Base implementation of RequestFactory.
  */
-public class RequestFactoryJsonImpl implements RequestFactory {
+public abstract class RequestFactoryJsonImpl implements RequestFactory {

   private ValueStoreJsonImpl valueStore;

   /**
    * @param handlerManager
    */
-  public void init(HandlerManager handlerManager) {
-    this.valueStore = new ValueStoreJsonImpl(handlerManager);
+  protected void init(HandlerManager handlerManager, RecordToTypeMap map) {
+    this.valueStore = new ValueStoreJsonImpl(handlerManager, map);
   }

   public void fire(final RequestObject<?> requestObject) {
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/rebind/RequestFactoryGenerator.java Tue May 4 15:09:04 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/rebind/RequestFactoryGenerator.java Thu May 6 12:47:56 2010
@@ -27,12 +27,14 @@
 import com.google.gwt.core.ext.typeinfo.JType;
 import com.google.gwt.core.ext.typeinfo.NotFoundException;
 import com.google.gwt.core.ext.typeinfo.TypeOracle;
+import com.google.gwt.event.shared.HandlerManager;
 import com.google.gwt.requestfactory.client.impl.AbstractDoubleRequest;
 import com.google.gwt.requestfactory.client.impl.AbstractIntegerRequest;
 import com.google.gwt.requestfactory.client.impl.AbstractJsonListRequest;
 import com.google.gwt.requestfactory.client.impl.AbstractJsonObjectRequest;
 import com.google.gwt.requestfactory.client.impl.AbstractLongRequest;
 import com.google.gwt.requestfactory.client.impl.ClientRequestHelper;
+import com.google.gwt.requestfactory.client.impl.RecordToTypeMap;
 import com.google.gwt.requestfactory.client.impl.RequestFactoryJsonImpl;
 import com.google.gwt.requestfactory.shared.RecordListRequest;
 import com.google.gwt.requestfactory.shared.RecordRequest;
@@ -223,6 +225,7 @@

     ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(
         packageName, implName);
+    f.addImport(HandlerManager.class.getName());
     f.addImport(RequestFactoryJsonImpl.class.getName());
     f.addImport(interfaceType.getQualifiedSourceName());
     f.addImplementedInterface(interfaceType.getName());
@@ -262,9 +265,19 @@
       sw.println("}");
       sw.println();
     }
+
+ JClassType recordToTypeInterface = generatorContext.getTypeOracle().findType(
+        RecordToTypeMap.class.getName());
+    String recordToTypeMapName = recordToTypeInterface.getName() + "Impl";
+    sw.println("public void init(HandlerManager handlerManager) {");
+    sw.indent();
+ sw.println("super.init(handlerManager, new " + recordToTypeMapName + "());");
     sw.outdent();
     sw.println("}");

+    sw.outdent();
+    sw.println("}");
+
     // generate an implementation for each request selector

     for (JClassType nestedInterface : requestSelectors) {
@@ -276,8 +289,62 @@
             nestedImplName);
       }
     }
+
+    // generate the mapping type implementation
+    PrintWriter pw = printWriters.makePrintWriterFor(recordToTypeMapName);
+    if (pw != null) {
+      generateRecordToTypeMap(logger, generatorContext, pw,
+          recordToTypeInterface, packageName, recordToTypeMapName);
+    }
+
     printWriters.commit();
   }
+
+  private void generateRecordToTypeMap(TreeLogger logger,
+      GeneratorContext generatorContext, PrintWriter out,
+      JClassType interfaceType, String packageName, String implName)
+      throws UnableToCompleteException {
+    logger = logger.branch(TreeLogger.DEBUG, String.format(
+        "Generating implementation of %s", interfaceType.getName()));
+
+    ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(
+        packageName, implName);
+    f.addImport(interfaceType.getQualifiedSourceName());
+    f.addImport(Record.class.getName());
+    f.addImport(RecordSchema.class.getName());
+    f.addImplementedInterface(interfaceType.getName());
+
+    f.addImplementedInterface(interfaceType.getName());
+
+    SourceWriter sw = f.createSourceWriter(generatorContext, out);
+    sw.println();
+
+ sw.println("public RecordSchema<? extends Record> getType(String token) {");
+    sw.indent();
+    for (JClassType publicRecordType : generatedRecordTypes) {
+      if (publicRecordType.getField("TOKEN") == null) {
+        logger.log(TreeLogger.ERROR, "Record type "
+            + publicRecordType.getQualifiedSourceName()
+            + " should have a field TOKEN");
+        throw new UnableToCompleteException();
+      }
+ sw.println("if (token == " + publicRecordType.getName() + ".TOKEN) {");
+      sw.indent();
+      sw.println("return " + publicRecordType.getName() + "Impl.SCHEMA;");
+      sw.outdent();
+      sw.println("}");
+    }
+
+ sw.println("throw new IllegalArgumentException(\"Unknown token \" + token + ");
+    sw.indent();
+ sw.println("\", does not match any of the TOKEN vairables of a Record\");");
+    sw.outdent();
+    sw.outdent();
+    sw.println("}");
+
+    sw.outdent();
+    sw.println("}");
+  }

   private void generateRequestSelectorImplementation(TreeLogger logger,
       GeneratorContext generatorContext, PrintWriterManager printWriters,
@@ -486,7 +553,8 @@
    */
   private JClassType printSchema(TypeOracle typeOracle,
       JClassType publicRecordType, String recordImplTypeName,
- JClassType eventType, SourceWriter sw, TreeLogger logger) throws UnableToCompleteException {
+      JClassType eventType, SourceWriter sw, TreeLogger logger)
+      throws UnableToCompleteException {
     sw.println(String.format(
         "public static class MySchema extends RecordSchema<%s> {",
         recordImplTypeName));
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/DeltaValueStoreJsonImpl.java Tue May 4 15:09:04 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/DeltaValueStoreJsonImpl.java Thu May 6 12:47:56 2010
@@ -17,6 +17,7 @@

 import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.core.client.JsArray;
+import com.google.gwt.requestfactory.client.impl.RecordToTypeMap;
 import com.google.gwt.requestfactory.shared.SyncResult;
 import com.google.gwt.requestfactory.shared.RequestFactory.WriteOperation;
 import com.google.gwt.valuestore.shared.DeltaValueStore;
@@ -118,6 +119,7 @@
private final FutureIdGenerator futureIdGenerator = new FutureIdGenerator();

   private final ValueStoreJsonImpl master;
+  private final RecordToTypeMap recordToTypeMap;

   // track C-U-D of CRUD operations
private final Map<RecordKey, RecordJsoImpl> creates = new HashMap<RecordKey, RecordJsoImpl>();
@@ -126,8 +128,10 @@

private final Map<RecordKey, WriteOperation> operations = new HashMap<RecordKey, WriteOperation>();

-  DeltaValueStoreJsonImpl(ValueStoreJsonImpl master) {
+  DeltaValueStoreJsonImpl(ValueStoreJsonImpl master,
+      RecordToTypeMap recordToTypeMap) {
     this.master = master;
+    this.recordToTypeMap = recordToTypeMap;
   }

   public void addValidation() {
@@ -251,9 +255,9 @@
   public Record create(String token) {
     assert !used;
     String futureId = futureIdGenerator.getFutureId();
-    // TODO: get schema from token
-    RecordJsoImpl newRecord = RecordJsoImpl.newCopy(null,
-        futureId, INITIAL_VERSION);
+
+    RecordJsoImpl newRecord = RecordJsoImpl.newCopy(
+        recordToTypeMap.getType(token), futureId, INITIAL_VERSION);
     RecordKey recordKey = new RecordKey(newRecord);
     assert operations.get(recordKey) == null;
     operations.put(recordKey, WriteOperation.CREATE);
@@ -375,7 +379,7 @@
        * 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.
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/ValueStoreJsonImpl.java Fri Apr 30 13:33:12 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/ValueStoreJsonImpl.java Thu May 6 12:47:56 2010
@@ -17,6 +17,7 @@

 import com.google.gwt.core.client.JsArray;
 import com.google.gwt.event.shared.HandlerManager;
+import com.google.gwt.requestfactory.client.impl.RecordToTypeMap;
 import com.google.gwt.requestfactory.shared.RequestFactory.WriteOperation;
 import com.google.gwt.valuestore.shared.ValueStore;
 import com.google.gwt.valuestore.shared.impl.RecordJsoImpl;
@@ -31,11 +32,13 @@
   // package protected fields for use by DeltaValueStoreJsonImpl

   final HandlerManager eventBus;
+  final RecordToTypeMap map;

final Map<RecordKey, RecordJsoImpl> records = new HashMap<RecordKey, RecordJsoImpl>();

-  public ValueStoreJsonImpl(HandlerManager eventBus) {
+  public ValueStoreJsonImpl(HandlerManager eventBus, RecordToTypeMap map) {
     this.eventBus = eventBus;
+    this.map = map;
   }

   public void addValidation() {
@@ -57,7 +60,7 @@
    * @return
    */
   public DeltaValueStoreJsonImpl spawnDeltaView() {
-    return new DeltaValueStoreJsonImpl(this);
+    return new DeltaValueStoreJsonImpl(this, map);
   }

   /**

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to