Changeset 8702 for trunk/gsdl3/src


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

Change program layout

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

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDescriptive.java

    r8461 r8702  
    3636
    3737public class METSDescriptive
    38 { MultiMap map;
    39   String   ID;
    40   String   name;
    41 
    42   public METSDescriptive(String ID, String name)
    43   { this.map  = new MultiMap();
    44     this.ID   = ID;
    45     this.name = name;
    46   }
    47 
    48   public String getID()
    49   { return this.ID;
    50   }
    51 
    52   public String getName()
    53   { return this.name;
    54   }
    55 
    56   public void addNamespace(METSNamespace namespace)
    57   { this.map.put(namespace.getName(), namespace);
    58   }
    59 
    60   public METSNamespace getNamespace(String namespace)
    61   { return (METSNamespace) this.map.get(namespace);
    62   }
    63 
    64   /**
    65    *  Get the version of a metadata namespace that can be used for
    66    *  adding new values to the document.  Remember, each namespace may
    67    *  occur more than once, but only one of the occurrences can be 'open'.
    68    *
    69    *  @param <code>String</code> the name of the namespace
    70    */
    71   public METSNamespace getOpenNamespace(String namespace)
    72   { if (this.map.getAll(namespace) == null)
    73     { return null;
    74     }
    75 
    76     Iterator namespaces = ((List) this.map.getAll(namespace)).iterator();
    77 
    78     while (namespaces.hasNext())
    79     { METSNamespace namespaceData = (METSNamespace) namespaces.next();
    80 
    81       if (namespaceData.isEditable())
    82       { return namespaceData;
    83       }
    84     }
    85     return null;
    86   }
    87  
    88   /**
    89    *  Add a piece of metadata to this document.  Existing values are preserved.
    90    *
    91    *  @param <code>String</code> the namespace in which the metadata occurs
    92    *  @param <code>String</code> the label of the metadata   
    93    *  @param <code>String</code> the value of the metadata
    94    */
    95   public void addMetadata(String namespace, String label, String value)
    96   { METSNamespace namespaceData = this.getOpenNamespace(namespace);
    97  
    98     if (namespaceData == null ||
    99     namespaceData.isEditable() == false)
    100     { namespaceData = NamespaceFactory.initNamespace(namespace);
    101       this.addNamespace(namespaceData);
    102     }
    103     namespaceData.addMetadata(label, value);
    104   }
    105 
    106   /**
    107    *  Add a piece of metadata to this document.  Existing values in editable
    108    *  parts of the document's metadata are destroyed.
    109    *
    110    *  @param <code>String</code> the namespace in which the metadata occurs
    111    *  @param <code>String</code> the label of the metadata   
    112    *  @param <code>String</code> the value of the metadata
    113    */
    114   public void setMetadata(String namespace, String label, String value)
    115   { METSNamespace namespaceData = this.getOpenNamespace(namespace);
    116  
    117     if (namespaceData == null ||
    118     namespaceData.isEditable() == false)
    119     { namespaceData = NamespaceFactory.initNamespace(namespace);
    120       this.addNamespace(namespaceData);
    121     }
    122     namespaceData.setMetadata(label, value);
    123   }
    124 
    125   /**
    126    *  Remove the metadata for a particular label.
    127    *
    128    *  @param <code>String</code> the namespace in which the metadata occurs
    129    *  @param <code>String</code> the label of the metadata
    130    */
    131   public void removeMetadata(String namespace, String label)
    132   { METSNamespace namespaceData = this.getOpenNamespace(namespace);
    133 
    134     if (namespaceData != null && namespaceData.isEditable() == true)
    135     { namespaceData.removeMetadata(label);
    136     }
    137   }
    138 
    139   /**
    140    *  Fetch a <code>List</code> of the values matching the given label in
    141    *  the requisite namespace.
    142    *
    143    *  @param <code>String</code> namespace
    144    *  @param <code>String</code> label of metadata to match
    145    *
    146    *  @return <code>List</code> the corresponding values found.
    147    */
    148   public List getMetadata(String namespace, String label)
    149   {
    150     // Simple case - no metadata
    151     if (!this.map.containsKey(namespace))
    152     { return null;
    153     }
    154 
    155     // if there's just one instance of the requisite namespace,
    156     // then again it is a simple case
    157     if (this.map.getAll(namespace).size() == 1)
    158     {   return this.getNamespace(namespace).getMetadata(label);
    159     }
    160 
    161     // otherwise, step through and accumulate
    162     List accumulatedValues = new ArrayList();
    163     Iterator namespaceIterator = this.map.getAll(namespace).iterator();
    164     while (namespaceIterator.hasNext())
    165     { METSNamespace localNamespace = (METSNamespace) namespaceIterator.next();
    166 
    167       List localList = localNamespace.getMetadata(label);
    168       if (localList != null && localList.size() > 0)
    169       { accumulatedValues.addAll(localList);
    170       }
    171     }
    172 
    173     // if the result of the accumulation is empty, return a null item
    174     if (accumulatedValues.size() == 0)
    175     { return null;
    176     }
    177 
    178     return accumulatedValues;
    179   }
    180  
     38{
     39    MultiMap map;
     40    String   ID;
     41    String   name;
     42   
     43    public METSDescriptive(String ID, String name)
     44    {
     45    this.map  = new MultiMap();
     46    this.ID   = ID;
     47    this.name = name;
     48    }
     49   
     50    public String getID()
     51    {
     52    return this.ID;
     53    }
     54   
     55    public String getName()
     56    {
     57    return this.name;
     58    }
     59   
     60    public void addNamespace(METSNamespace namespace)
     61    {
     62    this.map.put(namespace.getName(), namespace);
     63    }
     64   
     65    public METSNamespace getNamespace(String namespace)
     66    {
     67    return (METSNamespace) this.map.get(namespace);
     68    }
     69   
     70    /**
     71     *  Get the version of a metadata namespace that can be used for
     72     *  adding new values to the document.  Remember, each namespace may
     73     *  occur more than once, but only one of the occurrences can be 'open'.
     74     *
     75     *  @param <code>String</code> the name of the namespace
     76     */
     77    public METSNamespace getOpenNamespace(String namespace)
     78    {
     79    if (this.map.getAll(namespace) == null) {
     80        return null;
     81    }
     82   
     83    Iterator namespaces = ((List) this.map.getAll(namespace)).iterator();
     84   
     85    while (namespaces.hasNext()) {
     86        METSNamespace namespaceData = (METSNamespace) namespaces.next();
     87       
     88        if (namespaceData.isEditable()){
     89        return namespaceData;
     90        }
     91    }
     92    return null;
     93    }
     94   
     95    /**
     96     *  Add a piece of metadata to this document.  Existing values are preserved.
     97     *
     98     *  @param <code>String</code> the namespace in which the metadata occurs
     99     *  @param <code>String</code> the label of the metadata   
     100     *  @param <code>String</code> the value of the metadata
     101     */
     102    public void addMetadata(String namespace, String label, String value)
     103    {
     104    METSNamespace namespaceData = this.getOpenNamespace(namespace);
     105   
     106    if (namespaceData == null ||
     107        namespaceData.isEditable() == false) {
     108        namespaceData = NamespaceFactory.initNamespace(namespace);
     109        this.addNamespace(namespaceData);
     110    }
     111    namespaceData.addMetadata(label, value);
     112    }
     113   
     114    /**
     115     *  Add a piece of metadata to this document.  Existing values in editable
     116     *  parts of the document's metadata are destroyed.
     117     *
     118     *  @param <code>String</code> the namespace in which the metadata occurs
     119     *  @param <code>String</code> the label of the metadata   
     120     *  @param <code>String</code> the value of the metadata
     121     */
     122    public void setMetadata(String namespace, String label, String value)
     123    {
     124    METSNamespace namespaceData = this.getOpenNamespace(namespace);
     125   
     126    if (namespaceData == null ||
     127        namespaceData.isEditable() == false) {
     128        namespaceData = NamespaceFactory.initNamespace(namespace);
     129        this.addNamespace(namespaceData);
     130    }
     131    namespaceData.setMetadata(label, value);
     132    }
     133   
     134    /**
     135     *  Remove the metadata for a particular label.
     136     *
     137     *  @param <code>String</code> the namespace in which the metadata occurs
     138     *  @param <code>String</code> the label of the metadata
     139     */
     140    public void removeMetadata(String namespace, String label)
     141    {
     142    METSNamespace namespaceData = this.getOpenNamespace(namespace);
     143   
     144    if (namespaceData != null && namespaceData.isEditable() == true) {
     145        namespaceData.removeMetadata(label);
     146    }
     147    }
     148   
     149    /**
     150     *  Fetch a <code>List</code> of the values matching the given label in
     151     *  the requisite namespace.
     152     *
     153     *  @param <code>String</code> namespace
     154     *  @param <code>String</code> label of metadata to match
     155     *
     156     *  @return <code>List</code> the corresponding values found.
     157     */
     158    public List getMetadata(String namespace, String label)
     159    {
     160    // Simple case - no metadata
     161    if (!this.map.containsKey(namespace)){
     162        return null;
     163    }
     164   
     165    // if there's just one instance of the requisite namespace,
     166    // then again it is a simple case
     167    if (this.map.getAll(namespace).size() == 1){
     168        return this.getNamespace(namespace).getMetadata(label);
     169    }
     170   
     171    // otherwise, step through and accumulate
     172    List accumulatedValues = new ArrayList();
     173    Iterator namespaceIterator = this.map.getAll(namespace).iterator();
     174    while (namespaceIterator.hasNext()){
     175        METSNamespace localNamespace = (METSNamespace) namespaceIterator.next();
     176       
     177        List localList = localNamespace.getMetadata(label);
     178        if (localList != null && localList.size() > 0){
     179        accumulatedValues.addAll(localList);
     180        }
     181    }
     182   
     183    // if the result of the accumulation is empty, return a null item
     184    if (accumulatedValues.size() == 0){
     185        return null;
     186    }
     187   
     188    return accumulatedValues;
     189    }
     190   
    181191    protected void parse_xmlData(METSNamespace namespace, Element element)
    182192    {
     
    190200        continue;
    191201        }
    192 
     202       
    193203        Element child  = (Element) children.item(c);
    194204        String childName = child.getNodeName();
     
    204214    }
    205215    }
    206 
     216   
    207217    public void parse_mdWrap(Element element)
    208218    {
     
    217227        continue;
    218228        }
    219 
     229       
    220230        Element child  = (Element) children.item(c);
    221231        String childName = child.getNodeName();
     
    274284    }
    275285   
    276 
    277   /**
    278    *  Parse an XML Element as a METS Descriptive Metadata section
    279    *
    280    *  @param <code>Element</code> the XML element which represents the dmdSec itself
    281    */
     286   
     287    /**
     288     *  Parse an XML Element as a METS Descriptive Metadata section
     289     *
     290     *  @param <code>Element</code> the XML element which represents the dmdSec itself
     291     */
    282292    /*  public static METSDescriptive parseXML(Element element)
    283   { // Note: no parsing of attributes required, we just move onto the metadata
    284     //       namespaces/sections themselves
    285     String ID = element.getAttribute("ID");
    286     String label = element.getAttribute("GROUPID");
    287     METSDescriptive thisDescriptive = new METSDescriptive(ID, label);
    288    
    289     NodeList children = element.getChildNodes();
    290    
    291     for (int c = 0; c < children.getLength(); c ++)
    292     { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
     293    { // Note: no parsing of attributes required, we just move onto the metadata
     294    //       namespaces/sections themselves
     295    String ID = element.getAttribute("ID");
     296    String label = element.getAttribute("GROUPID");
     297    METSDescriptive thisDescriptive = new METSDescriptive(ID, label);
     298   
     299    NodeList children = element.getChildNodes();
     300   
     301    for (int c = 0; c < children.getLength(); c ++)
     302    { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
    293303        continue;
    294       }
    295 
    296       Element childElement = (Element) children.item(c);
    297       if (childElement.getNodeName().equals("mets:mdRef"))
    298       { METSNamespace namespace = NamespaceFactory.parseXML(element);
    299       }
    300       else if (childElement.getNodeName().equals("mets:mdWrap"))
    301       { METSNamespace namespace = NamespaceFactory.parseXML(element);
    302       }
    303       else
    304       { // TODO: raise an error!
    305       }
    306     }
    307     return thisDescriptive;
    308   }
     304    }
     305   
     306    Element childElement = (Element) children.item(c);
     307    if (childElement.getNodeName().equals("mets:mdRef"))
     308    {   METSNamespace namespace = NamespaceFactory.parseXML(element);
     309    }
     310    else if (childElement.getNodeName().equals("mets:mdWrap"))
     311    {   METSNamespace namespace = NamespaceFactory.parseXML(element);
     312    }
     313    else
     314    {   // TODO: raise an error!
     315    }
     316    }
     317    return thisDescriptive;
     318    }
    309319    */
    310320
    311   /**
    312    *  Write the document metadata to a <code>PrintWriter</code> in METS
    313    *  XML format.
    314    * 
    315    *  @param <code>PrintWriter</code> the writer to use for output
    316    */
    317   public void write(PrintWriter output)
    318   { String tag = XMLTools.getOpenTag("mets", "dmdSec");
    319     tag = XMLTools.addAttribute(tag, "ID", this.ID);
    320     tag = XMLTools.addAttribute(tag, "GROUPID", this.name);
    321     output.println(tag);
    322 
    323     Iterator keys = this.map.keySet().iterator();
    324 
    325     // get the keys one by one
    326     while (keys.hasNext())
    327     { Object nextKey = keys.next();
    328       if (nextKey == null)
    329       { continue;
    330       }
    331       String key = nextKey.toString();
    332 
    333       // get every copy of the current namespace name - namespaces may
    334       // occur more than once, so this is a List
    335       Iterator namespaces = this.map.getAll(key).iterator();
    336      
    337       // namespaces will write themselves...
    338       while (namespaces.hasNext())
    339       { METSNamespace namespace = (METSNamespace) namespaces.next();
    340    
    341         namespace.write(output);
    342       }
    343     }
    344 
    345     tag = XMLTools.getCloseTag("mets", "dmdSec");
    346     output.println(tag);
    347   }
    348 
     321    /**
     322     *  Write the document metadata to a <code>PrintWriter</code> in METS
     323     *  XML format.
     324     * 
     325     *  @param <code>PrintWriter</code> the writer to use for output
     326     */
     327    public void write(PrintWriter output)
     328    {
     329    String tag = XMLTools.getOpenTag("mets", "dmdSec");
     330    tag = XMLTools.addAttribute(tag, "ID", this.ID);
     331    tag = XMLTools.addAttribute(tag, "GROUPID", this.name);
     332    output.println(tag);
     333   
     334    Iterator keys = this.map.keySet().iterator();
     335   
     336    // get the keys one by one
     337    while (keys.hasNext()){
     338        Object nextKey = keys.next();
     339        if (nextKey == null){
     340        continue;
     341        }
     342        String key = nextKey.toString();
     343   
     344        // get every copy of the current namespace name - namespaces may
     345        // occur more than once, so this is a List
     346        Iterator namespaces = this.map.getAll(key).iterator();
     347       
     348        // namespaces will write themselves...
     349        while (namespaces.hasNext()){
     350        METSNamespace namespace = (METSNamespace) namespaces.next();
     351       
     352        namespace.write(output);
     353        }
     354    }
     355   
     356    tag = XMLTools.getCloseTag("mets", "dmdSec");
     357    output.println(tag);
     358    }
     359   
    349360    public void copy_metadata(METSDescriptive dst)
    350361    {
    351362    Iterator keys = this.map.keySet().iterator();
    352 
     363   
    353364    // get the keys one by one
    354365    while (keys.hasNext()) {
     
    358369        }
    359370        String key = nextKey.toString();
    360 
    361         System.err.println("*** Namespace = " + key);
    362371       
    363372        // get every copy of the current namespace name - namespaces may
     
    390399
    391400
    392   public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
    393   { // check if this node is in the
    394     int sqlId = -1;
    395 
    396     //    System.out.println("Writing " + connection.toString());
    397 
    398     GS3SQLSelect select = new GS3SQLSelect("metadata");
    399     select.addField("*");
    400     GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetaID", "=", this.ID);
    401     GS3SQLWhereItem item = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    402     GS3SQLWhere where = new GS3SQLWhere(whereItem);
    403     where.add(item);
    404     select.setWhere(where);
    405 
    406     connection.execute(select.toString());
    407 
    408     try {
    409       GS3SQLAction action;
    410 
    411       ResultSet resultSet = connection.getResultSet();
    412 
    413       if (resultSet != null &&
    414       resultSet.first()) {
    415     // the node already exists - no need to do anything as such
    416     GS3SQLUpdate update = new GS3SQLUpdate("metadata");
    417     update.setWhere(where);
    418     action = update;
    419       }
    420       else
    421       { // Set result set to null, just in case next() didn't work above...
    422     resultSet = null;
    423 
    424     // It is a new node and needs writing
    425     action = new GS3SQLInsert("metadata");
    426 
    427     action.addValue("DocID", document.getID().toString());
    428     action.addValue("MetaID", this.ID);
    429       }
    430    
    431       // always set the group identifier
    432       action.addValue("GroupID", this.name);
    433        
    434       // execute the action as required
    435       connection.execute(action.toString());
    436 
    437       // create a new resultset if necessary
    438       if (resultSet == null) {
    439     // now execute the select statement again to get the new identifier
    440     // 'MetadataRef'
    441     // System.out.println(select.toString());
     401    public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
     402    {
     403    // check if this node is in the
     404    int sqlId = -1;
     405   
     406    //    System.out.println("Writing " + connection.toString());
     407   
     408    GS3SQLSelect select = new GS3SQLSelect("metadata");
     409    select.addField("*");
     410    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetaID", "=", this.ID);
     411    GS3SQLWhereItem item = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     412    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     413    where.add(item);
     414    select.setWhere(where);
     415   
    442416    connection.execute(select.toString());
    443 
    444     resultSet = connection.getResultSet();
    445     resultSet.first();
    446       }
    447 
    448       // get the reference for this item...
    449       sqlId = resultSet.getInt("MetadataRef");
    450     }
    451     catch (SQLException sql)
    452     { System.out.println(sql);
    453     }
    454 
    455     Iterator keys = this.map.keySet().iterator();
    456 
    457     // get the keys one by one
    458     while (keys.hasNext())
    459     { Object nextKey = keys.next();
    460       if (nextKey == null)
    461       { continue;
    462       }
    463       String key = nextKey.toString();
    464 
    465       // get every current namespace - namespaces may
    466       // occur more than once, so this is a List
    467       Iterator namespaces = this.map.getAll(key).iterator();
    468    
    469       // namespaces will write themselves...
    470       while (namespaces.hasNext())
    471       { METSNamespace namespace = (METSNamespace) namespaces.next();
    472      
    473         if (!namespace.writeSQL(sqlId, connection))
    474         { return false;
    475     }
    476       }
    477     }
    478     return true;
    479   }
    480 
    481   public static METSDescriptive readSQL(DocumentInterface document, GS3SQLConnection connection,
    482                     ResultSet resultSet)
    483   {
    484     try {
    485       String ID   = resultSet.getString("MetaID");
    486       String name = resultSet.getString("GroupID");
    487 
    488       // create the metadata block object
    489       METSDescriptive descriptive = new METSDescriptive(ID, name);
    490      
    491       // get its metadata reference to retrieve namespaces
    492       int metadataRef = resultSet.getInt("MetadataRef");
    493 
    494       // query the database for matching namespaces for this metadata block
    495       GS3SQLSelect select = new GS3SQLSelect("namespaces");
    496       select.addField("*");
    497       GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(metadataRef),
    498                               GS3SQLField.INTEGER_TYPE);
    499       GS3SQLWhere where = new GS3SQLWhere(whereItem);
    500       select.setWhere(where);
    501 
    502       connection.execute(select.toString());
    503 
    504       // parse through the namespaces, calling the namespace class to create instances
    505       // as it will
    506       ResultSet namespaceSet = connection.getResultSet();
    507       if (namespaceSet != null && namespaceSet.first()) {
    508     do {
    509       METSNamespace namespace = METSNamespace.readSQL(connection, namespaceSet);
    510       if (namespace != null) {
    511         descriptive.addNamespace(namespace);
    512       }
    513       else {
    514         System.out.println("Null namespace output");
    515       }
    516     }
    517     while (namespaceSet.next());
    518       }
    519 
    520       return descriptive;
    521     }
    522     catch (SQLException sqlEx)
    523     { System.out.println(sqlEx);
    524       System.exit(1);
    525     }
    526     return null;
    527   }
     417   
     418    try {
     419        GS3SQLAction action;
     420       
     421        ResultSet resultSet = connection.getResultSet();
     422       
     423        if (resultSet != null &&
     424        resultSet.first()) {
     425        // the node already exists - no need to do anything as such
     426        GS3SQLUpdate update = new GS3SQLUpdate("metadata");
     427        update.setWhere(where);
     428        action = update;
     429        }
     430        else
     431        { // Set result set to null, just in case next() didn't work above...
     432            resultSet = null;
     433           
     434            // It is a new node and needs writing
     435            action = new GS3SQLInsert("metadata");
     436           
     437            action.addValue("DocID", document.getID().toString());
     438            action.addValue("MetaID", this.ID);
     439        }
     440       
     441        // always set the group identifier
     442        action.addValue("GroupID", this.name);
     443       
     444        // execute the action as required
     445        connection.execute(action.toString());
     446       
     447        // create a new resultset if necessary
     448        if (resultSet == null) {
     449        // now execute the select statement again to get the new identifier
     450        // 'MetadataRef'
     451        // System.out.println(select.toString());
     452        connection.execute(select.toString());
     453       
     454        resultSet = connection.getResultSet();
     455        resultSet.first();
     456        }
     457       
     458        // get the reference for this item...
     459        sqlId = resultSet.getInt("MetadataRef");
     460    }
     461    catch (SQLException sql){
     462        System.out.println(sql);
     463    }
     464   
     465    Iterator keys = this.map.keySet().iterator();
     466   
     467    // get the keys one by one
     468    while (keys.hasNext()){
     469        Object nextKey = keys.next();
     470        if (nextKey == null){
     471        continue;
     472        }
     473        String key = nextKey.toString();
     474       
     475        // get every current namespace - namespaces may
     476        // occur more than once, so this is a List
     477        Iterator namespaces = this.map.getAll(key).iterator();
     478       
     479        // namespaces will write themselves...
     480        while (namespaces.hasNext()){
     481        METSNamespace namespace = (METSNamespace) namespaces.next();
     482       
     483        if (!namespace.writeSQL(sqlId, connection)){
     484            return false;
     485        }
     486        }
     487    }
     488    return true;
     489    }
     490   
     491    public static METSDescriptive readSQL(DocumentInterface document, GS3SQLConnection connection,
     492                      ResultSet resultSet)
     493    {
     494    try {
     495        String ID   = resultSet.getString("MetaID");
     496        String name = resultSet.getString("GroupID");
     497       
     498        // create the metadata block object
     499        METSDescriptive descriptive = new METSDescriptive(ID, name);
     500       
     501        // get its metadata reference to retrieve namespaces
     502        int metadataRef = resultSet.getInt("MetadataRef");
     503       
     504        // query the database for matching namespaces for this metadata block
     505        GS3SQLSelect select = new GS3SQLSelect("namespaces");
     506        select.addField("*");
     507        GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(metadataRef),
     508                                GS3SQLField.INTEGER_TYPE);
     509        GS3SQLWhere where = new GS3SQLWhere(whereItem);
     510        select.setWhere(where);
     511       
     512        connection.execute(select.toString());
     513       
     514        // parse through the namespaces, calling the namespace class to create instances
     515        // as it will
     516        ResultSet namespaceSet = connection.getResultSet();
     517        if (namespaceSet != null && namespaceSet.first()) {
     518        do {
     519            METSNamespace namespace = METSNamespace.readSQL(connection, namespaceSet);
     520            if (namespace != null) {
     521            descriptive.addNamespace(namespace);
     522            }
     523            else {
     524            System.out.println("Null namespace output");
     525            }
     526        }
     527        while (namespaceSet.next());
     528        }
     529       
     530        return descriptive;
     531    }
     532    catch (SQLException sqlEx){
     533        System.out.println(sqlEx);
     534        System.exit(1);
     535    }
     536    return null;
     537    }
    528538}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileSet.java

    r8461 r8702  
    3030
    3131public class METSFileSet
    32 { Map    fileGroups;
    33   String reference;
    34   FileIdentifierFactory identifierFactory;
    35   FileGroupIdentifierFactory groupIdentifierFactory;
    36 
    37   class FileGroupIdentifierFactory extends AbstractIdentifierFactory
    38   { public static final String FILEGROUP_PRELUDE = "FG";
    39 
    40     public FileGroupIdentifierFactory()
    41     { super("FILEGROUP_PRELUDE");
    42     }
    43   }
    44 
    45   public METSFileSet()
    46   { this.fileGroups = new HashMap();
    47     this.fileGroups.put("default", new METSFileGroup("default"));
    48     this.reference = null;
    49     this.identifierFactory = new FileIdentifierFactory();
    50     this.groupIdentifierFactory = new FileGroupIdentifierFactory();
    51   }
    52 
    53   public long getModifiedDatestamp()
    54   { long reply = 0;
    55 
    56     Iterator groupsIter = this.fileGroups.values().iterator();
    57     while (groupsIter.hasNext())
    58     { METSFileGroup group = (METSFileGroup) groupsIter.next();
    59 
    60       long groupStamp = group.getModifiedDatestamp();
    61       if (groupStamp > reply)
    62       { reply = groupStamp;
    63       }
    64     }
    65 
    66     return reply;
    67   }
    68    
    69 
    70   /**
    71    *  Get a new FileGroup to lie within this fileset.  The group is not initialised with
    72    *  a parent, etc. - all that is done is the allocation of a unique group identifier.
    73    *
    74    *  @return <code>METSFileGroup</code> the new group.
    75    */
    76   public METSFileGroup createGroup()
    77   { METSFileGroup group = new METSFileGroup(this.groupIdentifierFactory.getNextIdentifier());
    78     return group;
    79   }
    80 
    81   public METSFile createFile(METSFilePos filePos, String type)
    82   { METSFile file = new METSFile(new METSFileID(this.identifierFactory.getNextIdentifier()), filePos, type);
    83     return file;
    84   }
    85  
    86   /**
    87    *  Get the group that corresponds to the given name...
    88    *
    89    *  @param <code>String</code> the name of the group.
    90    *  @return <code>METSFileGroup</code> the group object - this will be
    91    *          <code>null</code> if the group is not found
    92    */
    93   public METSFileGroup getGroup(String name)
    94   { if (!this.fileGroups.containsKey(name))
    95     { return null;
    96     }
    97     return (METSFileGroup) this.fileGroups.get(name);
    98   }
    99 
    100   /**
    101    *  Get the Nth file from the default group...
    102    *
    103    *  @param <code>int</code> the index into the default group to use...
    104    *  @return <code>METSFile</code> the file.
    105    */
    106   public METSFile getFile(int index)
    107   { METSFileGroup group = this.getGroup("default");
    108 
    109     if (group == null)
    110     { return null;
    111     }
    112     return group.getFile(0);
    113   }
    114 
    115   /**
    116    *  Add a file to the default file group
    117    */
    118   public void addFile(METSFile file)
    119   { METSFileGroup defaultGroup = (METSFileGroup) this.fileGroups.get("default");
    120     defaultGroup.addFile(file);
    121   }
    122 
    123   /**
    124    *  Add a file to the default file group
    125    *
    126    *  @return <code>METSFile</code> an object that wraps the given url into
    127    *          a METS file object
    128    */
    129   public METSFile addFile(URL url)
    130   { METSFile file = METSFile.makeMETSFile(url);
    131     this.addFile(file);
    132     return file;
    133   }
    134 
    135   public void addFile(METSFile file, String toGroup)
    136   { METSFileGroup destGroup = (METSFileGroup) this.fileGroups.get(toGroup);
    137 
    138     if (destGroup == null)
    139     { destGroup = new METSFileGroup(toGroup);
    140       this.fileGroups.put(destGroup.getId(), destGroup);
    141     }
    142        
    143     destGroup.addFile(file);
    144   }
    145 
    146   public void addFile(URL url, String toGroup)
    147   { METSFile file = METSFile.makeMETSFile(url);
    148     this.addFile(file, toGroup);
    149   }
    150 
    151   /**
    152    *  Add a group at the top level of the METS File set.
    153    *
    154    *  @param <code>METSFileGroup</code> the file group to add.
    155    */
    156   public void addGroup(METSFileGroup group)
    157   { this.fileGroups.put(group.getId(), group);
    158   }
    159 
    160   /**
    161    *  Get all the groups that contain a given URL/file.
    162    *
    163    *  @param <code>URL</code> the location of the file.
    164    *  @return <code>List</code> the list of group references that contain the file.
    165    */
    166   public List findGroups(URL file)
    167   { List resultList = new ArrayList();
    168 
    169     Iterator groups = this.fileGroups.values().iterator();
    170 
    171     while (groups.hasNext())
    172     { METSFileGroup group = (METSFileGroup) groups.next();
    173      
    174       group.findGroups(file, resultList);
    175     }
    176     return resultList;
    177   }
     32{
     33    Map    fileGroups;
     34    String reference;
     35    FileIdentifierFactory identifierFactory;
     36    FileGroupIdentifierFactory groupIdentifierFactory;
     37   
     38    class FileGroupIdentifierFactory extends AbstractIdentifierFactory
     39    {
     40    public static final String FILEGROUP_PRELUDE = "FG";
     41   
     42    public FileGroupIdentifierFactory()
     43    {
     44        super("FILEGROUP_PRELUDE");
     45    }
     46    }
     47   
     48    public METSFileSet()
     49    {
     50    this.fileGroups = new HashMap();
     51    this.fileGroups.put("default", new METSFileGroup("default"));
     52    this.reference = null;
     53    this.identifierFactory = new FileIdentifierFactory();
     54    this.groupIdentifierFactory = new FileGroupIdentifierFactory();
     55    }
     56   
     57    public long getModifiedDatestamp()
     58    {
     59    long reply = 0;
     60   
     61    Iterator groupsIter = this.fileGroups.values().iterator();
     62    while (groupsIter.hasNext()) {
     63        METSFileGroup group = (METSFileGroup) groupsIter.next();
     64        long groupStamp = group.getModifiedDatestamp();
     65        if (groupStamp > reply){
     66            reply = groupStamp;
     67        }
     68        }
     69    return reply;
     70    }
     71   
     72   
     73    /**
     74     *  Get a new FileGroup to lie within this fileset.  The group is not initialised with
     75     *  a parent, etc. - all that is done is the allocation of a unique group identifier.
     76     *
     77     *  @return <code>METSFileGroup</code> the new group.
     78     */
     79    public METSFileGroup createGroup()
     80    {
     81    METSFileGroup group = new METSFileGroup(this.groupIdentifierFactory.getNextIdentifier());
     82    return group;
     83    }
     84   
     85    public METSFile createFile(METSFilePos filePos, String type)
     86    {
     87    METSFile file = new METSFile(new METSFileID(this.identifierFactory.getNextIdentifier()), filePos, type);
     88    return file;
     89    }
     90   
     91    /**
     92     *  Get the group that corresponds to the given name...
     93     *
     94     *  @param <code>String</code> the name of the group.
     95     *  @return <code>METSFileGroup</code> the group object - this will be
     96     *          <code>null</code> if the group is not found
     97     */
     98    public METSFileGroup getGroup(String name)
     99    {
     100    if (!this.fileGroups.containsKey(name)){
     101        return null;
     102    }
     103    return (METSFileGroup) this.fileGroups.get(name);
     104    }
     105   
     106    /**
     107     *  Get the Nth file from the default group...
     108     *
     109     *  @param <code>int</code> the index into the default group to use...
     110     *  @return <code>METSFile</code> the file.
     111     */
     112    public METSFile getFile(int index)
     113    {
     114    METSFileGroup group = this.getGroup("default");
     115
     116    if (group == null) {
     117        return null;
     118    }
     119    return group.getFile(0);
     120    }
     121   
     122    /**
     123     *  Add a file to the default file group
     124     */
     125    public void addFile(METSFile file)
     126    {
     127    METSFileGroup defaultGroup = (METSFileGroup) this.fileGroups.get("default");
     128    defaultGroup.addFile(file);
     129    }
     130   
     131    /**
     132     *  Add a file to the default file group
     133     *
     134     *  @return <code>METSFile</code> an object that wraps the given url into
     135     *          a METS file object
     136     */
     137    public METSFile addFile(URL url)
     138    {
     139    METSFile file = METSFile.makeMETSFile(url);
     140    this.addFile(file);
     141    return file;
     142    }
     143   
     144    public void addFile(METSFile file, String toGroup)
     145    {
     146    METSFileGroup destGroup = (METSFileGroup) this.fileGroups.get(toGroup);
     147   
     148    if (destGroup == null){
     149        destGroup = new METSFileGroup(toGroup);
     150        this.fileGroups.put(destGroup.getId(), destGroup);
     151    }
     152   
     153    destGroup.addFile(file);
     154    }
     155   
     156    public void addFile(URL url, String toGroup)
     157    {
     158    METSFile file = METSFile.makeMETSFile(url);
     159    this.addFile(file, toGroup);
     160    }
     161   
     162    /**
     163     *  Add a group at the top level of the METS File set.
     164     *
     165     *  @param <code>METSFileGroup</code> the file group to add.
     166     */
     167    public void addGroup(METSFileGroup group)
     168    {
     169    this.fileGroups.put(group.getId(), group);
     170    }
     171   
     172    /**
     173     *  Get all the groups that contain a given URL/file.
     174     *
     175     *  @param <code>URL</code> the location of the file.
     176     *  @return <code>List</code> the list of group references that contain the file.
     177     */
     178    public List findGroups(URL file)
     179    {
     180    List resultList = new ArrayList();
     181   
     182    Iterator groups = this.fileGroups.values().iterator();
     183   
     184    while (groups.hasNext()){
     185        METSFileGroup group = (METSFileGroup) groups.next();
     186       
     187        group.findGroups(file, resultList);
     188    }
     189    return resultList;
     190    }
    178191   
    179192    /*
     
    181194     *
    182195     */
    183     public static METSFileSet parseXML(NodeList fileSecs, METSFileSet fileSet, String parseFilePath) {
    184    
     196    public static METSFileSet parseXML(NodeList fileSecs, METSFileSet fileSet, String parseFilePath)
     197    {
    185198    //METSFileSet set = new METSFileSet();>
    186199    // this is in effect a group without a sense of 'self'...
     
    196209   
    197210
    198   public void write(PrintWriter writer)
    199   { Iterator groups = this.fileGroups.values().iterator();
    200 
    201     writer.println("<mets:fileSec>");
    202     while (groups.hasNext())
    203     { METSFileGroup group = (METSFileGroup) groups.next();
    204 
    205       group.write(writer);
    206     }
    207     writer.println("</mets:fileSec>");
    208   }
    209 
    210   public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
    211   { Iterator groups = this.fileGroups.values().iterator();
    212 
    213     // write the set if the reference does not already exist...
    214     if (this.reference == null)
    215     { // Insert the file section into the database
    216       GS3SQLInsert insert = new GS3SQLInsert("filesection");
    217       insert.addValue("DocID", document.getID().toString());
    218       insert.addValue("FileSecID", "test"); // TODO: remove magic string
    219 
    220       if (!connection.execute(insert.toString()))
    221       { return false;
    222       }
    223 
    224       // find the file section number
    225       GS3SQLSelect select = new GS3SQLSelect("filesection");
    226       select.addField("*");
    227       GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileSecID", "=", "test");
    228       GS3SQLWhereItem whereDoc  = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    229       GS3SQLWhere where = new GS3SQLWhere(whereItem);
    230       where.add(whereDoc);
    231       select.setWhere(where);
    232 
    233       try {
     211    public void write(PrintWriter writer)
     212    {
     213    Iterator groups = this.fileGroups.values().iterator();
     214   
     215    writer.println("<mets:fileSec>");
     216    while (groups.hasNext()){
     217        METSFileGroup group = (METSFileGroup) groups.next();
     218       
     219        group.write(writer);
     220    }
     221    writer.println("</mets:fileSec>");
     222    }
     223   
     224    public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
     225    {
     226    Iterator groups = this.fileGroups.values().iterator();
     227   
     228    // write the set if the reference does not already exist...
     229    if (this.reference == null){
     230        // Insert the file section into the database
     231        GS3SQLInsert insert = new GS3SQLInsert("filesection");
     232        insert.addValue("DocID", document.getID().toString());
     233        insert.addValue("FileSecID", "test"); // TODO: remove magic string
     234       
     235        if (!connection.execute(insert.toString())){
     236        return false;
     237        }
     238       
     239        // find the file section number
     240        GS3SQLSelect select = new GS3SQLSelect("filesection");
     241        select.addField("*");
     242        GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileSecID", "=", "test");
     243        GS3SQLWhereItem whereDoc  = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     244        GS3SQLWhere where = new GS3SQLWhere(whereItem);
     245        where.add(whereDoc);
     246        select.setWhere(where);
     247       
     248        try {
     249        connection.execute(select.toString());
     250       
     251        ResultSet set = connection.getResultSet();
     252        set.first();
     253        int sectionRef = set.getInt("FileSectionRef");
     254       
     255        this.reference = Integer.toString(sectionRef);
     256        }
     257        catch (SQLException ex){
     258        System.out.println(ex);
     259        return false;
     260        }
     261    }
     262   
     263    // write out the children
     264    while (groups.hasNext()){
     265        METSFileGroup group = (METSFileGroup) groups.next();
     266       
     267        if (!group.writeSQL(document, this.reference, true, connection)){
     268        return false;
     269        }
     270    }
     271   
     272    return true;
     273    }
     274   
     275    public static METSFileSet readSQL(DocumentInterface document, GS3SQLConnection connection)
     276    {
     277    METSFileSet set = new METSFileSet();   
     278   
     279    // Get file sections from the filesection table (currently redundant)
     280    GS3SQLSelect select = new GS3SQLSelect("filesection");
     281    select.addField("*");
     282    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     283    select.setWhere(new GS3SQLWhere(whereItem));
    234284    connection.execute(select.toString());
    235285   
    236     ResultSet set = connection.getResultSet();
    237     set.first();
    238     int sectionRef = set.getInt("FileSectionRef");
    239 
    240     this.reference = Integer.toString(sectionRef);
    241       }
    242       catch (SQLException ex)
    243       { System.out.println(ex);
    244         return false;
    245       }
    246     }
    247    
    248     // write out the children
    249     while (groups.hasNext())
    250     { METSFileGroup group = (METSFileGroup) groups.next();
    251      
    252       if (!group.writeSQL(document, this.reference, true, connection))
    253       { return false;
    254       }
    255     }
    256 
    257     return true;
    258   }
    259 
    260   public static METSFileSet readSQL(DocumentInterface document, GS3SQLConnection connection)
    261   {
    262     METSFileSet set = new METSFileSet();   
    263 
    264     // Get file sections from the filesection table (currently redundant)
    265     GS3SQLSelect select = new GS3SQLSelect("filesection");
    266     select.addField("*");
    267     GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    268     select.setWhere(new GS3SQLWhere(whereItem));
    269     connection.execute(select.toString());
    270    
    271     // Get the identifier for this file set, etc.
    272     ResultSet sections = connection.getResultSet();
    273     int fileSetRef;
    274     try {
    275       sections.first();
    276       fileSetRef     = sections.getInt("FileSectionRef");
    277       set.reference = Integer.toString(fileSetRef);
    278     }
    279     catch (SQLException ex)
    280     { System.out.println(ex);
    281       return null;
    282     }
    283    
    284     // Get child file groups
    285     select = new GS3SQLSelect("filegroups");
    286     select.addField("*");
    287     whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());   
    288     GS3SQLWhere where = new GS3SQLWhere(whereItem);
    289     whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(fileSetRef),
    290                     GS3SQLField.INTEGER_TYPE);
    291     where.add(whereItem);
    292     whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.SECTION_PARENT);
    293     where.add(whereItem);
    294     select.setWhere(where);
    295     connection.execute(select.toString());
    296 
    297     // start going through the matching file groups
    298     try {
    299       ResultSet resultSet = connection.getResultSet();
    300       resultSet.first();
    301       do {
    302     METSFileGroup filegroup = METSFileGroup.readSQL(document, connection, resultSet);
    303     if (filegroup != null) {
    304       set.addGroup(filegroup);
    305     }
    306       } while (resultSet.next());
    307     }
    308     catch (SQLException sqlEx) {
    309       System.out.println(sqlEx);
    310       System.exit(1);
    311     }
    312 
    313     return set;
    314   }
    315 
     286    // Get the identifier for this file set, etc.
     287    ResultSet sections = connection.getResultSet();
     288    int fileSetRef;
     289    try {
     290        sections.first();
     291        fileSetRef     = sections.getInt("FileSectionRef");
     292        set.reference = Integer.toString(fileSetRef);
     293    }
     294    catch (SQLException ex){
     295        System.out.println(ex);
     296        return null;
     297    }
     298   
     299    // Get child file groups
     300    select = new GS3SQLSelect("filegroups");
     301    select.addField("*");
     302    whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());   
     303    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     304    whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(fileSetRef),
     305                    GS3SQLField.INTEGER_TYPE);
     306    where.add(whereItem);
     307    whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.SECTION_PARENT);
     308    where.add(whereItem);
     309    select.setWhere(where);
     310    connection.execute(select.toString());
     311   
     312    // start going through the matching file groups
     313    try {
     314        ResultSet resultSet = connection.getResultSet();
     315        resultSet.first();
     316        do {
     317        METSFileGroup filegroup = METSFileGroup.readSQL(document, connection, resultSet);
     318        if (filegroup != null) {
     319            set.addGroup(filegroup);
     320        }
     321        } while (resultSet.next());
     322    }
     323    catch (SQLException sqlEx) {
     324        System.out.println(sqlEx);
     325        System.exit(1);
     326    }
     327   
     328    return set;
     329    }
     330   
    316331}
Note: See TracChangeset for help on using the changeset viewer.