OLINGO-650: supporting the complex properties in expand context url generation

This commit is contained in:
Ramesh Reddy 2015-06-02 12:12:57 -05:00
parent cb0f7f2d70
commit 8f763aadea
3 changed files with 49 additions and 8 deletions

View File

@ -29,6 +29,8 @@ import org.apache.olingo.commons.api.edm.EdmStructuredType;
import org.apache.olingo.commons.core.Encoder;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.apache.olingo.server.api.uri.UriParameter;
import org.apache.olingo.server.api.uri.UriResource;
import org.apache.olingo.server.api.uri.UriResourceProperty;
import org.apache.olingo.server.api.uri.queryoption.ExpandItem;
import org.apache.olingo.server.api.uri.queryoption.ExpandOption;
import org.apache.olingo.server.api.uri.queryoption.SelectItem;
@ -117,11 +119,39 @@ public final class ContextURLHelper {
}
result.append(Encoder.encode(propertyName)).append('(').append(innerSelectList).append(')');
}
} else {
final List<UriResource> resourceParts = expandItem.getResourcePath().getUriResourceParts();
if(resourceParts.size() > 1) {
if (result.length() > 0) {
result.append(',');
}
final List<String> path = getPropertyPath(resourceParts);
String propertyPath = buildPropertyPath(path);
result.append(Encoder.encode(propertyName));
result.append("/").append(propertyPath);
}
}
}
}
}
private static List<String> getPropertyPath(final List<UriResource> path) {
List<String> result = new LinkedList<String>();
int index = 1;
while (index < path.size() && path.get(index) instanceof UriResourceProperty) {
result.add(((UriResourceProperty) path.get(index)).getProperty().getName());
index++;
}
return result;
}
private static String buildPropertyPath(final List<String> path) {
StringBuilder result = new StringBuilder();
for (final String segment : path) {
result.append(result.length() == 0 ? "" : '/').append(Encoder.encode(segment)); //$NON-NLS-1$
}
return result.length() == 0?null:result.toString();
}
private static List<List<String>> getComplexSelectedPaths(final EdmProperty edmProperty,
final Set<List<String>> selectedPaths) {
List<List<String>> result = new ArrayList<List<String>>();

View File

@ -127,15 +127,10 @@ public abstract class ExpandSelectHelper {
Set<String> expanded = new HashSet<String>();
for (final ExpandItem item : expandItems) {
final List<UriResource> resourceParts = item.getResourcePath().getUriResourceParts();
if (resourceParts.size() == 1) {
final UriResource resource = resourceParts.get(0);
if (resource instanceof UriResourceNavigation) {
expanded.add(((UriResourceNavigation) resource).getProperty().getName());
}
} else {
throw new SerializerException("Expand is not supported within complex properties.",
SerializerException.MessageKeys.NOT_IMPLEMENTED);
}
}
return expanded;
}

View File

@ -116,6 +116,22 @@ public class ContextURLHelperTest {
assertEquals("$metadata#ESTwoPrim", ContextURLBuilder.create(contextURL).toASCIIString());
}
@Test
public void buildExpandWithNavigationProperty() throws Exception {
final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoPrim");
final ExpandOption expand = ExpandSelectMock.mockExpandOption(Arrays.asList(
ExpandSelectMock.mockExpandItem(entitySet, "NavPropertyETAllPrimOne", "PropertyString")));
final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem(entitySet, "PropertyInt16");
final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(
selectItem1));
final ContextURL contextURL = ContextURL.with().entitySet(entitySet)
.selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), expand, select)).build();
assertEquals("$metadata#ESTwoPrim(PropertyInt16,NavPropertyETAllPrimOne/PropertyString)",
ContextURLBuilder.create(contextURL).toASCIIString());
}
@Test
public void buildExpandSelect() throws Exception {
final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoPrim");