org.mycore.datamodel.ifs
Class MCRDirectory

java.lang.Object
  extended by org.mycore.datamodel.ifs.MCRFilesystemNode
      extended by org.mycore.datamodel.ifs.MCRDirectory

public class MCRDirectory
extends MCRFilesystemNode

Represents a directory node with its metadata and content.

Version:
$Revision: 14528 $ $Date: 2008-12-09 17:45:41 +0100 (Di, 09 Dez 2008) $
Author:
Frank Lützenkirchen

Field Summary
static int DIRECTORIES
          Constant for choosing directory nodes *
static int FILES
          Constant for choosing file nodes *
static int HERE
          Constant for choosing only direct child nodes of this directory *
static int NODES
          Constant for choosing any node type *
static Comparator SORT_BY_DATE
          Sorts children by date of last modification *
static Comparator SORT_BY_NAME
          Sorts children by filename, case sensitive *
static Comparator SORT_BY_NAME_IGNORECASE
          Sorts children by filename, case insensitive *
static Comparator SORT_BY_SIZE
          Sorts children by file size or total directory size *
static int TOTAL
          Constant for choosing both direct and indirect child nodes contained in subdirectories of this directory *
 
Fields inherited from class org.mycore.datamodel.ifs.MCRFilesystemNode
deleted, formatter, ID, label, lastModified, manager, name, ownerID, parentID, size
 
Constructor Summary
MCRDirectory(String name, MCRDirectory parent)
          Creates a new, empty MCRDirectory with the given name in the parent MCRDirectory.
MCRDirectory(String name, MCRDirectory parent, boolean doExistCheck)
          Creates a new, empty MCRDirectory with the given name in the parent MCRDirectory.
MCRDirectory(String name, String ownerID)
          Creates a new, empty root MCRDirectory with the given name, belonging to the given ownerID.
MCRDirectory(String ID, String parentID, String ownerID, String name, String label, long size, GregorianCalendar date, int numchdd, int numchdf, int numchtd, int numchtf)
          Internal constructor, do not use on your own.
 
Method Summary
protected  void addChild(MCRFilesystemNode child)
          Adds a child node to this directory.
 byte[] buildFingerprint()
          Builds a fingerprint of all file's contents of this directory, by collecting the MD5 checksums of all direct or indirect child files of this directory, sorting them to ascending order and writing this list line by line as UTF-8 encoded bytes.
protected  void collectMD5Lines(List list)
          Creates a list of all MD5 checksums of all files that are direct or indirect children of this directory and adds them to the given list object.
 void delete()
          Deletes this directory and its content stored in the system
 MCRFilesystemNode getChild(int index)
          Returns the direct child of this directory at the given position in the natural order of the children
 MCRFilesystemNode getChild(String name)
          Returns the child node with the given filename.
 MCRFilesystemNode getChildByPath(String path)
          Returns the node that is addressed by the given absolute or relative path.
 MCRFilesystemNode[] getChildren()
          Returns all direct child nodes in this directory.
 MCRFilesystemNode[] getChildren(Comparator sortOrder)
          Returns all direct child nodes in this directory, sorted by the given Comparator implementation.
static MCRDirectory getDirectory(String ID)
          Returns the MCRDirectory with the given ID.
 int getNumChildren(int nodetype, int where)
          Returns the number of child nodes in this directory.
static MCRDirectory getRootDirectory(String ownerID)
          Returns the root MCRDirectory that has no parent and is logically owned by the object with the given ID.
 boolean hasChild(String name)
          Returns true, if this directory contains a direct child with the given filename.
 boolean hasChildren()
          Returns true if this directory is not empty and therefore contains any subdirectories or files.
protected  void removeChild(MCRFilesystemNode child)
          Removes a child node from this directory
protected  void sizeOfChildChanged(long sizeDiff)
          Internal method that is called when the size of a child node has changed, to update the total size of the parent directory.
 String toString()
           
protected  void touch()
          Updates the date of last modification to the current date and time, without changing anything else.
 
Methods inherited from class org.mycore.datamodel.ifs.MCRFilesystemNode
checkName, ensureNotDeleted, getAbsolutePath, getAdditionalData, getAllAdditionalData, getID, getLabel, getLastModified, getName, getNode, getOwnerID, getParent, getParentID, getPath, getRootDirectory, getRootNode, getSize, getSizeFormatted, getSizeFormatted, hasParent, removeAdditionalData, removeAllAdditionalData, setAdditionalData, setLabel, setName, storeNew
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FILES

public static final int FILES
Constant for choosing file nodes *

See Also:
Constant Field Values

DIRECTORIES

public static final int DIRECTORIES
Constant for choosing directory nodes *

See Also:
Constant Field Values

NODES

public static final int NODES
Constant for choosing any node type *

See Also:
Constant Field Values

HERE

public static final int HERE
Constant for choosing only direct child nodes of this directory *

See Also:
Constant Field Values

TOTAL

public static final int TOTAL
Constant for choosing both direct and indirect child nodes contained in subdirectories of this directory *

See Also:
Constant Field Values

SORT_BY_NAME_IGNORECASE

public static final Comparator SORT_BY_NAME_IGNORECASE
Sorts children by filename, case insensitive *


SORT_BY_NAME

public static final Comparator SORT_BY_NAME
Sorts children by filename, case sensitive *


SORT_BY_SIZE

public static final Comparator SORT_BY_SIZE
Sorts children by file size or total directory size *


SORT_BY_DATE

public static final Comparator SORT_BY_DATE
Sorts children by date of last modification *

Constructor Detail

MCRDirectory

public MCRDirectory(String name,
                    String ownerID)
Creates a new, empty root MCRDirectory with the given name, belonging to the given ownerID. The directory is assumed to be a standalone "root directory" that has no parent.

Parameters:
name - the name of the new MCRDirectory
ownerID - any ID String of the logical owner of this file

MCRDirectory

public MCRDirectory(String name,
                    MCRDirectory parent)
Creates a new, empty MCRDirectory with the given name in the parent MCRDirectory.

Parameters:
name - the name of the new MCRDirectory
parent - the parent directory that will contain the new child
Throws:
MCRUsageException - if that directory already contains a child with that name

MCRDirectory

public MCRDirectory(String name,
                    MCRDirectory parent,
                    boolean doExistCheck)
Creates a new, empty MCRDirectory with the given name in the parent MCRDirectory.

Parameters:
name - the name of the new MCRDirectory
parent - the parent directory that will contain the new child
doExistCheck - checks if file with that Name already exists
Throws:
MCRUsageException - if that directory already contains a child with that name

MCRDirectory

MCRDirectory(String ID,
             String parentID,
             String ownerID,
             String name,
             String label,
             long size,
             GregorianCalendar date,
             int numchdd,
             int numchdf,
             int numchtd,
             int numchtf)
Internal constructor, do not use on your own.

Method Detail

getDirectory

public static MCRDirectory getDirectory(String ID)
Returns the MCRDirectory with the given ID.

Parameters:
ID - the unique ID of the MCRDirectory to return
Returns:
the MCRDirectory with the given ID, or null if no such directory exists

getRootDirectory

public static MCRDirectory getRootDirectory(String ownerID)
Returns the root MCRDirectory that has no parent and is logically owned by the object with the given ID.

Parameters:
ownerID - the ID of the logical owner of that directory
Returns:
the root MCRDirectory stored for that owner ID, or null if no such directory exists

addChild

protected void addChild(MCRFilesystemNode child)
Adds a child node to this directory.

Parameters:
child - the new child

removeChild

protected void removeChild(MCRFilesystemNode child)
Removes a child node from this directory

Parameters:
child - the child to be removed from this directory

getChildren

public MCRFilesystemNode[] getChildren()
Returns all direct child nodes in this directory.

Returns:
a possibly empty array of MCRFilesystemNode objects

getChildren

public MCRFilesystemNode[] getChildren(Comparator sortOrder)
Returns all direct child nodes in this directory, sorted by the given Comparator implementation. You may use one of the Comparators defined as static constants in this class, or build your own Comparator.

Parameters:
sortOrder - the Comparator to be used to sort the children
Returns:
a possibly empty array of MCRFilesystemNode objects

hasChild

public boolean hasChild(String name)
Returns true, if this directory contains a direct child with the given filename.

Parameters:
name - the name of the child file or directory

getChild

public MCRFilesystemNode getChild(int index)
Returns the direct child of this directory at the given position in the natural order of the children

Parameters:
index - the index of the child in the list of children

getChild

public MCRFilesystemNode getChild(String name)
Returns the child node with the given filename. This method also accepts the aliases "." for the current directory or ".." for the parent directory.

Parameters:
name - the name of the child file or directory

getChildByPath

public MCRFilesystemNode getChildByPath(String path)
Returns the node that is addressed by the given absolute or relative path. If an absolute path starting with a slash "/" is given, the node will be located relative to the root directory of this directory. If a relative path is given, the path may contain the aliases "." for the current directory or ".." for the parent directory, and the node will be located relative to this directory.

Parameters:
path - the absolute path (starting with a "/") or path relative to this directory, possibly containing "." or ".." aliases
Returns:
the node that matches this path, or null if no such node exists

hasChildren

public boolean hasChildren()
Returns true if this directory is not empty and therefore contains any subdirectories or files.


getNumChildren

public int getNumChildren(int nodetype,
                          int where)
Returns the number of child nodes in this directory. The additional parameters control what type of nodes will be counted and if only direct children or all children will be counted.

Parameters:
nodetype - one of the constants FILES, DIRECTORIES, or NODES for both types
where - either HERE to only count direct children, or TOTAL to count also indirect children

sizeOfChildChanged

protected void sizeOfChildChanged(long sizeDiff)
Internal method that is called when the size of a child node has changed, to update the total size of the parent directory.


touch

protected void touch()
Updates the date of last modification to the current date and time, without changing anything else.


delete

public void delete()
            throws MCRPersistenceException
Deletes this directory and its content stored in the system

Overrides:
delete in class MCRFilesystemNode
Throws:
MCRPersistenceException

collectMD5Lines

protected void collectMD5Lines(List list)
Creates a list of all MD5 checksums of all files that are direct or indirect children of this directory and adds them to the given list object.


buildFingerprint

public byte[] buildFingerprint()
Builds a fingerprint of all file's contents of this directory, by collecting the MD5 checksums of all direct or indirect child files of this directory, sorting them to ascending order and writing this list line by line as UTF-8 encoded bytes. This method can be used to digitally sign the content of a directory in the future

Returns:
the fingerprint that changes when any content of a file in this directory changes

toString

public String toString()
Overrides:
toString in class MCRFilesystemNode