[OLINGO-260] V4 bound operation invoke

Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/25a62716
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/25a62716
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/25a62716

Branch: refs/heads/master
Commit: 25a6271633a2df04f54275647ba0c5f9024851b3
Parents: 620f4e9
Author: Francesco Chicchiriccò <--global>
Authored: Wed May 14 13:28:48 2014 +0200
Committer: Stephan Klevenz <[email protected]>
Committed: Mon May 19 14:33:40 2014 +0200

----------------------------------------------------------------------
 .../olingo/ext/proxy/utils/CoreUtils.java       | 35 +++++++++++++++-----
 .../v4/BoundOperationInvokeTestITCase.java      |  6 ++--
 .../fit/proxy/v4/SingletonTestITCase.java       | 14 ++++++--
 3 files changed, 41 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/25a62716/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java
----------------------------------------------------------------------
diff --git 
a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java
 
b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java
index d1ea8a3..2601db3 100644
--- 
a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java
+++ 
b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java
@@ -19,6 +19,7 @@
 package org.apache.olingo.ext.proxy.utils;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -239,15 +240,17 @@ public final class CoreUtils {
     }
   }
 
-  public static Object primitiveValueToObject(final ODataPrimitiveValue value) 
{
+  private static Object primitiveValueToObject(final ODataPrimitiveValue 
value, final Class<?> reference) {
     Object obj;
 
     try {
       obj = value.toValue() instanceof Timestamp
               ? value.toCastValue(Calendar.class)
-              : value.toValue();
+              : reference == null
+              ? value.toValue()
+              : value.toCastValue(reference);
     } catch (EdmPrimitiveTypeException e) {
-      LOG.warn("Could not read temporal value as Calendar, reverting to 
Timestamp", e);
+      LOG.warn("While casting primitive value {} to {}", value, reference, e);
       obj = value.toValue();
     }
 
@@ -262,6 +265,19 @@ public final class CoreUtils {
     bean.getClass().getMethod(setterName, getter.getReturnType()).invoke(bean, 
value);
   }
 
+  private static Class<?> getPropertyClass(final Class<?> entityClass, final 
String propertyName) {
+    Class<?> propertyClass = null;
+    try {
+      final Field field = entityClass.getField(propertyName);
+      if (field != null) {
+        propertyClass = field.getType();
+      }
+    } catch (Exception e) {
+      LOG.error("Could not determine the Java type of {}", propertyName, e);
+    }
+    return propertyClass;
+  }
+
   public static Object getKey(
           final CommonEdmEnabledODataClient<?> client, final Class<?> 
entityTypeRef, final CommonODataEntity entity) {
 
@@ -272,7 +288,8 @@ public final class CoreUtils {
       if (keyRef == null) {
         final CommonODataProperty property = 
entity.getProperty(firstValidEntityKey(entityTypeRef));
         if (property != null && property.hasPrimitiveValue()) {
-          res = primitiveValueToObject(property.getPrimitiveValue());
+          res = primitiveValueToObject(
+                  property.getPrimitiveValue(), 
getPropertyClass(entityTypeRef, property.getName()));
         }
       } else {
         try {
@@ -332,7 +349,8 @@ public final class CoreUtils {
             if (property.hasNullValue()) {
               setPropertyValue(bean, getter, null);
             } else if (property.hasPrimitiveValue()) {
-              setPropertyValue(bean, getter, 
primitiveValueToObject(property.getPrimitiveValue()));
+              setPropertyValue(bean, getter, primitiveValueToObject(
+                      property.getPrimitiveValue(), 
getPropertyClass(reference, property.getName())));
             } else if (property.hasComplexValue()) {
               final Object complex = Proxy.newProxyInstance(
                       Thread.currentThread().getContextClassLoader(),
@@ -356,7 +374,8 @@ public final class CoreUtils {
               while (collPropItor.hasNext()) {
                 final ODataValue value = collPropItor.next();
                 if (value.isPrimitive()) {
-                  collection.add(primitiveValueToObject(value.asPrimitive()));
+                  collection.add(primitiveValueToObject(
+                          value.asPrimitive(), getPropertyClass(reference, 
property.getName())));
                 } else if (value.isComplex()) {
                   final Object collItem = Proxy.newProxyInstance(
                           Thread.currentThread().getContextClassLoader(),
@@ -436,7 +455,7 @@ public final class CoreUtils {
       while (collPropItor.hasNext()) {
         final ODataValue value = collPropItor.next();
         if (value.isPrimitive()) {
-          
collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive()));
+          collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive(), 
internalRef));
         } else if (value.isComplex()) {
           final Object collItem = Proxy.newProxyInstance(
                   Thread.currentThread().getContextClassLoader(),
@@ -452,7 +471,7 @@ public final class CoreUtils {
     } else if (property instanceof ODataProperty && ((ODataProperty) 
property).hasEnumValue()) {
       res = buildEnumInstance(((ODataProperty) property).getEnumValue());
     } else {
-      res = primitiveValueToObject(property.getPrimitiveValue());
+      res = primitiveValueToObject(property.getPrimitiveValue(), internalRef);
     }
 
     return res;

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/25a62716/fit/src/test/java/org/apache/olingo/fit/proxy/v4/BoundOperationInvokeTestITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/BoundOperationInvokeTestITCase.java
 
b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/BoundOperationInvokeTestITCase.java
index 7ee2234..7fe47ce 100644
--- 
a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/BoundOperationInvokeTestITCase.java
+++ 
b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/BoundOperationInvokeTestITCase.java
@@ -41,7 +41,7 @@ public class BoundOperationInvokeTestITCase extends 
AbstractTestITCase {
 
   @Test
   public void getEmployeesCount() {
-    
assertNotNull(container.getCompany().get().operations().getEmployeesCount());
+    
assertNotNull(container.getCompany().get(0).operations().getEmployeesCount());
   }
 
   @Test
@@ -80,7 +80,7 @@ public class BoundOperationInvokeTestITCase extends 
AbstractTestITCase {
 
   @Test
   public void increaseRevenue() {
-    final Long result = 
container.getCompany().get().operations().increaseRevenue(12L);
+    final Long result = 
container.getCompany().get(0).operations().increaseRevenue(12L);
     assertNotNull(result);
   }
 
@@ -103,7 +103,7 @@ public class BoundOperationInvokeTestITCase extends 
AbstractTestITCase {
             resetAddress(Collections.singletonList(address), 0);
     assertEquals(2, person.getPersonID(), 0);
   }
-
+  
   @Test
   public void refreshDefaultPI() {
     final PaymentInstrument pi = 
container.getAccounts().get(101).operations().refreshDefaultPI(Calendar.getInstance());

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/25a62716/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java 
b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java
index 6886756..b4bdda7 100644
--- a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java
@@ -21,11 +21,9 @@ package org.apache.olingo.fit.proxy.v4;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import 
org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.Company;
 import org.junit.Test;
 
-/**
- * This is the unit test class to check entity create operations.
- */
 public class SingletonTestITCase extends AbstractTestITCase {
 
   @Test
@@ -37,4 +35,14 @@ public class SingletonTestITCase extends AbstractTestITCase {
     assertEquals(1, container.getCompany().count(), 0);
     entityContext.detachAll();
   }
+
+  @Test
+  public void update() {
+    final Company company = container.getCompany().get(0);
+    company.setRevenue(132520L);
+
+    container.flush();
+
+    assertEquals(132520L, container.getCompany().get(0).getRevenue(), 0);
+  }
 }

Reply via email to