[OLINGO-260] V4 bound operation invoke

This commit is contained in:
Francesco Chicchiriccò 2014-05-14 13:28:48 +02:00 committed by Stephan Klevenz
parent 620f4e99ba
commit 25a6271633
3 changed files with 41 additions and 14 deletions

View File

@ -19,6 +19,7 @@
package org.apache.olingo.ext.proxy.utils; package org.apache.olingo.ext.proxy.utils;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; 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; Object obj;
try { try {
obj = value.toValue() instanceof Timestamp obj = value.toValue() instanceof Timestamp
? value.toCastValue(Calendar.class) ? value.toCastValue(Calendar.class)
: value.toValue(); : reference == null
? value.toValue()
: value.toCastValue(reference);
} catch (EdmPrimitiveTypeException e) { } 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(); obj = value.toValue();
} }
@ -262,6 +265,19 @@ public final class CoreUtils {
bean.getClass().getMethod(setterName, getter.getReturnType()).invoke(bean, value); 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( public static Object getKey(
final CommonEdmEnabledODataClient<?> client, final Class<?> entityTypeRef, final CommonODataEntity entity) { final CommonEdmEnabledODataClient<?> client, final Class<?> entityTypeRef, final CommonODataEntity entity) {
@ -272,7 +288,8 @@ public final class CoreUtils {
if (keyRef == null) { if (keyRef == null) {
final CommonODataProperty property = entity.getProperty(firstValidEntityKey(entityTypeRef)); final CommonODataProperty property = entity.getProperty(firstValidEntityKey(entityTypeRef));
if (property != null && property.hasPrimitiveValue()) { if (property != null && property.hasPrimitiveValue()) {
res = primitiveValueToObject(property.getPrimitiveValue()); res = primitiveValueToObject(
property.getPrimitiveValue(), getPropertyClass(entityTypeRef, property.getName()));
} }
} else { } else {
try { try {
@ -332,7 +349,8 @@ public final class CoreUtils {
if (property.hasNullValue()) { if (property.hasNullValue()) {
setPropertyValue(bean, getter, null); setPropertyValue(bean, getter, null);
} else if (property.hasPrimitiveValue()) { } else if (property.hasPrimitiveValue()) {
setPropertyValue(bean, getter, primitiveValueToObject(property.getPrimitiveValue())); setPropertyValue(bean, getter, primitiveValueToObject(
property.getPrimitiveValue(), getPropertyClass(reference, property.getName())));
} else if (property.hasComplexValue()) { } else if (property.hasComplexValue()) {
final Object complex = Proxy.newProxyInstance( final Object complex = Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(), Thread.currentThread().getContextClassLoader(),
@ -356,7 +374,8 @@ public final class CoreUtils {
while (collPropItor.hasNext()) { while (collPropItor.hasNext()) {
final ODataValue value = collPropItor.next(); final ODataValue value = collPropItor.next();
if (value.isPrimitive()) { if (value.isPrimitive()) {
collection.add(primitiveValueToObject(value.asPrimitive())); collection.add(primitiveValueToObject(
value.asPrimitive(), getPropertyClass(reference, property.getName())));
} else if (value.isComplex()) { } else if (value.isComplex()) {
final Object collItem = Proxy.newProxyInstance( final Object collItem = Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(), Thread.currentThread().getContextClassLoader(),
@ -436,7 +455,7 @@ public final class CoreUtils {
while (collPropItor.hasNext()) { while (collPropItor.hasNext()) {
final ODataValue value = collPropItor.next(); final ODataValue value = collPropItor.next();
if (value.isPrimitive()) { if (value.isPrimitive()) {
collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive())); collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive(), internalRef));
} else if (value.isComplex()) { } else if (value.isComplex()) {
final Object collItem = Proxy.newProxyInstance( final Object collItem = Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(), Thread.currentThread().getContextClassLoader(),
@ -452,7 +471,7 @@ public final class CoreUtils {
} else if (property instanceof ODataProperty && ((ODataProperty) property).hasEnumValue()) { } else if (property instanceof ODataProperty && ((ODataProperty) property).hasEnumValue()) {
res = buildEnumInstance(((ODataProperty) property).getEnumValue()); res = buildEnumInstance(((ODataProperty) property).getEnumValue());
} else { } else {
res = primitiveValueToObject(property.getPrimitiveValue()); res = primitiveValueToObject(property.getPrimitiveValue(), internalRef);
} }
return res; return res;

View File

@ -41,7 +41,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
@Test @Test
public void getEmployeesCount() { public void getEmployeesCount() {
assertNotNull(container.getCompany().get().operations().getEmployeesCount()); assertNotNull(container.getCompany().get(0).operations().getEmployeesCount());
} }
@Test @Test
@ -80,7 +80,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
@Test @Test
public void increaseRevenue() { public void increaseRevenue() {
final Long result = container.getCompany().get().operations().increaseRevenue(12L); final Long result = container.getCompany().get(0).operations().increaseRevenue(12L);
assertNotNull(result); assertNotNull(result);
} }

View File

@ -21,11 +21,9 @@ package org.apache.olingo.fit.proxy.v4;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; 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; import org.junit.Test;
/**
* This is the unit test class to check entity create operations.
*/
public class SingletonTestITCase extends AbstractTestITCase { public class SingletonTestITCase extends AbstractTestITCase {
@Test @Test
@ -37,4 +35,14 @@ public class SingletonTestITCase extends AbstractTestITCase {
assertEquals(1, container.getCompany().count(), 0); assertEquals(1, container.getCompany().count(), 0);
entityContext.detachAll(); 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);
}
} }