Class GenericFileDao

java.lang.Object
de.xima.fc.file.GenericFileDao
All Implemented Interfaces:
IGenericFileDao

public class GenericFileDao extends Object implements IGenericFileDao
  • Constructor Details

    • GenericFileDao

      public GenericFileDao()
  • Method Details

    • getDataEntityLockingVersion

      public <Data extends IFileDataEntity<?>> EntityIdWithLockingVersion getDataEntityLockingVersion(IEntityContext ec, Class<Data> dataClass, IFileEntity<?,Data> fileEntity)
      Description copied from interface: IGenericFileDao
      Gets the ID and locking version of the data entity for the given file entity.
      Specified by:
      getDataEntityLockingVersion in interface IGenericFileDao
      Type Parameters:
      Data - Type of the data entity.
      Parameters:
      ec - Entity context for accessing the database.
      dataClass - Class of the data entity.
      fileEntity - File entity containing the data entity.
      Returns:
      ID and locking version of the data entity, or null if the file does not exist.
    • getDataEntityLockingVersion

      public <Data extends IFileDataEntity<?>> EntityIdWithLockingVersion getDataEntityLockingVersion(IEntityContext ec, Class<Data> dataClass, IFileProviding<?,Data> fileProviding)
      Description copied from interface: IGenericFileDao
      Gets the ID and locking version of the data entity for the given file entity.
      Specified by:
      getDataEntityLockingVersion in interface IGenericFileDao
      Type Parameters:
      Data - Type of the data entity.
      Parameters:
      ec - Entity context for accessing the database.
      dataClass - Class of the data entity.
      fileProviding - Entity providing a file entity.
      Returns:
      ID and locking version of the data entity, or null if the file does not exist.
    • getFileData

      public <E extends IFileDataEntity<?>, T extends IFileProviding<?,E>> byte[] getFileData(IEntityContext ec, Class<T> clazz, long id)
      Description copied from interface: IGenericFileDao
      Returns the file data of a file entity.

      Use with care! As the name suggests, this holds the entire file data in memory. Prefer using an input stream instead.

      Specified by:
      getFileData in interface IGenericFileDao
      Type Parameters:
      E - Type of the entity with the file data.
      T - Type of the entity with the file metadata.
      Parameters:
      ec - Entity context for database transactions.
      clazz - Type of the file entity with the file metadata.
      id - ID of the file entity with the file metadata.
      Returns:
      The file content as a byte array, or null when no file content was found.
    • getFileData

      public <E extends IFileDataEntity<?>, T extends IFileProviding<?,E>> byte[] getFileData(IEntityContext ec, T entity)
      Description copied from interface: IGenericFileDao
      Returns the file data of a file entity.

      Use with care! As the name suggests, this holds the entire file data in memory. Prefer using an input stream instead.

      Specified by:
      getFileData in interface IGenericFileDao
      Type Parameters:
      E - Type of the entity with the file data.
      T - Type of the entity with the file metadata.
      Parameters:
      ec - Entity context for database transactions.
      entity - File entity with the file metadata.
      Returns:
      The file content as a byte array.
    • getFileData

      public <D extends IFileDataEntity<F>, F extends IKeyDependentFileEntity<E,D>, E extends IFileMapProviding<F,D>> byte[] getFileData(IEntityContext ec, E entity, String fileKey)
      Description copied from interface: IGenericFileDao
      Returns the file data of a file belonging to the given entity with the given file key.

      Use with care! As the name suggests, this holds the entire file data in memory. Prefer using an input stream instead.

      Specified by:
      getFileData in interface IGenericFileDao
      Type Parameters:
      D - file data entity type
      F - file entity type
      E - file map providing base entity type
      Parameters:
      ec - entity context for database transactions.
      entity - that owns the file
      fileKey - Key identifying the file to retrieve.
      Returns:
      The file data as a byte array.
    • getFileDataEntity

      public <E extends IFileDataEntity<?>, T extends IFileEntity<?,E>> E getFileDataEntity(IEntityContext ec, T entity)
      Description copied from interface: IGenericFileDao
      Get the file data entity for a given file entity. The entity is read from the database if it is detached.
      Specified by:
      getFileDataEntity in interface IGenericFileDao
      Type Parameters:
      E - Type of the entity with the file data.
      T - Type of the entity with the file metadata.
      Parameters:
      ec - Entity context for database transactions.
      entity - File entity with the file metadata.
      Returns:
      The file data entity for the given file entity.
    • getFileDataEntity

      public <D extends IFileDataEntity<F>, F extends IKeyDependentFileEntity<E,D>, E extends IFileMapProviding<F,D>> D getFileDataEntity(IEntityContext ec, E entity, String fileKey)
      Description copied from interface: IGenericFileDao
      Returns the file data entity of a file belonging to the given entity with the given file key.
      Specified by:
      getFileDataEntity in interface IGenericFileDao
      Type Parameters:
      D - file data entity type
      F - file entity type
      E - file map providing base entity type
      Parameters:
      ec - Entity context for database transactions.
      entity - that owns the file
      fileKey - key identifying the file entity
      Returns:
      file data entity containing the data of the request file.
    • getFileEntityData

      public <T extends IFileEntity<?,?>> byte[] getFileEntityData(IEntityContext ec, T fileEntity)
      Description copied from interface: IGenericFileDao
      Gets the data of the given file entity as an in-memory byte array.

      Use with care! As the name suggests, this holds the entire file data in memory. Prefer using an input stream instead.

      Specified by:
      getFileEntityData in interface IGenericFileDao
      Type Parameters:
      T - Type of the file entity with the data.
      Parameters:
      ec - Entity context for database transactions.
      fileEntity - File entity with the data.
      Returns:
      The data as a byte array, or null if no data exists.
    • getFileEntityData

      public <FileData extends IFileDataEntity<?>> byte[] getFileEntityData(IEntityContext ec, Class<FileData> fileDataEntityType, de.xima.cmn.criteria.FilterCriterion filter)
      Specified by:
      getFileEntityData in interface IGenericFileDao
    • getFileEntityData

      public <TFileMeta extends IFileMetaEntity<?,?>> byte[] getFileEntityData(IEntityContext ec, TFileMeta fileEntity)
      Description copied from interface: IGenericFileDao
      Returns the file data of the given file entity.
      Specified by:
      getFileEntityData in interface IGenericFileDao
      Type Parameters:
      TFileMeta - type of file entity.
      Parameters:
      ec - entity context for database transactions.
      fileEntity - that owns the file data.
      Returns:
      binary data of the file or null if no data exists.
    • getFileEntityStream

      public <T extends IFileEntity<?,?>> InputStream getFileEntityStream(IEntityContext ec, T entity)
      Description copied from interface: IGenericFileDao
      Gets the data of the given file entity as an input stream.
      Specified by:
      getFileEntityStream in interface IGenericFileDao
      Type Parameters:
      T - Type of the file entity with the data.
      Parameters:
      ec - Entity context for database transactions.
      entity - File entity with the data.
      Returns:
      The data as an input stream, or null if no data exists.
    • getFileEntityStream

      public <TFileMeta extends IFileMetaEntity<?,?>> InputStream getFileEntityStream(IEntityContext ec, TFileMeta fileEntity)
      Description copied from interface: IGenericFileDao
      Returns the file data of the given file entity as an input stream.
      Specified by:
      getFileEntityStream in interface IGenericFileDao
      Type Parameters:
      TFileMeta - type of file entity.
      Parameters:
      ec - entity context for database transactions.
      fileEntity - that owns the file data.
      Returns:
      input stream of the file data or null if no data exists.
    • getFileEntityStream

      public <FileData extends IFileDataEntity<?>> InputStream getFileEntityStream(IEntityContext ec, Class<FileData> fileDataEntityType, de.xima.cmn.criteria.FilterCriterion filter)
      Specified by:
      getFileEntityStream in interface IGenericFileDao
    • openFileEntityStreamLazy

      public <FileEntity extends IFileEntity<?, FileDataEntity>, FileDataEntity extends IFileDataEntity<FileEntity>> InputStream openFileEntityStreamLazy(Supplier<IEntityContext> entityContextFactory, Class<FileDataEntity> fileDataEntityType, FileEntity fileEntity)
      Description copied from interface: IGenericFileDao
      Opens an input stream to the file data of the given file entity.

      Similar to IGenericFileDao.getFileEntityStream(IEntityContext, IFileEntity), but does not access the file data entity directly. That would cause the file data entity to be initialized with the file content. If you keep a list of file entities in memory, this would cause the data to be associated with those file entities, effectively loading all files into RAM. Instead, this method reads the file data directly.

      Note: this method assumes that the IFileEntity only ever has a single IFileDataEntity.

      Specified by:
      openFileEntityStreamLazy in interface IGenericFileDao
      Type Parameters:
      FileEntity - Type of the file entity with the data.
      FileDataEntity - Type of the file data entity.
      Parameters:
      entityContextFactory - Factory for the entity context for database transactions.
      fileDataEntityType - Type of the file data entity.
      fileEntity - File entity with the data.
      Returns:
      The data as an input stream, or null if no data exists.
    • openFileEntityStreamLazy

      public InputStream openFileEntityStreamLazy(Supplier<IEntityContext> entityContextFactory, Class<? extends IFileDataEntity<?>> fileDataEntityType, Long fileEntityId)
      Description copied from interface: IGenericFileDao
      Opens an input stream to the file data of the given file entity.

      Similar to IGenericFileDao.getFileEntityStream(IEntityContext, IFileEntity), but does not access the file data entity directly. That would cause the file data entity to be initialized with the file content. If you keep a list of file entities in memory, this would cause the data to be associated with those file entities, effectively loading all files into RAM. Instead, this method reads the file data directly.

      Note: this method assumes that the IFileEntity only ever has a single IFileDataEntity.

      Specified by:
      openFileEntityStreamLazy in interface IGenericFileDao
      Parameters:
      entityContextFactory - Factory for the entity context for database transactions.
      fileDataEntityType - Type of the file data entity.
      fileEntityId - ID of the file entity with the data.
      Returns:
      The data as an input stream, or null if no data exists.
    • getFileStream

      public <D extends IFileDataEntity<F>, F extends IKeyDependentFileEntity<E,D>, E extends IFileMapProviding<F,D>> InputStream getFileStream(IEntityContext ec, E entity, String fileKey)
      Description copied from interface: IGenericFileDao
      Returns the file data of a file belonging to the given entity with the given file key, as an input stream.
      Specified by:
      getFileStream in interface IGenericFileDao
      Type Parameters:
      D - file data entity type
      F - file entity type
      E - file map providing base entity type
      Parameters:
      ec - entity context for database transactions.
      entity - that owns the file
      fileKey - Key identifying the file to retrieve.
      Returns:
      The file data as an input stream.
    • getFileStream

      public <E extends IFileDataEntity<?>, T extends IFileProviding<?,E>> InputStream getFileStream(IEntityContext ec, T entity)
      Specified by:
      getFileStream in interface IGenericFileDao
      Type Parameters:
      E - Type of the entity with the file data.
      T - Type of the entity with the file metadata.
      Parameters:
      ec - Entity context for database transactions.
      entity - File entity with the file metadata.
      Returns:
      The data of the file entity as an input stream, or null when no data exists.
    • openFileEntityStreamOutputStreamLazy

      public OutputStream openFileEntityStreamOutputStreamLazy(Supplier<IEntityContext> entityContextFactory, Class<? extends IFileEntity<?,?>> fileEntityType, Class<? extends IFileDataEntity<?>> fileDataEntityType, Long fileEntityId, Integer fileEntityLockingVersion, Integer fileDataEntityLockingVersion)
      Description copied from interface: IGenericFileDao
      Opens an output stream for updating the given file entity with the data.

      Does not access the file data entity directly. That would cause the file data entity to be initialized with the file content. If you keep a list of file entities in memory, this would cause the data to be associated with those file entities, effectively loading all files into RAM. Instead, this method updates the file data directly.

      Note 1: this method assumes that the IFileEntity only ever has a single IFileDataEntity.

      Note 2: After a call to this method, existing entity managers may be out-of-sync. You should discard the entity manager after calling this method; or at least clear or refresh / EntityManager.detach(Object) the file entity.

      Specified by:
      openFileEntityStreamOutputStreamLazy in interface IGenericFileDao
      Parameters:
      entityContextFactory - Factory for the entity context to use for database transactions.
      fileEntityType - Type of the file entity with the data.
      fileDataEntityType - Type of the file data entity.
      fileEntityId - ID of the file entity with the data.
      fileEntityLockingVersion - Locking version of the file entity. When given, updates only if the version in the database matches this version.
      fileDataEntityLockingVersion - Locking version of the file data entity. When given, updates only if the version in the database matches this version.
      Returns:
      An output stream for writing the new data.
    • updateFileData

      public <E extends IFileDataEntity<?>, T extends IFileEntity<?,E>> T updateFileData(IEntityContext ec, T entity, byte[] data)
      Description copied from interface: IGenericFileDao
      Replaces the data of the given file entity with the new data.
      Specified by:
      updateFileData in interface IGenericFileDao
      Type Parameters:
      E - Type of the entity with the file data.
      T - Type of the entity with the file metadata.
      Parameters:
      ec - Entity context for database transactions.
      entity - File entity with the file metadata.
      data - New data to set.
      Returns:
      The updated file entity.
    • updateFileData

      public void updateFileData(IEntityContext ec, Class<? extends IFileDataEntity<?>> entityType, long fileEntityId, InputStream data) throws IOException
      Description copied from interface: IGenericFileDao
      Replaces the data of the given file entity with the new data. Does not read the file data entity into memory, it won't be part of the entity manager's cache.
      Specified by:
      updateFileData in interface IGenericFileDao
      Parameters:
      ec - Entity context for database transactions.
      entityType - Type of the file entity with the file metadata.
      fileEntityId - ID of the file entity that contains the data entity.
      data - New data to set.
      Throws:
      IOException