Revision: 8026
Author: amitman...@google.com
Date: Sat May  1 07:18:49 2010
Log: Use Long ids instead of String for server-side domain objects. Fixed the
swizzling code so that Longs are represented as String on the client side.

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

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

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

Modified:
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/server/RequestFactoryServlet.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/EmployeeRequest.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ExpenseRequest.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ReportRequest.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Employee.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Expense.java /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Report.java

=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/server/RequestFactoryServlet.java Thu Apr 29 07:36:37 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/requestfactory/server/RequestFactoryServlet.java Sat May 1 07:18:49 2010
@@ -198,11 +198,12 @@
     Class<?> entity = tokenToEntityRecord.get(recordToken).entity;
Class<? extends Record> record = tokenToEntityRecord.get(recordToken).record; Map<String, Class<?>> propertiesInRecord = getPropertiesFromRecord(record);
-    validateKeys(recordObject, propertiesInRecord);
+    validateKeys(recordObject, propertiesInRecord.keySet());
+    updatePropertyTypes(propertiesInRecord, entity);

     // get entityInstance
     Object entityInstance = getEntityInstance(writeOperation, entity,
-        recordObject.getString("id"), propertiesInRecord.get("id"));
+        recordObject.get("id"), propertiesInRecord.get("id"));

     // persist
     if (writeOperation == WriteOperation.DELETE) {
@@ -215,16 +216,18 @@
if (writeOperation == WriteOperation.CREATE && ("id".equals(key))) {
           // ignored. id is assigned by default.
         } else {
+ Object propertyValue = getPropertyValueFromRequest(recordObject, key,
+              propertyType);
+          propertyValue = getSwizzledObject(propertyValue, propertyType);
           entity.getMethod(getMethodNameFromPropertyName(key, "set"),
-              propertyType).invoke(entityInstance,
- getPropertyValueFromRequest(recordObject, key, propertyType));
+              propertyType).invoke(entityInstance, propertyValue);
         }
       }
       entity.getMethod("persist").invoke(entityInstance);
     }

     // return data back.
- return getReturnRecord(writeOperation, entity, entityInstance, recordObject);
+    return getReturnRecord(writeOperation, entityInstance, recordObject);
   }

private Collection<Property<?>> allProperties(Class<? extends Record> clazz) {
@@ -316,17 +319,16 @@
   }

   private Object getEntityInstance(WriteOperation writeOperation,
-      Class<?> entity, String idValue, Class<?> idType)
+      Class<?> entity, Object idValue, Class<?> idType)
throws SecurityException, InstantiationException, IllegalAccessException,
       InvocationTargetException, NoSuchMethodException {

     if (writeOperation == WriteOperation.CREATE) {
       return entity.getConstructor().newInstance();
     }
-
     // TODO: check "version" validity.
return entity.getMethod("find" + entity.getSimpleName(), idType).invoke(
-        null, idValue);
+        null, getSwizzledObject(idValue, idType));
   }

   /**
@@ -474,19 +476,37 @@
   }

   private JSONObject getReturnRecord(WriteOperation writeOperation,
-      Class<?> entity, Object entityInstance, JSONObject recordObject)
-      throws SecurityException, JSONException, IllegalAccessException,
-      InvocationTargetException, NoSuchMethodException {
+ Object entityInstance, JSONObject recordObject) throws SecurityException,
+      JSONException, IllegalAccessException, InvocationTargetException,
+      NoSuchMethodException {

     JSONObject returnObject = new JSONObject();
- returnObject.put("id", entity.getMethod("getId").invoke(entityInstance));
-    returnObject.put("version", entity.getMethod("getVersion").invoke(
-        entityInstance));
+    // currently sending back only two properties.
+    for (String propertyName : new String[] {"id", "version"}) {
+      returnObject.put(propertyName, getPropertyValueFromDataStore(
+          entityInstance, propertyName));
+    }
     if (writeOperation == WriteOperation.CREATE) {
       returnObject.put("futureId", recordObject.getString("id"));
     }
     return returnObject;
   }
+
+  /**
+ * Swizzle an idValue received from the client to the type expected by the
+   * server. Return the object of the new type.
+   */
+  private Object getSwizzledObject(Object idValue, Class<?> idType) {
+    if (idValue.getClass() == idType) {
+      return idValue;
+    }
+    // swizzle from String to Long
+    if (idValue.getClass() == String.class && idType == Long.class) {
+      return new Long((String) idValue);
+    }
+ throw new IllegalArgumentException("id is of type: " + idValue.getClass()
+        + ",  expected type: " + idType);
+  }

   /**
* returns true if the property has been requested. TODO: use the properties
@@ -539,13 +559,26 @@
       throw new IllegalArgumentException("sync failed: ", e);
     }
   }
+
+  /**
+   * Update propertiesInRecord based on the types of entity.
+   */
+ private void updatePropertyTypes(Map<String, Class<?>> propertiesInRecord,
+      Class<?> entity) {
+    for (Field field : entity.getDeclaredFields()) {
+      Class<?> fieldType = propertiesInRecord.get(field.getName());
+      if (fieldType != null) {
+        propertiesInRecord.put(field.getName(), field.getType());
+      }
+    }
+  }

   private void validateKeys(JSONObject recordObject,
-      Map<String, Class<?>> declaredProperties) {
+      Set<String> declaredProperties) {
     Iterator<?> keys = recordObject.keys();
     while (keys.hasNext()) {
       String key = (String) keys.next();
-      if (declaredProperties.get(key) == null) {
+      if (!declaredProperties.contains(key)) {
         throw new IllegalArgumentException("key " + key
             + " is not permitted to be set");
       }
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/EmployeeRequest.java Thu Apr 29 07:36:37 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/EmployeeRequest.java Sat May 1 07:18:49 2010
@@ -60,7 +60,7 @@
       }

       public Class<?>[] getParameterTypes() {
-        return new Class[] {String.class};
+        return new Class[] {Long.class};
       }

       public boolean isReturnTypeList() {
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ExpenseRequest.java Thu Apr 29 07:36:37 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ExpenseRequest.java Sat May 1 07:18:49 2010
@@ -46,7 +46,7 @@
       }

       public Class<?>[] getParameterTypes() {
-        return new Class[] {java.lang.String.class};
+        return new Class[] {java.lang.Long.class};
       }
     },

@@ -56,7 +56,7 @@
       }

       public Class<?>[] getParameterTypes() {
-        return new Class[] {java.lang.String.class};
+        return new Class[] {java.lang.Long.class};
       }
     };

=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ReportRequest.java Thu Apr 29 07:36:37 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ReportRequest.java Sat May 1 07:18:49 2010
@@ -60,7 +60,7 @@
       }

       public Class<?>[] getParameterTypes() {
-        return new Class[] {java.lang.String.class};
+        return new Class[] {java.lang.Long.class};
       }

       public boolean isReturnTypeList() {
@@ -74,7 +74,7 @@
       }

       public Class<?>[] getParameterTypes() {
-        return new Class[] {java.lang.String.class};
+        return new Class[] {java.lang.Long.class};
       }
     },

=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java Fri Apr 30 09:10:49 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java Sat May 1 07:18:49 2010
@@ -119,7 +119,7 @@
     }
   }

-  private void addExpenses(String reportId) {
+  private void addExpenses(Long reportId) {
     int num = rand.nextInt(5) + 1;
     for (int i = 0; i < num; i++) {
       String[] descCat = getDescriptionAndCategory();
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Employee.java Thu Apr 29 07:36:37 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Employee.java Sat May 1 07:18:49 2010
@@ -15,8 +15,6 @@
  */
 package com.google.gwt.sample.expenses.server.domain;

-import org.datanucleus.jpa.annotations.Extension;
-
 import java.util.List;

 import javax.persistence.Column;
@@ -25,7 +23,6 @@
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
-import javax.persistence.JoinColumn;
 import javax.persistence.Version;

 /**
@@ -60,7 +57,7 @@
     }
   }

-  public static Employee findEmployee(String id) {
+  public static Employee findEmployee(Long id) {
     if (id == null) {
       return null;
     }
@@ -93,14 +90,13 @@

   private String password;

-  @JoinColumn
-  private String supervisorKey;
+  // @JoinColumn
+  private Long supervisorKey;

   @Id
   @Column(name = "id")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
-  private String id;
+  private Long id;

   @Version
   @Column(name = "version")
@@ -110,7 +106,7 @@
     return this.displayName;
   }

-  public String getId() {
+  public Long getId() {
     return this.id;
   }

@@ -118,7 +114,7 @@
     return this.password;
   }

-  public String getSupervisorKey() {
+  public Long getSupervisorKey() {
     return supervisorKey;
   }

@@ -153,7 +149,7 @@
     this.displayName = displayName;
   }

-  public void setId(String id) {
+  public void setId(Long id) {
     this.id = id;
   }

@@ -161,7 +157,7 @@
     this.password = password;
   }

-  public void setSupervisorKey(String supervisorKey) {
+  public void setSupervisorKey(Long supervisorKey) {
     this.supervisorKey = supervisorKey;
   }

=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Expense.java Fri Apr 30 09:10:49 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Expense.java Sat May 1 07:18:49 2010
@@ -15,8 +15,6 @@
  */
 package com.google.gwt.sample.expenses.server.domain;

-import org.datanucleus.jpa.annotations.Extension;
-
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
@@ -27,7 +25,6 @@
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
-import javax.persistence.JoinColumn;
 import javax.persistence.Query;
 import javax.persistence.Version;

@@ -54,7 +51,7 @@
     }
   }

-  public static Expense findExpense(String id) {
+  public static Expense findExpense(Long id) {
     if (id == null) {
       return null;
     }
@@ -67,7 +64,7 @@
   }

   @SuppressWarnings("unchecked")
-  public static List<Expense> findExpensesByReport(String reportId) {
+  public static List<Expense> findExpensesByReport(Long reportId) {
     EntityManager em = entityManager();
     try {
Query query = em.createQuery("select o from Expense o where o.reportId =:reportId");
@@ -81,15 +78,14 @@
     }
   }

-  public static List<Expense> findListOfOneExpense(String id) {
+  public static List<Expense> findListOfOneExpense(Long id) {
     return Collections.singletonList(findExpense(id));
   }

   @Id
   @Column(name = "id")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
-  private String id;
+  private Long id;

   @Version
   @Column(name = "version")
@@ -107,9 +103,8 @@

   private String reasonDenied;

-  @JoinColumn
-  @Column(name = "reportId")
-  private String reportId;
+  // @JoinColumn
+  private Long reportId;

   public Double getAmount() {
     return this.amount;
@@ -131,7 +126,7 @@
     return description;
   }

-  public String getId() {
+  public Long getId() {
     return this.id;
   }

@@ -139,7 +134,7 @@
     return this.reasonDenied;
   }

-  public String getReportId() {
+  public Long getReportId() {
     return this.reportId;
   }

@@ -186,7 +181,7 @@
     this.description = description;
   }

-  public void setId(String id) {
+  public void setId(Long id) {
     this.id = id;
   }

@@ -194,7 +189,7 @@
     this.reasonDenied = reasonDenied;
   }

-  public void setReportId(String reportId) {
+  public void setReportId(Long reportId) {
     this.reportId = reportId;
   }

=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Report.java Thu Apr 29 07:36:37 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/server/domain/Report.java Sat May 1 07:18:49 2010
@@ -15,8 +15,6 @@
  */
 package com.google.gwt.sample.expenses.server.domain;

-import org.datanucleus.jpa.annotations.Extension;
-
 import java.util.Date;
 import java.util.List;

@@ -26,7 +24,6 @@
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
-import javax.persistence.JoinColumn;
 import javax.persistence.Query;
 import javax.persistence.Version;

@@ -62,7 +59,7 @@
     }
   }

-  public static Report findReport(String id) {
+  public static Report findReport(Long id) {
     if (id == null) {
       return null;
     }
@@ -89,7 +86,7 @@
   }

   @SuppressWarnings("unchecked")
-  public static List<Report> findReportsByEmployee(String employeeId) {
+  public static List<Report> findReportsByEmployee(Long employeeId) {
     EntityManager em = entityManager();
     try {
Query query = em.createQuery("select o from Report o where o.reporterKey =:reporterKey");
@@ -106,8 +103,7 @@
   @Id
   @Column(name = "id")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
-  private String id;
+  private Long id;

   @Version
   @Column(name = "version")
@@ -124,14 +120,13 @@
    * http://code.google.com/appengine
    * /docs/java/datastore/relationships.html#Unowned_Relationships
    */
-  @JoinColumn
-  @Column(name = "reporter")
-  private String reporterKey;
-
-  @JoinColumn
-  private String approvedSupervisorKey;
-
-  public String getApprovedSupervisorKey() {
+  // @JoinColumn
+  private Long reporterKey;
+
+  // @JoinColumn
+  private Long approvedSupervisorKey;
+
+  public Long getApprovedSupervisorKey() {
     return approvedSupervisorKey;
   }

@@ -139,7 +134,7 @@
     return this.created;
   }

-  public String getId() {
+  public Long getId() {
     return this.id;
   }

@@ -151,7 +146,7 @@
     return this.purpose;
   }

-  public String getReporterKey() {
+  public Long getReporterKey() {
     return this.reporterKey;
   }

@@ -178,7 +173,7 @@
     }
   }

-  public void setApprovedSupervisorKey(String approvedSupervisorKey) {
+  public void setApprovedSupervisorKey(Long approvedSupervisorKey) {
     this.approvedSupervisorKey = approvedSupervisorKey;
   }

@@ -186,7 +181,7 @@
     this.created = created;
   }

-  public void setId(String id) {
+  public void setId(Long id) {
     this.id = id;
   }

@@ -198,7 +193,7 @@
     this.purpose = purpose;
   }

-  public void setReporterKey(String reporter) {
+  public void setReporterKey(Long reporter) {
     this.reporterKey = reporter;
   }

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

Reply via email to