Class DefaultEntityModel<T,U extends Serializable>

java.lang.Object
com.foreach.across.modules.entity.registry.DefaultEntityModel<T,U>
Type Parameters:
T - entity type
U - id type of the entity
All Implemented Interfaces:
EntityFactory<T>, EntityModel<T,U>, org.springframework.data.repository.core.EntityInformation<T,U>, org.springframework.data.repository.core.EntityMetadata<T>

public class DefaultEntityModel<T,U extends Serializable> extends Object implements EntityModel<T,U>
Default implementation of EntityModel that allows customizing the different methods using callbacks and delegate instances.
  • EntityFactory for creation of instances and transforming them into dtos
  • EntityInformation for accessing an entity metadata
  • Printer for generating the label
  • Function for save and find methods
  • Consumer for delete method
Author:
Arne Vandamme
See Also:
  • Constructor Details

    • DefaultEntityModel

      public DefaultEntityModel()
  • Method Details

    • setLabelPrinter

      public void setLabelPrinter(@NonNull @NonNull org.springframework.format.Printer<T> labelPrinter)
      Set the printer to be used for calls to getLabel(Object) and getLabel(Object, Locale).
      Parameters:
      labelPrinter - instance
    • getLabel

      public String getLabel(T entity)
      Description copied from interface: EntityModel
      Generates a label for the entity and the default locale attached to the thread.
      Specified by:
      getLabel in interface EntityModel<T,U extends Serializable>
      Parameters:
      entity - to generate the label for
      Returns:
      label
    • getLabel

      public String getLabel(T entity, Locale locale)
      Description copied from interface: EntityModel
      Generates a label for the entity and the locale specified.
      Specified by:
      getLabel in interface EntityModel<T,U extends Serializable>
      Parameters:
      entity - to generate the label for
      locale - to use for formatting
      Returns:
      label
    • setEntityFactory

      public void setEntityFactory(@NonNull @NonNull EntityFactory<T> entityFactory)
      Set the EntityFactory delegate that should be used for creating a new instance or converting an existing instance to a dto.
      Parameters:
      entityFactory - instance
    • createNew

      public T createNew(Object... args)
      Description copied from interface: EntityModel
      Create a new instance of the entity type this model represents. Optionally a number of arguments can be passed, however for basic entity views the model should support a new instance without any arguments.
      Specified by:
      createNew in interface EntityFactory<T>
      Specified by:
      createNew in interface EntityModel<T,U extends Serializable>
      Parameters:
      args - optional list of arguments (eg. constructor parameters)
      Returns:
      newly created entity
    • createDto

      public T createDto(T entity)
      Description copied from interface: EntityModel
      Convert an existing entity into a dto. Used by default views to ensure operations are not done on managed entities.
      Specified by:
      createDto in interface EntityFactory<T>
      Specified by:
      createDto in interface EntityModel<T,U extends Serializable>
      Parameters:
      entity - to convert into a dto
      Returns:
      dto of the entity
    • setFindOneMethod

      public void setFindOneMethod(@NonNull @NonNull Function<U,T> findOneMethod)
      Set the callback method to be used for finding a single entity based on its id.
      Parameters:
      findOneMethod - callback
    • findOne

      public T findOne(U id)
      Description copied from interface: EntityModel
      Find a single entity based on its unique id.
      Specified by:
      findOne in interface EntityModel<T,U extends Serializable>
      Parameters:
      id - of the entity
      Returns:
      entity or null if not found
    • setSaveMethod

      public void setSaveMethod(@NonNull @NonNull UnaryOperator<T> saveMethod)
      Set the callback method to be used for saving a single entity.
      Parameters:
      saveMethod - callback
    • save

      public T save(T entity)
      Description copied from interface: EntityModel
      Saves an entity. Depending on the entity information this will perform an update or a create. The return value depends on the implementation but in default cases the entity passed in will be detached (dto) and the return value will be the managed entity.
      Specified by:
      save in interface EntityModel<T,U extends Serializable>
      Parameters:
      entity - to save
      Returns:
      saved entity
    • setDeleteMethod

      public void setDeleteMethod(@NonNull @NonNull Consumer<T> deleteMethod)
      Set the callback method to be used when deleting an entity.
      Parameters:
      deleteMethod - callback
    • delete

      public void delete(T entity)
      Description copied from interface: EntityModel
      Deletes the given entity.
      Specified by:
      delete in interface EntityModel<T,U extends Serializable>
      Parameters:
      entity - to delete
    • setEntityInformation

      public void setEntityInformation(@NonNull @NonNull org.springframework.data.repository.core.EntityInformation<T,U> entityInformation)
      Set the metadata provider for this entity type.
      Parameters:
      entityInformation - implementation
    • isNew

      public boolean isNew(T entity)
      Specified by:
      isNew in interface org.springframework.data.repository.core.EntityInformation<T,U extends Serializable>
    • getId

      public U getId(T entity)
      Specified by:
      getId in interface org.springframework.data.repository.core.EntityInformation<T,U extends Serializable>
    • getIdType

      public Class<U> getIdType()
      Specified by:
      getIdType in interface org.springframework.data.repository.core.EntityInformation<T,U extends Serializable>
    • getJavaType

      public Class<T> getJavaType()
      Specified by:
      getJavaType in interface org.springframework.data.repository.core.EntityMetadata<T>