Fixing ContextURL for contained entity sets

This commit is contained in:
Francesco Chicchiriccò 2014-07-21 16:42:59 +02:00
parent 08218d4edd
commit 3a060746c3
4 changed files with 98 additions and 69 deletions

View File

@ -323,6 +323,37 @@ public abstract class AbstractODataBinder implements CommonODataBinder {
}
}
private EdmEntityType findEntityType(
final String entitySetOrSingletonOrType, final EdmEntityContainer container) {
EdmEntityType type = null;
final String firstToken = StringUtils.substringBefore(entitySetOrSingletonOrType, "/");
EdmBindingTarget bindingTarget = container.getEntitySet(firstToken);
if (bindingTarget == null) {
bindingTarget = container.getSingleton(firstToken);
}
if (bindingTarget != null) {
type = bindingTarget.getEntityType();
}
if (entitySetOrSingletonOrType.indexOf('/') != -1) {
final String[] splitted = entitySetOrSingletonOrType.split("/");
if (splitted.length > 1) {
for (int i = 1; i < splitted.length && type != null; i++) {
final EdmNavigationProperty navProp = type.getNavigationProperty(splitted[i]);
if (navProp == null) {
type = null;
} else {
type = navProp.getType();
}
}
}
}
return type;
}
/**
* Infer type name from various sources of information including Edm and context URL, if available.
*
@ -340,19 +371,17 @@ public abstract class AbstractODataBinder implements CommonODataBinder {
for (EdmSchema schema : edm.getSchemas()) {
final EdmEntityContainer container = schema.getEntityContainer();
if (container != null) {
EdmBindingTarget bindingTarget = container.getEntitySet(contextURL.getEntitySetOrSingletonOrType());
if (bindingTarget == null) {
bindingTarget = container.getSingleton(contextURL.getEntitySetOrSingletonOrType());
}
if (bindingTarget != null) {
final EdmEntityType entityType = findEntityType(contextURL.getEntitySetOrSingletonOrType(), container);
if (entityType != null) {
if (contextURL.getNavOrPropertyPath() == null) {
type = bindingTarget.getEntityType();
type = entityType;
} else {
final EdmNavigationProperty navProp = bindingTarget.getEntityType().
getNavigationProperty(contextURL.getNavOrPropertyPath());
final EdmNavigationProperty navProp =
entityType.getNavigationProperty(contextURL.getNavOrPropertyPath());
type = navProp == null
? bindingTarget.getEntityType()
? entityType
: navProp.getType();
}
}

View File

@ -18,6 +18,10 @@
*/
package org.apache.olingo.client.core.v4;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@ -30,7 +34,6 @@ import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.ODataFormat;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.ArrayList;
@ -38,10 +41,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class JSONTest extends AbstractTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

View File

@ -82,19 +82,15 @@ public class ContextURL {
} else if (contextURLasString.endsWith("/$ref")) {
instance.suffix = Suffix.REFERENCE;
contextURLasString = contextURLasString.replace("/$ref", StringUtils.EMPTY);
} else if (contextURLasString.endsWith("/$delta")) {
instance.suffix = Suffix.DELTA;
contextURLasString = contextURLasString.replace("/$delta", StringUtils.EMPTY);
} else if (contextURLasString.endsWith("/$deletedEntity")) {
instance.suffix = Suffix.DELTA_DELETED_ENTITY;
contextURLasString = contextURLasString.replace("/$deletedEntity", StringUtils.EMPTY);
} else if (contextURLasString.endsWith("/$link")) {
instance.suffix = Suffix.DELTA_LINK;
contextURLasString = contextURLasString.replace("/$link", StringUtils.EMPTY);
} else if (contextURLasString.endsWith("/$deletedLink")) {
instance.suffix = Suffix.DELTA_DELETED_LINK;
contextURLasString = contextURLasString.replace("/$deletedLink", StringUtils.EMPTY);
@ -125,8 +121,8 @@ public class ContextURL {
}
}
final int slashIdx = instance.entitySetOrSingletonOrType.indexOf('/');
if (slashIdx != -1) {
final int slashIdx = instance.entitySetOrSingletonOrType.lastIndexOf('/');
if (slashIdx != -1 && instance.entitySetOrSingletonOrType.substring(slashIdx + 1).indexOf('.') != -1) {
final String clone = instance.entitySetOrSingletonOrType;
instance.entitySetOrSingletonOrType = clone.substring(0, slashIdx);
instance.derivedEntity = clone.substring(slashIdx + 1);
@ -226,7 +222,7 @@ public class ContextURL {
}
public ContextURL build() {
StringBuilder result = new StringBuilder();
final StringBuilder result = new StringBuilder();
if (contextURL.serviceRoot != null) {
result.append(contextURL.serviceRoot);
}

View File

@ -18,6 +18,11 @@
*/
package org.apache.olingo.commons.api.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.apache.olingo.commons.api.data.ContextURL.Suffix;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmEntityType;
@ -27,11 +32,6 @@ import org.mockito.Mockito;
import java.net.URI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class ContextURLTest {
@Test
@ -52,6 +52,11 @@ public class ContextURLTest {
assertNull(contextURL.getSelectList());
assertEquals("Items", contextURL.getNavOrPropertyPath());
assertFalse(contextURL.isEntity());
contextURL = ContextURL.getInstance(URI.create("http://host/service/$metadata#Me/Folders('Inbox')/Messages"));
assertEquals("Me/Folders", contextURL.getEntitySetOrSingletonOrType());
assertEquals("Messages", contextURL.getNavOrPropertyPath());
}
@Test