Class AbstractCrudService

java.lang.Object
org.fryske_akademy.services.AbstractCrudService
All Implemented Interfaces:
CrudReadService, CrudWriteService
Direct Known Subclasses:
AbstractCrudServiceEnvers

@DeclareRoles("editor") public abstract class AbstractCrudService extends Object implements CrudWriteService, CrudReadService

This base class enables you to create crud service beans with minimal effort. All you have to do is override this abstract class, implement getEntityManager() and annotate with for example @Stateless, @Local(Auditing.class, CrudWriteService.class). You can also just define CDI beans instead of EJB, but be aware you won't get declarative security then.

Inject the crud interfaces using @Inject where you need them.

This base class declares Txtype.NOT_SUPPORTED and EDITORROLE to protect write operations. When overriding don't forget transaction and role annotations are not inherited. If you don't need security, override and use PermitAll.

A JpqlBuilder is injected and used for building dynamic queries and for setting values when using named queries.

Author:
eduard
  • Field Details

    • DEFAULT_PAGE_SIZE

      public static final int DEFAULT_PAGE_SIZE
      default page size (max results), used when no maximum is given.
      See Also:
    • EDITORROLE

      public static final String EDITORROLE
      See Also:
  • Constructor Details

    • AbstractCrudService

      public AbstractCrudService()
  • Method Details

    • find

      public <T> T find(Serializable id, Class<T> type)
      Specified by:
      find in interface CrudReadService
    • findAll

      public <T> List<T> findAll(Class<T> type)
      Specified by:
      findAll in interface CrudReadService
    • findDynamic

      public <T> List<T> findDynamic(Integer first, Integer max, Map<String,CrudReadService.SORTORDER> sort, List<Param> params, Class<T> type)
      Specified by:
      findDynamic in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      first - defaults to 0
      max - defaults to CrudReadService.getDefaultPageSize()
      sort -
      params -
      type -
      Returns:
    • countDynamic

      public int countDynamic(List<Param> params, Class type)
      Description copied from interface: CrudReadService
      Call this using the same parameters as in findDynamic to get to know the total number of results
      Specified by:
      countDynamic in interface CrudReadService
      Parameters:
      params -
      type -
      Returns:
    • find

      public <T> List<T> find(String namedQuery, List<Param> params, Integer first, Integer max, Class<T> type)
      Specified by:
      find in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      namedQuery -
      params -
      first - defaults to 0
      max - defaults to CrudReadService.getDefaultPageSize()
      type -
      Returns:
    • findNative

      public <T> List<T> findNative(String namedNativeQuery, List<Param> params, Integer first, Integer max, Class<T> type)
      Specified by:
      findNative in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      namedNativeQuery -
      params -
      first - defaults to 0
      max - defaults to CrudReadService.getDefaultPageSize()
      type -
      Returns:
    • count

      public int count(String namedQuery, List<Param> params)
      Description copied from interface: CrudReadService
      Call this using the same parameters as in CrudReadService.find(java.lang.String, java.util.List, java.lang.Integer, java.lang.Integer, java.lang.Class) to get to know the total number of results
      Specified by:
      count in interface CrudReadService
      Parameters:
      namedQuery -
      params -
      Returns:
    • getDefaultPageSize

      public int getDefaultPageSize()
      Description copied from interface: CrudReadService
      Return a default page size when no max is given, negative value means no limit
      Specified by:
      getDefaultPageSize in interface CrudReadService
      Returns:
      DEFAULT_PAGE_SIZE
    • findOne

      public <T> T findOne(String namedQuery, List<Param> params, Class<T> type)
      Description copied from interface: CrudReadService
      return one result or null, multiple results throws an exception
      Specified by:
      findOne in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      namedQuery -
      params -
      type -
      Returns:
    • findExactlyOne

      public <T> T findExactlyOne(String namedQuery, List<Param> params, Class<T> type) throws PreventEJBLoggingException
      Description copied from interface: CrudReadService
      return one result, no or multiple results throws an exception
      Specified by:
      findExactlyOne in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      namedQuery -
      params -
      type -
      Returns:
      Throws:
      PreventEJBLoggingException
    • findJpql

      public <T> List<T> findJpql(String jpql, Class<T> clazz)
      possibly unsafe
      Specified by:
      findJpql in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      jpql -
      clazz -
      Returns:
    • findNative

      public <T> List<T> findNative(String sql, Class<T> clazz)
      possibly unsafe
      Specified by:
      findNative in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      sql -
      clazz -
      Returns:
    • findNative

      public List findNative(String sql)
      possibly unsafe
      Specified by:
      findNative in interface CrudReadService
      Parameters:
      sql -
      Returns:
    • create

      @RolesAllowed("editor") public <T> T create(T t)
      When overriding look into roles to use, these are not inherited. If you don't need security, override and use PermitAll.
      Specified by:
      create in interface CrudWriteService
      Type Parameters:
      T -
      Parameters:
      t -
      Returns:
    • save

      @RolesAllowed("editor") public <T extends EntityInterface> T save(T t)
      When overriding look into roles to use, these are not inherited. If you don't need security, override and use PermitAll.
      Specified by:
      save in interface CrudWriteService
      Type Parameters:
      T -
      Parameters:
      t -
      Returns:
    • update

      @RolesAllowed("editor") public <T> T update(T t)
      When overriding look into roles to use, these are not inherited. If you don't need security, override and use PermitAll.
      Specified by:
      update in interface CrudWriteService
      Type Parameters:
      T -
      Parameters:
      t -
      Returns:
    • delete

      @RolesAllowed("editor") public void delete(EntityInterface t)
      When overriding look into roles to use, these are not inherited. If you don't need security, override and use PermitAll.
      Specified by:
      delete in interface CrudWriteService
      Parameters:
      t -
    • batchSave

      @RolesAllowed("editor") public <T extends EntityInterface> Stream<T> batchSave(Collection<T> entities, Integer flushCount)
      Description copied from interface: CrudWriteService
      save multiple entities, NOTE that normally only after commit or flush entitymanagers will reflect changes caused by saving all entities, this may cause entitylisteners to fail. You may need to perform checks on the collection before calling this method. Also note that entitylisteners will execute within the batch transaction, you may need to specify Transactional.TxType on the bean that you use in your listeners, use isolation level 0 (not recommended) or use XA when you need to access resources from your listeners. When flushCount != null flushes and clears the entitymanager every flushCount entities. NOTE that this method executes in one (possibly big) sql transaction! You can use EntityException in for example your listeners to find out in case of failure which enitity caused it.
      Specified by:
      batchSave in interface CrudWriteService
      Parameters:
      entities -
      flushCount - when not null try to optimize (flush/clear) every so many entities
      Returns:
      the saved entities
    • batchDelete

      @RolesAllowed("editor") public int batchDelete(Collection<? extends EntityInterface> entities, Integer flushCount)
      Description copied from interface: CrudWriteService
      save multiple entities, NOTE that normally only after commit or flush entitymanagers will reflect changes caused by saving all entities, this may cause entitylisteners to fail. You may need to perform checks on the collection before calling this method. Also note that entitylisteners will execute within the batch transaction, you may need to specify Transactional.TxType on the bean that you use in your listeners, use isolation level 0 (not recommended) or use XA when you need to access resources from your listeners. When flushCount != null flushes and clears the entitymanager every flushCount entities. NOTE that this method executes in one (possibly big) sql transaction! You can use EntityException in for example your listeners to find out in case of failure which enitity caused it.
      Specified by:
      batchDelete in interface CrudWriteService
      Parameters:
      entities -
      flushCount - when not null try to optimize (flush/clear) every so many entities
      Returns:
      number of entities deleted
    • streamAll

      public <T> Stream<T> streamAll(Class<T> type)
      Specified by:
      streamAll in interface CrudReadService
    • streamDynamic

      public <T> Stream<T> streamDynamic(Integer first, Integer max, Map<String,CrudReadService.SORTORDER> sort, List<Param> params, Class<T> type)
      Specified by:
      streamDynamic in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      first - defaults to 0
      max - defaults to CrudReadService.getDefaultPageSize()
      sort -
      params -
      type -
      Returns:
    • stream

      public <T> Stream<T> stream(String namedQuery, List<Param> params, Integer first, Integer max, Class<T> type)
      Specified by:
      stream in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      namedQuery -
      params -
      first - defaults to 0
      max - defaults to CrudReadService.getDefaultPageSize()
      type -
      Returns:
    • streamNative

      public <T> Stream<T> streamNative(String namedNativeQuery, List<Param> params, Integer first, Integer max, Class<T> type)
      Specified by:
      streamNative in interface CrudReadService
      Type Parameters:
      T -
      Parameters:
      namedNativeQuery -
      params -
      first - defaults to 0
      max - defaults to CrudReadService.getDefaultPageSize()
      type -
      Returns:
    • refresh

      @RolesAllowed("editor") public <T> T refresh(T t)
      Description copied from interface: CrudWriteService
      refresh an entity from the database
      Specified by:
      refresh in interface CrudWriteService
      Type Parameters:
      T -
      Parameters:
      t -
      Returns:
    • setJpqlBuilder

      @Inject protected void setJpqlBuilder(JpqlBuilder jpqlBuilder)
      override to inject your own version of JpqlBuilder
      Parameters:
      jpqlBuilder -
    • getJpqlBuilder

      protected JpqlBuilder getJpqlBuilder()