@ SuppressWarnings ( "unchecked" ) public Result < Set < Resource > > lookupRelatedResource ( IncludeRequest request , Collection < Resource > sourceResources , ResourceField relationshipField ) { if ( sourceResources . isEmpty ( ) ) { return resultFactory . just ( Collections . emptySet ( ) ) ; } // directly load where relationship data is available Collection < Resource > sourceResourcesWithData = new ArrayList <> ( ) ; Collection < Resource > sourceResourcesWithoutData = new ArrayList <> ( ) ; for ( Resource sourceResource : sourceResources ) { boolean present = sourceResource . getRelationships ( ) . get ( relationshipField . getJsonName ( ) ) . getData ( ) . isPresent ( ) ; if ( present ) { sourceResourcesWithData . add ( sourceResource ) ; } else { sourceResourcesWithoutData . add ( sourceResource ) ; } } Set < Resource > relatedResources = new HashSet <> ( ) ; Result < Set < Resource > > result = resultFactory . just ( relatedResources ) ; if ( ! sourceResourcesWithData . isEmpty ( ) ) { Result < Set < Resource >> lookupWithId = lookupRelatedResourcesWithId ( request , sourceResourcesWithData , relationshipField ) ; result = result . zipWith ( lookupWithId , this :: mergeList ) ; } if ( ! sourceResourcesWithoutData . isEmpty ( ) ) { Result < Set < Resource >> lookupWithoutData = lookupRelatedResourceWithRelationship ( request , sourceResourcesWithoutData , relationshipField ) ; result = result . zipWith ( lookupWithoutData , this :: mergeList ) ; } return result ; }
Loads all related resources for the given resources and relationship field . It updates the relationship data of the source resources accordingly and returns the loaded resources for potential inclusion in the result resource .