Class BaseService<T extends BaseEntity>

  • Direct Known Subclasses:
    BaseCodeAndDescriptionService

    public class BaseService<T extends BaseEntity>
    extends java.lang.Object
    Class that provides base functionality for all service classes. Every service class must extend this class.
    Author:
    Collin Alpert
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected BaseService()
      Constructor for the base class of all services.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean any()
      Checks if a table has at least one row.
      boolean any​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
      Checks if a value matching the condition exists in the table.
      long count()
      An overload of the count(SqlPredicate) method.
      long count​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
      Counts the rows matching a certain condition.
      void create​(T instance)
      Creates this Java entity on the database.
      protected Query<T> createQuery()  
      void delete​(long id)
      Deletes a row by an id.
      void delete​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
      Deletes rows based on a condition.
      void delete​(T instance)
      Deletes the corresponding row on the database.
      java.util.List<T> getAll()  
      java.util.List<T> getAll​(int limit)
      Gets all values from the table but limits the result.
      java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy)
      Gets all values from the table and orders them in an ascending order.
      java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy, int limit)
      Gets all values from the table, orders them in an ascending order and limits the result.
      java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy, OrderTypes sortingType)
      Gets all values from the table and orders them in the specified order.
      java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy, OrderTypes sortingType, int limit)
      Gets all values from the table, orders them in a specific order and limits the result.
      java.util.Optional<T> getById​(long id)  
      protected Query<T> getMultiple​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
      Retrieves list of entities which match the predicate.
      protected java.util.Optional<T> getSingle​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
      Retrieves a single entity which matches the predicate.
      void update​(T instance)
      Updates this entity's row on the database.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BaseService

        protected BaseService()
        Constructor for the base class of all services. It is not possible to create instances of it.
    • Method Detail

      • create

        public void create​(T instance)
                    throws java.sql.SQLException
        Creates this Java entity on the database.
        Parameters:
        instance - The instance to create on the database.
        Throws:
        java.sql.SQLException - if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type.
      • count

        public long count()
        An overload of the count(SqlPredicate) method. It will count all the rows in a table.
        Returns:
        The amount of rows in this table.
      • count

        public long count​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
        Counts the rows matching a certain condition.
        Parameters:
        predicate - The condition to test for.
        Returns:
        The number of rows matching the condition.
      • any

        public boolean any()
        Checks if a table has at least one row.
        Returns:
        true if at least one row exists in the table, false if not.
      • any

        public boolean any​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
        Checks if a value matching the condition exists in the table.
        Parameters:
        predicate - The condition to check for.
        Returns:
        true if the predicate matches one or more records, false if not.
      • createQuery

        protected Query<T> createQuery()
        Returns:
        a Query object with which a DQL statement can be build, using operations like order by, limit etc.
      • getSingle

        protected java.util.Optional<T> getSingle​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
        Retrieves a single entity which matches the predicate. It is protected as it is only meant for use in methods of the respective service. This is to keep good programming practice and create descriptive methods for what kind of data you are getting.
        Parameters:
        predicate - The SqlPredicate to add constraints to a DQL query.
        Returns:
        An entity matching the result of the query.
      • getMultiple

        protected Query<T> getMultiple​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
        Retrieves list of entities which match the predicate. It is protected as it is only meant for use in methods of the respective service. This is to keep good programming practice and create descriptive methods for what kind of data you are getting.
        Parameters:
        predicate - The SqlPredicate to add constraints to a DQL statement.
        Returns:
        A list of entities matching the result of the query.
      • getById

        public java.util.Optional<T> getById​(long id)
        Parameters:
        id - The id of the desired entity.
        Returns:
        Gets an entity by its id.
      • getAll

        public java.util.List<T> getAll()
        Returns:
        All entities in this table.
      • getAll

        public java.util.List<T> getAll​(int limit)
        Gets all values from the table but limits the result.
        Parameters:
        limit - The maximum of records to return.
        Returns:
        A list with the maximum size of the parameter specified.
      • getAll

        public java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy)
        Gets all values from the table and orders them in an ascending order.
        Parameters:
        orderBy - The property to order by.
        Returns:
        A list of all records ordered by a specific property in an ascending order.
      • getAll

        public java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy,
                                        OrderTypes sortingType)
        Gets all values from the table and orders them in the specified order.
        Parameters:
        orderBy - The property to order by.
        sortingType - The order direction. Can be either ascending or descending.
        Returns:
        A list of all records ordered by a specific property in the specified order.
      • getAll

        public java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy,
                                        int limit)
        Gets all values from the table, orders them in an ascending order and limits the result.
        Parameters:
        orderBy - The property to order by.
        limit - The maximum records to return.
        Returns:
        A list with the maximum size of the parameter specified and in an ascending order.
      • getAll

        public java.util.List<T> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,​?> orderBy,
                                        OrderTypes sortingType,
                                        int limit)
        Gets all values from the table, orders them in a specific order and limits the result.
        Parameters:
        orderBy - The property to order by.
        sortingType - The order direction. Can be either ascending or descending.
        limit - The maximum records to return.
        Returns:
        A list with the maximum size of the parameter specified and in an ascending order.
      • update

        public void update​(T instance)
                    throws java.sql.SQLException
        Updates this entity's row on the database.
        Parameters:
        instance - The instance to update on the database.
        Throws:
        java.sql.SQLException - if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type.
      • delete

        public void delete​(T instance)
                    throws java.sql.SQLException
        Deletes the corresponding row on the database.
        Parameters:
        instance - The instance to delete on the database.
        Throws:
        java.sql.SQLException - for example because of a foreign key constraint.
      • delete

        public void delete​(long id)
                    throws java.sql.SQLException
        Deletes a row by an id.
        Parameters:
        id - The row with this id to delete.
        Throws:
        java.sql.SQLException - for example because of a foreign key constraint.
      • delete

        public void delete​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
                    throws java.sql.SQLException
        Deletes rows based on a condition.
        Parameters:
        predicate - The condition to delete by.
        Throws:
        java.sql.SQLException - in case the condition cannot be applied or if a foreign key constraint fails.