Skip to content
Permalink
Browse files

[OLINGO-1054]Inline Consumer Callback Data handling

  • Loading branch information...
ramya vasanth
ramya vasanth committed Aug 28, 2019
1 parent 9631139 commit e8ec21c40efe29c9ac0aaee507e905ff69dc249c
@@ -321,7 +321,7 @@ private void readNavigationProperty(final String navigationPropertyName) throws
resultEntry.setContainsInlineEntry(true);
} else {
ReadFeedResult result = new ReadFeedResult(inlineReadProperties, navigationProperty,
feed, entryMetadata.getId());
feed, entryMetadata.getId() != null ? entryMetadata.getId() : fetchParentIdInfo(eia, resultEntry));
callback.handleReadFeed(result);
}
} else {
@@ -333,7 +333,7 @@ private void readNavigationProperty(final String navigationPropertyName) throws
resultEntry.setContainsInlineEntry(true);
} else {
ReadEntryResult result = new ReadEntryResult(inlineReadProperties, navigationProperty,
entry, entryMetadata.getId());
entry, entryMetadata.getId() != null ? entryMetadata.getId() : fetchParentIdInfo(eia, resultEntry));
callback.handleReadEntry(result);
}
}
@@ -374,7 +374,7 @@ private void readNavigationProperty(final String navigationPropertyName) throws
resultEntry.setContainsInlineEntry(true);
} else {
ReadFeedResult result = new ReadFeedResult(inlineReadProperties, navigationProperty,
feed, entryMetadata.getId());
feed, entryMetadata.getId() != null ? entryMetadata.getId() : fetchParentIdInfo(eia, resultEntry));
try {
callback.handleReadFeed(result);
} catch (final ODataApplicationException e) {
@@ -385,6 +385,31 @@ private void readNavigationProperty(final String navigationPropertyName) throws
}
}

/**
*
* @param eia
* @param resultEntry
* @return
* @throws EdmException
*/
private String fetchParentIdInfo(EntityInfoAggregator eia, ODataEntryImpl resultEntry) throws EdmException {
String keyInfo = "";
if (null != resultEntry) {
if (eia != null && eia.getEntityType() != null) {
List<String> keys = eia.getEntityType().getKeyPropertyNames();
int i = 0;
for (String key : keys) {
keyInfo += key + "=" + resultEntry.getProperties().get(key);
i++;
if (i < keys.size()) {
keyInfo += ",";
}
}
}
}
return keyInfo;
}

private void updateExpandSelectTree(final String navigationPropertyName, final ODataFeed feed) {
List<ODataEntry> entries = feed.getEntries();
if (!entries.isEmpty()) {
@@ -26,19 +26,25 @@
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.olingo.odata2.api.edm.EdmEntitySet;
import org.apache.olingo.odata2.api.edm.EdmEntityType;
import org.apache.olingo.odata2.api.edm.EdmException;
import org.apache.olingo.odata2.api.edm.EdmNavigationProperty;
import org.apache.olingo.odata2.api.edm.EdmType;
import org.apache.olingo.odata2.api.ep.EntityProviderException;
import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties;
import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode;
import org.apache.olingo.odata2.testutil.fit.BaseTest;
import org.apache.olingo.odata2.testutil.mock.MockFacade;

@@ -172,4 +178,85 @@ protected ODataFeed prepareAndExecuteFeed(final String fileName, final String en
return result;
}

/**
* @param inlineEntries
* @param feed
* @param entry
* @param edmEntityType
* @throws EdmException
*/
protected void getExpandedData(Map<String, Object> inlineEntries,
ODataEntry entry, EdmType edmType) throws EdmException {
assertNotNull(entry);
Map<String, ExpandSelectTreeNode> expandNodes = entry.getExpandSelectTree().getLinks();
for (Entry<String, ExpandSelectTreeNode> expand : expandNodes.entrySet()) {
assertNotNull(expand.getKey());
String keyName = extractKey(entry, (EdmEntityType)edmType, expand);
if (inlineEntries.containsKey(keyName)) {
if (inlineEntries.get(keyName) instanceof ODataFeed) {
ODataFeed innerFeed = (ODataFeed) inlineEntries.get(keyName);
assertNotNull(innerFeed);
getExpandedData(inlineEntries, innerFeed,
((EdmNavigationProperty)((EdmEntityType)edmType).getProperty(expand.getKey())).getType());
entry.getProperties().put(expand.getKey(), innerFeed);
} else if (inlineEntries.get(keyName) instanceof ODataEntry) {
ODataEntry innerEntry = (ODataEntry) inlineEntries.get(keyName);
assertNotNull(innerEntry);
getExpandedData(inlineEntries, innerEntry,
((EdmNavigationProperty)((EdmEntityType)edmType).getProperty(expand.getKey())).getType());
entry.getProperties().put(expand.getKey(), innerEntry);
}
}
}
}

/**
* Extract key information to map the parent entry to child entry
* @param entry
* @param edmEntityType
* @param expand
* @return
* @throws EdmException
*/
private String extractKey(ODataEntry entry, EdmEntityType edmEntityType, Entry<String,
ExpandSelectTreeNode> expand) throws EdmException {
return entry.getMetadata().getId() != null ?
(expand.getKey() + entry.getMetadata().getId()) :
(expand.getKey() + edmEntityType.getKeyPropertyNames().get(0) + "=" +
entry.getProperties().get(edmEntityType.getKeyPropertyNames().get(0)));
}

/**
* @param inlineEntries
* @param feed
* @param entry
* @throws EdmException
*/
protected void getExpandedData(Map<String, Object> inlineEntries,
ODataFeed feed, EdmType edmType) throws EdmException {
assertNotNull(feed.getEntries());
List<ODataEntry> entries = feed.getEntries();
for (ODataEntry entry : entries) {
Map<String, ExpandSelectTreeNode> expandNodes = entry.getExpandSelectTree().getLinks();
for (Entry<String, ExpandSelectTreeNode> expand : expandNodes.entrySet()) {
assertNotNull(expand.getKey());
String keyName = extractKey(entry, (EdmEntityType) edmType, expand);
if (inlineEntries.containsKey(keyName)) {
if (inlineEntries.get(keyName) instanceof ODataFeed) {
ODataFeed innerFeed = (ODataFeed) inlineEntries.get(keyName);
assertNotNull(innerFeed);
getExpandedData(inlineEntries, innerFeed,
((EdmNavigationProperty)((EdmEntityType)edmType).getProperty(expand.getKey())).getType());
feed.getEntries().get(feed.getEntries().indexOf(entry)).getProperties().put(expand.getKey(), innerFeed);
} else if (inlineEntries.get(keyName) instanceof ODataEntry) {
ODataEntry innerEntry = (ODataEntry) inlineEntries.get(keyName);
assertNotNull(innerEntry);
getExpandedData(inlineEntries, innerEntry,
((EdmNavigationProperty)((EdmEntityType)edmType).getProperty(expand.getKey())).getType());
feed.getEntries().get(feed.getEntries().indexOf(entry)).getProperties().put(expand.getKey(), innerEntry);
}
}
}
}
}
}
@@ -29,7 +29,6 @@
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.Map.Entry;

import org.apache.olingo.odata2.api.edm.EdmEntitySet;
import org.apache.olingo.odata2.api.ep.EntityProviderException;
@@ -38,7 +37,6 @@
import org.apache.olingo.odata2.api.ep.entry.MediaMetadata;
import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode;
import org.apache.olingo.odata2.testutil.mock.MockFacade;
import org.junit.Test;

@@ -371,7 +369,37 @@ public void employeesEntryWithEmployeeToRoomToBuilding() throws Exception {
assertEquals(9, result.getProperties().size());

Map<String, Object> inlineEntries = callback.getNavigationProperties();
getExpandedData(inlineEntries, result);
getExpandedData(inlineEntries, result, entitySet.getEntityType());
assertEquals(10, result.getProperties().size());
assertEquals(5, ((ODataEntry)result.getProperties().get("ne_Room")).getProperties().size());
assertEquals(3, ((ODataEntry)((ODataEntry)result.getProperties().get("ne_Room")).getProperties()
.get("nr_Building")).getProperties().size());
}

/**
* Employee with inline entity Room with inline entity Buildings with no metadata
* Scenario of 1:1:1 navigation
* E.g: Employees('1')?$expand=ne_Room/nr_Building
* @throws Exception
*/
@Test
public void employeesEntryWithEmployeeToRoomToBuildingWithoutMetadata() throws Exception {
InputStream stream = getFileAsStream("JsonEmployeeInlineRoomBuildingWithoutMetadata.json");
assertNotNull(stream);
FeedCallback callback = new FeedCallback();

EntityProviderReadProperties readProperties = EntityProviderReadProperties.init()
.mergeSemantic(false).callback(callback).build();

EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
JsonEntityConsumer xec = new JsonEntityConsumer();
ODataEntry result =
xec.readEntry(entitySet, stream, readProperties);
assertNotNull(result);
assertEquals(9, result.getProperties().size());

Map<String, Object> inlineEntries = callback.getNavigationProperties();
getExpandedData(inlineEntries, result, entitySet.getEntityType());
assertEquals(10, result.getProperties().size());
assertEquals(5, ((ODataEntry)result.getProperties().get("ne_Room")).getProperties().size());
assertEquals(3, ((ODataEntry)((ODataEntry)result.getProperties().get("ne_Room")).getProperties()
@@ -401,13 +429,45 @@ public void RoomEntryWithInlineEmployeeInlineTeam() throws Exception {
assertEquals(4, result.getProperties().size());

Map<String, Object> inlineEntries = callback.getNavigationProperties();
getExpandedData(inlineEntries, result);
getExpandedData(inlineEntries, result, entitySet.getEntityType());
assertEquals(5, result.getProperties().size());
for (ODataEntry employeeEntry : ((ODataFeed)result.getProperties().get("nr_Employees")).getEntries()) {
assertEquals(10, employeeEntry.getProperties().size());
assertEquals(3, ((ODataEntry)employeeEntry.getProperties().get("ne_Team")).getProperties().size());
}
}

/**
* Room has inline entity to Employees and has inline entry To Team
* Scenario of 1:n:1 navigation
* E.g: Rooms('1')?$expand=nr_Employees/ne_Team
* @throws Exception
*/
@Test
public void RoomEntryWithInlineEmployeeInlineTeamWithoutMetadata() throws Exception {
InputStream stream = getFileAsStream("JsonRoom_InlineEmployeesToTeamWithoutMetadata.json");
assertNotNull(stream);
FeedCallback callback = new FeedCallback();

EntityProviderReadProperties readProperties = EntityProviderReadProperties.init()
.mergeSemantic(false).callback(callback).build();

EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
JsonEntityConsumer xec = new JsonEntityConsumer();
ODataEntry result =
xec.readEntry(entitySet, stream, readProperties);
assertNotNull(result);
assertEquals(4, result.getProperties().size());

Map<String, Object> inlineEntries = callback.getNavigationProperties();
getExpandedData(inlineEntries, result, entitySet.getEntityType());
assertEquals(5, result.getProperties().size());
for (ODataEntry employeeEntry : ((ODataFeed)result.getProperties().get("nr_Employees")).getEntries()) {
assertEquals(10, employeeEntry.getProperties().size());
assertEquals(3, ((ODataEntry)employeeEntry.getProperties().get("ne_Team")).getProperties().size());
}
}

/**
* Room has empty inline entity to Employees and has inline entry To Team
* E.g: Rooms('10')?$expand=nr_Employees/ne_Team
@@ -430,7 +490,7 @@ public void RoomEntryWithEmptyInlineEmployeeInlineTeam() throws Exception {
assertEquals(4, result.getProperties().size());

Map<String, Object> inlineEntries = callback.getNavigationProperties();
getExpandedData(inlineEntries, result);
getExpandedData(inlineEntries, result, entitySet.getEntityType());
assertEquals(5, result.getProperties().size());
assertEquals(0, ((ODataFeed)result.getProperties().get("nr_Employees")).getEntries().size());
}
@@ -461,58 +521,4 @@ public void roomsFeedWithRoomInlineDifferent() throws Exception {
assertNull(entry.getExpandSelectTree().getExpandedList().get(2)
.getLinks().get("nr_Employees").getLinks().get("ne_Room"));
}

/**
* @param inlineEntries
* @param feed
* @param entry
*/
private void getExpandedData(Map<String, Object> inlineEntries, ODataEntry entry) {
assertNotNull(entry);
Map<String, ExpandSelectTreeNode> expandNodes = entry.getExpandSelectTree().getLinks();
for (Entry<String, ExpandSelectTreeNode> expand : expandNodes.entrySet()) {
assertNotNull(expand.getKey());
if (inlineEntries.containsKey(expand.getKey() + entry.getMetadata().getId())) {
if (inlineEntries.get(expand.getKey() + entry.getMetadata().getId()) instanceof ODataFeed) {
ODataFeed innerFeed = (ODataFeed) inlineEntries.get(expand.getKey() + entry.getMetadata().getId());
assertNotNull(innerFeed);
getExpandedData(inlineEntries, innerFeed);
entry.getProperties().put(expand.getKey(), innerFeed);
} else if (inlineEntries.get(expand.getKey() + entry.getMetadata().getId()) instanceof ODataEntry) {
ODataEntry innerEntry = (ODataEntry) inlineEntries.get(expand.getKey() + entry.getMetadata().getId());
assertNotNull(innerEntry);
getExpandedData(inlineEntries, innerEntry);
entry.getProperties().put(expand.getKey(), innerEntry);
}
}
}
}
/**
* @param inlineEntries
* @param feed
* @param entry
*/
private void getExpandedData(Map<String, Object> inlineEntries, ODataFeed feed) {
assertNotNull(feed.getEntries());
List<ODataEntry> entries = feed.getEntries();
for (ODataEntry entry : entries) {
Map<String, ExpandSelectTreeNode> expandNodes = entry.getExpandSelectTree().getLinks();
for (Entry<String, ExpandSelectTreeNode> expand : expandNodes.entrySet()) {
assertNotNull(expand.getKey());
if (inlineEntries.containsKey(expand.getKey() + entry.getMetadata().getId())) {
if (inlineEntries.get(expand.getKey() + entry.getMetadata().getId()) instanceof ODataFeed) {
ODataFeed innerFeed = (ODataFeed) inlineEntries.get(expand.getKey() + entry.getMetadata().getId());
assertNotNull(innerFeed);
getExpandedData(inlineEntries, innerFeed);
feed.getEntries().get(feed.getEntries().indexOf(entry)).getProperties().put(expand.getKey(), innerFeed);
} else if (inlineEntries.get(expand.getKey() + entry.getMetadata().getId()) instanceof ODataEntry) {
ODataEntry innerEntry = (ODataEntry) inlineEntries.get(expand.getKey() + entry.getMetadata().getId());
assertNotNull(innerEntry);
getExpandedData(inlineEntries, innerEntry);
feed.getEntries().get(feed.getEntries().indexOf(entry)).getProperties().put(expand.getKey(), innerEntry);
}
}
}
}
}
}

0 comments on commit e8ec21c

Please sign in to comment.
You can’t perform that action at this time.