Class EntityFileSystem<Entry extends IFileSystemEntry<Entry>,​PersistenceContext,​Entity,​Scope>

  • Type Parameters:
    Entry - The type of the file system entry used by the entity.
    PersistenceContext - The type of the persistence context used to access the persistence layer.
    Entity - The type of the persistent entity representing the file metadata, such as a JPA entity with the appropriate fields.
    Scope - The type of the scope for the persisted entities, such as a client or project.
    All Implemented Interfaces:
    ICommonFileSystem<Entry>, Serializable, AutoCloseable

    public final class EntityFileSystem<Entry extends IFileSystemEntry<Entry>,​PersistenceContext,​Entity,​Scope>
    extends Object
    implements ICommonFileSystem<Entry>, Serializable
    A file system implementation for entities that represent the files in the file system and are persisted in some kind of persistence layer, such as for example JPA entities. All changes are immediately synchronized with the persistence layer.

    In order not to couple too tightly to the exact type of persistent entity, this class uses a persistence adapter that provides the necessary persistence-related operations for the entities.

    It also uses a separate entry accessor to map the entities to the corresponding file system entries, and to change the entry of an entity (e.g. when moving the entity to a different location in the file system). Each entity is associated with a file system entry that represents the location of the corresponding file in the file system. For example, if the entity stores the file path in a string field, the accessor could split that string at the path separator and return a new file system entry with those path parts.

    The entity type should be the metadata of the file, such as the name or size. A list of those entities is kept in memory, the file data is loaded on-demand from the persistence layer, via the configured file node entity adapter).

    Only a subset of entities can be selected by specifying a scope. For example, this could be a client for client-scoped file entities, or a form for form-scoped file entities. Only the entities that match the context will be loaded into the file system. If you wish to change to a different scope, use changeScope.

    Since:
    8.3.0
    Author:
    XIMA MEDIA GmbH
    See Also:
    Serialized Form
    • Method Detail

      • changeScope

        public void changeScope​(Scope scope)
                         throws IOException
        Changes the scope of the entities, and reloads the entities for the new scope from the persistence layer.

        For example, when the scope is a client, the file system will contain only entities from that client. You can use this method to switch to the entities of a different client.

        Parameters:
        scope - The new scope for the entities. Can be null if the entity adapter supports null scopes (which usually corresponds the global scope).
        Throws:
        IOException - When the entities for the new scope cannot be loaded.
      • copy

        public void copy​(Entry source,
                         Entry target)
                  throws IOException
        Description copied from interface: ICommonFileSystem
        Copies the file with the given source key to the target key. If the source key does not exist, this method does nothing. If the target key exists already, an IOException is thrown.

        If the source is a directory, all contents of the source must also be copied to the target.

        Specified by:
        copy in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        source - The descriptor of the file to copy.
        target - The descriptor of the file to copy to.
        Throws:
        IOException - If an error occurs while copying the file data.
      • exists

        public boolean exists​(Entry entry)
        Description copied from interface: ICommonFileSystem
        Checks whether this file system contains a file (or folder) with the given key. Returns false if the file could not be checked.
        Specified by:
        exists in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        entry - The file descriptor.
        Returns:
        true if the file (or folder) exists, false otherwise.
      • findEntity

        public Entity findEntity​(Object entry)
        Finds the entity for the given file entry, if it exists.
        Parameters:
        entry - A file entry.
        Returns:
        The entity for the file entry, or null if no entity exists for the entry.
      • getFileLastModifiedBy

        public String getFileLastModifiedBy​(Entry fileKey)
        Description copied from interface: ICommonFileSystem
        Gets the user who last modified the file with the given key. If the file does not exist, or if the last modified user is not available, the empty string is returned.
        Specified by:
        getFileLastModifiedBy in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        fileKey - The key of the file.
        Returns:
        The user who last modified the file, or the empty string if the file does not exist.
      • getFileSize

        public FileSize getFileSize​(Entry entry)
        Description copied from interface: ICommonFileSystem
        Gets the size of the file with the given key. If the file does not exist, or if the size cannot be determined 0 is returned.
        Specified by:
        getFileSize in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        entry - The file descriptor.
        Returns:
        The size of the file, or 0 if unavailable.
      • hasPendingChanges

        public boolean hasPendingChanges​(Entry entry)
        Description copied from interface: ICommonFileSystem
        Checks whether the content of the file with the given key has been changed and is in a pending state, not yet persisted or synchronized with an external storage medium.
        Specified by:
        hasPendingChanges in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        entry - The file descriptor.
        Returns:
        true if the content has pending changes, false otherwise.
      • listFolder

        public Iterable<Entry> listFolder​(Entry folder)
        Description copied from interface: ICommonFileSystem
        Gets the keys of all existing files within the given folder, non-recursively. Only the files in the given folder are returned, not files in subfolders.

        The given folder is not included in the result.

        Specified by:
        listFolder in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        folder - The key to start at.
        Returns:
        The keys of all existing files within the given folder.
      • listFolderRecursively

        public Iterable<Entry> listFolderRecursively​(Entry folder)
        Description copied from interface: ICommonFileSystem
        Gets the keys of all existing files within the given folder, recursively. This is a recursive operation, so all files in all subfolders are included. Folder contents are returned in breadth-first order, i.e. all files in the same folder are returned before files in subfolders.

        The given folder is not included in the result.

        Specified by:
        listFolderRecursively in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        folder - The file descriptor of the folder.
        Returns:
        The keys of all existing files within the given folder and subfolders.
      • openFileInput

        public InputStream openFileInput​(Entry entry)
                                  throws IOException
        Description copied from interface: ICommonFileSystem
        Gets an input stream for the data of the file with the given key. If the file does not exist, or if the file data is not available, an empty input stream is returned.

        No guarantees are made regarding the features of the input stream, other than data can be read from it.

        Specified by:
        openFileInput in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        entry - The file descriptor.
        Returns:
        An input stream for the data of the file, or an empty stream if the file does not exist or the data is not available.
        Throws:
        IOException - If an error occurs while opening the file data from the temporary file.
      • openFileInputMarkable

        public InputStream openFileInputMarkable​(Entry entry)
                                          throws IOException
        Description copied from interface: ICommonFileSystem
        Gets an input stream for the data of the file with the given key. If the file does not exist, or if the file data is not available, an empty input stream is returned.

        The returned input stream supports mark and reset operations.

        Specified by:
        openFileInputMarkable in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        entry - The file descriptor.
        Returns:
        An input stream for the data of the file, or an empty stream if the file does not exist or the data is not available.
        Throws:
        IOException - If an error occurs while opening the file data from the temporary file.
      • openFileOutput

        public OutputStream openFileOutput​(Entry entry)
                                    throws IOException
        Description copied from interface: ICommonFileSystem
        Opens an output stream to the given file key. If the file does not exist yet, it is created. If the file exists already, the content is overwritten.
        Specified by:
        openFileOutput in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        entry - The file descriptor.
        Returns:
        An output stream for writing the file data.
        Throws:
        IOException - If an error occurs while opening the file output stream.
      • refresh

        public void refresh​(Iterable<Entry> files)
                     throws IOException
        Description copied from interface: ICommonFileSystem
        If the implementation employs any kind of caching: discards all caches and refreshes the file system so that all given entries represent the most up-to-date state. If a file is given, only that file is refreshed. If a folder is given, all files and folder in that folder are refreshed (recursively).
        Specified by:
        refresh in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        files - The files to refresh.
        Throws:
        IOException - If an error occurs while refreshing the file system.
      • rename

        public Map<Entry,​Entry> rename​(Entry source,
                                             Entry target)
                                      throws IOException
        Description copied from interface: ICommonFileSystem
        Renames the file system entry with the given source key to the target key. If the source key does not exist, this method does nothing.

        When source and target are directories, all contents of the source must also be moved to the target.

        Specified by:
        rename in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Parameters:
        source - The descriptor of the file to rename.
        target - The descriptor of the file towards which to rename.
        Returns:
        Mapping from the old to the new files or directories. Includes the contents of directories if the source is a directory.
        Throws:
        IOException - If an error occurs while renaming the file.
      • unwrap

        public <FileSystem> FileSystem unwrap​(Class<? extends FileSystem> type)
        Description copied from interface: ICommonFileSystem
        Unwraps this file system to the given type. If this file system is an instance of the given type, it is returned. Otherwise, potential wrappers are unwrapped until the given type is reached. If the file system cannot be unwrapped to the given type, null is returned.

        If multiple wrappers are present, the first one that can be unwrapped to the given type is returned.

        Specified by:
        unwrap in interface ICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
        Type Parameters:
        FileSystem - The type of the desired file system.
        Parameters:
        type - The type to unwrap to.
        Returns:
        The unwrapped file system, or null if the file system cannot be unwrapped to the given type.