Revision: 8048
Author: [email protected]
Date: Tue May 4 15:09:04 2010
Log: The TOKEN on a Record class is now a field instead of an annotation.
The field
can be generated by a JPA-savvy tool. The TOKEN, in addition to being used
for
SYNC requests, will also be used for DeltaValueStore.create(..) method
Patch by: amitmanjhi
Review by: rjrjr (desk review)
Review at http://gwt-code-reviews.appspot.com/459802
http://code.google.com/p/google-web-toolkit/source/detail?r=8048
Modified:
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/rebind/RequestFactoryGenerator.java
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/server/RequestFactoryServlet.java
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/shared/ServerType.java
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/EmployeeRecord.java
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ExpenseRecord.java
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ReportRecord.java
/branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/DeltaValueStoreJsonImpl.java
/branches/2.1/bikeshed/src/com/google/gwt/valuestore/shared/DeltaValueStore.java
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/rebind/RequestFactoryGenerator.java
Thu Apr 29 07:36:37 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/rebind/RequestFactoryGenerator.java
Tue May 4 15:09:04 2010
@@ -37,7 +37,6 @@
import com.google.gwt.requestfactory.shared.RecordListRequest;
import com.google.gwt.requestfactory.shared.RecordRequest;
import com.google.gwt.requestfactory.shared.ServerOperation;
-import com.google.gwt.requestfactory.shared.ServerType;
import com.google.gwt.requestfactory.shared.RequestFactory.WriteOperation;
import com.google.gwt.requestfactory.shared.impl.RequestDataManager;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
@@ -167,7 +166,7 @@
sw.println();
JClassType propertyType = printSchema(typeOracle, publicRecordType,
- recordImplTypeName, eventType, sw);
+ recordImplTypeName, eventType, sw, logger);
sw.println();
String simpleImplName = publicRecordType.getSimpleSourceName()
+ "Impl";
@@ -483,10 +482,11 @@
* @param eventType
* @param sw
* @return
+ * @throws UnableToCompleteException
*/
private JClassType printSchema(TypeOracle typeOracle,
JClassType publicRecordType, String recordImplTypeName,
- JClassType eventType, SourceWriter sw) {
+ JClassType eventType, SourceWriter sw, TreeLogger logger) throws
UnableToCompleteException {
sw.println(String.format(
"public static class MySchema extends RecordSchema<%s> {",
recordImplTypeName));
@@ -546,12 +546,10 @@
sw.println();
sw.println("public String getToken() {");
sw.indent();
- ServerType serverType =
publicRecordType.getAnnotation(ServerType.class);
- String token = serverType.token();
- if ("[UNASSIGNED]".equals(token)) {
- token = publicRecordType.getName();
- }
- sw.println("return \"" + token + "\";");
+ String fieldName = "TOKEN";
+ validateTokenField(publicRecordType, fieldName, typeOracle, logger);
+ sw.println("return " + publicRecordType.getName() + "." + fieldName
+ + "; // special field");
sw.outdent();
sw.println("}");
@@ -559,4 +557,16 @@
sw.println("}");
return propertyType;
}
-}
+
+ private void validateTokenField(JClassType publicRecordType,
+ String fieldName, TypeOracle typeOracle, TreeLogger logger)
+ throws UnableToCompleteException {
+ JField field = publicRecordType.getField(fieldName);
+ if (field == null
+ || field.getType() != typeOracle.findType("java.lang.String")) {
+ logger.log(TreeLogger.ERROR, "The field " + fieldName
+ + " of type String must be defined on " +
publicRecordType.getName());
+ throw new UnableToCompleteException();
+ }
+ }
+}
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/server/RequestFactoryServlet.java
Mon May 3 13:28:30 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/server/RequestFactoryServlet.java
Tue May 4 15:09:04 2010
@@ -280,9 +280,18 @@
tokenToEntityRecord = new HashMap<String, EntityRecordPair>();
for (Class<? extends Record> recordClass :
config.recordTypes()) {
ServerType serverType =
recordClass.getAnnotation(ServerType.class);
- String token = serverType.token();
- if ("[UNASSIGNED]".equals(token)) {
- token = recordClass.getSimpleName();
+ String token = (String)
recordClass.getField("TOKEN").get(null);
+ if (token == null) {
+ throw new IllegalStateException("TOKEN field on "
+ + recordClass.getName() + " can not be null");
+ }
+ EntityRecordPair previousValue =
tokenToEntityRecord.get(token);
+ if (previousValue != null) {
+ throw new IllegalStateException(
+ "TOKEN fields have to be unique. TOKEN fields for
both "
+ + recordClass.getName() + " and "
+ + previousValue.record.getName()
+ + " have the same value, value = " + token);
}
tokenToEntityRecord.put(token, new EntityRecordPair(
serverType.type(), recordClass));
@@ -299,6 +308,8 @@
failConfig(e);
} catch (ClassCastException e) {
failConfig(e);
+ } catch (NoSuchFieldException e) {
+ failConfig(e);
}
}
}
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/shared/ServerType.java
Tue Apr 27 18:21:20 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/requestfactory/shared/ServerType.java
Tue May 4 15:09:04 2010
@@ -21,16 +21,12 @@
import java.lang.annotation.Target;
/**
- * Annotation on Record classes specifying 'type' and 'token'. 'type'
represents
- * the server-side counterpart of the Record. 'token' is an optional
String that
- * is sent in sync requests to the server. server and their type.
If 'token' is
- * absent, the record name is used instead.
+ * Annotation on Record classes specifying 'type'. 'type' represents the
+ * server-side counterpart of the Record.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ServerType {
- String token() default "[UNASSIGNED]";
-
Class<?> type();
}
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/EmployeeRecord.java
Tue Apr 27 18:21:20 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/EmployeeRecord.java
Tue May 4 15:09:04 2010
@@ -25,8 +25,15 @@
* <p>
* IRL this class will be generated by a JPA-savvy tool run before
compilation.
*/
-...@servertype(type =
com.google.gwt.sample.expenses.server.domain.Employee.class, token
= "Employee")
+...@servertype(type =
com.google.gwt.sample.expenses.server.domain.Employee.class)
public interface EmployeeRecord extends Record {
+
+ /**
+ * Used as input to {...@link
com.google.gwt.valuestore.shared.DeltaValueStore#create(Record)
+ * DeltaValueStore#create()} and in the wire format during sync requests.
+ */
+ String TOKEN = "EmployeeRecord";
+
Property<String> userName = new Property<String>("userName",
String.class);
Property<String> displayName = new Property<String>("displayName",
String.class);
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ExpenseRecord.java
Fri Apr 30 09:10:49 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ExpenseRecord.java
Tue May 4 15:09:04 2010
@@ -30,6 +30,12 @@
@ServerType(type =
com.google.gwt.sample.expenses.server.domain.Expense.class)
public interface ExpenseRecord extends Record {
+ /**
+ * Used as input to {...@link
com.google.gwt.valuestore.shared.DeltaValueStore#create(Record)
+ * DeltaValueStore#create()} and in the wire format during sync requests.
+ */
+ String TOKEN = "ExpenseRecord";
+
Property<Double> amount = new Property<Double>("amount", Double.class);
Property<String> approval = new Property<String>("approval",
String.class);
Property<String> category = new Property<String>("category",
String.class);
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ReportRecord.java
Wed Apr 28 10:57:16 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/sample/expenses/gwt/request/ReportRecord.java
Tue May 4 15:09:04 2010
@@ -30,6 +30,12 @@
@ServerType(type =
com.google.gwt.sample.expenses.server.domain.Report.class)
public interface ReportRecord extends Record {
+ /**
+ * Used as input to {...@link
com.google.gwt.valuestore.shared.DeltaValueStore#create(Record)
+ * DeltaValueStore#create()} and in the wire format during sync requests.
+ */
+ String TOKEN = "ReportRecord";
+
Property<String> approvedSupervisorKey = new
Property<String>("approvedSupervisorKey",
String.class);
Property<Date> created = new Property<Date>("created", Date.class);
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/DeltaValueStoreJsonImpl.java
Mon May 3 13:28:30 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/valuestore/client/DeltaValueStoreJsonImpl.java
Tue May 4 15:09:04 2010
@@ -248,13 +248,11 @@
return syncResults;
}
- // TODO: don't use RecordSchema
- public Record create(Record record) {
+ public Record create(String token) {
assert !used;
- assert record instanceof RecordImpl;
- RecordImpl recordImpl = (RecordImpl) record;
String futureId = futureIdGenerator.getFutureId();
- RecordJsoImpl newRecord = RecordJsoImpl.newCopy(recordImpl.getSchema(),
+ // TODO: get schema from token
+ RecordJsoImpl newRecord = RecordJsoImpl.newCopy(null,
futureId, INITIAL_VERSION);
RecordKey recordKey = new RecordKey(newRecord);
assert operations.get(recordKey) == null;
=======================================
---
/branches/2.1/bikeshed/src/com/google/gwt/valuestore/shared/DeltaValueStore.java
Mon May 3 13:28:30 2010
+++
/branches/2.1/bikeshed/src/com/google/gwt/valuestore/shared/DeltaValueStore.java
Tue May 4 15:09:04 2010
@@ -26,7 +26,7 @@
*/
void clearUsed();
- Record create(Record existingRecord);
+ Record create(String token);
void delete(Record record);
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors