Package com.alpaca.service.impl
Class GenericServiceImpl<T,ID>
java.lang.Object
com.alpaca.service.impl.GenericServiceImpl<T,ID>
- Type Parameters:
T- the type of entity managedID- the type of the entity's identifier
- All Implemented Interfaces:
IGenericService<T,ID>
- Direct Known Subclasses:
AdvertiserServiceImpl,PermissionServiceImpl,ProfileServiceImpl,RoleServiceImpl,UserServiceImpl
Abstract base implementation of
IGenericService, providing reusable CRUD operations with
standard validations and error handling for any entity type T and identifier type
ID.
Concrete services should extend this class and provide specific IGenericDAO instances
and entity names for exception messages.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddeleteById(ID id) Deletes an entity by its ID with validation of existence.booleanexistsAllByIds(Collection<ID> ids) Verifies that all provided IDs correspond to existing entities.booleanexistsById(ID id) Checks whether an entity exists by its ID.booleanChecks for existence of an entity based on unique properties.findAll()Retrieves all entities of typeT.findAllByIds(Collection<ID> ids) Fetches all entities matching the provided collection of IDs, with validation and existence check.findAllByIdsToSet(Collection<ID> ids) Retrieves entities by IDs and returns them as aSet.org.springframework.data.domain.Page<T> findAllPage(org.springframework.data.domain.Pageable pageable) Retrieves entities in a paginated fashion.Retrieves an entity by its identifier with validation and error handling.protected abstract IGenericDAO<T, ID> getDAO()Supplies the DAO component for data access operations.protected abstract StringProvides a human-readable entity name to be used in exception messages.Saves a new entity with validation against duplication.saveAll(Collection<T> t) Saves a collection of entities in bulk.updateById(T t, ID id) Updates an entity identified by its ID using the provided updated entity.
-
Constructor Details
-
GenericServiceImpl
public GenericServiceImpl()
-
-
Method Details
-
getDAO
Supplies the DAO component for data access operations.- Returns:
- the
IGenericDAOcorresponding to the entity typeT
-
getEntityName
Provides a human-readable entity name to be used in exception messages.- Returns:
- the name of the entity (e.g., "User")
-
findById
Retrieves an entity by its identifier with validation and error handling.- Specified by:
findByIdin interfaceIGenericService<T,ID> - Parameters:
id- the entity identifier to find; must not benull- Returns:
- the entity if found
- Throws:
BadRequestException- ifidisnullNotFoundException- if no entity is found for the givenid
-
findAllByIds
Fetches all entities matching the provided collection of IDs, with validation and existence check.- Specified by:
findAllByIdsin interfaceIGenericService<T,ID> - Parameters:
ids- the collection of identifiers; must not benull, empty, or containnull- Returns:
- a list of entities matching the provided IDs
- Throws:
BadRequestException- if validation failsNotFoundException- if any of the requested entities are not found
-
findAllByIdsToSet
Retrieves entities by IDs and returns them as aSet.- Specified by:
findAllByIdsToSetin interfaceIGenericService<T,ID> - Parameters:
ids- the collection of identifiers- Returns:
- a set of entities corresponding to the provided IDs
-
save
Saves a new entity with validation against duplication.- Specified by:
savein interfaceIGenericService<T,ID> - Parameters:
t- the entity to save; must not benull- Returns:
- the saved entity
- Throws:
BadRequestException- iftisnullor already exists
-
saveAll
Saves a collection of entities in bulk.- Specified by:
saveAllin interfaceIGenericService<T,ID> - Parameters:
t- the collection of entities; must not benull, empty, or containnull- Returns:
- the list of saved entities
- Throws:
BadRequestException- if validation fails
-
updateById
Updates an entity identified by its ID using the provided updated entity.- Specified by:
updateByIdin interfaceIGenericService<T,ID> - Parameters:
t- the entity with updated information; must not benullid- the identifier of the entity to update; must not benull- Returns:
- the updated entity
- Throws:
BadRequestException- if validation fails or the entity cannot be updated
-
deleteById
Deletes an entity by its ID with validation of existence.- Specified by:
deleteByIdin interfaceIGenericService<T,ID> - Parameters:
id- the identifier of the entity to delete; must not benull- Throws:
BadRequestException- ifidisnullor does not exist
-
findAll
Retrieves all entities of typeT.- Specified by:
findAllin interfaceIGenericService<T,ID> - Returns:
- a list of all entities
-
findAllPage
@Transactional public org.springframework.data.domain.Page<T> findAllPage(org.springframework.data.domain.Pageable pageable) Retrieves entities in a paginated fashion.- Specified by:
findAllPagein interfaceIGenericService<T,ID> - Parameters:
pageable- pagination parameters; must not benull- Returns:
- a paginated list of entities
- Throws:
BadRequestException- ifpageableisnull
-
existsById
Checks whether an entity exists by its ID.- Specified by:
existsByIdin interfaceIGenericService<T,ID> - Parameters:
id- the identifier to check; may benull- Returns:
trueif entity exists,falseotherwise
-
existsAllByIds
Verifies that all provided IDs correspond to existing entities.- Specified by:
existsAllByIdsin interfaceIGenericService<T,ID> - Parameters:
ids- the collection of IDs; must not benull, empty, or containnull- Returns:
trueif all IDs exist;falseotherwise
-
existsByUniqueProperties
Checks for existence of an entity based on unique properties.- Specified by:
existsByUniquePropertiesin interfaceIGenericService<T,ID> - Parameters:
t- the entity to check; may benull- Returns:
trueif such an entity exists;falseotherwise
-