Changeset 8703


Ignore:
Timestamp:
2004-11-30T11:26:44+13:00 (20 years ago)
Author:
chi
Message:

Modifications for the validated METS format and program layout change

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/DocumentWriter.java

    r5800 r8703  
    77public class DocumentWriter
    88{
    9   public DocumentWriter()
    10   {
    11   }
     9    public DocumentWriter()
     10    {
     11    }
    1212
    13   public boolean writeDocument(DocumentInterface document, PrintWriter output)
    14   {     
    15     // TODO: output document in METS format, with Greenstone-specific metadata
    16     // placed in a wrapper inside the descriptive metadata block
    17     String tag = XMLTools.getOpenTag("mets", "mets");
    18     if (document.getID() != null)
    19     { tag = XMLTools.addAttribute(tag, "OBJID", document.getID().toString());
     13    public boolean writeDocument(DocumentInterface document, PrintWriter output)
     14    {       
     15    // TODO: output <xml header>
     16    String xml_header ="<?xml ";
     17    xml_header = XMLTools.addAttribute(xml_header, "version", "1.0");
     18    xml_header = XMLTools.addAttribute(xml_header, "encoding", "UTF-8");
     19    xml_header = XMLTools.addAttribute(xml_header, "standalone", "no");
     20    xml_header = xml_header+"?>";
     21    output.println(xml_header);
     22
     23    // TODO: output document in METS format, with Greenstone-specific metadata
     24    // placed in a wrapper inside the descriptive metadata block
     25    String tag = XMLTools.getOpenTag("mets", "mets");
     26    if (document.getID() != null){
     27        tag = XMLTools.addAttribute(tag,"xmlns:mets","http://www.loc/gov/METS/");
     28        tag = XMLTools.addAttribute(tag,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
     29        tag = XMLTools.addAttribute(tag,"xmlns:gsdl3","http://www.greenstone.org/gsdlmetadata/1.0/");
     30        tag = XMLTools.addAttribute(tag,"xmlns:xlink","http://www.w3.org/TR/xlink/");
     31        tag = XMLTools.addAttribute(tag,"xsi:schemaLocation","http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/mets.xsd http://www.greenstone.org/gsdlmetadata/1.0/ http://www.greenstone.org/gsdlmetadata/1.0/gsdl_metadata_schema.xsd");
     32        tag = XMLTools.addAttribute(tag, "OBJID", document.getID().toString());
     33    }
     34    output.println(tag);
     35   
     36    // Writing the structural metadata:
     37    //   if in "Create METS" mode:
     38    //     take all the documents as being in the one group, of the "physical"
     39    //     document; particular document plugins may extend this behaviour,
     40    //     which would suggest placing it in the AbstractDocument or similar
     41    //     class?!
     42    document.getDocumentMetadata().write(output);
     43
     44    // TODO: only output the document if there is some
     45    // content/formatting change between the original and
     46    // what greenstone actually does; if the original is
     47    // good, then place a marker saying "get the document
     48    // "from the original file"
     49    if  (document.isMETSCompatible()){
     50        document.getDocumentFiles().write(output);
     51        document.getDocumentStructure().write(output);
     52    }
     53    else {
     54        DocumentWriter writer = document.getMETSWriter();
     55       
     56        // ensure that this call isn't recursive -
     57        // TODO: This is a rather naive check and needs improving
     58        if (writer != this) {
     59        writer.writeDocument(document, output);
     60        }
     61    }
     62    output.write("</mets:mets>\n");
     63    return true;
    2064    }
    21     output.println(tag);
    22    
    23     // Writing the structural metadata:
    24     //   if in "Create METS" mode:
    25     //     take all the documents as being in the one group, of the "physical"
    26     //     document; particular document plugins may extend this behaviour,
    27     //     which would suggest placing it in the AbstractDocument or similar
    28     //     class?!
    29     document.getDocumentMetadata().write(output);
    30 
    31     // TODO: only output the document if there is some
    32     // content/formatting change between the original and
    33     // what greenstone actually does; if the original is
    34     // good, then place a marker saying "get the document
    35     // "from the original file"
    36     if  (document.isMETSCompatible())
    37     {
    38       document.getDocumentFiles().write(output);
    39       document.getDocumentStructure().write(output);
    40     }
    41     else
    42     { DocumentWriter writer = document.getMETSWriter();
    43    
    44       // ensure that this call isn't recursive -
    45       // TODO: This is a rather naive check and needs improving
    46       if (writer != this) {
    47     writer.writeDocument(document, output);
    48       }
    49     }
    50     output.write("</mets:mets>\n");
    51     return true;
    52   }
    5365}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDescriptiveSection.java

    r5800 r8703  
    22
    33public abstract class METSDescriptiveSection
    4 { METSLocation location;
    5     String       type;
    6     String       otherType;
    7 
    8     public METSDescriptiveSection(String type)
    9     {   this.location  = null;
    10         this.type      = type;
    11         this.otherType = null;
    12     }
     4{
     5    METSLocation location;
     6    String       type;
     7    String       otherType;
     8   
     9    public METSDescriptiveSection(String type)
     10    {   
     11    this.location  = null;
     12    this.type      = type;
     13    this.otherType = null;
     14    }
    1315   
    14     public METSDescriptiveSection(METSLocation location, String type)
    15     {   this.location  = location;
    16         this.type      = type;
    17         this.otherType = null;
    18     }
     16    public METSDescriptiveSection(METSLocation location, String type)
     17    {   
     18    this.location  = location;
     19    this.type      = type;
     20    this.otherType = null;
     21    }
    1922}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDivision.java

    r8461 r8703  
    2222public class METSDivision extends AbstractStructure
    2323{
    24   String       order;       // see the METS documentation for the meaning of the different
    25   String       orderLabel;  // label types.
    26   String       userLabel;
    27   String       type;        // e.g. section, chapter, physical or logical, etc.
    28   List         metadataRefs;
    29   List         adminRefs;
    30   List         fileRefs;
    31  
    32   public static final String DIVISION_TYPE  = "Division";
    33   public static final String DIVISION_TAG   = "div";
    34   public static final String FPTR_TAG       = "fptr";
    35 
    36   public METSDivision(String ID, String order, String orderLabel, String userLabel, String type)
    37   { super(ID);
    38 
    39     this.order = order;
    40     this.orderLabel = orderLabel;
    41     this.userLabel = userLabel;
    42     this.type = type;
    43     this.fileRefs = new ArrayList();
    44     this.metadataRefs = new ArrayList();
    45   }
    46 
    47   public String getStructureType()
    48   { return DIVISION_TYPE;
    49   }
    50 
    51   public String getLabel()
    52   { return this.orderLabel;
    53   }
    54 
    55   /**
    56    *  Find all the divisions that match a given list of file group
    57    *  identifiers...
    58    *
    59    *  @param <code>List</code> the list of identifiers to find
    60    *  @param <code>List</code> the list into which to place any
    61    *         matching divisions.
    62    */
    63   public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList)
    64   { Iterator iterator = this.fileRefs.iterator();
    65    
    66     // get the references on this division, and mark if necessary
    67     while (iterator.hasNext())
    68     { String reference = iterator.next().toString();
    69 
    70       Iterator fileIterator = listOfFileIdentifiers.iterator();
    71       while (fileIterator.hasNext())
    72       { String fileRef = fileIterator.next().toString();
    73      
    74         if (fileRef.equals(reference))
    75     { resultList.add(this);
    76     }
    77       }
    78     }
    79    
    80     // get the child groups, and check each in turn...
    81     Iterator groups = this.children.values().iterator();
    82     while (groups.hasNext())
    83     { METSDivision group = (METSDivision) groups.next();
    84 
    85       group.findDivisionsForFiles(listOfFileIdentifiers, resultList);
    86     }   
    87   }
    88 
    89   /**
    90    *  Get the namespace from the given set of descriptive blocks...
    91    */
    92   public METSNamespace findNamespace(String namespaceName, boolean isOpenNamespace,
    93                      METSDescriptiveSet descriptiveSet)
    94   { METSNamespace namespace = null;
    95 
    96     // Special case - no existing metadata references
    97     if (this.metadataRefs.size() == 0) {
    98       // Create new Descriptive and add it to the descriptive set
    99       METSDescriptive descriptive = new METSDescriptive("DM"+this.ID, this.orderLabel);
    100       descriptiveSet.addDescriptive(descriptive);
    101 
    102       // Create the namespace and add it to the Descriptive
    103       namespace = NamespaceFactory.initNamespace(namespaceName);
    104       if (namespace != null)
    105       { descriptive.addNamespace(namespace);
    106       }
    107 
    108       return namespace;
    109     }
    110 
    111     Iterator descriptiveIterator = this.metadataRefs.iterator();
    112 
    113     while (descriptiveIterator.hasNext())
    114     { String descriptiveRef = descriptiveIterator.next().toString();
    115 
    116       METSDescriptive descriptive = descriptiveSet.getDescriptiveById(descriptiveRef);
    117 
    118       if (isOpenNamespace) {
    119     namespace = descriptive.getOpenNamespace(namespaceName);
    120       }
    121       else {
    122     namespace = descriptive.getNamespace(namespaceName);
    123       }
    124    
    125       if (namespace != null)
    126     break;
    127     }
    128 
    129     // Special case - no such namespace in any descriptive list...
    130     if (namespace == null)
    131     { // take the first descriptive area...
    132       METSDescriptive descriptive = descriptiveSet.getDescriptiveById(this.metadataRefs.get(0).toString());
    133 
    134       // create the namespace, and add it to the METSDescriptive
    135       namespace = NamespaceFactory.initNamespace(namespaceName);
    136       if (namespace != null)
    137       { descriptive.addNamespace(namespace);
    138       }
    139     }
    140 
    141     return namespace;
    142   }
    143 
    144   /**
    145    *  Add a new file reference to the structure
    146    *
    147    *  @param <code>METSFileID</code> the file to be referenced.
    148    */
    149   public void addFileReference(METSFileID fileRef)
    150   { this.addFileReference(fileRef.toString());
    151   }
    152 
    153   public void addFileReference(String fileRef)
    154   { this.fileRefs.add(fileRef);
    155   }
    156 
    157   /**
    158    *  Add a new metadata reference to the structure
    159    */
    160   public void addMetadataReference(String metadataRef)
    161   { // TODO: correct the type
    162     this.metadataRefs.add(metadataRef);
    163   }
    164 
    165   /**
    166    *  Get all the metadata references
    167    *
    168    *  @return <code>List</code> containing all the references
    169    */
    170   public List getMetadataReferences()
    171   { return this.metadataRefs;
    172   }
    173 
    174   /**
    175    *  Get all the default (first) metadata reference...
    176    *
    177    *  @return <code>List</code> containing all the references
    178    */
    179   //TODO: check for this first *open* metadata item...
    180   public String getDefaultMetadataReference()
    181   { if (this.metadataRefs.size() == 0) {
    182       return null;
     24    String       order;       // see the METS documentation for the meaning of the different
     25    String       orderLabel;  // label types.
     26    String       userLabel;
     27    String       type;        // e.g. section, chapter, physical or logical, etc.
     28    List         metadataRefs;
     29    List         adminRefs;
     30    List         fileRefs;
     31   
     32    public static final String DIVISION_TYPE  = "Division";
     33    public static final String DIVISION_TAG   = "div";
     34    public static final String FPTR_TAG       = "fptr";
     35   
     36    public METSDivision(String ID, String order, String orderLabel, String userLabel, String type)
     37    {
     38    super(ID);
     39   
     40    this.order = order;
     41    this.orderLabel = orderLabel;
     42    this.userLabel = userLabel;
     43    this.type = type;
     44    this.fileRefs = new ArrayList();
     45    this.metadataRefs = new ArrayList();
     46    }
     47   
     48    public String getStructureType()
     49    {
     50    return DIVISION_TYPE;
     51    }
     52   
     53    public String getLabel()
     54    {
     55    return this.orderLabel;
     56    }
     57   
     58    /**
     59     *  Find all the divisions that match a given list of file group
     60     *  identifiers...
     61     *
     62     *  @param <code>List</code> the list of identifiers to find
     63     *  @param <code>List</code> the list into which to place any
     64     *         matching divisions.
     65     */
     66    public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList)
     67    {
     68    Iterator iterator = this.fileRefs.iterator();
     69   
     70    // get the references on this division, and mark if necessary
     71    while (iterator.hasNext()) {
     72        String reference = iterator.next().toString();
     73       
     74        Iterator fileIterator = listOfFileIdentifiers.iterator();
     75        while (fileIterator.hasNext()){
     76        String fileRef = fileIterator.next().toString();
     77       
     78        if (fileRef.equals(reference)){
     79            resultList.add(this);
     80        }
     81        }
     82    }
     83   
     84    // get the child groups, and check each in turn...
     85    Iterator groups = this.children.values().iterator();
     86    while (groups.hasNext()){
     87        METSDivision group = (METSDivision) groups.next();
     88       
     89        group.findDivisionsForFiles(listOfFileIdentifiers, resultList);
     90    }   
     91    }
     92   
     93    /**
     94     *  Get the namespace from the given set of descriptive blocks...
     95     */
     96    public METSNamespace findNamespace(String namespaceName, boolean isOpenNamespace,
     97                       METSDescriptiveSet descriptiveSet)
     98    {
     99    METSNamespace namespace = null;
     100   
     101    // Special case - no existing metadata references
     102    if (this.metadataRefs.size() == 0) {
     103        // Create new Descriptive and add it to the descriptive set
     104        METSDescriptive descriptive = new METSDescriptive("DM"+this.ID, this.orderLabel);
     105        descriptiveSet.addDescriptive(descriptive);
     106       
     107        // Create the namespace and add it to the Descriptive
     108        namespace = NamespaceFactory.initNamespace(namespaceName);
     109        if (namespace != null){
     110        descriptive.addNamespace(namespace);
     111        }
     112       
     113        return namespace;
     114    }
     115   
     116    Iterator descriptiveIterator = this.metadataRefs.iterator();
     117   
     118    while (descriptiveIterator.hasNext()){
     119        String descriptiveRef = descriptiveIterator.next().toString();
     120       
     121        METSDescriptive descriptive = descriptiveSet.getDescriptiveById(descriptiveRef);
     122       
     123        if (isOpenNamespace) {
     124        namespace = descriptive.getOpenNamespace(namespaceName);
     125        }
     126        else {
     127        namespace = descriptive.getNamespace(namespaceName);
     128        }
     129       
     130        if (namespace != null)
     131        break;
     132    }
     133   
     134    // Special case - no such namespace in any descriptive list...
     135    if (namespace == null){
     136        // take the first descriptive area...
     137        METSDescriptive descriptive = descriptiveSet.getDescriptiveById(this.metadataRefs.get(0).toString());
     138       
     139        // create the namespace, and add it to the METSDescriptive
     140        namespace = NamespaceFactory.initNamespace(namespaceName);
     141        if (namespace != null){
     142        descriptive.addNamespace(namespace);
     143        }
     144    }
     145   
     146    return namespace;
     147    }
     148   
     149    /**
     150     *  Add a new file reference to the structure
     151     *
     152     *  @param <code>METSFileID</code> the file to be referenced.
     153     */
     154    public void addFileReference(METSFileID fileRef)
     155    {
     156    this.addFileReference(fileRef.toString());
     157    }
     158   
     159    public void addFileReference(String fileRef)
     160    {
     161    this.fileRefs.add(fileRef);
     162    }
     163   
     164    /**
     165     *  Add a new metadata reference to the structure
     166     */
     167    public void addMetadataReference(String metadataRef)
     168    {
     169    // TODO: correct the type
     170    this.metadataRefs.add(metadataRef);
     171    }
     172
     173    /**
     174     *  Get all the metadata references
     175     *
     176     *  @return <code>List</code> containing all the references
     177     */
     178    public List getMetadataReferences()
     179    {
     180    return this.metadataRefs;
     181    }
     182
     183    /**
     184     *  Get all the default (first) metadata reference...
     185     *
     186     *  @return <code>List</code> containing all the references
     187     */
     188    //TODO: check for this first *open* metadata item...
     189    public String getDefaultMetadataReference()
     190    {
     191    if (this.metadataRefs.size() == 0) {
     192    return null;
    183193    }
    184194    return this.metadataRefs.get(0).toString();
    185   }
    186 
    187   /**
    188    *  Get all the default (first) first filegroup reference...
    189    *
    190    *  @return <code>List</code> containing all the references
    191    */
    192   public String getDefaultFileReference()
    193   { if (this.fileRefs.size() == 0) {
    194       return null;
     195    }
     196
     197    /**
     198     *  Get all the default (first) first filegroup reference...
     199     *
     200     *  @return <code>List</code> containing all the references
     201     */
     202    public String getDefaultFileReference()
     203    {
     204    if (this.fileRefs.size() == 0) {
     205    return null;
    195206    }
    196207    return this.fileRefs.get(0).toString();
    197   }
    198 
    199   /**
    200    *  Get a child or descendant division
    201    *
    202    *  @param <code>String</code> the path to the child division
    203    *  @return <code>METSDivision</code> the division found.
    204    */
    205   public METSDivision getDivision(String divisionLabel)
    206   { // if the division ID equals this, then process it
    207     if (divisionLabel.equals(this.orderLabel)) {
    208       return this;
    209     }
    210 
    211     // if we just shouldn't be here, then error...
    212     if (!divisionLabel.startsWith(this.orderLabel + ".")) {
    213       return null;
    214     }
    215 
    216     // get the next division in the division path...
    217     String subdivisionPath = divisionLabel.substring(this.orderLabel.length() + 1);
    218     String nextDivision = getTopDivisionName(subdivisionPath);
    219     String nextLabel;
    220 
    221     // first try the current division name with an appended '.' and the next child id...
    222     METSDivision child = (METSDivision) this.children.get(this.orderLabel + "." + nextDivision);
    223     if (child == null)
    224     { // if that didn't work, try the next division on its own
    225       child = (METSDivision) this.children.get(nextDivision);
    226 
    227       // if neither worked, then abort...
    228       if (child == null) {
    229     return null;
    230       }
    231       else {
    232     // set the next identifier
    233     nextLabel = METSDivision.getSubDivisionName(divisionLabel);
    234       }
    235     }
    236     else {
    237       // set the next identifier...
    238       nextLabel = divisionLabel;
    239     }
    240 
    241     return child.getDivision(nextLabel);
    242   }
    243 
    244   /**
    245    *  A convenience method for splitting a division name - this gives the name
    246    *  without the leading division label...  To be used when division identifiers
    247    *  are given as '.' separated paths and division names do not contain the
    248    *  full path of their parents.
    249    *
    250    *  @return <code>String</code>
    251    */
    252   public static String getSubDivisionName(String divisionName)
    253   { int at = divisionName.indexOf('.');
    254 
    255     if (at >= 0)
    256     { return divisionName.substring(at+1);
    257     }
    258     else
    259     { return null;
    260     }
    261   }
    262 
    263   /**
    264    *  Get the parent division name.  Division identifiers
    265    *  concatenate the ancestor's names with intervening
    266    *  '.' characters.
    267    *
    268    *  @return <code>String</code> the division name
    269    */
    270   public static String getParentName(String divisionName)
    271   { int at = divisionName.lastIndexOf('.');
    272 
    273     if (at >= 0) {
    274       return divisionName.substring(0, at);
    275     }
    276     return "";
    277   }
    278 
    279   /**
    280    *  Get the top division name.
    281    *
    282    *  @return <code>String</code> the division name
    283    */
    284   public static String getTopDivisionName(String divisionName)
    285   { int at = divisionName.indexOf('.');
    286 
    287     if (at >= 0)
    288     { return divisionName.substring(0, at);
    289     }
    290     else
    291     { return divisionName;
    292     }
    293   }
    294 
    295       /*  Parse an XML Element as a METSDivision section
    296       *
    297       *  @param <code>Element</code> the XML element which represents
    298       *  the mets:div itself
    299       */
    300    
    301      public static METSDivision parseXML(Element element)
    302      {
     208    }
     209
     210    /**
     211     *  Get a child or descendant division
     212     *
     213     *  @param <code>String</code> the path to the child division
     214     *  @return <code>METSDivision</code> the division found.
     215     */
     216    public METSDivision getDivision(String divisionLabel)
     217    {
     218    // if the division ID equals this, then process it
     219    if (divisionLabel.equals(this.orderLabel)) {
     220        return this;
     221    }
     222
     223    // if we just shouldn't be here, then error...
     224    if (!divisionLabel.startsWith(this.orderLabel + ".")) {
     225        return null;
     226    }
     227
     228    // get the next division in the division path...
     229    String subdivisionPath = divisionLabel.substring(this.orderLabel.length() + 1);
     230    String nextDivision = getTopDivisionName(subdivisionPath);
     231    String nextLabel;
     232
     233    // first try the current division name with an appended '.' and the next child id...
     234    METSDivision child = (METSDivision) this.children.get(this.orderLabel + "." + nextDivision);
     235    if (child == null){
     236        // if that didn't work, try the next division on its own
     237        child = (METSDivision) this.children.get(nextDivision);
     238       
     239        // if neither worked, then abort...
     240        if (child == null) {
     241        return null;
     242        }
     243        else {
     244        // set the next identifier
     245        nextLabel = METSDivision.getSubDivisionName(divisionLabel);
     246        }
     247    }
     248    else {
     249        // set the next identifier...
     250        nextLabel = divisionLabel;
     251    }
     252   
     253    return child.getDivision(nextLabel);
     254    }
     255   
     256    /**
     257     *  A convenience method for splitting a division name - this gives the name
     258     *  without the leading division label...  To be used when division identifiers
     259     *  are given as '.' separated paths and division names do not contain the
     260     *  full path of their parents.
     261     *
     262     *  @return <code>String</code>
     263     */
     264    public static String getSubDivisionName(String divisionName)
     265    {
     266    int at = divisionName.indexOf('.');
     267
     268    if (at >= 0){
     269        return divisionName.substring(at+1);
     270    }
     271    else {
     272        return null;
     273    }
     274    }
     275
     276    /**
     277     *  Get the parent division name.  Division identifiers
     278     *  concatenate the ancestor's names with intervening
     279     *  '.' characters.
     280     *
     281     *  @return <code>String</code> the division name
     282     */
     283    public static String getParentName(String divisionName)
     284    {
     285    int at = divisionName.lastIndexOf('.');
     286
     287    if (at >= 0) {
     288        return divisionName.substring(0, at);
     289    }
     290    return "";
     291    }
     292   
     293    /**
     294     *  Get the top division name.
     295     *
     296     *  @return <code>String</code> the division name
     297     */
     298    public static String getTopDivisionName(String divisionName)
     299    {
     300    int at = divisionName.indexOf('.');
     301
     302    if (at >= 0){
     303        return divisionName.substring(0, at);
     304    }
     305    else {
     306        return divisionName;
     307    }
     308    }
     309
     310    /*  Parse an XML Element as a METSDivision section
     311     *
     312     *  @param <code>Element</code> the XML element which represents
     313     *  the mets:div itself
     314     */
     315   
     316    public static METSDivision parseXML(Element element)
     317    {
    303318    METSDivision thisDivision = null;
    304319 
     
    334349    }
    335350    return thisDivision;
    336      }
     351    }
    337352 
    338353 
    339  
    340      /**
    341       * Parse an XML element as a  METSFptr Section
    342       * @param <code>Element</code> the XML element which represents the mets:fptr itself
    343       */
    344      private static void fptr_parseXML(METSDivision thisDivision, Element element)
    345      {
     354    /**
     355     * Parse an XML element as a  METSFptr Section
     356     * @param <code>Element</code> the XML element which represents the mets:fptr itself
     357     */
     358    private static void fptr_parseXML(METSDivision thisDivision, Element element)
     359    {
    346360    String fileRef = element.getAttribute("FILEID");
    347361    thisDivision.addFileReference(fileRef);
    348      }
     362    }
    349363 
    350      /**
    351 
    352 
    353   /**
    354    *  Write the METS file in an XML to a text-output sink.
    355    *
    356    *  @param <code>PrintWriter</code> the destination of the output
    357    */
    358   public boolean write(PrintWriter writer)
    359   { // Get the main tag for the division
    360     String tag = XMLTools.getOpenTag("mets", DIVISION_TAG);
    361     tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
    362     tag = XMLTools.addAttribute(tag, "TYPE", this.type);
    363     tag = XMLTools.addAttribute(tag, "ORDER", this.order);
    364     if (this.orderLabel != null)
    365     { tag = XMLTools.addAttribute(tag, "ORDERLABEL", this.orderLabel);
    366     }
    367     if (this.userLabel != null)
    368     { tag = XMLTools.addAttribute(tag, "LABEL", this.userLabel);
    369     }
    370 
    371     // Add a metadata reference attribute
    372     if (this.metadataRefs.size() > 0)
    373     { String metaList = this.writeList(this.metadataRefs);
    374 
    375       tag = XMLTools.addAttribute(tag, "DMDID", metaList);
    376     }
    377 
    378     // write the central tag
    379     writer.println(tag);
    380 
    381     // write the list of file pointers for this structural element
    382     if (this.fileRefs.size() > 0)
    383     { String fileList = this.writeTagList("mets", "fptr", "FILEID", this.fileRefs);
    384 
    385       writer.println(fileList);
    386     }
    387 
    388     // write out any children in turn
    389     Iterator groups = this.children.values().iterator();
    390 
    391     while (groups.hasNext())
    392     { METSDivision group = (METSDivision) groups.next();
    393    
    394       if (!group.write(writer)) {
    395     return false;
    396       }
    397     }
    398    
    399     // close the div element
    400     writer.println(XMLTools.getCloseTag("mets", DIVISION_TAG));
    401 
    402     return true;
    403   }
    404 
    405   /**
    406    *  A convenience method for writing a <code>List</code> as a series of tags...
    407    */
    408   private String writeTagList(String namespace, String tagname,
    409                   String attribute, List list)
    410   { StringBuffer listString = new StringBuffer();
     364    /**
     365
     366
     367    /**
     368    *  Write the METS file in an XML to a text-output sink.
     369    *
     370    *  @param <code>PrintWriter</code> the destination of the output
     371    */
     372    public boolean write(PrintWriter writer)
     373    {
     374    // Get the main tag for the division
     375    String tag = XMLTools.getOpenTag("mets", DIVISION_TAG);
     376    tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
     377    tag = XMLTools.addAttribute(tag, "TYPE", this.type);
     378    tag = XMLTools.addAttribute(tag, "ORDER", this.order);
     379    if (this.orderLabel != null){
     380        tag = XMLTools.addAttribute(tag, "ORDERLABEL", this.orderLabel);
     381    }
     382    if (this.userLabel != null){
     383        tag = XMLTools.addAttribute(tag, "LABEL", this.userLabel);
     384    }
     385   
     386    // Add a metadata reference attribute
     387    if (this.metadataRefs.size() > 0){
     388        String metaList = this.writeList(this.metadataRefs);
     389        tag = XMLTools.addAttribute(tag, "DMDID", metaList);
     390    }
     391   
     392    // write the central tag
     393    writer.println(tag);
     394
     395    // write the list of file pointers for this structural element
     396    if (this.fileRefs.size() > 0) {
     397        String fileList = this.writeTagList("mets", "fptr", "FILEID", this.fileRefs);
     398        writer.println(fileList);
     399    }
     400   
     401    // write out any children in turn
     402    Iterator groups = this.children.values().iterator();
     403   
     404    while (groups.hasNext()){
     405        METSDivision group = (METSDivision) groups.next();
     406       
     407        if (!group.write(writer)) {
     408        return false;
     409        }
     410    }
     411   
     412    // close the div element
     413    writer.println(XMLTools.getCloseTag("mets", DIVISION_TAG));
     414
     415    return true;
     416    }
     417
     418    /**
     419     *  A convenience method for writing a <code>List</code> as a series of tags...
     420     */
     421    private String writeTagList(String namespace, String tagname,
     422                String attribute, List list)
     423    {
     424    StringBuffer listString = new StringBuffer();
     425   
     426    Iterator iterator = list.iterator();
     427    while (iterator.hasNext()){
     428        String item = iterator.next().toString();
     429       
     430        String tag = XMLTools.getOpenTag(namespace, tagname);
     431        tag = XMLTools.addAttribute(tag, attribute, item);
     432        tag = XMLTools.makeSingleton(tag);
     433       
     434        listString.append(tag);
     435    }
     436    return listString.toString();
     437    }
     438
     439    /**
     440     *  A convenience method for writing a <code>List</code> as a space-separated <code>String</code>.
     441     */
     442    private String writeList(List list)
     443    {
     444    StringBuffer listString = new StringBuffer();
     445   
     446    Iterator iterator = list.iterator();
     447    while (iterator.hasNext()){
     448        String item = iterator.next().toString();
     449       
     450        if (listString.length() > 0) {
     451        listString.append(" ");
     452        }
     453       
     454        listString.append(item);
     455    }
     456    return listString.toString();
     457    }
     458   
     459    public boolean writeSQL(DocumentID docId, int parentRef, boolean parentIsStructure,
     460                GS3SQLConnection connection)
     461    {
     462    int sqlRef = -1;
     463   
     464    // set the "where" to find the reference for this division
     465    GS3SQLSelect select = new GS3SQLSelect("divisions");
     466    select.addField("DivisionRef");
     467    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(parentRef), GS3SQLField.INTEGER_TYPE);
     468    GS3SQLWhereItem parentItem = new GS3SQLWhereItem("ParentType", "=", parentIsStructure ? METSStructure.STRUCTURE_TYPE : METSDivision.DIVISION_TYPE);
     469    GS3SQLWhereItem item = new GS3SQLWhereItem("SectionID", "=", this.ID.toString());
     470    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     471    where.add(parentItem);
     472    where.add(item);
     473    select.setWhere(where);
     474   
     475    connection.execute(select.toString());
     476   
     477    // Do the actual writing
     478    GS3SQLAction action;
     479   
     480    ResultSet resultSet = connection.getResultSet();
     481    try {
     482        if (resultSet == null ||
     483        !resultSet.first()) {
     484        resultSet = null;
     485       
     486        GS3SQLInsert insert = new GS3SQLInsert("divisions");
     487        insert.addValue("DocID", docId.toString());
     488        insert.addValue("SectionID", this.ID.toString());
     489        insert.addValue("ParentRef", Integer.toString(parentRef));
     490        insert.addValue("ParentType", parentIsStructure == true ? METSStructure.STRUCTURE_TYPE : METSDivision.DIVISION_TYPE);
     491       
     492        action = insert;
     493        }
     494        else {
     495        sqlRef = resultSet.getInt("DivisionRef");
     496       
     497        GS3SQLUpdate update = new GS3SQLUpdate("divisions");
     498        GS3SQLWhere  updateWhere =
     499            new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(sqlRef),
     500                            GS3SQLField.INTEGER_TYPE));
     501        update.setWhere(updateWhere);
     502        action = update;
     503        }
     504    }
     505    catch (SQLException sql) {
     506        System.err.println(sql + " " + select.toString());
     507        return false;
     508    }
     509   
     510    action.addValue("DivisionType", this.type.toString());
     511    action.addValue("LabelOrder", this.order);
     512    action.addValue("ShortLabel", this.orderLabel);
     513    action.addValue("UserLabel", this.userLabel);
     514   
     515    if (!connection.execute(action.toString())){
     516        return false;
     517    }
     518   
     519    // if doing a fresh item, get the new structure reference...
     520    if (resultSet == null){
     521        // get the new structure reference
     522        connection.execute(select.toString());
     523       
     524        // get the sql reference for the division
     525        try {
     526        // read in the structure reference
     527        resultSet = connection.getResultSet();
     528        if (resultSet == null) {
     529            return false;
     530        }
     531        resultSet.first();
     532        sqlRef = resultSet.getInt("DivisionRef");
     533        resultSet.close();
     534        resultSet = null;
     535        }
     536        catch (SQLException sqlex) {
     537        System.err.println("Unable to retrieve reference for Division " + sqlex);
     538        return false;
     539        }
     540    }
     541    // close the open resultSet, as we don't need it any longer...
     542    else {
     543        try {
     544        resultSet.close();
     545        }
     546        catch (SQLException sql) {
     547        System.err.println(sql + " " + select.toString());
     548        }
     549    }
     550   
     551    // delete the old file/metadata references
     552    GS3SQLWhere referenceWhere =
     553        new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(sqlRef),
     554                        GS3SQLField.INTEGER_TYPE));
     555   
     556    GS3SQLDelete delete = new GS3SQLDelete("divisionfilerefs");
     557    delete.setWhere(referenceWhere);
     558    connection.execute(delete.toString());
     559   
     560    delete = new GS3SQLDelete("divisionmetarefs");
     561    delete.setWhere(referenceWhere);
     562    connection.execute(delete.toString());
     563   
     564    // write the new file references
     565    if (this.fileRefs.size() > 0){
     566        Iterator iterator = this.fileRefs.iterator();
     567       
     568        while (iterator.hasNext()) {
     569        GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs");
     570        fileinsert.addValue("DocID", docId.toString());
     571        fileinsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
     572        fileinsert.addValue("DivisionType", "Group");
     573        fileinsert.addValue("FileID", iterator.next().toString());
     574        connection.execute(fileinsert.toString());
     575        }
     576    }
     577   
     578    // write the metadata references
     579    if (this.metadataRefs.size() > 0){
     580        Iterator iterator = this.metadataRefs.iterator();
     581       
     582        while (iterator.hasNext()) {
     583        GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs");
     584        metainsert.addValue("DocID", docId.toString());
     585        metainsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
     586        metainsert.addValue("DivisionType", "Group");
     587        metainsert.addValue("MetaID", iterator.next().toString());
     588        connection.execute(metainsert.toString());
     589        }
     590    }
     591   
     592    // write out any children in turn
     593    Iterator groups = this.children.values().iterator();
     594   
     595    while (groups.hasNext()){
     596        METSDivision group = (METSDivision) groups.next();
     597       
     598        if (!group.writeSQL(docId, sqlRef, false, connection)) {
     599        return false;
     600        }
     601    }
     602    return true;
     603    }
     604   
     605    public static METSDivision readSQL(GS3SQLConnection connection, ResultSet resultSet)
     606    {
     607    METSLocation metsLocation = null;
     608   
     609    GS3SQLSelect select = null;
     610    try {
     611        // read the basic information
     612        String ID   = resultSet.getString("SectionID");
     613        String type = resultSet.getString("DivisionType");
     614        String order = resultSet.getString("LabelOrder");
     615        String orderLabel = resultSet.getString("ShortLabel");
     616        String userLabel  = resultSet.getString("UserLabel");
    411617     
    412     Iterator iterator = list.iterator();
    413     while (iterator.hasNext())
    414     { String item = iterator.next().toString();
    415 
    416       String tag = XMLTools.getOpenTag(namespace, tagname);
    417       tag = XMLTools.addAttribute(tag, attribute, item);
    418       tag = XMLTools.makeSingleton(tag);
    419 
    420       listString.append(tag);
    421     }
    422     return listString.toString();
    423   }
    424 
    425   /**
    426    *  A convenience method for writing a <code>List</code> as a space-separated <code>String</code>.
    427    */
    428   private String writeList(List list)
    429   { StringBuffer listString = new StringBuffer();
     618        // construct the division object
     619        METSDivision division = new METSDivision(ID, order, orderLabel, userLabel, type);
    430620     
    431     Iterator iterator = list.iterator();
    432     while (iterator.hasNext())
    433     { String item = iterator.next().toString();
    434 
    435       if (listString.length() > 0) {
    436     listString.append(" ");
    437       }
    438 
    439       listString.append(item);
    440     }
    441     return listString.toString();
    442   }
    443 
    444   public boolean writeSQL(DocumentID docId, int parentRef, boolean parentIsStructure,
    445               GS3SQLConnection connection)
    446   { int sqlRef = -1;
    447 
    448     // set the "where" to find the reference for this division
    449     GS3SQLSelect select = new GS3SQLSelect("divisions");
    450     select.addField("DivisionRef");
    451     GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(parentRef), GS3SQLField.INTEGER_TYPE);
    452     GS3SQLWhereItem parentItem = new GS3SQLWhereItem("ParentType", "=", parentIsStructure ? METSStructure.STRUCTURE_TYPE : METSDivision.DIVISION_TYPE);
    453     GS3SQLWhereItem item = new GS3SQLWhereItem("SectionID", "=", this.ID.toString());
    454     GS3SQLWhere where = new GS3SQLWhere(whereItem);
    455     where.add(parentItem);
    456     where.add(item);
    457     select.setWhere(where);
    458 
    459     connection.execute(select.toString());
    460 
    461     // Do the actual writing
    462     GS3SQLAction action;
    463 
    464     ResultSet resultSet = connection.getResultSet();
    465     try {
    466       if (resultSet == null ||
    467       !resultSet.first())
    468       { resultSet = null;
    469 
    470         GS3SQLInsert insert = new GS3SQLInsert("divisions");
    471     insert.addValue("DocID", docId.toString());
    472     insert.addValue("SectionID", this.ID.toString());
    473     insert.addValue("ParentRef", Integer.toString(parentRef));
    474     insert.addValue("ParentType", parentIsStructure == true ? METSStructure.STRUCTURE_TYPE : METSDivision.DIVISION_TYPE);
    475    
    476     action = insert;
    477       }
    478       else {
    479     sqlRef = resultSet.getInt("DivisionRef");
    480    
    481     GS3SQLUpdate update = new GS3SQLUpdate("divisions");
    482     GS3SQLWhere  updateWhere =
    483       new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(sqlRef),
    484                           GS3SQLField.INTEGER_TYPE));
    485     update.setWhere(updateWhere);
    486     action = update;
    487       }
    488     }
    489     catch (SQLException sql) {
    490       System.err.println(sql + " " + select.toString());
    491       return false;
    492     }
     621        // obtain a list of child divisions
     622        int divisionRef = resultSet.getInt("DivisionRef");
     623
     624        select = new GS3SQLSelect("divisions");
     625        select.addField("*");
     626        GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(divisionRef),
     627                                GS3SQLField.INTEGER_TYPE);
     628        GS3SQLWhere where = new GS3SQLWhere(whereItem);
     629        whereItem = new GS3SQLWhereItem("ParentType", "=", METSDivision.DIVISION_TYPE);
     630        where.add(whereItem);
     631        select.setWhere(where);
     632
     633        connection.execute(select.toString());
    493634     
    494     action.addValue("DivisionType", this.type.toString());
    495     action.addValue("LabelOrder", this.order);
    496     action.addValue("ShortLabel", this.orderLabel);
    497     action.addValue("UserLabel", this.userLabel);
    498 
    499     if (!connection.execute(action.toString()))
    500     { return false;
    501     }
    502 
    503     // if doing a fresh item, get the new structure reference...
    504     if (resultSet == null)
    505     { // get the new structure reference
    506       connection.execute(select.toString());
    507 
    508       // get the sql reference for the division
    509       try {
    510     // read in the structure reference
    511     resultSet = connection.getResultSet();
    512     if (resultSet == null) {
    513       return false;
    514     }
    515     resultSet.first();
    516     sqlRef = resultSet.getInt("DivisionRef");
    517     resultSet.close();
    518     resultSet = null;
    519       }
    520       catch (SQLException sqlex) {
    521     System.err.println("Unable to retrieve reference for Division " + sqlex);
    522     return false;
    523       }
    524     }
    525     // close the open resultSet, as we don't need it any longer...
    526     else {
    527       try {
    528     resultSet.close();
    529       }
    530       catch (SQLException sql) {
    531     System.err.println(sql + " " + select.toString());
    532       }
    533     }
    534 
    535     // delete the old file/metadata references
    536     GS3SQLWhere referenceWhere =
    537       new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(sqlRef),
    538                       GS3SQLField.INTEGER_TYPE));
    539 
    540     GS3SQLDelete delete = new GS3SQLDelete("divisionfilerefs");
    541     delete.setWhere(referenceWhere);
    542     connection.execute(delete.toString());
    543 
    544     delete = new GS3SQLDelete("divisionmetarefs");
    545     delete.setWhere(referenceWhere);
    546     connection.execute(delete.toString());
    547 
    548     // write the new file references
    549     if (this.fileRefs.size() > 0)
    550     { Iterator iterator = this.fileRefs.iterator();
    551    
    552       while (iterator.hasNext())
    553       { GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs");
    554         fileinsert.addValue("DocID", docId.toString());
    555         fileinsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
    556     fileinsert.addValue("DivisionType", "Group");
    557     fileinsert.addValue("FileID", iterator.next().toString());
    558     connection.execute(fileinsert.toString());
    559       }
    560     }
    561 
    562     // write the metadata references
    563     if (this.metadataRefs.size() > 0)
    564     { Iterator iterator = this.metadataRefs.iterator();
    565    
    566       while (iterator.hasNext())
    567       { GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs");
    568         metainsert.addValue("DocID", docId.toString());
    569         metainsert.addValue("DivisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
    570     metainsert.addValue("DivisionType", "Group");
    571     metainsert.addValue("MetaID", iterator.next().toString());
    572     connection.execute(metainsert.toString());
    573       }
    574     }
    575 
    576     // write out any children in turn
    577     Iterator groups = this.children.values().iterator();
    578 
    579     while (groups.hasNext())
    580     { METSDivision group = (METSDivision) groups.next();
    581    
    582       if (!group.writeSQL(docId, sqlRef, false, connection)) {
    583     return false;
    584       }
    585     }
    586     return true;
    587   }
    588 
    589   public static METSDivision readSQL(GS3SQLConnection connection, ResultSet resultSet)
    590   {
    591     METSLocation metsLocation = null;
    592    
    593     GS3SQLSelect select = null;
    594     try {
    595       // read the basic information
    596       String ID   = resultSet.getString("SectionID");
    597       String type = resultSet.getString("DivisionType");
    598       String order = resultSet.getString("LabelOrder");
    599       String orderLabel = resultSet.getString("ShortLabel");
    600       String userLabel  = resultSet.getString("UserLabel");
    601      
    602       // construct the division object
    603       METSDivision division = new METSDivision(ID, order, orderLabel, userLabel, type);
    604      
    605       // obtain a list of child divisions
    606       int divisionRef = resultSet.getInt("DivisionRef");
    607 
    608       select = new GS3SQLSelect("divisions");
    609       select.addField("*");
    610       GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(divisionRef),
    611                               GS3SQLField.INTEGER_TYPE);
    612       GS3SQLWhere where = new GS3SQLWhere(whereItem);
    613       whereItem = new GS3SQLWhereItem("ParentType", "=", METSDivision.DIVISION_TYPE);
    614       where.add(whereItem);
    615       select.setWhere(where);
    616 
    617       connection.execute(select.toString());
    618      
    619       // circulate through to obtain further children
    620       ResultSet childSet = connection.getResultSet();
    621       if (childSet.first())
    622       {
    623     do {
    624       METSDivision childDivision = METSDivision.readSQL(connection, childSet);
    625       division.addDivision(childDivision);
    626     }
    627     while (childSet.next());
    628       }
    629 
    630       select = new GS3SQLSelect("divisionfilerefs");
    631       select.addField("*");
    632       where = new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(divisionRef),
    633                           GS3SQLField.INTEGER_TYPE));
    634       select.setWhere(where);
    635 
    636       connection.execute(select.toString());
    637 
    638       ResultSet fileSet = connection.getResultSet();
    639       if (fileSet != null && fileSet.first())
    640       { do
    641     { String reference = fileSet.getString("FileID");
    642       division.fileRefs.add(reference);
    643     }
    644     while (fileSet.next()); 
    645       }
    646 
    647       select = new GS3SQLSelect("divisionmetarefs");
    648       select.addField("*");
    649       where = new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(divisionRef),
    650                           GS3SQLField.INTEGER_TYPE));
    651       select.setWhere(where);
    652 
    653       connection.execute(select.toString());
    654 
    655       ResultSet metaSet = connection.getResultSet();
    656       if (metaSet != null && metaSet.first())
    657       { do
    658     { String reference = metaSet.getString("MetaID");
    659       division.metadataRefs.add(reference);
    660     }
    661     while (metaSet.next()); 
    662       }
     635        // circulate through to obtain further children
     636        ResultSet childSet = connection.getResultSet();
     637        if (childSet.first())
     638        {
     639            do {
     640            METSDivision childDivision = METSDivision.readSQL(connection, childSet);
     641            division.addDivision(childDivision);
     642            }
     643            while (childSet.next());
     644        }
     645
     646        select = new GS3SQLSelect("divisionfilerefs");
     647        select.addField("*");
     648        where = new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(divisionRef),
     649                            GS3SQLField.INTEGER_TYPE));
     650        select.setWhere(where);
     651
     652        connection.execute(select.toString());
     653
     654        ResultSet fileSet = connection.getResultSet();
     655        if (fileSet != null && fileSet.first()){
     656        do {
     657            String reference = fileSet.getString("FileID");
     658            division.fileRefs.add(reference);
     659        }
     660        while (fileSet.next()); 
     661        }
     662
     663        select = new GS3SQLSelect("divisionmetarefs");
     664        select.addField("*");
     665        where = new GS3SQLWhere(new GS3SQLWhereItem("DivisionRef", "=", Integer.toString(divisionRef),
     666                            GS3SQLField.INTEGER_TYPE));
     667        select.setWhere(where);
     668
     669        connection.execute(select.toString());
     670
     671        ResultSet metaSet = connection.getResultSet();
     672        if (metaSet != null && metaSet.first()){
     673        do {
     674            String reference = metaSet.getString("MetaID");
     675            division.metadataRefs.add(reference);
     676        }
     677        while (metaSet.next()); 
     678        }
    663679       
    664       return division;
    665     }
    666     catch (SQLException sqlEx)
    667     { System.out.println(sqlEx + " " +select.toString());
    668     }   
    669     return null;
    670   }
     680        return division;
     681    }
     682    catch (SQLException sqlEx){
     683        System.out.println(sqlEx + " " +select.toString());
     684    }   
     685    return null;
     686    }
    671687}
     688
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFile.java

    r8461 r8703  
    3232public class METSFile
    3333{
    34   METSFileID    id;
    35   METSFilePos   location;
    36   String        MIMEType;
    37   int           sequenceNo;
    38   long          size;
    39   METSDate      created;
    40   long          checkSum;
    41   List          adminRefs;
    42   List          describeRefs;
    43   METSFileGroup group;
    44 
    45   /**
    46    *  Create a new METSFile object, with the given properties.  The remaining
    47    *  fields are filled in with default values.
    48    *
    49    *  @param <code>String</code> the id of the file
    50    *  @param <code>METSFilePos</code> the location of the file
    51    *  @param <code>String</code> the MIME type of the file
    52    */
    53   public METSFile(METSFileID id, METSFilePos location, String mimeType)
    54   { this.id           = id;        // required
    55     this.location     = location;  // the location
    56     this.MIMEType     = mimeType;  // the MIME type;
    57     this.sequenceNo   = -1;       // -1 indicates unset
    58     this.size         = -1;        // -1 indicates unset
    59     this.checkSum     = -1;        // -1 indicates unset
    60     this.created      = null;
    61     this.adminRefs    = new ArrayList();
    62     this.describeRefs = new ArrayList();
    63     this.group        = null;
    64   }
    65 
    66   public boolean equals(URL url)
    67   { return this.location.equals(url);
    68   }
    69 
    70   /**
    71    *  Set the parent group
    72    *
    73    *  @param <code>METSFileGroup</code> the new holding file group
    74    */
    75   public void setGroup(METSFileGroup group)
    76   { this.group = group;
    77   }
    78 
    79   /**
    80    *  Get the parent group of this file
    81    *
    82    *  @return <code>METSFileGroup</code> the holding file group
    83    */
    84   public METSFileGroup getGroup()
    85   { return this.group;
    86   }
    87 
    88   /**
    89    *  Get the identifier of this file
    90    *
    91    *  @return <code>METSFileID</code> the file identifier
    92    */
    93   public METSFileID getID()
    94   { return this.id;
    95   }
    96  
    97   /**
    98    *  Set the identifier of this file
    99    *
    100    *  @param <code>METSFileID</code> the file identifier
    101    */
    102   public void setID(METSFileID id)
    103   { this.id = id;
    104   }
    105 
    106   /**
    107    *  @return <code>String</code> the MIME (content) type of the file
    108    */
    109   public String getMIMEType()
    110   { return this.MIMEType;
    111   }
    112 
    113   /**
    114    *  @return <code>URL</code> the location of the file.  This will often be in
    115    *          "file://" form.
    116    */
    117   public URL getLocation()
    118   { return this.location.getLocation();
    119   }
    120 
    121   /**
    122    *  Take a url and make a METSFile object out of it - works out
    123    *  details such as the Mime type, identifiers and other necessary
    124    *  information to call the constructor for METSFile
    125    *
    126    *  @param <code>URL</code> the url of the object - this may be a
    127    *                          reference to a file on the local filestore
    128    *
    129    *  @return <code>METSFile</code> the METS file object for the corresponding
    130    *                                document component.
    131    */
    132   public static METSFile makeMETSFile(URL url, String mimeType)
    133   {
    134     METSFile reply = new METSFile(new METSFileID(), new METSFilePos(url), mimeType);
    135     return reply;
    136   }
     34    METSFileID    id;
     35    METSFilePos   location;
     36    String        MIMEType;
     37    int           sequenceNo;
     38    long          size;
     39    METSDate      created;
     40    long          checkSum;
     41    List          adminRefs;
     42    List          describeRefs;
     43    METSFileGroup group;
     44   
     45    /**
     46     *  Create a new METSFile object, with the given properties.  The remaining
     47     *  fields are filled in with default values.
     48     *
     49     *  @param <code>String</code> the id of the file
     50     *  @param <code>METSFilePos</code> the location of the file
     51     *  @param <code>String</code> the MIME type of the file
     52     */
     53    public METSFile(METSFileID id,  METSFilePos location, String mimeType)
     54    {
     55    this.id           = id;        // required
     56    this.location     = location;  // the location
     57    this.MIMEType     = mimeType;  // the MIME type;
     58    this.sequenceNo   = -1;       // -1 indicates unset
     59    this.size         = -1;        // -1 indicates unset
     60    this.checkSum     = -1;        // -1 indicates unset
     61    this.created      = null;
     62    this.adminRefs    = new ArrayList();
     63    this.describeRefs = new ArrayList();
     64    this.group        = null;
     65    }
     66   
     67    public boolean equals(URL url)
     68    {
     69    return this.location.equals(url);
     70    }
     71   
     72    /**
     73     *  Set the parent group
     74     *
     75     *  @param <code>METSFileGroup</code> the new holding file group
     76     */
     77    public void setGroup(METSFileGroup group)
     78    {
     79    this.group = group;
     80    }
     81   
     82    /**
     83     *  Get the parent group of this file
     84     *
     85     *  @return <code>METSFileGroup</code> the holding file group
     86     */
     87    public METSFileGroup getGroup()
     88    {
     89    return this.group;
     90    }
     91   
     92    /**
     93     *  Get the identifier of this file
     94     *
     95     *  @return <code>METSFileID</code> the file identifier
     96     */
     97    public METSFileID getID()
     98    {
     99    return this.id;
     100    }
     101   
     102    /**
     103     *  Set the identifier of this file
     104     *
     105     *  @param <code>METSFileID</code> the file identifier
     106     */
     107    public void setID(METSFileID id)
     108    {
     109    this.id = id;
     110    }
     111   
     112    /**
     113     *  @return <code>String</code> the MIME (content) type of the file
     114     */
     115    public String getMIMEType()
     116    {
     117    return this.MIMEType;
     118    }
     119   
     120    /**
     121     *  @return <code>URL</code> the location of the file.  This will often be in
     122     *          "file://" form.
     123     */
     124    public URL getLocation()
     125    {
     126    return this.location.getLocation();
     127    }
     128   
     129    /**
     130     *  Take a url and make a METSFile object out of it - works out
     131     *  details such as the Mime type, identifiers and other necessary
     132     *  information to call the constructor for METSFile
     133     *
     134     *  @param <code>URL</code> the url of the object - this may be a
     135     *                          reference to a file on the local filestore
     136     *
     137     *  @return <code>METSFile</code> the METS file object for the corresponding
     138     *                                document component.
     139     */
     140    public static METSFile makeMETSFile(URL url, String mimeType)
     141    {   
     142    METSFile reply = new METSFile(new METSFileID(), new METSFilePos(url), mimeType);
     143    return reply;
     144    }
     145   
     146    public static METSFile makeMETSFile(URL url)
     147    {
     148    String mimeType;
     149   
     150    if (url.getProtocol().equals("file")){
     151        // TODO: Work out the MIME type
     152        mimeType = URLConnection.getFileNameMap().getContentTypeFor(url.toString().substring(7));
     153        if (mimeType == null) {
     154        mimeType = "text/plain";
     155        }
     156    } else {
     157        // TODO: look up the MIME type through the pertinent connection
     158        mimeType = HTTPTools.getMIMEType(url);
     159    }
     160    // TODO: make the identifier...
     161   
     162    return makeMETSFile(url, mimeType);
     163    }
     164   
     165    /**
     166     *  Write the METS file in an XML to a text-output sink
     167     *
     168     *  @param <code>PrintWriter</code> the destination of the output
     169     */
     170    public void write(PrintWriter writer)
     171    {
     172    String tag = XMLTools.getOpenTag("mets", "file");
     173    tag = XMLTools.addAttribute(tag, "MIMETYPE", this.MIMEType);
     174    tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
     175    writer.println(tag);
     176   
     177    tag = XMLTools.getOpenTag("mets", "FLocat");
     178    tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
     179    tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
     180   
     181    tag = XMLTools.makeSingleton(tag);
     182    writer.println(tag);
     183   
     184    writer.println(XMLTools.getCloseTag("mets", "file"));
     185    }
     186   
     187   
     188    public boolean writeSQL(int groupReference, GS3SQLConnection connection)
     189    {
     190    // check if this file is in the table already...
     191    GS3SQLAction action;
     192   
     193    GS3SQLSelect select = new GS3SQLSelect("files");
     194    select.addField("*");
     195    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupReference),
     196                            GS3SQLField.INTEGER_TYPE);
     197    GS3SQLWhere  where  = new GS3SQLWhere(whereItem);
     198    whereItem = new GS3SQLWhereItem("FileID","=", this.id.toString());
     199    where.add(whereItem);
     200    select.setWhere(where);
     201    connection.execute(select.toString());
     202   
     203    // if not, then make an insert action
     204    try {
     205        ResultSet results = connection.getResultSet();
     206        if (results == null ||
     207        !results.first()){
     208        GS3SQLInsert insert = new GS3SQLInsert("files");
    137209       
    138   public static METSFile makeMETSFile(URL url)
    139   { String mimeType;
    140  
    141     if (url.getProtocol().equals("file"))
    142     { // TODO: Work out the MIME type
    143       mimeType = URLConnection.getFileNameMap().getContentTypeFor(url.toString().substring(7));
    144       if (mimeType == null)
    145       { mimeType = "text/plain";
    146       }
    147     }
    148     else
    149     { // TODO: look up the MIME type through the pertinent connection
    150       mimeType = HTTPTools.getMIMEType(url);
    151     }
    152     // TODO: make the identifier...
    153    
    154     return makeMETSFile(url, mimeType);
    155   }
    156 
    157   /**
    158    *  Write the METS file in an XML to a text-output sink
    159    *
    160    *  @param <code>PrintWriter</code> the destination of the output
    161    */
    162   public void write(PrintWriter writer)
    163   { String tag = XMLTools.getOpenTag("mets", "file");
    164     tag = XMLTools.addAttribute(tag, "MIMETYPE", this.MIMEType);
    165     tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
    166     writer.println(tag);
    167    
    168     tag = XMLTools.getOpenTag("mets", "FLocat");
    169     tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
    170     tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
    171     tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
    172     tag = XMLTools.makeSingleton(tag);
    173     writer.println(tag);
    174    
    175     writer.println(XMLTools.getCloseTag("mets", "file"));
    176   }
    177 
    178    
    179   public boolean writeSQL(int groupReference, GS3SQLConnection connection)
    180   { // check if this file is in the table already...
    181     GS3SQLAction action;
    182 
    183     GS3SQLSelect select = new GS3SQLSelect("files");
    184     select.addField("*");
    185     GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupReference),
    186                             GS3SQLField.INTEGER_TYPE);
    187     GS3SQLWhere  where  = new GS3SQLWhere(whereItem);
    188     whereItem = new GS3SQLWhereItem("FileID","=", this.id.toString());
    189     where.add(whereItem);
    190     select.setWhere(where);
    191     connection.execute(select.toString());
    192 
    193     // if not, then make an insert action
    194     try {
    195       ResultSet results = connection.getResultSet();
    196       if (results == null ||
    197       !results.first())
    198       { GS3SQLInsert insert = new GS3SQLInsert("files");
    199 
    200         insert.addValue("FileGroupRef", Integer.toString(groupReference), GS3SQLField.INTEGER_TYPE);
    201     insert.addValue("FileID", this.id.toString());
    202    
    203     action = insert;
    204    
    205       }
    206       else {
    207     GS3SQLUpdate update = new GS3SQLUpdate("files");
    208 
    209     update.setWhere(where);
    210     action = update;
    211       }
    212     }
    213     catch (SQLException ex)
    214     { System.err.println(ex);
    215       return false;
    216     }
    217     action.addValue("FileLocType", this.location.getType());
    218     action.addValue("FileLocation", this.location.getLocation().toString());
    219     action.addValue("MIMEType", this.MIMEType);
    220 
    221     return connection.execute(action.toString());
    222   }
    223 
    224   public static METSFile readSQL(GS3SQLConnection connection, ResultSet parentSet)
    225   {
    226     try {
    227       String locType = parentSet.getString("FileLocType");
    228       String location = parentSet.getString("FileLocation");
    229       String mimeType = parentSet.getString("MIMEType");
    230       String id = parentSet.getString("FileID");
    231 
    232       METSFileID metsID = new METSFileID(id);
    233       METSFilePos metsFilePos = new METSFilePos(locType, location);
    234      
    235       METSFile reply = new METSFile(metsID, metsFilePos, mimeType);
    236       return reply;
    237     }
    238     catch (SQLException ex)
    239     { System.out.println(ex);
    240     }
    241     catch (java.net.MalformedURLException urlEx)
    242     { System.out.println(urlEx);
    243     }
    244     return null;
    245   }
    246    
    247   /**
    248    *  Parse an XML Element as a METS File
    249    */
    250     /*
    251   public static METSFile parseXML(Element element, METSFileGroup parentGroup)
    252   { METSFile file = null;
    253     NodeList children = element.getChildNodes();
     210        insert.addValue("FileGroupRef", Integer.toString(groupReference), GS3SQLField.INTEGER_TYPE);
     211        insert.addValue("FileID", this.id.toString());
    254212       
    255     for (int c = 0; c < children.getLength(); c ++)
    256     { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
    257         continue;
    258       }
    259    
    260       Element childElement = (Element) children.item(c);
    261       if (childElement.getNodeName().equals("mets:FLocat"))
    262       { METSFilePos filePos;
    263    
    264         // get most of the information from the child FLocat node
    265         String locationType = childElement.getAttribute("LOCTYPE");
    266     String href = childElement.getAttribute("xlink:href");
    267     String id   = childElement.getAttribute("ID");
    268      
    269     // some more data from the parent node
    270     String mimeType = element.getAttribute("MIMETYPE");
    271 
    272     try
    273     { filePos = new METSFilePos(href, locationType);
    274     }
    275     catch (java.net.MalformedURLException ex)
    276     { // TODO: raise error
    277       continue;
    278     }
    279     file = new METSFile(new METSFileID(id), filePos, mimeType);
    280       }
    281       else if (childElement.getNodeName().equals("mets:FContent"))
    282       {
    283       }
    284       else
    285       { // TODO: raise an error!
    286       }
    287     }
    288     return file;
    289   }
    290     */
    291 
    292 
     213        action = insert;
     214        }
     215        else {
     216        GS3SQLUpdate update = new GS3SQLUpdate("files");
     217       
     218        update.setWhere(where);
     219        action = update;
     220        }
     221    }
     222    catch (SQLException ex){
     223        System.err.println(ex);
     224        return false;
     225    }
     226    action.addValue("FileLocType", this.location.getType());
     227    action.addValue("FileLocation", this.location.getLocation().toString());
     228    action.addValue("MIMEType", this.MIMEType);
     229   
     230    return connection.execute(action.toString());
     231    }
     232   
     233    public static METSFile readSQL(GS3SQLConnection connection, ResultSet parentSet)
     234    {
     235    try {
     236        String locType = parentSet.getString("FileLocType");
     237        String location = parentSet.getString("FileLocation");
     238        String mimeType = parentSet.getString("MIMEType");
     239        String id = parentSet.getString("FileID");
     240   
     241        METSFileID metsID = new METSFileID(id);
     242        METSFilePos metsFilePos = new METSFilePos(locType, location);
     243       
     244        METSFile reply = new METSFile(metsID, metsFilePos, mimeType);
     245        return reply;
     246    }
     247    catch (SQLException ex){
     248        System.out.println(ex);
     249    }
     250    catch (java.net.MalformedURLException urlEx){
     251        System.out.println(urlEx);
     252    }
     253    return null;
     254    }
     255   
    293256    public static METSFile parse_file(Element element, METSFileGroup group, String parseFilePath)
    294257    {
     
    296259
    297260    METSFile thisFile = null;
    298 
     261   
    299262    for (int c = 0; c < children.getLength(); c++){
    300263        if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE){
     
    303266        Element child = (Element) children.item(c);
    304267        String childName = child.getNodeName();
    305 
     268       
    306269        if (childName.equals("mets:file")){
    307270        METSFile.parse_file(child, group, parseFilePath);
     
    310273        String fileId = element.getAttribute("ID");   
    311274       
    312         METSFilePos thisFilePos = METSFile.parse_flocateXML(child, parseFilePath);
    313         thisFile = new METSFile(new METSFileID(fileId), thisFilePos, mimeType);
     275        METSFilePos thisFilePos = METSFile.parse_flocateXML(child, parseFilePath, fileId);
     276        thisFile = new METSFile(new METSFileID(fileId), thisFilePos, mimeType);   
    314277        group.addFile(thisFile);
    315278        } else {
     
    320283    }
    321284   
    322     public static METSFilePos parse_flocateXML(Element element, String parseFilePath)
     285    public static METSFilePos parse_flocateXML(Element element, String parseFilePath, String fileId)
    323286    {
    324287    METSFilePos thisFilePos = null;
     
    327290    String locationType = element.getAttribute("LOCTYPE");
    328291    String href = element.getAttribute("xlink:href");
    329     String fileId = element.getAttribute("ID");
    330    
     292    //String fileId = element.getAttribute("ID");
    331293    try {
    332294        thisFilePos = new METSFilePos(locationType, href);
    333        
     295        
    334296        if (fileId.startsWith("default.")) {
    335297        //retrieve the string after ..import/
     
    348310    }
    349311    return thisFilePos;
    350 //  file = new METSFile(new METSFileID(id), filePos, mimeType);
    351 //       }
    352 //       else if (childElement.getNodeName().equals("mets:FContent"))
    353 //       {
    354 //       }
    355 //       else
    356 //       { // TODO: raise an error!
    357 //       }
    358 //     }
    359 //     return file;
    360 // }
    361    
    362     }
    363 
    364 
    365   /**
    366    *  Get the location of this file as a URL
    367    */
    368   public URL getURL()
    369   { return this.location.getLocation();
    370   }
    371 
    372   /**
    373    *  Get modified file date
    374    */
    375   public long getModifiedDatestamp()
    376   { return this.location.getModifiedDatestamp();
    377   }
    378 
    379   /**
    380    *  Overridden <code>toString</code> for convenience - returns the location of the file.
    381    *
    382    *  @return <code>String</code> the location of the file as a string
    383    */
    384   public String toString()
    385   { return this.location.toString();
    386   }
     312    }
     313   
     314   
     315    /**
     316     *  Get the location of this file as a URL
     317     */
     318    public URL getURL()
     319    {
     320    return this.location.getLocation();
     321    }
     322   
     323    /**
     324     *  Get modified file date
     325     */
     326    public long getModifiedDatestamp()
     327    {
     328    return this.location.getModifiedDatestamp();
     329    }
     330   
     331    /**
     332     *  Overridden <code>toString</code> for convenience - returns the location of the file.
     333     *
     334     *  @return <code>String</code> the location of the file as a string
     335     */
     336    public String toString()
     337    {
     338    return this.location.toString();
     339    }
    387340}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileGroup.java

    r8461 r8703  
    2828
    2929public class METSFileGroup
    30 { List   children;
    31   List   childGroups;
    32   String id;
    33 
    34   public static final String SECTION_PARENT = "Section";
    35   public static final String GROUP_PARENT = "Group";
    36 
    37   public METSFileGroup(String id)
    38   { this.children = new ArrayList();
    39     this.childGroups = new ArrayList();
    40     this.id = id;
    41   }
    42 
    43   /**
    44    *  Get the modified datestamp that applies for this file group.  An empty group
    45    *  will return <code>0</code>.
    46    *
    47    *  @return <code>long</code> the date modified as milliseconds from the 1st January 1970
    48    */
    49   public long getModifiedDatestamp()
    50   { long reply = 0; // empty groups reply '0'
    51 
    52     Iterator childIter = children.iterator();
    53     while (childIter.hasNext())
    54     { METSFile file = (METSFile) childIter.next();
    55       long fileStamp = file.getModifiedDatestamp();
    56       if (fileStamp > reply) {
    57     reply = fileStamp;
    58       }
    59     }
    60 
    61     childIter = childGroups.iterator();
    62     while (childIter.hasNext())
    63     { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
    64       long fileStamp = fileGroup.getModifiedDatestamp();
    65       if (fileStamp > reply) {
    66     reply = fileStamp;
    67       }
    68     }
    69 
    70     return reply;
    71   }
    72    
    73   /**
    74    *  Find all the occurrences of a given file, and place them
    75    *  in a <code>List</code>.
    76    */
    77   public void findGroups(URL findFile, List resultList)
    78   {
    79     // find in this particular group
    80     Iterator childIter = children.iterator();
    81     while (childIter.hasNext())
    82     { METSFile file = (METSFile) childIter.next();
    83       if (file.equals(findFile))
    84       { resultList.add(this.id);
    85       }
    86     }
    87 
    88     // iterate over the child groups
    89     childIter = childGroups.iterator();
    90     while (childIter.hasNext())
    91     { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
    92 
    93       fileGroup.findGroups(findFile, resultList);
    94     }
    95   }
    96 
    97   /**
    98    *  Get a sub group of this METSFileGroup.
    99    *
    100    *  @param <code>String</code> the name of the subgroup
    101    *  @return <code>METSFileGroup</code> the child group, which will
    102    *          be <code>null</code> if the group is not known.
    103    */
    104   public METSFileGroup getSubgroup(String id)
    105   { String childId;
    106  
    107     int dotAt = id.indexOf(':');
    108     if (dotAt == -1)
    109     { childId = null;
    110     }
    111     else
    112     { childId = id.substring(dotAt+1);
    113       id = id.substring(0, dotAt);
    114     }
    115    
    116     // iterate over the child groups
    117     Iterator childIter = childGroups.iterator();
    118     while (childIter.hasNext())
    119     { METSFileGroup group = (METSFileGroup) childIter.next();
    120    
    121       if (group.id.equals(id)) {
    122     if (childId == null) {
    123       return group;
    124     }
    125     else {
    126       return group.getSubgroup(childId);
    127     }
    128       }
    129     }
    130     return null;
    131   }
    132 
    133   /**
    134    *  Add a filegroup to this group of files.
    135    *
    136    *    @param <code>METSFileGroup</code> the filegroup.
    137    */
    138   public void addGroup(METSFileGroup group)
    139   { this.childGroups.add(group);
    140   }
    141 
    142   /**
    143    *  Add a file to this group of files.
    144    *
    145    *    @param <code>METSFile</code> the file in a METS File object.
    146    */
    147   public void addFile(METSFile file)
    148   { this.children.add(file);
    149     file.setGroup(this);
    150     if (!file.getID().isDefined())
    151     { file.setID(new METSFileID(this.id + "." + Integer.toString(this.children.size())));
    152     }
    153   }
    154 
    155   /**
    156    *  Get the nth child of the group
    157    *
    158    *  @param <code>int</code> the index into the group
    159    *  @return <code>METSFile</code> the corresponding child.  This may be <code>null</code>
    160    *          particularly if the index given falls outside of the valid range of child
    161    *          indexes.
    162    */
    163   public METSFile getFile(int index)
    164   { if (index < 0 || index >= this.children.size())
    165     { return null;
    166     }
    167  
    168     return (METSFile) this.children.get(index);
    169   }
    170 
    171   /**
    172    *  Get all the files in the group as a <code>List</code>
    173    *
    174    *  @return <code>List</code> the file childen of the group.
    175    */
    176   public List getFiles()
    177   { return this.children;
    178   }
    179 
    180   /**
    181    *  Get all the subgroups as a <code>List</code>
    182    */
    183   public List getSubgroups()
    184   { return this.childGroups;
    185   }
    186 
    187   /**
    188    *  Get the number of files in the group
    189    *
    190    *  @return <code>int</code> the count
    191    */
    192   public int noOfFiles()
    193   { return this.children.size();
    194   }
    195 
    196   /**
    197    *  Get the number of subgroups.
    198    */
    199   public int noOfSubgroups()
    200   { return this.childGroups.size();
    201   }
    202 
    203   /**
    204    *  @return <code>String</code> the name of this group
    205    */
    206   public String getId()
    207   { return this.id;
    208   }
    209 
    210      public static METSFileGroup parse_groupXML(Element element, METSFileSet set, String parseFilePath, METSFileGroup group)
    211      {
     30{
     31    List   children;
     32    List   childGroups;
     33    String id;
     34   
     35    public static final String SECTION_PARENT = "Section";
     36    public static final String GROUP_PARENT = "Group";
     37   
     38    public METSFileGroup(String id)
     39    {
     40    this.children = new ArrayList();
     41    this.childGroups = new ArrayList();
     42    this.id = id;
     43    }
     44   
     45    /**
     46     *  Get the modified datestamp that applies for this file group.  An empty group
     47     *  will return <code>0</code>.
     48     *
     49     *  @return <code>long</code> the date modified as milliseconds from the 1st January 1970
     50     */
     51    public long getModifiedDatestamp()
     52    {
     53    long reply = 0; // empty groups reply '0'
     54   
     55    Iterator childIter = children.iterator();
     56    while (childIter.hasNext()){
     57        METSFile file = (METSFile) childIter.next();
     58        long fileStamp = file.getModifiedDatestamp();
     59        if (fileStamp > reply) {
     60        reply = fileStamp;
     61        }
     62    }
     63   
     64    childIter = childGroups.iterator();
     65    while (childIter.hasNext()){
     66        METSFileGroup fileGroup = (METSFileGroup) childIter.next();
     67        long fileStamp = fileGroup.getModifiedDatestamp();
     68        if (fileStamp > reply) {
     69        reply = fileStamp;
     70        }
     71    }
     72   
     73    return reply;
     74    }
     75   
     76    /**
     77     *  Find all the occurrences of a given file, and place them
     78     *  in a <code>List</code>.
     79     */
     80    public void findGroups(URL findFile, List resultList)
     81    {
     82    // find in this particular group
     83    Iterator childIter = children.iterator();
     84    while (childIter.hasNext()){
     85        METSFile file = (METSFile) childIter.next();
     86        if (file.equals(findFile))
     87        { resultList.add(this.id);
     88        }
     89    }
     90   
     91    // iterate over the child groups
     92    childIter = childGroups.iterator();
     93    while (childIter.hasNext()){
     94        METSFileGroup fileGroup = (METSFileGroup) childIter.next();
     95       
     96        fileGroup.findGroups(findFile, resultList);
     97    }
     98    }
     99   
     100    /**
     101     *  Get a sub group of this METSFileGroup.
     102     *
     103     *  @param <code>String</code> the name of the subgroup
     104     *  @return <code>METSFileGroup</code> the child group, which will
     105     *          be <code>null</code> if the group is not known.
     106     */
     107    public METSFileGroup getSubgroup(String id)
     108    {
     109    String childId;
     110   
     111    int dotAt = id.indexOf(':');
     112    if (dotAt == -1){
     113        childId = null;
     114    }
     115    else{
     116        childId = id.substring(dotAt+1);
     117        id = id.substring(0, dotAt);
     118    }
     119   
     120    // iterate over the child groups
     121    Iterator childIter = childGroups.iterator();
     122    while (childIter.hasNext()){
     123        METSFileGroup group = (METSFileGroup) childIter.next();
     124       
     125        if (group.id.equals(id)) {
     126        if (childId == null) {
     127            return group;
     128        }
     129        else {
     130            return group.getSubgroup(childId);
     131        }
     132        }
     133    }
     134    return null;
     135    }
     136   
     137    /**
     138     *  Add a filegroup to this group of files.
     139     *
     140     *  @param <code>METSFileGroup</code> the filegroup.
     141     */
     142    public void addGroup(METSFileGroup group)
     143    {
     144    this.childGroups.add(group);
     145    }
     146   
     147    /**
     148     *  Add a file to this group of files.
     149     *
     150     *  @param <code>METSFile</code> the file in a METS File object.
     151     */
     152    public void addFile(METSFile file)
     153    {
     154    this.children.add(file);
     155    file.setGroup(this);
     156    if (!file.getID().isDefined()){
     157        file.setID(new METSFileID(this.id + "." + Integer.toString(this.children.size())));
     158    }
     159    }
     160   
     161    /**
     162     *  Get the nth child of the group
     163     *
     164     *  @param <code>int</code> the index into the group
     165     *  @return <code>METSFile</code> the corresponding child.  This may be <code>null</code>
     166     *          particularly if the index given falls outside of the valid range of child
     167     *          indexes.
     168     */
     169    public METSFile getFile(int index)
     170    {
     171    if (index < 0 || index >= this.children.size()){
     172        return null;
     173    }
     174   
     175    return (METSFile) this.children.get(index);
     176    }
     177   
     178    /**
     179     *  Get all the files in the group as a <code>List</code>
     180     *
     181     *  @return <code>List</code> the file childen of the group.
     182     */
     183    public List getFiles()
     184    {
     185    return this.children;
     186    }
     187   
     188    /**
     189     *  Get all the subgroups as a <code>List</code>
     190     */
     191    public List getSubgroups()
     192    {
     193    return this.childGroups;
     194    }
     195   
     196    /**
     197     *  Get the number of files in the group
     198     *
     199     *  @return <code>int</code> the count
     200     */
     201    public int noOfFiles()
     202    {
     203    return this.children.size();
     204    }
     205   
     206    /**
     207     *  Get the number of subgroups.
     208     */
     209    public int noOfSubgroups()
     210    {
     211    return this.childGroups.size();
     212    }
     213   
     214    /**
     215     *  @return <code>String</code> the name of this group
     216     */
     217    public String getId()
     218    {
     219    return this.id;
     220    }
     221   
     222    public static METSFileGroup parse_groupXML(Element element, METSFileSet set, String parseFilePath, METSFileGroup group)
     223    {
    212224    //deal with mets:fileGrp
    213225    NodeList children = element.getChildNodes();
     
    219231        Element child = (Element) children.item(c);
    220232        String childName = child.getNodeName();
    221 
     233       
    222234        if (childName.equals("mets:fileGrp")) {
    223235        String groupId = child.getAttribute("ID");
    224236        METSFileGroup childGroup = new METSFileGroup(groupId);
    225  
     237       
    226238        if (group != null){
    227239            group.addGroup(childGroup);
     
    233245    }
    234246    return group;
    235      }
    236 
    237   /**
    238    *  Write the object in an XML METS format.
    239    *
    240    *  @param <code>PrintWriter</code> the printwriter being used to write the object
    241    */
    242   public void write(PrintWriter writer)
    243   { String tag = XMLTools.getOpenTag("mets","fileGrp");
    244     tag = XMLTools.addAttribute(tag, "ID", this.id);
    245     writer.println(tag);
     247    }
     248
     249    /**
     250     *  Write the object in an XML METS format.
     251     *
     252     *  @param <code>PrintWriter</code> the printwriter being used to write the object
     253     */
     254    public void write(PrintWriter writer)
     255    {
     256    String tag = XMLTools.getOpenTag("mets","fileGrp");
     257    tag = XMLTools.addAttribute(tag, "ID", this.id);
     258    writer.println(tag);
     259   
     260    Iterator childIter = children.iterator();
     261    while (childIter.hasNext()){
     262        METSFile file = (METSFile) childIter.next();
     263        file.write(writer);
     264    }
     265    writer.println(XMLTools.getCloseTag("mets","fileGrp"));
     266    }
     267   
     268    public boolean writeSQL(DocumentInterface document, String parentRef, boolean parentIsSection,
     269                GS3SQLConnection connection)
     270    {
     271    int sqlId = -1;
     272   
     273    // check if this node is in the database already
     274    GS3SQLSelect select = new GS3SQLSelect("filegroups");
     275    select.addField("*");
     276    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupID", "=", this.id);
     277    GS3SQLWhereItem whereDoc  = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     278    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     279    where.add(whereDoc);
     280    select.setWhere(where);
     281   
     282    connection.execute(select.toString());
     283   
     284    ResultSet selectResult = connection.getResultSet();
     285   
     286    try {
     287        if (selectResult == null ||
     288        !selectResult.first()) {
     289        GS3SQLInsert insert = new GS3SQLInsert("filegroups");
    246290       
    247     Iterator childIter = children.iterator();
    248     while (childIter.hasNext())
    249     { METSFile file = (METSFile) childIter.next();
    250       file.write(writer);
    251     }
    252     writer.println(XMLTools.getCloseTag("mets","fileGrp"));
    253   }
    254 
    255   public boolean writeSQL(DocumentInterface document, String parentRef, boolean parentIsSection,
    256               GS3SQLConnection connection)
    257   { int sqlId = -1;
    258 
    259     // check if this node is in the database already
    260     GS3SQLSelect select = new GS3SQLSelect("filegroups");
    261     select.addField("*");
    262     GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupID", "=", this.id);
    263     GS3SQLWhereItem whereDoc  = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    264     GS3SQLWhere where = new GS3SQLWhere(whereItem);
    265     where.add(whereDoc);
    266     select.setWhere(where);
    267 
    268     connection.execute(select.toString());
    269 
    270     ResultSet selectResult = connection.getResultSet();
    271 
    272     try {
    273       if (selectResult == null ||
    274       !selectResult.first()) {
    275     GS3SQLInsert insert = new GS3SQLInsert("filegroups");
    276 
    277     insert.addValue("DocID", document.getID().toString());
    278     insert.addValue("FileGroupID", this.id);
    279     insert.addValue("ParentRef", parentRef, GS3SQLField.INTEGER_TYPE);
    280     insert.addValue("ParentType", parentIsSection ? SECTION_PARENT : GROUP_PARENT);
    281    
    282     if (!connection.execute(insert.toString())) {
    283       return false;
    284     }
    285       }
    286       else {
    287     // TODO: update the data for this file group...
    288       }
    289     }
    290     catch (SQLException ex)
    291     { System.err.println(ex);
    292       return false;
    293     }
     291        insert.addValue("DocID", document.getID().toString());
     292        insert.addValue("FileGroupID", this.id);
     293        insert.addValue("ParentRef", parentRef, GS3SQLField.INTEGER_TYPE);
     294        insert.addValue("ParentType", parentIsSection ? SECTION_PARENT : GROUP_PARENT);
    294295       
    295     // get the filegroup reference now
    296     connection.execute(select.toString());
    297    
    298     try {
    299       ResultSet results = connection.getResultSet();
    300       if (results == null) {
    301     return false;
    302       }
    303       results.first();
    304       sqlId = results.getInt("FileGroupRef");
    305     }
    306     catch (SQLException sqlex) {
    307       System.out.println(sqlex);
    308       return false;
    309     }
    310 
    311     // iterate over the child groups
    312     Iterator childIter = childGroups.iterator();
    313     while (childIter.hasNext())
    314     { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
    315 
    316       if (!fileGroup.writeSQL(document, Integer.toString(sqlId), false, connection))
    317       { return false;
    318       }
    319     }
    320 
    321     // iterate over the child files
    322     childIter = children.iterator();
    323     while (childIter.hasNext())
    324     { METSFile file = (METSFile) childIter.next();
    325    
    326       if (!file.writeSQL(sqlId, connection))
    327       { return false;
    328       }
    329     }
    330 
    331     return true;
    332   }
    333 
    334   public static METSFileGroup readSQL(DocumentInterface document, GS3SQLConnection connection,
    335                       ResultSet resultSet)
    336   {
    337     try {
    338       String ID         = resultSet.getString("FileGroupID");
    339       String parentType = resultSet.getString("ParentType");
    340       String parentRef  = resultSet.getString("ParentRef");
    341 
    342       // create the metadata block object
    343       METSFileGroup group = new METSFileGroup(ID);
    344      
    345       // get its metadata reference to retrieve divisions
    346       int groupRef = resultSet.getInt("FileGroupRef");
    347 
    348       // query the database for matching groups which are children of this one
    349       GS3SQLSelect select = new GS3SQLSelect("filegroups");
    350       select.addField("*");
    351       GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(groupRef),
    352                               GS3SQLField.INTEGER_TYPE);
    353       GS3SQLWhere where = new GS3SQLWhere(whereItem);
    354       whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.GROUP_PARENT);
    355       where.add(whereItem);
    356       select.setWhere(where);
    357 
    358       connection.execute(select.toString());
    359 
    360       // parse through the child groups
    361       ResultSet childSet = connection.getResultSet();
    362       if (childSet.first()) {
    363     do {
    364       METSFileGroup fileGroup = METSFileGroup.readSQL(document, connection, childSet);
    365       if (fileGroup != null) {
    366         group.addGroup(fileGroup);
    367       }
    368     }
    369     while (childSet.next());
    370       }
    371 
    372       // now scan for file members
    373       select = new GS3SQLSelect("files");
    374       select.addField("*");
    375       whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupRef),
    376                       GS3SQLField.INTEGER_TYPE);
    377       where = new GS3SQLWhere(whereItem);
    378       select.setWhere(where);
    379       connection.execute(select.toString());
    380 
    381       ResultSet childFileSet = connection.getResultSet();
    382       if (childFileSet != null && childFileSet.first()) {
    383     do {
    384       METSFile file = METSFile.readSQL(connection, childFileSet);
    385       if (file != null) {
    386         group.addFile(file);
    387       }
    388     }
    389     while (childFileSet.next());
    390       }
    391 
    392       return group;
    393     }
    394     catch (SQLException sqlEx)
    395     { System.out.println(sqlEx);
    396       System.exit(1);
    397     }
    398     return null;
    399   }
     296        if (!connection.execute(insert.toString())) {
     297            return false;
     298        }
     299        }
     300        else {
     301        // TODO: update the data for this file group...
     302        }
     303    }
     304    catch (SQLException ex){
     305        System.err.println(ex);
     306        return false;
     307    }
     308   
     309    // get the filegroup reference now
     310    connection.execute(select.toString());
     311   
     312    try {
     313        ResultSet results = connection.getResultSet();
     314        if (results == null) {
     315        return false;
     316        }
     317        results.first();
     318        sqlId = results.getInt("FileGroupRef");
     319    }
     320    catch (SQLException sqlex) {
     321        System.out.println(sqlex);
     322        return false;
     323    }
     324   
     325    // iterate over the child groups
     326    Iterator childIter = childGroups.iterator();
     327    while (childIter.hasNext()){
     328        METSFileGroup fileGroup = (METSFileGroup) childIter.next();
     329       
     330        if (!fileGroup.writeSQL(document, Integer.toString(sqlId), false, connection)){
     331        return false;
     332        }
     333    }
     334   
     335    // iterate over the child files
     336    childIter = children.iterator();
     337    while (childIter.hasNext()){
     338        METSFile file = (METSFile) childIter.next();
     339       
     340        if (!file.writeSQL(sqlId, connection)){
     341        return false;
     342        }
     343    }
     344   
     345    return true;
     346    }
     347   
     348    public static METSFileGroup readSQL(DocumentInterface document, GS3SQLConnection connection,
     349                    ResultSet resultSet)
     350    {
     351    try {
     352        String ID         = resultSet.getString("FileGroupID");
     353        String parentType = resultSet.getString("ParentType");
     354        String parentRef  = resultSet.getString("ParentRef");
     355       
     356        // create the metadata block object
     357        METSFileGroup group = new METSFileGroup(ID);
     358       
     359        // get its metadata reference to retrieve divisions
     360        int groupRef = resultSet.getInt("FileGroupRef");
     361       
     362        // query the database for matching groups which are children of this one
     363        GS3SQLSelect select = new GS3SQLSelect("filegroups");
     364        select.addField("*");
     365        GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(groupRef),
     366                                GS3SQLField.INTEGER_TYPE);
     367        GS3SQLWhere where = new GS3SQLWhere(whereItem);
     368        whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.GROUP_PARENT);
     369        where.add(whereItem);
     370        select.setWhere(where);
     371       
     372        connection.execute(select.toString());
     373       
     374        // parse through the child groups
     375        ResultSet childSet = connection.getResultSet();
     376        if (childSet.first()) {
     377        do {
     378            METSFileGroup fileGroup = METSFileGroup.readSQL(document, connection, childSet);
     379            if (fileGroup != null) {
     380            group.addGroup(fileGroup);
     381            }
     382        }
     383        while (childSet.next());
     384        }
     385       
     386        // now scan for file members
     387        select = new GS3SQLSelect("files");
     388        select.addField("*");
     389        whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupRef),
     390                        GS3SQLField.INTEGER_TYPE);
     391        where = new GS3SQLWhere(whereItem);
     392        select.setWhere(where);
     393        connection.execute(select.toString());
     394       
     395        ResultSet childFileSet = connection.getResultSet();
     396        if (childFileSet != null && childFileSet.first()) {
     397        do {
     398            METSFile file = METSFile.readSQL(connection, childFileSet);
     399            if (file != null) {
     400            group.addFile(file);
     401            }
     402        }
     403        while (childFileSet.next());
     404        }
     405       
     406        return group;
     407    }
     408    catch (SQLException sqlEx){
     409        System.out.println(sqlEx);
     410        System.exit(1);
     411    }
     412    return null;
     413    }
    400414}
Note: See TracChangeset for help on using the changeset viewer.