Skip to content

Commit

Permalink
#214 Clean up some redundant code on included relationship extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch2na committed Nov 16, 2016
1 parent 72d4821 commit ebf7bdc
Showing 1 changed file with 38 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public IncludedRelationshipExtractor(ResourceRegistry resourceRegistry) {
public Map<ResourceDigest, Container> extractIncludedResources(Object resource, BaseResponseContext response) {
Map<ResourceDigest, Container> includedResources = new LinkedHashMap<>();

populateIncludedByDefaultResources(resource, response, ContainerType.TOP, includedResources, 1);
populateIncludedByDefaultResources(resource, response, ContainerType.TOP, includedResources);
populateIncludedRelationships(resource, response, includedResources);
LOGGER.debug("Extracted included resources {}", includedResources.toString());
return includedResources;
Expand All @@ -57,12 +57,7 @@ public Map<ResourceDigest, Container> extractIncludedResources(Object resource,
private void populateIncludedByDefaultResources(Object resource,
BaseResponseContext response,
ContainerType containerType,
Map<ResourceDigest, Container> includedResources,
int recurrenceLevel) {
int recurrenceLevelCounter = recurrenceLevel;
if (recurrenceLevel >= 42 || resource == null) {
return;
}
Map<ResourceDigest, Container> includedResources) {

Set<ResourceField> relationshipFields = getRelationshipFields(resource);

Expand All @@ -74,16 +69,48 @@ private void populateIncludedByDefaultResources(Object resource,
}
if ((containerType.equals(ContainerType.TOP) || containerType.equals(ContainerType.INCLUDED_DEFAULT))
&& resourceField.getIncludeByDefault()) {
recurrenceLevelCounter++;
populateIncludedByDefaultResourcesRecursive(targetDataObj, response, containerType, includedResources, recurrenceLevelCounter, resourceField);
populateIncludedByDefaultResourcesRecursive(targetDataObj, response, containerType, includedResources, resourceField);
} // if this is a top level container and its field matches the included parameters traverse further to find defaults
else if (containerType.equals(ContainerType.TOP) && isFieldIncluded(response, resourceField.getUnderlyingName())) {
recurrenceLevelCounter++;
populateIncludedByDefaultResourcesRecursive(targetDataObj, response, containerType, includedResources, recurrenceLevelCounter, resourceField);
populateIncludedByDefaultResourcesRecursive(targetDataObj, response, containerType, includedResources, resourceField);
}
}
}

private void populateIncludedByDefaultResourcesRecursive(Object targetDataObj,
BaseResponseContext response,
ContainerType containerType,
Map<ResourceDigest, Container> includedResourceContainers,
ResourceField resourceField) {
if (targetDataObj instanceof Iterable) {
for (Object objectItem : (Iterable) targetDataObj) {
if (objectItem == null) {
continue;
}
populateIncludedResourceContainers(objectItem, response, containerType, includedResourceContainers, resourceField);
}
} else {
populateIncludedResourceContainers(targetDataObj, response, containerType, includedResourceContainers, resourceField);
}

}

private void populateIncludedResourceContainers(Object targetDataObj,
BaseResponseContext response,
ContainerType containerType,
Map<ResourceDigest, Container> includedResourceContainers,
ResourceField resourceField) {
if (containerType.equals(ContainerType.TOP)) {
includedResourceContainers.put(getResourceDigest(targetDataObj), new Container(targetDataObj, response, containerType, resourceField.getJsonName(), 0, null));
populateIncludedByDefaultResources(targetDataObj, response, ContainerType.INCLUDED_DEFAULT, includedResourceContainers);
} else if (containerType.equals(ContainerType.INCLUDED_DEFAULT)) {
includedResourceContainers.put(getResourceDigest(targetDataObj), new Container(targetDataObj, response, containerType, resourceField.getJsonName(), 1, null));
populateIncludedByDefaultResources(targetDataObj, response, ContainerType.INCLUDED_DEFAULT_NESTED, includedResourceContainers);
} else if (containerType.equals(ContainerType.INCLUDED_DEFAULT_NESTED)) {
includedResourceContainers.put(getResourceDigest(targetDataObj), new Container(targetDataObj, response, containerType, resourceField.getJsonName(), 2, null));
}
}

private boolean isFieldIncluded(BaseResponseContext response, String fieldName) {
if (response.getQueryAdapter() == null ||
response.getQueryAdapter().getIncludedRelations() == null ||
Expand All @@ -106,38 +133,6 @@ private boolean isFieldIncluded(BaseResponseContext response, String fieldName)

}

private void populateIncludedByDefaultResourcesRecursive(Object targetDataObj,
BaseResponseContext response,
ContainerType containerType,
Map<ResourceDigest, Container> includedResourceContainers,
int recurrenceLevelCounter,
ResourceField resourceField) {
if (targetDataObj instanceof Iterable) {
for (Object objectItem : (Iterable) targetDataObj) {
if (objectItem == null) {
continue;
}
if (containerType.equals(ContainerType.TOP)) {
includedResourceContainers.put(getResourceDigest(objectItem), new Container(objectItem, response, containerType, resourceField.getJsonName(), 0, null));
populateIncludedByDefaultResources(objectItem, response, ContainerType.INCLUDED_DEFAULT, includedResourceContainers, recurrenceLevelCounter);
} else if (containerType.equals(ContainerType.INCLUDED_DEFAULT)) {
includedResourceContainers.put(getResourceDigest(objectItem), new Container(objectItem, response, containerType, resourceField.getJsonName(), 1, null));
populateIncludedByDefaultResources(objectItem, response, ContainerType.INCLUDED_DEFAULT_NESTED, includedResourceContainers, recurrenceLevelCounter);
}
}
} else {
includedResourceContainers.put(getResourceDigest(targetDataObj), new Container(targetDataObj, response, containerType));
if (containerType.equals(ContainerType.TOP)) {
includedResourceContainers.put(getResourceDigest(targetDataObj), new Container(targetDataObj, response, containerType, resourceField.getJsonName(), 0, null));
populateIncludedByDefaultResources(targetDataObj, response, ContainerType.INCLUDED_DEFAULT, includedResourceContainers, recurrenceLevelCounter);
} else if (containerType.equals(ContainerType.INCLUDED_DEFAULT)) {
includedResourceContainers.put(getResourceDigest(targetDataObj), new Container(targetDataObj, response, containerType, resourceField.getJsonName(), 1, null));
populateIncludedByDefaultResources(targetDataObj, response, ContainerType.INCLUDED_DEFAULT_NESTED, includedResourceContainers, recurrenceLevelCounter);
}
}

}

private void populateIncludedRelationships(Object resource, BaseResponseContext response, Map<ResourceDigest, Container> includedResources) {
if (response.getQueryAdapter() == null || response.getJsonPath() == null) {
return;
Expand Down

0 comments on commit ebf7bdc

Please sign in to comment.