Revision: 7768
Author: [email protected]
Date: Tue Mar 23 06:32:25 2010
Log: Got rid of the MethodName enum. This change is on way to relying solely on RequestFactory config

Patch by: amitmanjhi
Review by: rjrjr.

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

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

Deleted:
 /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/MethodName.java
Modified:
/trunk/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java /trunk/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java

=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/MethodName.java Thu Mar 18 15:14:08 2010
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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.sample.expenses.gen;
-
-/**
- * Represents the MethodName.
- *
- * TODO: Remove this class in preference to RequestFactory interfaces or
- * generate automatically from RequestFactory interfaces.
- */
-public enum MethodName {
-  FIND_ALL_EMPLOYEES("Employee", "findAllEmployees"), FIND_ALL_REPORTS(
- "Report", "findAllReports"), FIND_EMPLOYEE("Employee", "findEmployee"), FIND_REPORTS_BY_EMPLOYEE(
-      "Report", "findReportsByEmployee"), SYNC("", "");
-
-  /* the className that contains the method */
-  private final String className;
-
-  /* the methodName */
-  private final String methodName;
-
-  private MethodName(String className, String methodName) {
-    this.className = className;
-    this.methodName = methodName;
-  }
-
-  /**
-   * @return the className
-   */
-  public String getClassName() {
-    return className;
-  }
-
-  /**
-   * @return the methodName
-   */
-  public String getMethodName() {
-    return methodName;
-  }
-}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java Mon Mar 22 16:05:18 2010 +++ /trunk/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java Tue Mar 23 06:32:25 2010
@@ -24,7 +24,6 @@
 import com.google.gwt.requestfactory.shared.RequestFactory;
 import com.google.gwt.requestfactory.shared.SyncRequest;
 import com.google.gwt.requestfactory.shared.impl.RequestDataManager;
-import com.google.gwt.sample.expenses.gen.MethodName;
 import com.google.gwt.user.client.ui.HasValue;
 import com.google.gwt.user.client.ui.HasValueList;
 import com.google.gwt.valuestore.client.ValuesImpl;
@@ -123,8 +122,9 @@
         }
         requestData.append("]");

+        // TODO: Fix the className for this request
builder.setRequestData(ClientRequestObject.getRequestString(RequestDataManager.getRequestMap(
-            MethodName.SYNC, null, requestData.toString())));
+            "", "sync", null, requestData.toString())));
         builder.setCallback(new RequestCallback() {

           public void onError(Request request, Throwable exception) {
=======================================
--- /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java Mon Mar 22 16:05:18 2010 +++ /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java Tue Mar 23 06:32:25 2010
@@ -15,8 +15,6 @@
  */
 package com.google.gwt.requestfactory.shared.impl;

-import com.google.gwt.sample.expenses.gen.MethodName;
-
 import java.util.HashMap;
 import java.util.Map;

@@ -28,6 +26,7 @@
  */
 public class RequestDataManager {

+  public static final String CLASS_TOKEN = "className";
   public static final String CONTENT_TOKEN = "contentData";
   public static final String METHOD_TOKEN = "methodName";
   public static final String PARAM_TOKEN = "param";
@@ -47,10 +46,11 @@
    * Returns the string that encodes the request data.
    *
    */
-  public static Map<String, String> getRequestMap(MethodName methodName,
-      Object values[], String content) {
+  public static Map<String, String> getRequestMap(String className,
+      String methodName, Object values[], String content) {
     Map<String, String> requestMap = new HashMap<String, String>();
-    requestMap.put(METHOD_TOKEN, methodName.name());
+    requestMap.put(CLASS_TOKEN, className);
+    requestMap.put(METHOD_TOKEN, methodName);
     if (values != null) {
       for (int i = 0; i < values.length; i++) {
         Object value = values[i];
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java Mon Mar 22 16:05:18 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java Tue Mar 23 06:32:25 2010
@@ -58,8 +58,8 @@
   public EntityListRequest<EmployeeKey> findAllEmployees() {
     return new Request() {
       public String getRequestData() {
- return ClientRequestObject.getRequestString(RequestDataManager.getRequestMap(MethodName.FIND_ALL_EMPLOYEES,
-            null, null));
+ return ClientRequestObject.getRequestString(RequestDataManager.getRequestMap(
+            "Employee", "findAllEmployees", null, null));
       }
     };
   }
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java Mon Mar 22 16:05:18 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java Tue Mar 23 06:32:25 2010
@@ -60,7 +60,7 @@
     return new Request() {
       public String getRequestData() {
return ClientRequestObject.getRequestString(RequestDataManager.getRequestMap(
-            MethodName.FIND_ALL_REPORTS, null, null));
+            "Report", "findAllReports", null, null));
       }

     };
@@ -71,7 +71,7 @@
     return new Request() {
       public String getRequestData() {
return ClientRequestObject.getRequestString(RequestDataManager.getRequestMap( - MethodName.FIND_REPORTS_BY_EMPLOYEE, new Object[] {id.get()}, null)); + "Report", "findReportsByEmployee", new Object[] {id.get()}, null));
       }
     };
   }
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java Mon Mar 22 16:05:18 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java Tue Mar 23 06:32:25 2010
@@ -17,7 +17,6 @@

 import com.google.gwt.requestfactory.shared.EntityKey;
 import com.google.gwt.requestfactory.shared.impl.RequestDataManager;
-import com.google.gwt.sample.expenses.gen.MethodName;
 import com.google.gwt.sample.expenses.server.domain.Report;
 import com.google.gwt.sample.expenses.server.domain.Storage;
 import com.google.gwt.sample.expenses.shared.ReportKey;
@@ -62,57 +61,52 @@
protected void doPost(HttpServletRequest request, HttpServletResponse response)
       throws IOException {

-    MethodName methodName = null;
+    String className = null;
+    String methodName = null;
     try {
       response.setStatus(HttpServletResponse.SC_OK);
       JSONObject topLevelJsonObject = new JSONObject(getContent(request));
- methodName = getMethodName(topLevelJsonObject.getString(RequestDataManager.METHOD_TOKEN)); + className = topLevelJsonObject.getString(RequestDataManager.CLASS_TOKEN); + methodName = topLevelJsonObject.getString(RequestDataManager.METHOD_TOKEN);
       PrintWriter writer = response.getWriter();
-      switch (methodName) {
-        case FIND_ALL_EMPLOYEES:
-        case FIND_ALL_REPORTS:
-        case FIND_EMPLOYEE:
-        case FIND_REPORTS_BY_EMPLOYEE:
- Class<?> classOperation = Class.forName("com.google.gwt.sample.expenses.server.domain."
-              + methodName.getClassName());
-          Method methodOperation = null;
-          // TODO: check if method names must be unique in a class.
-          for (Method method : classOperation.getDeclaredMethods()) {
-            if (method.getName().equals(methodName.getMethodName())) {
-              methodOperation = method;
-              break;
-            }
-          }
-          if (methodOperation == null) {
-            throw new IllegalArgumentException("unable to find "
-                + methodName.getMethodName() + " in " + classOperation);
-          }
-          if (!Modifier.isStatic(methodOperation.getModifiers())) {
-            throw new IllegalArgumentException("the "
-                + methodOperation.getName() + " is not static");
-          }
-          Object args[] = RequestDataManager.getObjectsFromParameterMap(
- getParameterMap(topLevelJsonObject), methodOperation.getParameterTypes());
-          Object resultList = methodOperation.invoke(null, args);
-          if (!(resultList instanceof List)) {
-            throw new IllegalArgumentException("return value not a list "
-                + resultList);
-          }
-          JSONArray jsonArray = getJsonArray((List<?>) resultList);
-          writer.print(jsonArray.toString());
-          break;
-        case SYNC:
- sync(topLevelJsonObject.getString(RequestDataManager.CONTENT_TOKEN), writer);
-          break;
-        default:
-          System.err.println("POST: unknown method " + methodName);
-          break;
+      if (methodName.equals("sync")) {
+ sync(topLevelJsonObject.getString(RequestDataManager.CONTENT_TOKEN),
+            writer);
+      } else {
+ Class<?> classOperation = Class.forName("com.google.gwt.sample.expenses.server.domain."
+            + className);
+        Method methodOperation = null;
+        // TODO: check if method names must be unique in a class.
+        for (Method method : classOperation.getDeclaredMethods()) {
+          if (method.getName().equals(methodName)) {
+            methodOperation = method;
+            break;
+          }
+        }
+        if (methodOperation == null) {
+          throw new IllegalArgumentException("unable to find " + methodName
+              + " in " + classOperation);
+        }
+        if (!Modifier.isStatic(methodOperation.getModifiers())) {
+ throw new IllegalArgumentException("the " + methodOperation.getName()
+              + " is not static");
+        }
+        Object args[] = RequestDataManager.getObjectsFromParameterMap(
+            getParameterMap(topLevelJsonObject),
+            methodOperation.getParameterTypes());
+        Object resultList = methodOperation.invoke(null, args);
+        if (!(resultList instanceof List)) {
+          throw new IllegalArgumentException("return value not a list "
+              + resultList);
+        }
+        JSONArray jsonArray = getJsonArray((List<?>) resultList);
+        writer.print(jsonArray.toString());
       }
       writer.flush();
       // TODO: clean exception handling code below.
     } catch (ClassNotFoundException e) {
       throw new IllegalArgumentException("unable to load the class: "
-          + methodName);
+          + className);
     } catch (IllegalAccessException e) {
       throw new IllegalArgumentException(e);
     } catch (InvocationTargetException e) {
@@ -160,7 +154,8 @@
Class<?> entityKeyClass = Class.forName("com.google.gwt.sample.expenses.shared."
         + entityClass.getSimpleName() + "Key");

- EntityKey<?> key = (EntityKey<?>) entityKeyClass.getMethod("get").invoke(null); + EntityKey<?> key = (EntityKey<?>) entityKeyClass.getMethod("get").invoke(
+        null);
     for (Object entityElement : resultList) {
       JSONObject jsonObject = new JSONObject();
       for (Property<?, ?> p : key.getProperties()) {
@@ -175,19 +170,6 @@
     }
     return jsonArray;
   }
-
-  /**
-   * @param request
-   * @return
-   */
-  private MethodName getMethodName(String methodString) {
-    for (MethodName method : MethodName.values()) {
-      if (method.name().equals(methodString)) {
-        return method;
-      }
-    }
- throw new IllegalArgumentException("unknown methodName: " + methodString);
-  }

   /**
* Returns methodName corresponding to the propertyName that can be invoked on
@@ -211,7 +193,8 @@
    * @return
    * @throws JSONException
    */
- private Map<String, String> getParameterMap(JSONObject jsonObject) throws JSONException {
+  private Map<String, String> getParameterMap(JSONObject jsonObject)
+      throws JSONException {
     Map<String, String> parameterMap = new HashMap<String, String>();
     Iterator keys = jsonObject.keys();
     while (keys.hasNext()) {

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

To unsubscribe from this group, send email to 
google-web-toolkit-contributors+unsubscribegooglegroups.com or reply to this email with 
the words "REMOVE ME" as the subject.

Reply via email to