Class DefaultUserRoleService<P extends UserRole,D extends com.broadleafcommerce.data.tracking.core.mapping.BusinessTypeAware & com.broadleafcommerce.data.tracking.core.mapping.ModelMapperMappable>
- java.lang.Object
-
- com.broadleafcommerce.auth.security.service.DefaultUserRoleService<P,D>
-
- All Implemented Interfaces:
UserRoleService<P>
public class DefaultUserRoleService<P extends UserRole,D extends com.broadleafcommerce.data.tracking.core.mapping.BusinessTypeAware & com.broadleafcommerce.data.tracking.core.mapping.ModelMapperMappable> extends Object implements UserRoleService<P>
-
-
Constructor Summary
Constructors Constructor Description DefaultUserRoleService(RoleRepository<D> repository, SimplePayloadMapper mapper, com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidatorManager validator)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
applyChangesForArchive(P role)
Modifies the given instance for archival.void
archive(@NonNull String id)
Soft-deletes a record by setting itsUserRole.archived
totrue
.P
createOnlyIfDoesNotExist(P role, @NonNull Instant lastUpdated)
A specialized create operation for creating an entity with a pre-defined id with the minimum guarantee that concurrent requests to this method for the same entity ID will fail.P
createRole(P payload)
Creates a new role in the data store.void
delete(@NonNull String id)
Hard-deletes the given record.List<P>
findAllByIdInAndNotArchived(Collection<String> ids)
Finds and returns a list of entities that are notUserRole.archived
and whose id matches one of the given values.org.springframework.data.domain.Page<P>
findAllByNameContainingAndNotArchived(String name, org.springframework.data.domain.Pageable page)
Finds and returns all entities that are notUserRole.archived
whose name contains the given value.Stream<P>
findAllStreamByIdInAndNotArchived(Collection<String> ids)
Finds and returns a stream of entities that are notUserRole.archived
and whose id matches one of the given values.Optional<P>
findById(String id)
Finds and returns the entity with the given ID.Optional<P>
findByIdAndNotArchived(String id)
Finds and returns the entity with the given ID if notUserRole.archived
.protected com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidatorManager
getValidator()
P
replaceOnlyIfLastUpdatedBefore(@NonNull String entityId, P role, @NonNull Instant lastUpdated)
A specialized replace operation that does not invoke the "save" create-or-update behavior.P
replaceRole(String id, P payload)
Replaces the role in the data store with the givenid
withpayload
.protected void
validateNoChildrenOfRoleBeforeDeletion(@NonNull String roleToDeleteId)
Given a role that is requested to be deleted or archived, checks that it does not have any child roles in the data store.
-
-
-
Constructor Detail
-
DefaultUserRoleService
public DefaultUserRoleService(RoleRepository<D> repository, SimplePayloadMapper mapper, com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidatorManager validator)
-
-
Method Detail
-
findById
public Optional<P> findById(String id)
Description copied from interface:UserRoleService
Finds and returns the entity with the given ID.- Specified by:
findById
in interfaceUserRoleService<P extends UserRole>
- Parameters:
id
- the id of the entity to find- Returns:
- an
Optional
containing the entity if found,Optional.empty()
otherwise
-
findByIdAndNotArchived
public Optional<P> findByIdAndNotArchived(String id)
Description copied from interface:UserRoleService
Finds and returns the entity with the given ID if notUserRole.archived
.- Specified by:
findByIdAndNotArchived
in interfaceUserRoleService<P extends UserRole>
- Parameters:
id
- the id of the entity to find- Returns:
- an
Optional
containing the entity if found,Optional.empty()
otherwise
-
findAllByNameContainingAndNotArchived
public org.springframework.data.domain.Page<P> findAllByNameContainingAndNotArchived(@Nullable String name, org.springframework.data.domain.Pageable page)
Description copied from interface:UserRoleService
Finds and returns all entities that are notUserRole.archived
whose name contains the given value.- Specified by:
findAllByNameContainingAndNotArchived
in interfaceUserRoleService<P extends UserRole>
- Parameters:
name
- the name value to match withpage
- information about which page of results to return from the database- Returns:
- all entities that are not archived whose name matches the given value
-
findAllStreamByIdInAndNotArchived
public Stream<P> findAllStreamByIdInAndNotArchived(Collection<String> ids)
Description copied from interface:UserRoleService
Finds and returns a stream of entities that are notUserRole.archived
and whose id matches one of the given values.- Specified by:
findAllStreamByIdInAndNotArchived
in interfaceUserRoleService<P extends UserRole>
- Parameters:
ids
- the ids of the entities to find- Returns:
- a stream of the entities that were found
-
findAllByIdInAndNotArchived
public List<P> findAllByIdInAndNotArchived(Collection<String> ids)
Description copied from interface:UserRoleService
Finds and returns a list of entities that are notUserRole.archived
and whose id matches one of the given values.- Specified by:
findAllByIdInAndNotArchived
in interfaceUserRoleService<P extends UserRole>
- Parameters:
ids
- the ids of the entities to find- Returns:
- a list of the entities that were found
-
createRole
public P createRole(P payload)
Description copied from interface:UserRoleService
Creates a new role in the data store.- Specified by:
createRole
in interfaceUserRoleService<P extends UserRole>
- Parameters:
payload
- the role to create- Returns:
- the final role after creation
-
replaceRole
public P replaceRole(String id, P payload)
Description copied from interface:UserRoleService
Replaces the role in the data store with the givenid
withpayload
.- Specified by:
replaceRole
in interfaceUserRoleService<P extends UserRole>
- Parameters:
id
- the ID of the role to replacepayload
- the replacement role- Returns:
- the final role after replacement
-
delete
public void delete(@NonNull @NonNull String id)
Description copied from interface:UserRoleService
Hard-deletes the given record.In all but exceptional cases,
UserRoleService.archive(String)
should be preferred to this operation.- Specified by:
delete
in interfaceUserRoleService<P extends UserRole>
- Parameters:
id
- the id of the entity to delete- See Also:
UserRoleService.archive(String)
-
validateNoChildrenOfRoleBeforeDeletion
protected void validateNoChildrenOfRoleBeforeDeletion(@NonNull @NonNull String roleToDeleteId)
Given a role that is requested to be deleted or archived, checks that it does not have any child roles in the data store. This protects against descendants being suddenly orphaned.- Parameters:
roleToDeleteId
- the id of the role which should be validated to have no children in the data store- Throws:
InvalidUserRoleDeleteException
- if the role has children in the data store
-
archive
public void archive(@NonNull @NonNull String id)
Description copied from interface:UserRoleService
Soft-deletes a record by setting itsUserRole.archived
totrue
.As part of this operation, the
UserRole.permissions
collections should be cleared such that those relationships are deleted.- Specified by:
archive
in interfaceUserRoleService<P extends UserRole>
- Parameters:
id
- the id of the entity to archive- See Also:
UserRoleService.delete(String)
-
applyChangesForArchive
protected void applyChangesForArchive(P role)
Modifies the given instance for archival.- Parameters:
role
- the instance to modify
-
createOnlyIfDoesNotExist
@Nullable public P createOnlyIfDoesNotExist(@NonNull P role, @NonNull @NonNull Instant lastUpdated)
Description copied from interface:UserRoleService
A specialized create operation for creating an entity with a pre-defined id with the minimum guarantee that concurrent requests to this method for the same entity ID will fail.This is intended to be used in a message handler, a highly concurrent scenario where only one operation should succeed.
Furthermore, this operation must not validate the request as
UserRoleService.createRole(UserRole)
does, since we expect message handlers to receive out-of-order messages that may temporarily have invalid references.- Specified by:
createOnlyIfDoesNotExist
in interfaceUserRoleService<P extends UserRole>
- Parameters:
role
- the role to create. Must have a non-null id present.lastUpdated
- theUserRole.lastUpdated
to set on the role- Returns:
- the role if successfully created, or
null
if creation failed due to the entity already existing. - See Also:
AdminRolePersistenceHandler
-
replaceOnlyIfLastUpdatedBefore
@Nullable public P replaceOnlyIfLastUpdatedBefore(@NonNull @NonNull String entityId, @NonNull P role, @NonNull @NonNull Instant lastUpdated)
Description copied from interface:UserRoleService
A specialized replace operation that does not invoke the "save" create-or-update behavior. This operation should directly attempt an atomic update that the data store will reject if the entity is not found or has a lastUpdated ahead of the current change.This is intended to be used in a message handler, a highly concurrent scenario where only one operation should succeed.
Furthermore, this operation must not validate the request as
UserRoleService.replaceRole(String, UserRole)
does, since we expect message handlers to receive out-of-order messages that may temporarily have invalid references.- Specified by:
replaceOnlyIfLastUpdatedBefore
in interfaceUserRoleService<P extends UserRole>
- Parameters:
entityId
- the id of the entity to replacerole
- the replacement entitylastUpdated
- the timestamp of the current change. This will be compared to the lastUpdated on the existing entity for determination of whether the replacement should occur. Furthermore, the replacement will have itsUserRole.lastUpdated
set to this value.- Returns:
- the role after replacement if successfully replaced, or
null
if the entity was not found or could not be updated due to its lastUpdated value. - See Also:
AdminRolePersistenceHandler
-
getValidator
@NonNull protected com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidatorManager getValidator()
-
-