Class AdminRoleHydrationService
java.lang.Object
com.broadleafcommerce.adminuser.user.service.hydration.AdminRoleHydrationService
A service that hydrates
AdminRoleRef
with values from the data store, and retrieves all
of the given roles along with their ancestors.
An AdminRoleRef
can have ancestors, starting with its direct parent defined in
AdminRoleRef.parentRoleId
up to the top-level ancestor.
This component enables efficient retrieval of all of a role's ancestors from the data store.
Furthermore, despite validation in AdminRoleValidator
against ancestry cycle formation
and broken references in standard CRUD operations, there are still scenarios where these issues
may come up.
This component accounts for these possibilities when fetching the ancestry of a role.
- Author:
- Samarth Dhruva (samarthd)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
protected void
attemptHydrationOfNextAncestor
(String inProgressRoleToHydrate, AdminRoleRef highestAncestorThusFar, List<String> ancestorIdsSeenThusFar, Set<String> failedToFindRoleIds, Map<String, AdminRoleRef> foundRolesCache, Set<String> roleIdsToRemoveFromInProgress, Set<String> roleIdsToFindInDataStore, Map<String, AdminRoleAncestryHydrationResponse> completed) findInDataStoreAndReturnMissingIds
(Set<String> roleIdsToFind, Map<String, AdminRoleRef> cacheToAddFoundTo) Searches the data store for roles matching the givenroleIdsToFind
.protected AuthProvider
protected String
getCycleFoundErrorMessage
(AdminRoleRef roleWithCycleCausingParent) Same asgetFlattenedWithAllAncestors(Collection)
except this method throws an exception when there's one or moreAdminRoleAncestryHydrationResponse
not successful.protected String
getLastElement
(List<String> list) protected String
getMissingAncestorErrorMessage
(String missingAncestorId) Fetches all of the given roles' ancestors from the data store and returns a result containing them and the given roles themselves.protected List<AdminRoleAncestryHydrationResponse>
getSuccessfulAndLogErrors
(@NonNull Map<String, AdminRoleAncestryHydrationResponse> 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.Fetches all of the given roles' ancestors from the data store and returns a result containing them and the given roles themselves.protected boolean
hasAnyFailedRoleAncestryHydrationResponses
(Map<String, AdminRoleAncestryHydrationResponse> responses) protected boolean
hasParentRoleId
(AdminRoleRef role) hydrateRoleReferences
(Set<AdminRoleRef> toHydrate) Queries for each element intoHydrate
in the data store.protected Map<String,
AdminRoleRef> initializeFoundRolesCache
(@NonNull Collection<AdminRoleRef> roles) initializeInProgressRoles
(@NonNull Collection<AdminRoleRef> roles) protected boolean
isSuccessfulOrLogError
(Map.Entry<String, AdminRoleAncestryHydrationResponse> responseEntry) protected void
markComplete
(String roleIdToMarkComplete, AdminRoleAncestryHydrationResponse completionResultForRole, Set<String> roleIdsToRemoveFromInProgress, Map<String, AdminRoleAncestryHydrationResponse> completed)
-
Constructor Details
-
AdminRoleHydrationService
-
-
Method Details
-
hydrateRoleReferences
Queries for each element intoHydrate
in the data store. Returns a new set of elements that are either the hydrated roles (for each element that was found), or the given roles as-is (for each element that was not found).- Parameters:
toHydrate
- the set of elements to hydrate- Returns:
- a new set of hydrated elements
-
getWithAllAncestors
public Map<String,AdminRoleAncestryHydrationResponse> getWithAllAncestors(Collection<AdminRoleRef> roles) 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
AdminRoleAncestryHydrationResponse.successful
.- 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.
-
assertAllRolesHaveIds
-
hasParentRoleId
-
markComplete
protected void markComplete(String roleIdToMarkComplete, AdminRoleAncestryHydrationResponse completionResultForRole, Set<String> roleIdsToRemoveFromInProgress, Map<String, AdminRoleAncestryHydrationResponse> 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, AdminRoleRef> 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
-
initializeFoundRolesCache
protected Map<String,AdminRoleRef> initializeFoundRolesCache(@NonNull @NonNull Collection<AdminRoleRef> roles) -
initializeInProgressRoles
protected Map<String,List<String>> initializeInProgressRoles(@NonNull @NonNull Collection<AdminRoleRef> roles) -
attemptHydrationOfNextAncestor
protected void attemptHydrationOfNextAncestor(String inProgressRoleToHydrate, AdminRoleRef highestAncestorThusFar, List<String> ancestorIdsSeenThusFar, Set<String> failedToFindRoleIds, Map<String, AdminRoleRef> foundRolesCache, Set<String> roleIdsToRemoveFromInProgress, Set<String> roleIdsToFindInDataStore, Map<String, AdminRoleAncestryHydrationResponse> 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-emptyAdminRoleRef.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
)
-
getCycleFoundErrorMessage
-
getMissingAncestorErrorMessage
-
getLastElement
-
getFlattenedWithAllAncestors
-
getFlattenedWithAllAncestorsAndThrowIfAnyFailed
public Collection<AdminRoleRef> getFlattenedWithAllAncestorsAndThrowIfAnyFailed(Collection<AdminRoleRef> roles) Same asgetFlattenedWithAllAncestors(Collection)
except this method throws an exception when there's one or moreAdminRoleAncestryHydrationResponse
not successful.- Parameters:
roles
- a collection of roles to get along with its ancestors- Returns:
- a collection containing the given roles along with their ancestors
- Throws:
IllegalStateException
- if there is one or more failedAdminRoleAncestryHydrationResponse
-
getRolesWithAllAncestorsByRoleIdAndThrowIfAnyFailed
public Map<String,Collection<AdminRoleRef>> getRolesWithAllAncestorsByRoleIdAndThrowIfAnyFailed(Collection<AdminRoleRef> roles) Fetches all of the given roles' ancestors from the data store and returns a result containing them and the given roles themselves.- 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 base role id
- Throws:
IllegalStateException
- if there is one or more failedAdminRoleAncestryHydrationResponse
-
hasAnyFailedRoleAncestryHydrationResponses
protected boolean hasAnyFailedRoleAncestryHydrationResponses(Map<String, AdminRoleAncestryHydrationResponse> responses) -
getSuccessfulAndLogErrors
protected List<AdminRoleAncestryHydrationResponse> getSuccessfulAndLogErrors(@NonNull @NonNull Map<String, AdminRoleAncestryHydrationResponse> 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
AdminRoleAncestryHydrationResponse.successful
-
isSuccessfulOrLogError
protected boolean isSuccessfulOrLogError(Map.Entry<String, AdminRoleAncestryHydrationResponse> responseEntry) -
getAuthProvider
-