public class File extends CommonItem<FileData> implements Transactional, DiskConsumable, Subtypable
Modifier and Type | Class and Description |
---|---|
static class |
File.Action
A fileaction describes if a file should be moved to secondary storage
or brought back from it.
|
private class |
File.UploadStream
This class is used for uploading a file.
|
Modifier and Type | Field and Description |
---|---|
static String |
ALIGNED_SEQUENCE
The id for the
FileType item representing a
file containing aligned sequence data. |
private StreamCacher |
downloadCache
Files that are EXTERNAL are cached locally per transaction
|
static String |
EXTRAVALUE_DATA
The id for the
FileType item representing a
file containing analysed extra value data. |
static String |
IMAGE
The id for the
FileType item representing an
image file. |
private File |
internalFile
The path to the internal file.
|
static int |
MAX_CHARSET_LENGTH
The maximum length of the character set that can be stored in the database.
|
static int |
MAX_MIMETYPE_LENGTH
The maximum length of the MIME type that can be stored in the database.
|
static int |
MAX_URL_LENGTH
The maximum length of the URL type that can be stored in the database.
|
private File |
newInternalFile
The path to a newly uploaded file.
|
static String |
PLATE
The id for the
FileType item representing a
plate file. |
static String |
PLATE_MAPPING
The id for the
FileType item representing a
plate mapping file. |
static String |
PRINT_MAP
The id for the
FileType item representing a
print map file. |
static String |
PROTOCOL
The id for the
FileType item representing a
protocol. |
static String |
RAW_DATA
The id for the
FileType item representing a
raw data file. |
static String |
REPORTER
The id for the
FileType item representing a
reporter file. |
static String |
REPORTER_MAP
The id for the
FileType item representing a
reporter map file. |
private static String |
separator |
static String |
SPOT_DATA
The id for the
FileType item representing a
file containing analysed spot data. |
static String |
SPOT_IMAGES
The id for the
FileType item representing a
spot images file. |
private static SimpleDateFormat |
SUBDIR_FORMAT |
static Item |
TYPE
The type of item represented by this class.
|
private boolean |
uploadCompressed
If the next upload should use compression or not.
|
private File.UploadStream |
uploadStream
A stream for uploading a physical file.
|
MAX_DESCRIPTION_LENGTH, MAX_NAME_LENGTH
Constructor and Description |
---|
File(FileData data)
Creates a new file item from the given data.
|
Modifier and Type | Method and Description |
---|---|
void |
compress(ProgressReporter progress)
Compress the file stored on the disk.
|
private void |
compressOrDecompress(ProgressReporter progress,
boolean compress) |
void |
decompress(ProgressReporter progress)
Decompress the file stored on the disk.
|
void |
download(OutputStream out,
long offset)
Download the physical file for this file item.
|
static boolean |
exists(DbControl dc,
Directory directory,
String fileName)
Checks if a file with the specified name exists in the directory.
|
(package private) static File |
getAbsolutePath(String relativePath)
Convert a relative path back to an absolute file path.
|
File.Action |
getAction()
Get the
File.Action of this file. |
static File |
getById(DbControl dc,
int id)
Get a
File item when you know the id. |
static File |
getByPath(DbControl dc,
Path path,
boolean create)
Get a
File item when you know the path. |
long |
getBytes()
Get the absolute number of bytes this item uses.
|
InputStream |
getCachedDownloadStream()
Get a (possible) cached stream for reading from the file.
|
String |
getCharacterSet()
Get the character set used in this file (for text-files only).
|
long |
getCompressedSize()
Get the compressed size of this file.
|
long |
getDifference()
Always 0.
|
Directory |
getDirectory()
Get the associated
Directory item. |
InputStream |
getDownloadStream(long offset)
Get an
InputStream that can be used
to download the physical file for this file item. |
static File |
getFile(DbControl dc,
Directory directory,
String fileName,
boolean create)
Get a
File item when you know the directory and filename. |
FileServer |
getFileServer()
Get the file server on which the external file is located.
|
private String |
getInternalName()
Get the internal name of this file.
|
ItemSubtype |
getItemSubtype()
Get the subtype of the item.
|
Date |
getLastUpdate()
Get the date and time the actual file data was last updated.
|
Location |
getLocation()
Get the
Location the items is stored at. |
String |
getMd5()
Get the MD5 hash of the file contents, or null if not known.
|
String |
getMimeType()
The mimetype of this
File . |
static File |
getNew(DbControl dc,
Directory directory)
Create a new
File item. |
private File |
getNewFile(boolean compress)
Get a new random file path.
|
Path |
getPath()
Get the complete path of this file.
|
static ItemQuery<File> |
getQuery()
Get a query configured to retrieve
File items. |
String |
getQuotaTypeSystemId()
Always
QuotaType.FILE . |
(package private) static String |
getRelativePath(File file)
Get the relative path of the specified file from the base upload
directory and convert directory separator to forward slash.
|
private ConnectionParameters |
getServerInfo() |
long |
getSize()
Get the (uncompressed) size of this file.
|
Item |
getType()
Get the type of item represented by the object.
|
OutputStream |
getUploadStream(boolean checkMd5)
Get an
OutputStream that can be used
to upload a physical file for this file item. |
OutputStream |
getUploadStream(boolean checkMd5,
Boolean compress)
Get an
OutputStream that can be used
to upload a physical file for this file item. |
URI |
getURI()
Get the URL of an external file as an URI.
|
String |
getUrl()
Get the URL to the (external) file.
|
Set<ItemProxy> |
getUsingItems()
Get all:
FileAttachable items with this file attached
SpotImages using this file
|
(package private) void |
initPermissions(int granted,
int denied)
Check the write protected flag and deny DELETE if it is set.
|
boolean |
isCompressed()
If this file is stored in a compressed format or not.
|
boolean |
isUsed()
Checks if:
A
FileAttachable item is using this key. |
boolean |
isWriteProtected()
Check if the file data is write protected.
|
(package private) void |
onAfterCommit(Transactional.Action action)
This method is called on each
Transactional object
after a successful commit to the database. |
(package private) void |
onBeforeCommit(Transactional.Action action)
If a project is active, automatically share the new item according to
the settings of that project, unless a project or item key has been
explicitely set (including null).
|
(package private) void |
onRollback(Transactional.Action action)
This method is called on each
Transactional object
after an unsuccessful commit to the database. |
void |
setAction(File.Action action)
Set the action of this
File item. |
void |
setCharacterSet(String charset)
Set the character set used in this file (for text-files only).
|
void |
setDirectory(Directory directory)
Set the directory of this
File item. |
void |
setFileServer(FileServer fileServer)
Set the file server on which this external file is located.
|
private void |
setInternalName(String internalName)
Set the internal name of this file.
|
void |
setItemSubtype(ItemSubtype subtype)
Set the subtype on the item.
|
void |
setLocation(Location location)
Set the location of this
File item. |
private void |
setMetadataFromURI(UriMetadata metadata) |
void |
setMimeType(String mimeType)
Set the mime type for this
File item. |
void |
setMimeTypeAuto(String defaultMimeType,
ItemSubtype defaultFileType)
Set the mime type automatically by checking the extension of the
name.
|
void |
setName(String name)
Set the name of the item.
|
void |
setUrl(String url,
boolean loadMetadata)
Set the URL to an external file that should represent this file
object.
|
void |
setWriteProtected(boolean writeProtected)
Set the write protected flag for this file.
|
void |
updateURLMetadata()
If this is an external file, try to update the metadata.
|
void |
upload(InputStream in,
boolean checkMd5)
Upload a physical file for this file item.
|
void |
upload(InputStream in,
boolean checkMd5,
Boolean compress)
Upload a physical file for this file item.
|
boolean |
uploadCompressed()
Return the result of auto-compress check from the
last call to
setMimeTypeAuto(String, ItemSubtype) . |
boolean |
useDifference()
Always FALSE.
|
getDescription, getName, isRemoved, setDescription, setRemoved
getItemKey, getProjectKey, isShared, setItemKey, setProjectKey
getOwner, isOwner, setOwner, takeOwnership
addUsingItems, addUsingItems, checkPermission, equals, getData, getDbControl, getId, getPermissions, getPluginPermissions, getSessionControl, getVersion, hashCode, hasPermission, isDetached, isInDatabase, onAfterInsert, setDbControl, setProjectDefaults, toString, validate
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
getOwner, isOwner, setOwner, takeOwnership
getId, getVersion
checkPermission, getPermissions, hasPermission
public static final Item TYPE
public static final String PROTOCOL
FileType
item representing a
protocol.public static final String RAW_DATA
FileType
item representing a
raw data file.public static final String PLATE_MAPPING
FileType
item representing a
plate mapping file.public static final String REPORTER_MAP
FileType
item representing a
reporter map file.public static final String PRINT_MAP
FileType
item representing a
print map file.public static final String IMAGE
FileType
item representing an
image file.public static final String SPOT_IMAGES
FileType
item representing a
spot images file.public static final String REPORTER
FileType
item representing a
reporter file.public static final String PLATE
FileType
item representing a
plate file.public static final String SPOT_DATA
FileType
item representing a
file containing analysed spot data.public static final String EXTRAVALUE_DATA
FileType
item representing a
file containing analysed extra value data.public static final String ALIGNED_SEQUENCE
FileType
item representing a
file containing aligned sequence data.private File newInternalFile
private StreamCacher downloadCache
private File internalFile
private File.UploadStream uploadStream
private boolean uploadCompressed
public static final int MAX_MIMETYPE_LENGTH
setMimeType(String)
,
Constant Field Valuespublic static final int MAX_CHARSET_LENGTH
setCharacterSet(String)
,
Constant Field Valuespublic static final int MAX_URL_LENGTH
setUrl(String, boolean)
,
Constant Field Valuesprivate static final String separator
private static final SimpleDateFormat SUBDIR_FORMAT
File(FileData data)
data
- the data.public static File getNew(DbControl dc, Directory directory) throws BaseException
File
item.dc
- The DbControl
which will be used for
permission checking and database access.directory
- The Directory
that holds the file. It may
not be null.File
itemBaseException
- This exception is thrown if there is another
errorpublic static File getById(DbControl dc, int id) throws ItemNotFoundException, PermissionDeniedException, BaseException
File
item when you know the id.dc
- The DbControl
which will be used for
permission checking and database access.id
- The id of the item to loadFile
itemItemNotFoundException
- If an item with the specified id
is not foundPermissionDeniedException
- If the logged in user doesn't have
Permission.READ
permission to the itemBaseException
- If there is another errorpublic static File getByPath(DbControl dc, Path path, boolean create) throws ItemNotFoundException, PermissionDeniedException, BaseException
File
item when you know the path. Use the create
parameter to specify if a new file should be create if it doesn't
exists. The directory part of the path must still be an existing
path, otherwise an ItemNotFoundException
is thrown.dc
- The DbControl
which will be used for
permission checking and database access.path
- The path to the file to loadcreate
- TRUE if a new file should be created if one doesn't
eixts, FALSE to throw an exception (DbControl.saveItem must always be
called)File
itemItemNotFoundException
- If an item with the specified path
is not found and create is FALSEPermissionDeniedException
- If the logged in user doesn't
have Permission.READ
permission to the itemBaseException
- If there is another errorpublic static File getFile(DbControl dc, Directory directory, String fileName, boolean create) throws ItemNotFoundException, PermissionDeniedException, BaseException
File
item when you know the directory and filename.
Use the create parameter to specify if a new file should be create if
it doesn't exists.dc
- The DbControl
which will be used for
permission checking and database access.directory
- The directory where the file is located.fileName
- The name of the file.create
- TRUE if a new file should be created if one doesn't
eixts, FALSE to throw an exception (DbControl.saveItem must always be
called)File
itemItemNotFoundException
- If an item with the specified path
is not found and create is FALSEPermissionDeniedException
- If the logged in user doesn't
have Permission.READ
permission to the itemBaseException
- If there is another errorpublic static boolean exists(DbControl dc, Directory directory, String fileName) throws BaseException
dc
- The DbControl
which will be used for
permission checking and database access.directory
- The directory to check in.fileName
- The name of the file to look for.BaseException
public static ItemQuery<File> getQuery()
File
items.ItemQuery
objectpublic Item getType()
Identifiable
Item
enumeration.getType
in interface Identifiable
public void setName(String name) throws PermissionDeniedException, InvalidDataException
Nameable
Nameable.MAX_NAME_LENGTH
constant.setName
in interface Nameable
setName
in class CommonItem<FileData>
name
- The new name for the itemPermissionDeniedException
- If the logged in user doesn't
have write permissionInvalidDataException
- If the name is null or longer
than specified by the Nameable.MAX_NAME_LENGTH
constantpublic boolean useDifference()
useDifference
in interface DiskConsumable
DiskConsumable.getBytes()
,
DiskConsumable.getDifference()
public long getBytes()
DiskConsumable
getBytes
in interface DiskConsumable
DiskConsumable.useDifference()
,
DiskConsumable.getDifference()
public long getDifference()
getDifference
in interface DiskConsumable
public String getQuotaTypeSystemId()
QuotaType.FILE
.getQuotaTypeSystemId
in interface DiskConsumable
public Location getLocation()
DiskConsumable
Location
the items is stored at.getLocation
in interface DiskConsumable
public ItemSubtype getItemSubtype()
Subtypable
getItemSubtype
in interface Subtypable
SubtypableRelatedItems
public void setItemSubtype(ItemSubtype subtype)
Subtypable
setItemSubtype
in interface Subtypable
subtype
- A subtype or nullItemSubtype.setOnItem(Subtypable)
public boolean isUsed() throws BaseException
FileAttachable
item is using this key.
isUsed
in class BasicItem<FileData>
BaseException
- If there is another errorBasicItem.getUsingItems()
public Set<ItemProxy> getUsingItems()
FileAttachable
items with this file attached
SpotImages
using this file
getUsingItems
in class BasicItem<FileData>
BasicItem.addUsingItems(Set, Item, org.hibernate.Query)
void onBeforeCommit(Transactional.Action action) throws BaseException
SharedItem
onBeforeCommit
in class SharedItem<FileData>
BaseException
- If there is another errorTransactional
,
Developer documentation: Transactions,
Developer documentation: Coding rules and guidelines for item classesvoid onAfterCommit(Transactional.Action action)
BasicItem
Transactional
object
after a successful commit to the database. If the subclass
overrides this method it should also propagate the call
to the superclass, ie. super.onAfterCommit(action)
. It is forbidden
to access the DbControl
object from this method and
it must not throw any exceptions. All exceptions should be logged
using the Application.getLogger()
object.void onRollback(Transactional.Action action)
BasicItem
Transactional
object
after an unsuccessful commit to the database. If the subclass
overrides this method it should also propagate the call
to the superclass, ie. super.onRollback(action)
. It is forbidden
to access the DbControl
object from this method and
it must not throw any exceptions. All exceptions should be logged
using the Application.getLogger()
object.onRollback
in class BasicItem<FileData>
Transactional
,
Developer documentation: Transactions,
Developer documentation: Coding rules and guidelines for item classesvoid initPermissions(int granted, int denied)
initPermissions
in class SharedItem<FileData>
granted
- Permissions that have been granted by the subclassdenied
- Permissions that have been denied by the subclassprivate String getInternalName()
private void setInternalName(String internalName)
internalName
- The internal filename.public void setLocation(Location location) throws PermissionDeniedException, InvalidUseOfNullException
File
item.location
- The new Location
PermissionDeniedException
- If the logged in user doesn't have
write permission, or if the file is write protected and the new location isn't
Location.PRIMARY
InvalidUseOfNullException
- If location is nullpublic File.Action getAction()
File.Action
of this file.public void setAction(File.Action action) throws PermissionDeniedException, InvalidUseOfNullException
File
item. This method call
has no affect on external files.action
- The new File.Action
PermissionDeniedException
- If the logged in user doesn't have
write permissionInvalidUseOfNullException
- If action is nullpublic long getSize()
getCompressedSize()
public long getCompressedSize()
getSize()
isCompressed()
public boolean isCompressed()
public String getMd5()
public String getMimeType()
File
. It can be a defined MimeType
, any mimetype or null.public void setMimeType(String mimeType) throws PermissionDeniedException, InvalidDataException
File
item. It can be any mimetype or one
avalible in the mimetype table.mimeType
- The new mime type for this itemPermissionDeniedException
- If the logged in user doesn't have
write permissionInvalidDataException
- If the mime type is longer than MAX_MIMETYPE_LENGTH
public String getCharacterSet()
Charset.name()
, or null if not known or applicable to
the file typepublic void setCharacterSet(String charset)
charset
- The name of the character set as returned by
Charset.name()
public Directory getDirectory() throws PermissionDeniedException, BaseException
Directory
item.Directory
itemPermissionDeniedException
- This exception is thrown if
the logged in user doesn't have READ
permission to the itemsBaseException
- If there is another errorpublic void setDirectory(Directory directory) throws PermissionDeniedException, InvalidUseOfNullException
File
item.directory
- The new Directory
PermissionDeniedException
- If the logged in user doesn't
have Permission.WRITE
permission to the file or
Permission.USE
permission to the directoryInvalidUseOfNullException
- If directory is nullpublic boolean isWriteProtected()
public void setWriteProtected(boolean writeProtected) throws PermissionDeniedException
writeProtected
- TRUE if the file should be write protectedPermissionDeniedException
- If the logged in user doesn't
have write permission on the file item.isWriteProtected()
public Date getLastUpdate()
public String getUrl()
public URI getURI()
public void setUrl(String url, boolean loadMetadata)
url
- The url to the file (null is not allowed)loadMetadata
- If TRUE, a HEAD request will be issued to the given
URL and metadata such as size, MIME type, etc. will be extracted if
given in the responseInvalidDataException
- If the URL is null, too long, or not a valid URLPermissionDeniedException
- If the logged in user doesn't have write permissionpublic void updateURLMetadata()
public FileServer getFileServer()
public void setFileServer(FileServer fileServer) throws PermissionDeniedException, InvalidUseOfNullException
fileServer
- The new FileServer
PermissionDeniedException
- If the logged in user doesn't
have Permission.WRITE
permission to the file or
Permission.USE
permission to the file serverInvalidUseOfNullException
private ConnectionParameters getServerInfo()
private void setMetadataFromURI(UriMetadata metadata)
public void setMimeTypeAuto(String defaultMimeType, ItemSubtype defaultFileType) throws PermissionDeniedException
MimeType.getAutoCompress()
flag and select
auto-compression for the next upload if it is set. Auto-compression
is also selected if the Directory.getAutoCompress()
flag is
set.defaultMimeType
- The default value to use if no match is found for the file
extensiondefaultFileType
- The default file type to set if no match is found or null
to leave it unchangedPermissionDeniedException
- If the logged in user doesn't have
Permission.WRITE
permissionuploadCompressed()
public boolean uploadCompressed()
setMimeTypeAuto(String, ItemSubtype)
.
If this method returns true, the next call to upload(InputStream, boolean)
or getUploadStream(boolean)
will store the uploaded file in a
compressed format.public void compress(ProgressReporter progress)
Location.PRIMARY
location this method does nothing.progress
- An optional progress reporterPermissionDeniedException
- If the logged in user doesn't have write
permission on the filepublic void decompress(ProgressReporter progress)
Location.PRIMARY
location this method does nothing.progress
- An optional progress reporterPermissionDeniedException
- If the logged in user doesn't have write
permission on the fileprivate void compressOrDecompress(ProgressReporter progress, boolean compress)
public void upload(InputStream in, boolean checkMd5) throws PermissionDeniedException, BaseException
upload(InputStream, boolean, Boolean)
. A null value is used
for the compress parameter to enable auto-compression.in
- Input stream from the file to be uploaded.checkMd5
- TRUE if the Md5 sum should be checked, FALSE otherwise.PermissionDeniedException
- If the logged in user doesn't have permission
to upload the fileBaseException
- If there is some other error.public void upload(InputStream in, boolean checkMd5, Boolean compress) throws PermissionDeniedException, BaseException
in
- An InputStream
which will be read
until end of file is foundcheckMd5
- If the MD5 sum of the uploaded data should
be checked against the MD5 sum already stored in the database.
If no MD5 exists in the database this parameter is ignoredcompress
- TRUE to store the file in a compressed format, or null
to use whatever the Directory.getAutoCompress()
is specifying
or what auto-detection from the setMimeTypeAuto(String, ItemSubtype)
foundPermissionDeniedException
- If the logged in user doesn't have
Permission.WRITE
permission for this item, or if the
file is write protectedBaseException
- If there is another errorpublic OutputStream getUploadStream(boolean checkMd5)
OutputStream
that can be used
to upload a physical file for this file item. This method calls
getUploadStream(boolean, Boolean)
. A null value is used
for the compress parameter to enable auto-compression.checkMd5
- TRUE if the Md5 sum should be checked, FALSE otherwisepublic OutputStream getUploadStream(boolean checkMd5, Boolean compress) throws PermissionDeniedException, BaseException
OutputStream
that can be used
to upload a physical file for this file item. The client
application should call close()
method to
finish the upload. Note! Calling this method multiple times
without closing the returned stream will return the same stream
and the checkMd5 and compress parameters are ignored.checkMd5
- If the MD5 sum of the uploaded data should
be checked against the MD5 sum already stored in the database.
If no MD5 exists in the database this parameter is ignoredcompress
- TRUE to store the file in a compressed format, or null
to use whatever the Directory.getAutoCompress()
is specifying
or what auto-detection from the setMimeTypeAuto(String, ItemSubtype)
foundOutputStream
objectPermissionDeniedException
- If the logged in user doesn't have
Permission.WRITE
permission for this item or if the
file is write protectedBaseException
- If there is another errorpublic void download(OutputStream out, long offset) throws BaseException
out
- An OutputStream
object which
will be used to write the fileoffset
- Start reading the physical at the specified offset,
use 0 to start reading from the beginningBaseException
- If there is an errorpublic InputStream getDownloadStream(long offset) throws BaseException
InputStream
that can be used
to download the physical file for this file item. The client
application should call close()
method to
finish the download. If the file is an external file, the file
will be loaded from the URL given by getUrl()
, otherwise
if the physical file isn't at the primary location an empty stream
is returned.offset
- Start reading the physical at the specified offset,
use 0 to start reading from the beginningInputStream
objectBaseException
- If there is an errorpublic InputStream getCachedDownloadStream()
InputStream
objectStreamCacher
private File getNewFile(boolean compress) throws BaseException
BaseException
- If there is an error.static String getRelativePath(File file)
getAbsolutePath(String)
static File getAbsolutePath(String relativePath)
getRelativePath(java.io.File)