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 TypeMethodDescriptionvoid
deleteById
(ID id) Deletes an entity by its ID with validation of existence.boolean
existsAllByIds
(Collection<ID> ids) Verifies that all provided IDs correspond to existing entities.boolean
existsById
(ID id) Checks whether an entity exists by its ID.boolean
Checks 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 String
Provides 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
IGenericDAO
corresponding 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:
findById
in interfaceIGenericService<T,
ID> - Parameters:
id
- the entity identifier to find; must not benull
- Returns:
- the entity if found
- Throws:
BadRequestException
- ifid
isnull
NotFoundException
- 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:
findAllByIds
in 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:
findAllByIdsToSet
in 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:
save
in interfaceIGenericService<T,
ID> - Parameters:
t
- the entity to save; must not benull
- Returns:
- the saved entity
- Throws:
BadRequestException
- ift
isnull
or already exists
-
saveAll
Saves a collection of entities in bulk.- Specified by:
saveAll
in 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:
updateById
in interfaceIGenericService<T,
ID> - Parameters:
t
- the entity with updated information; must not benull
id
- 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:
deleteById
in interfaceIGenericService<T,
ID> - Parameters:
id
- the identifier of the entity to delete; must not benull
- Throws:
BadRequestException
- ifid
isnull
or does not exist
-
findAll
Retrieves all entities of typeT
.- Specified by:
findAll
in 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:
findAllPage
in interfaceIGenericService<T,
ID> - Parameters:
pageable
- pagination parameters; must not benull
- Returns:
- a paginated list of entities
- Throws:
BadRequestException
- ifpageable
isnull
-
existsById
Checks whether an entity exists by its ID.- Specified by:
existsById
in interfaceIGenericService<T,
ID> - Parameters:
id
- the identifier to check; may benull
- Returns:
true
if entity exists,false
otherwise
-
existsAllByIds
Verifies that all provided IDs correspond to existing entities.- Specified by:
existsAllByIds
in interfaceIGenericService<T,
ID> - Parameters:
ids
- the collection of IDs; must not benull
, empty, or containnull
- Returns:
true
if all IDs exist;false
otherwise
-
existsByUniqueProperties
Checks for existence of an entity based on unique properties.- Specified by:
existsByUniqueProperties
in interfaceIGenericService<T,
ID> - Parameters:
t
- the entity to check; may benull
- Returns:
true
if such an entity exists;false
otherwise
-