Package net.sf.basedb.core
Class Directory
- java.lang.Object
-
- net.sf.basedb.core.BasicItem
-
- net.sf.basedb.core.OwnedItem
-
- net.sf.basedb.core.SharedItem
-
- net.sf.basedb.core.Directory
-
- All Implemented Interfaces:
AccessControlled
,Identifiable
,Nameable
,Ownable
,Removable
,Shareable
,SystemItem
public class Directory extends SharedItem implements Nameable, Removable, SystemItem
This is used to represent directory items. Directories can be used to groupFile
:s into more manageable compartments. Directories can be organised into an arbitrarily deep tree-like structure. For each directory no two sub-directories may have the same name, and no two files may have the same name. However, it is legal for a file to have the same name as a directory.- Version:
- 2.0
- Author:
- enell
- Last modified
- $Date: 2017-05-22 14:35:27 +0200 (må, 22 maj 2017) $
-
-
Field Summary
Fields Modifier and Type Field Description private static ThreadLocal<Map<String,Directory>>
DIRECTORY_CACHE
static String
HOME
The id for theDirectory
item representing the home directory.static String
ROOT
The id for theDirectory
item representing the root.static String
TEMPLATE
The id for theDirectory
item representing the template directory.static Item
TYPE
The type of item represented by this class.-
Fields inherited from interface net.sf.basedb.core.Nameable
MAX_DESCRIPTION_LENGTH, MAX_NAME_LENGTH
-
Fields inherited from interface net.sf.basedb.core.SystemItem
MAX_SYSTEM_ID_LENGTH
-
-
Constructor Summary
Constructors Constructor Description Directory(DirectoryData data)
Creates a new directory item from the given data.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
copySubdirectories(DbControl dc, Directory from, Directory to, User owner)
This method copies a directory strucure to the destination directory.static Directory
createHomeDirectory(DbControl dc, User user, boolean copyTemplate)
Creates a homedirectory for a specified user.static boolean
exists(DbControl dc, Directory directory, String subdirectoryName)
Checks if a subdirectory with the specified name exists in a directory.boolean
getAutoCompress()
If files that are uploaded to this directory should be compressed or not.boolean
getAutoShare()
If new files and subdirectories that are created in this directory automatically should be shared wtih the same permissions as this directory.static Directory
getById(DbControl dc, int id)
Get aDirectory
item when you know the ID.static Directory
getByPath(DbControl dc, Path path)
Get aDirectory
item when you know the path.(package private) DirectoryData
getData()
Get theBasicData
object that holds all data for this item.String
getDescription()
Get the description for the item.ItemQuery<File>
getFiles()
Get a query that return all files in this directory.static int
getIdFromPath(DbControl dc, Path path)
Get the id of a directory when you know the path.String
getName()
Get the name of the item.static Directory
getNew(DbControl dc, Directory parent)
Create a newDirectory
item.static Directory
getNew(DbControl dc, Path path)
Create a new or existingDirectory
from a path.Directory
getParent()
Get the parent directory of this directory.Path
getPath()
Get the complete path to this directory.static ItemQuery<Directory>
getQuery()
Get a query configured to retrieve directories.User
getRemovedBy()
Get the user that flagged this item for removal.ItemQuery<Directory>
getSubDirectories()
Get a query that return subdirectories of this directory.String
getSystemId()
Get the system id for the item.Item
getType()
Get the type of item represented by the object.Set<ItemProxy>
getUsingItems()
Get all:File
:s in this directory Sub-Directory
:s in this directoryUser
:s having this directory as their home-directoryExperiment
:s having this directory as the plugin directory(package private) void
initPermissions(int granted, int denied)
Grant permissions according to the item and project keys.boolean
isHomeDirectory()
Returns TRUE if this directory is a subdirectory toHOME
.boolean
isRemoved()
Check if the removed flag is set for this item.boolean
isRootDirectory()
Returns TRUE if this directories parent equals null and therfore is a root directory.boolean
isSystemItem()
Check if the item is a system item or not.boolean
isUsed()
Check that: no files are in the directory no subdirectories are in the directory no user has this directory as the home directoryDirectory
newSubDirectory()
Create a new subdirectory to this directory.void
setAutoCompress(boolean autoCompress)
Set if files of should automatically be compressed when they are uploaded to this directory.void
setAutoShare(boolean autoShare)
Set if new files and subdirectories of should automatically be shared with the same permissions as this directory.void
setDescription(String description)
Set the description for the item.void
setName(String name)
Set the name of the item.void
setParent(Directory parent)
Set the parent directory of this directory.void
setRemoved(boolean removed)
Set the removed flag for this item.-
Methods inherited from class net.sf.basedb.core.SharedItem
getItemKey, getProjectKey, isShared, onBeforeCommit, setItemKey, setProjectKey
-
Methods inherited from class net.sf.basedb.core.OwnedItem
getOwner, isOwner, setOwner, takeOwnership
-
Methods inherited from class net.sf.basedb.core.BasicItem
addAnnotatableParents, addUsingItems, addUsingItems, breakCircularReferences, checkBatchAnnotatableUsage, checkPermission, equals, getBasicData, getDbControl, getId, getPermissions, getPluginPermissions, getSessionControl, getVersion, hashCode, hasPermission, isDetached, isInDatabase, onAfterCommit, onAfterInsert, onRollback, setDbControl, setProjectDefaults, toString, validate
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.basedb.core.AccessControlled
checkPermission, getPermissions, hasPermission
-
Methods inherited from interface net.sf.basedb.core.Identifiable
getId, getVersion
-
Methods inherited from interface net.sf.basedb.core.Ownable
getOwner, isOwner, setOwner, takeOwnership
-
-
-
-
Field Detail
-
TYPE
public static final Item TYPE
The type of item represented by this class.- See Also:
Item.DIRECTORY
,getType()
-
ROOT
public static final String ROOT
The id for theDirectory
item representing the root.- See Also:
- Constant Field Values
-
HOME
public static final String HOME
The id for theDirectory
item representing the home directory. The home directory is the parent directory for the user's home directories.- See Also:
- Constant Field Values
-
TEMPLATE
public static final String TEMPLATE
The id for theDirectory
item representing the template directory. The template directory is used to create the directory structure for new users.- See Also:
- Constant Field Values
-
DIRECTORY_CACHE
private static ThreadLocal<Map<String,Directory>> DIRECTORY_CACHE
-
-
Constructor Detail
-
Directory
Directory(DirectoryData data)
Creates a new directory item from the given data.- Parameters:
data
- the data.
-
-
Method Detail
-
getNew
public static Directory getNew(DbControl dc, Directory parent) throws BaseException
Create a newDirectory
item.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.parent
- The parent directory for this directory. It cannot be null- Returns:
- The new
Directory
item - Throws:
PermissionDeniedException
- This exception is thrown if the logged in user doesn't haveCREATE
permission for this type of itemsBaseException
- This exception is thrown if there is another error
-
getNew
public static Directory getNew(DbControl dc, Path path)
Create a new or existingDirectory
from a path. Every directory that doesn't exist is created.NOTE! If the directory given by the path already exists, the existing directory object is returned. Since there is existing code that expects this behavior we will not change the implementation to throw an exception which might have been expected.
- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accesspath
- The path to create- Returns:
- The directory representing the path
-
getById
public static Directory getById(DbControl dc, int id) throws ItemNotFoundException, PermissionDeniedException, BaseException
Get aDirectory
item when you know the ID.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.id
- The ID of the item to load- Returns:
- The
Directory
item - Throws:
ItemNotFoundException
- If an item with the specified ID is not foundPermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission to the itemBaseException
- If there is another error
-
getByPath
public static Directory getByPath(DbControl dc, Path path) throws ItemNotFoundException, PermissionDeniedException, BaseException
Get aDirectory
item when you know the path.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.path
- The path to the directory to load- Returns:
- The
Directory
item - Throws:
ItemNotFoundException
- If an item with the specified path is not foundPermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission to the itemBaseException
- If there is another error
-
getIdFromPath
public static int getIdFromPath(DbControl dc, Path path) throws BaseException
Get the id of a directory when you know the path.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.path
- The path to the directory to load- Returns:
- The id of the
Directory
item - Throws:
ItemNotFoundException
- If an item with the specified path is not foundBaseException
- If there is another error
-
exists
public static boolean exists(DbControl dc, Directory directory, String subdirectoryName) throws BaseException
Checks if a subdirectory with the specified name exists in a directory.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.directory
- The directory to check insubdirectoryName
- The name of the subdirectory to look for- Returns:
- TRUE if the file exists, FALSE otherwise
- Throws:
BaseException
- If there is an error
-
createHomeDirectory
public static Directory createHomeDirectory(DbControl dc, User user, boolean copyTemplate) throws PermissionDeniedException, BaseException
Creates a homedirectory for a specified user. The Directory will be placed in the predefined home directory,HOME
.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.user
- The user whose home directry should be created.copyTemplate
- TRUE if the new home directory shold copy the directory structure in the template directory.- Returns:
- The new hom directory.
- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.WRITE
orPermission.CREATE
permission to the item.BaseException
- If there is another error.
-
copySubdirectories
public static void copySubdirectories(DbControl dc, Directory from, Directory to, User owner) throws BaseException
This method copies a directory strucure to the destination directory.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.from
- The source directory to copyto
- The destination directory where the new directory structure is createdowner
- The owner of the destination directory- Throws:
BaseException
- If there is an error
-
getQuery
public static ItemQuery<Directory> getQuery()
Get a query configured to retrieve directories.- Returns:
- An
ItemQuery
object
-
getData
DirectoryData getData()
Description copied from class:BasicItem
Get theBasicData
object that holds all data for this item.- Overrides:
getData
in classSharedItem
-
getName
public String getName()
Description copied from interface:Nameable
Get the name of the item.
-
setName
public void setName(String name) throws PermissionDeniedException, InvalidDataException
Description copied from interface:Nameable
Set the name of the item. The name cannot be null and mustn't be longer than the value specified by theNameable.MAX_NAME_LENGTH
constant.- Specified by:
setName
in interfaceNameable
- Parameters:
name
- The new name for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the name is null or longer than specified by theNameable.MAX_NAME_LENGTH
constant
-
getDescription
public String getDescription()
Description copied from interface:Nameable
Get the description for the item.- Specified by:
getDescription
in interfaceNameable
- Returns:
- A
String
with a description of the item
-
setDescription
public void setDescription(String description) throws PermissionDeniedException, InvalidDataException
Description copied from interface:Nameable
Set the description for the item. The description can be null but mustn't be longer than the value specified by theNameable.MAX_DESCRIPTION_LENGTH
constant.- Specified by:
setDescription
in interfaceNameable
- Parameters:
description
- The new description for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the description longer than specified by theNameable.MAX_DESCRIPTION_LENGTH
constant
-
getType
public Item getType()
Description copied from interface:Identifiable
Get the type of item represented by the object. The returned value is one of the values defined in theItem
enumeration.- Specified by:
getType
in interfaceIdentifiable
- Returns:
- A value indicating the type of item
-
isRemoved
public boolean isRemoved()
Description copied from interface:Removable
Check if the removed flag is set for this item.
-
setRemoved
public void setRemoved(boolean removed) throws PermissionDeniedException
Description copied from interface:Removable
Set the removed flag for this item.- Specified by:
setRemoved
in interfaceRemovable
- Parameters:
removed
- TRUE if the item should be flagged as removed, FALSE otherwise- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.DELETE
permission for setting the flag to TRUE orPermission.WRITE
permission for setting the flag to FALSE
-
getRemovedBy
public User getRemovedBy() throws PermissionDeniedException, ItemNotFoundException
Description copied from interface:Removable
Get the user that flagged this item for removal.- Specified by:
getRemovedBy
in interfaceRemovable
- Returns:
- A User object, or null if this item has not been flagged
- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission for the userItemNotFoundException
- If the user that removed this item can't be found
-
getSystemId
public String getSystemId()
Description copied from interface:SystemItem
Get the system id for the item.- Specified by:
getSystemId
in interfaceSystemItem
- Returns:
- The id of the item or null if it is not a system item
-
isSystemItem
public boolean isSystemItem()
Description copied from interface:SystemItem
Check if the item is a system item or not. A system item have a non-null value for the system id.- Specified by:
isSystemItem
in interfaceSystemItem
- Returns:
- TRUE if this item is a system item, FALSE otherwise
-
isUsed
public boolean isUsed() throws BaseException
Check that:- no files are in the directory
- no subdirectories are in the directory
- no user has this directory as the home directory
- Overrides:
isUsed
in classBasicItem
- Returns:
- TRUE if the directory is used.
- Throws:
BaseException
- If there is an error.- See Also:
BasicItem.getUsingItems()
-
getUsingItems
public Set<ItemProxy> getUsingItems()
Get all:File
:s in this directory- Sub-
Directory
:s in this directory User
:s having this directory as their home-directoryExperiment
:s having this directory as the plugin directory
- Overrides:
getUsingItems
in classBasicItem
- Returns:
- A set containing proxies for the items, or an empty set if no items are using this item
- Since:
- 2.2
- See Also:
BasicItem.addUsingItems(Set, Item, org.hibernate.query.Query)
-
initPermissions
void initPermissions(int granted, int denied) throws BaseException
Description copied from class:SharedItem
Grant permissions according to the item and project keys.- Overrides:
initPermissions
in classSharedItem
- Parameters:
granted
- Permissions that have been granted by the subclassdenied
- Permissions that have been denied by the subclass- Throws:
BaseException
- If the permissions couldn't be initialised
-
getParent
public Directory getParent() throws PermissionDeniedException, BaseException
Get the parent directory of this directory.- Returns:
- The parent
Directory
, or null if this directory is a root directory - Throws:
PermissionDeniedException
- This exception is thrown if the logged in user doesn't haveREAD
permission to the itemsBaseException
- If there is another error
-
setParent
public void setParent(Directory parent) throws PermissionDeniedException, InvalidDataException
Set the parent directory of this directory.- Parameters:
parent
- The new parent directory. Cant be null.- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.WRITE
permission to this directory orPermission.USE
permission to the parent directory permission for the parent directoryInvalidDataException
- If parent is null or is a subdirectory to this directory
-
getAutoCompress
public boolean getAutoCompress()
If files that are uploaded to this directory should be compressed or not. This setting only affects files when they are uploaded. It doesn't affect files that are moved between directories.- Returns:
- TRUE to automatically compress files uploaded to this directory
- Since:
- 2.5
-
setAutoCompress
public void setAutoCompress(boolean autoCompress) throws PermissionDeniedException
Set if files of should automatically be compressed when they are uploaded to this directory.- Parameters:
autoCompress
- TRUE to enable auto-compression- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission- Since:
- 2.5
-
getAutoShare
public boolean getAutoShare()
If new files and subdirectories that are created in this directory automatically should be shared wtih the same permissions as this directory. This setting only affects new files/directories.- Returns:
- TRUE to automatically share new files and subdreictories
- Since:
- 2.8
-
setAutoShare
public void setAutoShare(boolean autoShare) throws PermissionDeniedException
Set if new files and subdirectories of should automatically be shared with the same permissions as this directory. If a project is active and this directory isn't already shared to the project, the permissions will be extended to also share the new item to the project.- Parameters:
autoShare
- TRUE to enable auto-sharing- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission- Since:
- 2.8
-
isRootDirectory
public boolean isRootDirectory()
Returns TRUE if this directories parent equals null and therfore is a root directory.- Returns:
- TRUE if this directory is root directory.
-
isHomeDirectory
public boolean isHomeDirectory()
Returns TRUE if this directory is a subdirectory toHOME
.- Returns:
- TRUE is this directory is a home directory.
-
newSubDirectory
public Directory newSubDirectory() throws PermissionDeniedException, BaseException
Create a new subdirectory to this directory.- Returns:
- The new
Directory
item - Throws:
PermissionDeniedException
- This exception is thrown if the logged in user doesn't haveCREATE
permission for directory items, orWRITE
permission for this directoryBaseException
- This exception is thrown if there is another error
-
getSubDirectories
public ItemQuery<Directory> getSubDirectories()
Get a query that return subdirectories of this directory.- Returns:
- An
ItemQuery
object
-
-