Class AdminRoleHydrationService

java.lang.Object
com.broadleafcommerce.adminuser.user.service.hydration.AdminRoleHydrationService

public class AdminRoleHydrationService extends Object
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 Details

    • AdminRoleHydrationService

      public AdminRoleHydrationService(AuthProvider authProvider)
  • Method Details

    • hydrateRoleReferences

      public Set<AdminRoleRef> hydrateRoleReferences(Set<AdminRoleRef> toHydrate)
      Queries for each element in toHydrate 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

      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

      protected void assertAllRolesHaveIds(Collection<AdminRoleRef> roles)
    • hasParentRoleId

      protected boolean hasParentRoleId(AdminRoleRef role)
    • 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 complete
      completionResultForRole - the result that the role should be completed with
      roleIdsToRemoveFromInProgress - a set containing ids of roles that should no longer be considered in-progress. The given roleIdToMarkComplete will be added to this set.
      completed - a map of completed role ids to their results. The given roleIdToMarkComplete will be put into this map with the value of completionResultForRole.
    • findInDataStoreAndReturnMissingIds

      protected Set<String> findInDataStoreAndReturnMissingIds(Set<String> roleIdsToFind, Map<String,AdminRoleRef> cacheToAddFoundTo)
      Searches the data store for roles matching the given roleIdsToFind. Roles that are found will be added to the cacheToAddFoundTo. Any ids for which roles were not found will be returned.
      Parameters:
      roleIdsToFind - the set of role ids to find in the data store
      cacheToAddFoundTo - 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 requested
      highestAncestorThusFar - the highest-level ancestor that has been successfully found for the inProgressRoleToHydrate thus far. Guaranteed to have a non-empty AdminRoleRef.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 the inProgressRoleToHydrate thus far. The very last id in this list will belong to highestAncestorThusFar. 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 found
      foundRolesCache - all roles that have already been successfully found in the data store, mapped by their id
      roleIdsToRemoveFromInProgress - a set of role ids that will be removed from the group of roles for which ancestor hydration is considered unfinished. The inProgressRoleToHydrate 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 to completed).
      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 in failedToFindRoleIds and foundRolesCache on the next invocation of this method for inProgressRoleToHydrate.
      completed - the final map of role ids for which ancestor hydration is finished, to the result of performing that hydration. If this method determines that inProgressRoleToHydrate should be marked completed, it should be added to this map (in addition to being added to roleIdsToRemoveFromInProgress)
    • getCycleFoundErrorMessage

      protected String getCycleFoundErrorMessage(AdminRoleRef roleWithCycleCausingParent)
    • getMissingAncestorErrorMessage

      protected String getMissingAncestorErrorMessage(String missingAncestorId)
    • getLastElement

      protected String getLastElement(List<String> list)
    • getFlattenedWithAllAncestors

      public Collection<AdminRoleRef> getFlattenedWithAllAncestors(Collection<AdminRoleRef> roles)
    • getFlattenedWithAllAncestorsAndThrowIfAnyFailed

      public Collection<AdminRoleRef> getFlattenedWithAllAncestorsAndThrowIfAnyFailed(Collection<AdminRoleRef> roles)
      Same as getFlattenedWithAllAncestors(Collection) except this method throws an exception when there's one or more AdminRoleAncestryHydrationResponse 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 failed AdminRoleAncestryHydrationResponse
    • 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 failed AdminRoleAncestryHydrationResponse
    • 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 from getWithAllAncestors(Collection) by logging errors on all roles which failed hydration and returning only the successful responses.
      Parameters:
      responses - the responses returned by getWithAllAncestors(Collection)
      Returns:
      a list containing only the responses which were AdminRoleAncestryHydrationResponse.successful
    • isSuccessfulOrLogError

      protected boolean isSuccessfulOrLogError(Map.Entry<String,AdminRoleAncestryHydrationResponse> responseEntry)
    • getAuthProvider

      protected AuthProvider getAuthProvider()