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 systemimplementation 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 adapterthat provides the necessary persistence-related operations for the entities.It also uses a separate
entry accessorto 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 entrythat 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 classEntityFileSystem.BuilderStage1A builder for configuring a newEntityFileSystem.static classEntityFileSystem.BuilderStage2<Entry extends IFileSystemEntry<Entry>,Entity,Scope>A builder for configuring a newEntityFileSystem.static classEntityFileSystem.BuilderStage3<Entry extends IFileSystemEntry<Entry>,PersistenceContext,Entity,Scope>A builder for configuring a newEntityFileSystem.static classEntityFileSystem.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.BuilderStage1builder()Gets a new builder for configuring a newEntityFileSystem.voidchangeScope(Scope scope)Changes the scope of the entities, and reloads the entities for the new scope from the persistence layer.voidclose()voidcopy(Entry source, Entry target)Copies the file with the given source key to the target key.voiddelete(Entry source)Deletes the file or directory with the given key.booleanexists(Entry entry)Checks whether this file system contains a file (or folder) with the given key.EntityfindEntity(Object entry)Finds the entity for the given file entry, if it exists.InstantgetFileLastModifiedAt(Entry entry)Gets the last modified time of the file with the given key.StringgetFileLastModifiedBy(Entry fileKey)Gets the user who last modified the file with the given key.Iterable<Entry>getFiles()Gets the keys of all existing files.FileSizegetFileSize(Entry entry)Gets the size of the file with the given key.booleanhasPendingChanges(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.booleanisOpen()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.voidmakeFile(Entry entry)Creates a new file with no content.voidmakeFolder(Entry entry)Creates a new folder with no content.InputStreamopenFileInput(Entry entry)Gets an input stream for the data of the file with the given key.InputStreamopenFileInputMarkable(Entry entry)Gets an input stream for the data of the file with the given key.OutputStreamopenFileOutput(Entry entry)Opens an output stream to the given file key.voidrefresh()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.voidrefresh(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 adaptersupports 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:
closein interfaceAutoCloseable
-
copy
public void copy(Entry source, Entry target) throws IOException
Description copied from interface:ICommonFileSystemCopies 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, anIOExceptionis thrown.If the source is a directory, all contents of the source must also be copied to the target.
- Specified by:
copyin 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:ICommonFileSystemDeletes the file or directory with the given key. If the file or directory does not exist, this method does nothing.- Specified by:
deletein 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:ICommonFileSystemChecks whether this file system contains a file (or folder) with the given key. Returns false if the file could not be checked.- Specified by:
existsin 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:ICommonFileSystemGets 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.EPOCHis returned.- Specified by:
getFileLastModifiedAtin interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>- Parameters:
entry- The file descriptor.- Returns:
- The last modified time of the file, or
Instant.EPOCHif the file does not exist.
-
getFileLastModifiedBy
public String getFileLastModifiedBy(Entry fileKey)
Description copied from interface:ICommonFileSystemGets 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:
getFileLastModifiedByin 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:ICommonFileSystemGets 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:
getFileSizein 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:ICommonFileSystemGets the keys of all existing files.- Specified by:
getFilesin interfaceICommonFileSystem<Entry extends IFileSystemEntry<Entry>>- Returns:
- The keys of all existing files.
-
hasPendingChanges
public boolean hasPendingChanges(Entry entry)
Description copied from interface:ICommonFileSystemChecks 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:
hasPendingChangesin 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:ICommonFileSystemChecks whether this file system is open. If the file system is not open, no operations can be performed. SeeAutoCloseable.close().- Specified by:
isOpenin 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:ICommonFileSystemGets 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:
listFolderin 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:ICommonFileSystemGets 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:
listFolderRecursivelyin 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:ICommonFileSystemCreates a new file with no content. Does nothing if the file already exists.- Specified by:
makeFilein 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:ICommonFileSystemCreates a new folder with no content. Does nothing if the file already exists.- Specified by:
makeFolderin 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:ICommonFileSystemGets 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:
openFileInputin 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:ICommonFileSystemGets 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
markandresetoperations.- Specified by:
openFileInputMarkablein 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:ICommonFileSystemOpens 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:
openFileOutputin 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 IOExceptionDescription copied from interface:ICommonFileSystemIf 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:
refreshin 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:ICommonFileSystemIf 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:
refreshin 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:ICommonFileSystemRenames 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:
renamein 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:ICommonFileSystemUnwraps 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:
unwrapin 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.
-
-