Class DefaultUserRoleAncestryHydrationService
- java.lang.Object
-
- com.broadleafcommerce.auth.security.service.hydration.DefaultUserRoleAncestryHydrationService
-
- All Implemented Interfaces:
UserRoleAncestryHydrationService
public class DefaultUserRoleAncestryHydrationService extends Object implements UserRoleAncestryHydrationService
- Author:
- Samarth Dhruva (samarthd)
-
-
Constructor Summary
Constructors Constructor Description DefaultUserRoleAncestryHydrationService(UserRoleService<UserRole> userRoleService)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
attemptHydrationOfNextAncestor(String inProgressRoleToHydrate, UserRole highestAncestorThusFar, List<String> ancestorIdsSeenThusFar, Set<String> failedToFindRoleIds, Map<String,UserRole> foundRolesCache, Set<String> roleIdsToRemoveFromInProgress, Set<String> roleIdsToFindInDataStore, Map<String,UserRoleAncestryHydrationResponse> completed)
protected Set<String>
findInDataStoreAndReturnMissingIds(Set<String> roleIdsToFind, Map<String,UserRole> cacheToAddFoundTo)
Searches the data store for roles matching the givenroleIdsToFind
.Collection<UserRole>
getFlattenedWithAllAncestors(Collection<UserRole> roles)
A convenience method to enable fetching a flattened collection consisting of all of the given roles along with their ancestors.Collection<UserRole>
getFlattenedWithAllAncestorsAndThrowIfAnyFailed(Collection<UserRole> roles)
Same logic as documented forUserRoleAncestryHydrationService.getFlattenedWithAllAncestors(Collection)
except this method throws an exception when there's one or moreUserRoleAncestryHydrationResponse
not successful.protected List<UserRoleAncestryHydrationResponse>
getSuccessfulAndLogErrors(@NonNull Map<String,UserRoleAncestryHydrationResponse> responses)
A convenient method to process the response fromgetWithAllAncestors(Collection)
by logging errors on all roles which failed hydration and returning only the successful responses.protected UserRoleService<UserRole>
getUserRoleService()
Map<String,UserRoleAncestryHydrationResponse>
getWithAllAncestors(Collection<UserRole> roles)
Fetches all of the given roles' ancestors from the data store and returns a result containing them and the given roles themselves.Map<String,Collection<UserRole>>
getWithAllAncestorsByRoleIdAndThrowIfAnyFailed(Collection<UserRole> roles)
Same logic as documented forUserRoleAncestryHydrationService.getWithAllAncestors(Collection)
except this method throws an exception when there's one or moreUserRoleAncestryHydrationResponse
not successful, and the value of the map is a collection of the given roles along with their ancestors mapped by the given role id.protected void
markComplete(String roleIdToMarkComplete, UserRoleAncestryHydrationResponse completionResultForRole, Set<String> roleIdsToRemoveFromInProgress, Map<String,UserRoleAncestryHydrationResponse> completed)
-
-
-
Constructor Detail
-
DefaultUserRoleAncestryHydrationService
public DefaultUserRoleAncestryHydrationService(UserRoleService<UserRole> userRoleService)
-
-
Method Detail
-
getWithAllAncestors
public Map<String,UserRoleAncestryHydrationResponse> getWithAllAncestors(Collection<UserRole> roles)
Description copied from interface:UserRoleAncestryHydrationService
Fetches all of the given roles' ancestors from the data store and returns a result containing them and the given roles themselves.If a role does not have any ancestors, it will be returned by itself.
If any broken references or cycles are found in the ancestry of a role, its response will not be
UserRoleAncestryHydrationResponse.successful
.- Specified by:
getWithAllAncestors
in interfaceUserRoleAncestryHydrationService
- Parameters:
roles
- the roles whose ancestors should be fetched. These should be instances with their fields already fully populated (as they would be from the data store). Roles provided here are considered to be already-found and will re-appear in the result.- Returns:
- a map from each given role's id to the hydration response for it. There is guaranteed to be an entry in this map for each given role. The response for a particular role will have it along with all of its ancestors if successful, or a description of any error that was encountered. Do note that if multiple roles share the same ancestor, each of their ancestry lists will contain the same instance of that ancestor.
-
markComplete
protected void markComplete(String roleIdToMarkComplete, UserRoleAncestryHydrationResponse completionResultForRole, Set<String> roleIdsToRemoveFromInProgress, Map<String,UserRoleAncestryHydrationResponse> completed)
- Parameters:
roleIdToMarkComplete
- the id of the role which should be marked completecompletionResultForRole
- the result that the role should be completed withroleIdsToRemoveFromInProgress
- a set containing ids of roles that should no longer be considered in-progress. The givenroleIdToMarkComplete
will be added to this set.completed
- a map of completed role ids to their results. The givenroleIdToMarkComplete
will be put into this map with the value ofcompletionResultForRole
.
-
findInDataStoreAndReturnMissingIds
protected Set<String> findInDataStoreAndReturnMissingIds(Set<String> roleIdsToFind, Map<String,UserRole> cacheToAddFoundTo)
Searches the data store for roles matching the givenroleIdsToFind
. Roles that are found will be added to thecacheToAddFoundTo
. Any ids for which roles were not found will be returned.- Parameters:
roleIdsToFind
- the set of role ids to find in the data storecacheToAddFoundTo
- a map of role id to role, which all found roles will be added to- Returns:
- a set containing any ids for which roles were not found in the data store
-
attemptHydrationOfNextAncestor
protected void attemptHydrationOfNextAncestor(String inProgressRoleToHydrate, UserRole highestAncestorThusFar, List<String> ancestorIdsSeenThusFar, Set<String> failedToFindRoleIds, Map<String,UserRole> foundRolesCache, Set<String> roleIdsToRemoveFromInProgress, Set<String> roleIdsToFindInDataStore, Map<String,UserRoleAncestryHydrationResponse> completed)
- Parameters:
inProgressRoleToHydrate
- the id of a role whose ancestor hydration was originally requestedhighestAncestorThusFar
- the highest-level ancestor that has been successfully found for theinProgressRoleToHydrate
thus far. Guaranteed to have a non-emptyUserRole.parentRoleId
representing the next ancestor to find.ancestorIdsSeenThusFar
- the list (in order of lowest to highest-level) of ids of ancestors that have been successfully found for theinProgressRoleToHydrate
thus far. The very last id in this list will belong tohighestAncestorThusFar
. If the next ancestor is found, it should be appended to this list.failedToFindRoleIds
- the ids of roles for which a query to the data store has already been attempted, but no corresponding record was foundfoundRolesCache
- all roles that have already been successfully found in the data store, mapped by their idroleIdsToRemoveFromInProgress
- a set of role ids that will be removed from the group of roles for which ancestor hydration is considered unfinished. TheinProgressRoleToHydrate
is currently a member of that group, so if this method determines that it should be marked completed, it should be added to this set (in addition to being added tocompleted
).roleIdsToFindInDataStore
- a set of role ids that will be queried for in the data store. If a role id needs to be found in the data store, it should be added to this set. The query will be performed and the results will be reflected infailedToFindRoleIds
andfoundRolesCache
on the next invocation of this method forinProgressRoleToHydrate
.completed
- the final map of role ids for which ancestor hydration is finished, to the result of performing that hydration. If this method determines thatinProgressRoleToHydrate
should be marked completed, it should be added to this map (in addition to being added toroleIdsToRemoveFromInProgress
)
-
getWithAllAncestorsByRoleIdAndThrowIfAnyFailed
public Map<String,Collection<UserRole>> getWithAllAncestorsByRoleIdAndThrowIfAnyFailed(Collection<UserRole> roles)
Description copied from interface:UserRoleAncestryHydrationService
Same logic as documented forUserRoleAncestryHydrationService.getWithAllAncestors(Collection)
except this method throws an exception when there's one or moreUserRoleAncestryHydrationResponse
not successful, and the value of the map is a collection of the given roles along with their ancestors mapped by the given role id.- Specified by:
getWithAllAncestorsByRoleIdAndThrowIfAnyFailed
in interfaceUserRoleAncestryHydrationService
- Parameters:
roles
- a collection of roles to get along with its ancestors- Returns:
- a collection containing the given roles along with their ancestors mapped by given role id
-
getFlattenedWithAllAncestors
public Collection<UserRole> getFlattenedWithAllAncestors(Collection<UserRole> roles)
Description copied from interface:UserRoleAncestryHydrationService
A convenience method to enable fetching a flattened collection consisting of all of the given roles along with their ancestors.This method should internally delegate to
UserRoleAncestryHydrationService.getWithAllAncestors(Collection)
to perform the actual ancestor hydration. Each role whose ancestry was not successfully hydrated will be omitted from the result, and an error will be logged for it.The result will be filtered for duplicates (can occur for example if an ancestor is shared by multiple roles) and thus each element should only appear once in the results.
- Specified by:
getFlattenedWithAllAncestors
in interfaceUserRoleAncestryHydrationService
- Parameters:
roles
- the roles whose ancestors should be fetched. These should be instances with their fields already fully populated (as they would be from the data store). Roles provided here are considered to be already-found and will re-appear in the result.- Returns:
- a flattened collection consisting of all of the given roles along with their ancestors, excluding any roles for which ancestry hydration failed
-
getFlattenedWithAllAncestorsAndThrowIfAnyFailed
public Collection<UserRole> getFlattenedWithAllAncestorsAndThrowIfAnyFailed(Collection<UserRole> roles)
Description copied from interface:UserRoleAncestryHydrationService
Same logic as documented forUserRoleAncestryHydrationService.getFlattenedWithAllAncestors(Collection)
except this method throws an exception when there's one or moreUserRoleAncestryHydrationResponse
not successful.- Specified by:
getFlattenedWithAllAncestorsAndThrowIfAnyFailed
in interfaceUserRoleAncestryHydrationService
- Parameters:
roles
- a collection of roles to get along with its ancestors- Returns:
- a collection containing the given roles along with their ancestors
-
getSuccessfulAndLogErrors
protected List<UserRoleAncestryHydrationResponse> getSuccessfulAndLogErrors(@NonNull @NonNull Map<String,UserRoleAncestryHydrationResponse> responses)
A convenient method to process the response fromgetWithAllAncestors(Collection)
by logging errors on all roles which failed hydration and returning only the successful responses.- Parameters:
responses
- the responses returned bygetWithAllAncestors(Collection)
- Returns:
- a list containing only the responses which were
UserRoleAncestryHydrationResponse.successful
-
getUserRoleService
protected UserRoleService<UserRole> getUserRoleService()
-
-