Class GenericServiceImpl<T,ID>

java.lang.Object
com.alpaca.service.impl.GenericServiceImpl<T,ID>
Type Parameters:
T - the type of entity managed
ID - the type of the entity's identifier
All Implemented Interfaces:
IGenericService<T,ID>
Direct Known Subclasses:
AdvertiserServiceImpl, PermissionServiceImpl, ProfileServiceImpl, RoleServiceImpl, UserServiceImpl

public abstract class GenericServiceImpl<T,ID> extends Object implements IGenericService<T,ID>
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 Details

    • GenericServiceImpl

      public GenericServiceImpl()
  • Method Details

    • getDAO

      protected abstract IGenericDAO<T,ID> getDAO()
      Supplies the DAO component for data access operations.
      Returns:
      the IGenericDAO corresponding to the entity type T
    • getEntityName

      protected abstract String getEntityName()
      Provides a human-readable entity name to be used in exception messages.
      Returns:
      the name of the entity (e.g., "User")
    • findById

      @Transactional public T findById(ID id)
      Retrieves an entity by its identifier with validation and error handling.
      Specified by:
      findById in interface IGenericService<T,ID>
      Parameters:
      id - the entity identifier to find; must not be null
      Returns:
      the entity if found
      Throws:
      BadRequestException - if id is null
      NotFoundException - if no entity is found for the given id
    • findAllByIds

      @Transactional public List<T> findAllByIds(Collection<ID> ids)
      Fetches all entities matching the provided collection of IDs, with validation and existence check.
      Specified by:
      findAllByIds in interface IGenericService<T,ID>
      Parameters:
      ids - the collection of identifiers; must not be null, empty, or contain null
      Returns:
      a list of entities matching the provided IDs
      Throws:
      BadRequestException - if validation fails
      NotFoundException - if any of the requested entities are not found
    • findAllByIdsToSet

      public Set<T> findAllByIdsToSet(Collection<ID> ids)
      Retrieves entities by IDs and returns them as a Set.
      Specified by:
      findAllByIdsToSet in interface IGenericService<T,ID>
      Parameters:
      ids - the collection of identifiers
      Returns:
      a set of entities corresponding to the provided IDs
    • save

      @Transactional public T save(T t)
      Saves a new entity with validation against duplication.
      Specified by:
      save in interface IGenericService<T,ID>
      Parameters:
      t - the entity to save; must not be null
      Returns:
      the saved entity
      Throws:
      BadRequestException - if t is null or already exists
    • saveAll

      @Transactional public List<T> saveAll(Collection<T> t)
      Saves a collection of entities in bulk.
      Specified by:
      saveAll in interface IGenericService<T,ID>
      Parameters:
      t - the collection of entities; must not be null, empty, or contain null
      Returns:
      the list of saved entities
      Throws:
      BadRequestException - if validation fails
    • updateById

      @Transactional public T updateById(T t, ID id)
      Updates an entity identified by its ID using the provided updated entity.
      Specified by:
      updateById in interface IGenericService<T,ID>
      Parameters:
      t - the entity with updated information; must not be null
      id - the identifier of the entity to update; must not be null
      Returns:
      the updated entity
      Throws:
      BadRequestException - if validation fails or the entity cannot be updated
    • deleteById

      @Transactional public void deleteById(ID id)
      Deletes an entity by its ID with validation of existence.
      Specified by:
      deleteById in interface IGenericService<T,ID>
      Parameters:
      id - the identifier of the entity to delete; must not be null
      Throws:
      BadRequestException - if id is null or does not exist
    • findAll

      @Transactional public List<T> findAll()
      Retrieves all entities of type T.
      Specified by:
      findAll in interface IGenericService<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 interface IGenericService<T,ID>
      Parameters:
      pageable - pagination parameters; must not be null
      Returns:
      a paginated list of entities
      Throws:
      BadRequestException - if pageable is null
    • existsById

      @Transactional public boolean existsById(ID id)
      Checks whether an entity exists by its ID.
      Specified by:
      existsById in interface IGenericService<T,ID>
      Parameters:
      id - the identifier to check; may be null
      Returns:
      true if entity exists, false otherwise
    • existsAllByIds

      @Transactional public boolean existsAllByIds(Collection<ID> ids)
      Verifies that all provided IDs correspond to existing entities.
      Specified by:
      existsAllByIds in interface IGenericService<T,ID>
      Parameters:
      ids - the collection of IDs; must not be null, empty, or contain null
      Returns:
      true if all IDs exist; false otherwise
    • existsByUniqueProperties

      @Transactional public boolean existsByUniqueProperties(T t)
      Checks for existence of an entity based on unique properties.
      Specified by:
      existsByUniqueProperties in interface IGenericService<T,ID>
      Parameters:
      t - the entity to check; may be null
      Returns:
      true if such an entity exists; false otherwise