Class EntityFileSystem<Entry extends IFileSystemEntry<Entry>,PersistenceContext,Entity,Scope>
- java.lang.Object
-
- de.xima.fc.logic.filesystem.EntityFileSystem<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
Afile 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 afile 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, usechangeScope
.- Since:
- 8.3.0
- Author:
- XIMA MEDIA GmbH
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
EntityFileSystem.BuilderStage1
A builder for configuring a newEntityFileSystem
.static class
EntityFileSystem.BuilderStage2<Entry extends IFileSystemEntry<Entry>,Entity,Scope>
A builder for configuring a newEntityFileSystem
.static class
EntityFileSystem.BuilderStage3<Entry extends IFileSystemEntry<Entry>,PersistenceContext,Entity,Scope>
A builder for configuring a newEntityFileSystem
.static class
EntityFileSystem.BuilderStage4<Entry extends IFileSystemEntry<Entry>,PersistenceContext,Entity,Scope>
A builder for configuring a newEntityFileSystem
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static EntityFileSystem.BuilderStage1
builder()
Gets a new builder for configuring a newEntityFileSystem
.void
changeScope(Scope scope)
Changes the scope of the entities, and reloads the entities for the new scope from the persistence layer.void
close()
void
copy(Entry source, Entry target)
Copies the file with the given source key to the target key.void
delete(Entry source)
Deletes the file or directory with the given key.boolean
exists(Entry entry)
Checks whether this file system contains a file (or folder) with the given key.Entity
findEntity(Object entry)
Finds the entity for the given file entry, if it exists.Instant
getFileLastModifiedAt(Entry entry)
Gets the last modified time of the file with the given key.String
getFileLastModifiedBy(Entry fileKey)
Gets the user who last modified the file with the given key.Iterable<Entry>
getFiles()
Gets the keys of all existing files.FileSize
getFileSize(Entry entry)
Gets the size of the file with the given key.boolean
hasPendingChanges(Entry entry)
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.boolean
isOpen()
Checks whether this file system is open.Iterable<Entry>
listFolder(Entry folder)
Gets the keys of all existing files within the given folder, non-recursively.Iterable<Entry>
listFolderRecursively(Entry folder)
Gets the keys of all existing files within the given folder, recursively.void
makeFile(Entry entry)
Creates a new file with no content.void
makeFolder(Entry entry)
Creates a new folder with no content.InputStream
openFileInput(Entry entry)
Gets an input stream for the data of the file with the given key.InputStream
openFileInputMarkable(Entry entry)
Gets an input stream for the data of the file with the given key.OutputStream
openFileOutput(Entry entry)
Opens an output stream to the given file key.void
refresh()
If the implementation employs any kind of caching: discards all caches and refreshes the file system so that all entries represent the most up-to-date state.void
refresh(Iterable<Entry> files)
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.Map<Entry,Entry>
rename(Entry source, Entry target)
Renames the file system entry with the given source key to the target key.<FileSystem>
FileSystemunwrap(Class<? extends FileSystem> type)
Unwraps this file system to the given type.
-
-
-
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 theentity adapter
supports null scopes (which usually corresponds the global scope).- Throws:
IOException
- When the entities for the new scope cannot be loaded.
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
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, anIOException
is thrown.If the source is a directory, all contents of the source must also be copied to the target.
- Specified by:
copy
in interfaceICommonFileSystem<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.
-
delete
public void delete(Entry source) throws IOException
Description copied from interface:ICommonFileSystem
Deletes the file or directory with the given key. If the file or directory does not exist, this method does nothing.- Specified by:
delete
in interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Parameters:
source
- The file descriptor of the file to delete.- Throws:
IOException
- If an error occurs while deleting the file.
-
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 interfaceICommonFileSystem<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.
-
getFileLastModifiedAt
public Instant getFileLastModifiedAt(Entry entry)
Description copied from interface:ICommonFileSystem
Gets the last modified time of the file with the given key. If the file does not exist, or if the last modified time is not available,Instant.EPOCH
is returned.- Specified by:
getFileLastModifiedAt
in interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Parameters:
entry
- The file descriptor.- Returns:
- The last modified time of the file, or
Instant.EPOCH
if the file does not exist.
-
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 interfaceICommonFileSystem<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 interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Parameters:
entry
- The file descriptor.- Returns:
- The size of the file, or 0 if unavailable.
-
getFiles
public Iterable<Entry> getFiles()
Description copied from interface:ICommonFileSystem
Gets the keys of all existing files.- Specified by:
getFiles
in interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Returns:
- The keys of all existing files.
-
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 interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Parameters:
entry
- The file descriptor.- Returns:
- true if the content has pending changes, false otherwise.
-
isOpen
public boolean isOpen()
Description copied from interface:ICommonFileSystem
Checks whether this file system is open. If the file system is not open, no operations can be performed. SeeAutoCloseable.close()
.- Specified by:
isOpen
in interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Returns:
- true if the file system is still open, 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 interfaceICommonFileSystem<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 interfaceICommonFileSystem<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.
-
makeFile
public void makeFile(Entry entry) throws IOException
Description copied from interface:ICommonFileSystem
Creates a new file with no content. Does nothing if the file already exists.- Specified by:
makeFile
in interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Parameters:
entry
- The file descriptor.- Throws:
IOException
- If an error occurs while creating the file.
-
makeFolder
public void makeFolder(Entry entry) throws IOException
Description copied from interface:ICommonFileSystem
Creates a new folder with no content. Does nothing if the file already exists.- Specified by:
makeFolder
in interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Parameters:
entry
- The key of the folder.- Throws:
IOException
- If an error occurs while creating the folder.
-
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 interfaceICommonFileSystem<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
andreset
operations.- Specified by:
openFileInputMarkable
in interfaceICommonFileSystem<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 interfaceICommonFileSystem<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() 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 entries represent the most up-to-date state.- Specified by:
refresh
in interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>
- Throws:
IOException
- If an error occurs while refreshing the file system.
-
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 interfaceICommonFileSystem<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 interfaceICommonFileSystem<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 interfaceICommonFileSystem<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.
-
builder
public static EntityFileSystem.BuilderStage1 builder()
Gets a new builder for configuring a newEntityFileSystem
.- Returns:
- A new builder instance.
-
-