org.bibalex.daf.entities.phase
Class StepThroughCheckStrategy

java.lang.Object
  extended by org.bibalex.daf.entities.phase.CheckStrategy
      extended by org.bibalex.daf.entities.phase.StepThroughCheckStrategy

public class StepThroughCheckStrategy
extends CheckStrategy

Used to apply phase checks against a folder structure by evaluating one node at a time. The name 'StepThrough' refers to querying the file system at each step not one time in order to decrease memory consumption.

Author:
Shehab.Kamal

Field Summary
private  java.util.ArrayList<NameCountCompareTriple> filesCount
          Stores each file as it is being visited and it physical count.
private  FileHandler handler
          A handler to get information about the folder physically.
private  java.lang.String jobName
          Holds the name of the job.
 
Constructor Summary
StepThroughCheckStrategy()
          Creates a new instance of StepThroughCheckStrategy.
 
Method Summary
 void applyChecks(PhysicalDef physicalDef, java.lang.String jobPath, FileHandler fileHandler, PhysicalDef alternateDef, java.lang.String alternatePath, FileHandler alternateHandler)
          Compares the folder structure against the given definition.
private  void checkFilesExist(FolderElement folder, java.lang.String path)
          Checks that each file type defined within the given FolderElement has a corresponding physical files.
private  void checkFoldersExist(FolderElement folder, java.lang.String path)
          Checks that each folder defined within the given FolderElement has a corresponding physical folder.
private  void checkFolderStructure(FolderElement folder, java.lang.String folderPath)
          Makes sure that folder structure conforms to the defintion.
private  void checkNoMoreFilesExist(FolderElement folder, java.lang.String path, NameCountCompareTriple[] files)
          For a 'Restricted' mode folder, this method makes sure that no extra file types exists except for those specified in the definition.
private  void checkNoMoreFoldersExist(FolderElement folder, java.lang.String path)
          For a 'Restricted' mode folder, this method makes sure that no extra folders exists except for those specified in the definition.
private  void compareFiles(NameCountCompareTriple[] files)
          Compare the files count.
private  void countAlternateFiles(PhysicalDef physicalDef, java.lang.String path, FileHandler handler)
          Counts the files mentioned in the PhysicalDef.
private  void countFiles(FolderElement folder, java.lang.String path, FileHandler handler)
          Count the files within the given folder.
private  int getCountByName(NameCountCompareTriple[] files, java.lang.String name)
          Gets the count associated with the given name.
private  FolderElement[] getTestFolders(FolderElement[] folders)
          Gets the folders that will have their structure tested, that is folders with 'Create' attribute set to false.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

handler

private FileHandler handler
A handler to get information about the folder physically. The type of the hanlder determines whether the check are performed against a local or remote folder.


filesCount

private java.util.ArrayList<NameCountCompareTriple> filesCount
Stores each file as it is being visited and it physical count.


jobName

private java.lang.String jobName
Holds the name of the job. It is used in case of validating the files names.

Constructor Detail

StepThroughCheckStrategy

public StepThroughCheckStrategy()
Creates a new instance of StepThroughCheckStrategy.

Method Detail

applyChecks

public void applyChecks(PhysicalDef physicalDef,
                        java.lang.String jobPath,
                        FileHandler fileHandler,
                        PhysicalDef alternateDef,
                        java.lang.String alternatePath,
                        FileHandler alternateHandler)
                 throws PhaseCheckException
Compares the folder structure against the given definition.

Specified by:
applyChecks in class CheckStrategy
Parameters:
physicalDef - The XML definition of the job folder.
jobPath - The complete path of the job folder. The path corresponds to the given PhysicalDef. That is if the physicalDef is PrePhase then the path is FTP path and if the physicalDef is PostPhase then the path is local.
fileHandler - A concrete FileHandler to get information about the job folder. The handler corresponds to the given physicalDef. That is if the given PhysicalDef is PrePhase then the handler is FTPImpl and if the PhysicalDef is PostPhase then the handler is LocalImpl.
alternateDef - The alternate XML definition of the job. By alternate we mean if the given physicalDef is PrePhase then this should be PostPhase and vice versa.
alternatePath - The alternate path of the job folder. By alternate we mean if the given job path is a local one then this will be a remote path and vice versa.
alternateHandler - A concrete FileHandler to get information about the folder physically. By alternate we mean if the given handler is a FTPImpl then this one is LocalImpl and vice versa.
Throws:
PhaseCheckException - If the checks fails.

checkFolderStructure

private void checkFolderStructure(FolderElement folder,
                                  java.lang.String folderPath)
                           throws PhaseCheckException
Makes sure that folder structure conforms to the defintion.

Parameters:
folder - The folder definition in the XML file.
folderPath - The complete physical path corresponding to the folder being checked.
Throws:
PhaseCheckException - If the checking of the folder fails.

checkFoldersExist

private void checkFoldersExist(FolderElement folder,
                               java.lang.String path)
                        throws PhaseCheckException
Checks that each folder defined within the given FolderElement has a corresponding physical folder. Folder that have the 'Create' property set to true won't be checked.

Parameters:
folder - The FolderElement that defines other elements within it.
path - The path of the physical folder that corresponds to the folder being tested.
Throws:
PhaseCheckException - If the folders defined in the FolderElement don't exist physically.

getTestFolders

private FolderElement[] getTestFolders(FolderElement[] folders)
Gets the folders that will have their structure tested, that is folders with 'Create' attribute set to false.

Parameters:
folders - The entire set of folders.
Returns:
Only the folders that have the 'Create' attributes set to false, if no folders exists an empty array is returned.

checkFilesExist

private void checkFilesExist(FolderElement folder,
                             java.lang.String path)
                      throws PhaseCheckException
Checks that each file type defined within the given FolderElement has a corresponding physical files. This method also compares the count fo files against each other.

Parameters:
folder - The FolderElement that defines other elements within it.
path - The path of the physical folder that corresponds to the folder being tested.
Throws:
PhaseCheckException - If the file types defined in the FolderElement don't exist physically or comparing fails.

checkNoMoreFoldersExist

private void checkNoMoreFoldersExist(FolderElement folder,
                                     java.lang.String path)
                              throws PhaseCheckException
For a 'Restricted' mode folder, this method makes sure that no extra folders exists except for those specified in the definition.

Parameters:
folder - The FolderElement that defines other elements within it.
path - The path of the physical folder that corresponds to the folder being tested.
Throws:
PhaseCheckException - If extra folders are found.

checkNoMoreFilesExist

private void checkNoMoreFilesExist(FolderElement folder,
                                   java.lang.String path,
                                   NameCountCompareTriple[] files)
                            throws PhaseCheckException
For a 'Restricted' mode folder, this method makes sure that no extra file types exists except for those specified in the definition.

Parameters:
folder - The FolderElement that defines other elements within it.
path - The path of the physical folder that corresponds to the folder being tested.
files - An array holding the name and the count of each file type.
Throws:
PhaseCheckException - If extra file types are found.

countAlternateFiles

private void countAlternateFiles(PhysicalDef physicalDef,
                                 java.lang.String path,
                                 FileHandler handler)
Counts the files mentioned in the PhysicalDef.

Parameters:
physicalDef - The physical defintion describing the job.
path - The physical path of the job.
handler - The handler used to get information about the job.

countFiles

private void countFiles(FolderElement folder,
                        java.lang.String path,
                        FileHandler handler)
Count the files within the given folder. Note: The folder doesn't return anything because as each file is encountered, it is counted and added to the filesCount instance field.

Parameters:
folder - The FolderElement to count the files defined within it.
path - The path of the folder.
handler - The FileHandler used to count the files.

compareFiles

private void compareFiles(NameCountCompareTriple[] files)
                   throws PhaseCheckException
Compare the files count.

Parameters:
files - A Hashtable that holds each FileElement and its physical count.
Throws:
PhaseCheckException - If the comparison fails. A comparison can fail due to numerous reasons: 1) If the file doesn't have an associated entry doesn't have an entry in the given Hashtable. 2) If the file mentioned in the 'Compare' attribute doesn't have an entry in the given Hashtable. 3) The count of files doesn't match.

getCountByName

private int getCountByName(NameCountCompareTriple[] files,
                           java.lang.String name)
Gets the count associated with the given name.

Parameters:
files - An array holding the name and count of each file. The compare field is not used.
name - The name of the entry we are looking for.
Returns:
The assocaited count if an entry having the given name is found, otherwise -1.