Changeset 5945


Ignore:
Timestamp:
2003-11-24T14:26:53+13:00 (20 years ago)
Author:
cs025
Message:

Extensive additions to metadata

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

Legend:

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

    r5800 r5945  
    88
    99import java.sql.SQLException;
     10import java.sql.ResultSet;
    1011
    1112import org.w3c.dom.Document;
     
    3536
    3637public class METSDescriptive
    37 {   MultiMap map;
    38     String   ID;
    39     String   name;
    40 
    41     public METSDescriptive(String ID, String name)
    42     {   this.map  = new MultiMap();
    43         this.ID   = ID;
    44         this.name = name;
    45     }
    46 
    47     public String getID()
    48     {   return this.ID;
    49     }
    50 
    51     public String getName()
    52     {   return this.name;
    53     }
    54 
    55     public void addNamespace(METSNamespace namespace)
    56     {   this.map.put(namespace.getName(), namespace);
    57     }
    58 
    59     public METSNamespace getNamespace(String namespace)
    60     {   return (METSNamespace) this.map.get(namespace);
    61     }
    62 
    63     /**
    64      *  Get the version of a metadata namespace that can be used for
    65      *  adding new values to the document.  Remember, each namespace may
    66      *  occur more than once, but only one of the occurrences can be 'open'.
    67      *
    68      *  @param <code>String</code> the name of the namespace
    69      */
    70     public METSNamespace getOpenNamespace(String namespace)
    71     {   if (this.map.getAll(namespace) == null)
    72         { return null;
    73         }
    74 
    75         Iterator namespaces = ((List) this.map.getAll(namespace)).iterator();
    76 
    77         while (namespaces.hasNext())
    78         {   METSNamespace namespaceData = (METSNamespace) namespaces.next();
    79 
    80             if (namespaceData.isEditable())
    81             {   return namespaceData;
    82             }
    83         }
    84         return null;
    85     }
    86 
    87     /**
    88      *  Add a piece of metadata to this document.  Existing values are preserved.
    89      *
    90      *  @param <code>String</code> the namespace in which the metadata occurs
    91      *  @param <code>String</code> the label of the metadata
    92 
    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 
    113      *  @param <code>String</code> the value of the metadata
    114      */
    115     public void setMetadata(String namespace, String label, String value)
    116     { METSNamespace namespaceData = this.getOpenNamespace(namespace);
    117 
    118         if (namespaceData == null ||
    119                 namespaceData.isEditable() == false)
    120         {   namespaceData = NamespaceFactory.initNamespace(namespace);
    121             this.addNamespace(namespaceData);
    122         }
    123         namespaceData.setMetadata(label, value);
    124     }
     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  }
    125124
    126125    /**
     
    238237  }
    239238
    240   public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
     239  public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
    241240  { // check if this node is in the
    242     int sqlId = 0;
     241    int sqlId = -1;
    243242
    244243    //    System.out.println("Writing " + connection.toString());
     
    253252
    254253    connection.execute(select.toString());
     254
    255255    try {
    256     if (connection.getResultSet() != null &&
    257         connection.getResultSet().next()) {
    258         // the node already exists - no need to do anything as such
    259     }
    260     else
    261     {
    262         // It is a new node and needs writing
    263         GS3SQLInsert insert = new GS3SQLInsert("metadata");
    264         insert.addValue("DocID", document.getID().toString());
    265         insert.addValue("MetaID", this.ID);
     256      GS3SQLAction action;
     257
     258      ResultSet resultSet = connection.getResultSet();
     259
     260      if (resultSet != null &&
     261      resultSet.first()) {
     262    // the node already exists - no need to do anything as such
     263    GS3SQLUpdate update = new GS3SQLUpdate("metadata");
     264    update.setWhere(where);
     265    action = update;
     266      }
     267      else
     268      { // Set result set to null, just in case next() didn't work above...
     269    resultSet = null;
     270
     271    // It is a new node and needs writing
     272    action = new GS3SQLInsert("metadata");
     273
     274    action.addValue("DocID", document.getID().toString());
     275    action.addValue("MetaID", this.ID);
     276      }
     277   
     278      // always set the group identifier
     279      action.addValue("GroupID", this.name);
    266280       
    267         connection.execute(insert.toString());
    268        
    269         // now execute the select statement again to get the new identifier
    270         // 'MetadataRef'
    271         //      System.out.println(select.toString());
    272         connection.execute(select.toString());
    273     }
    274 
    275     // get the reference for this item...   
    276     connection.getResultSet().first();
    277     sqlId = connection.getResultSet().getInt("MetadataRef");
     281      // execute the action as required
     282      connection.execute(action.toString());
     283
     284      // create a new resultset if necessary
     285      if (resultSet == null) {
     286    // now execute the select statement again to get the new identifier
     287    // 'MetadataRef'
     288    // System.out.println(select.toString());
     289    connection.execute(select.toString());
     290
     291    resultSet = connection.getResultSet();
     292    resultSet.first();
     293      }
     294
     295      // get the reference for this item...
     296      sqlId = resultSet.getInt("MetadataRef");
    278297    }
    279298    catch (SQLException sql)
    280     { System.out.println(sql);
    281     }
     299    { System.out.println(sql);
     300    }
    282301
    283302    Iterator keys = this.map.keySet().iterator();
     
    299318      { METSNamespace namespace = (METSNamespace) namespaces.next();
    300319     
    301         namespace.writeSQL(sqlId, connection);
    302       }
    303     }
     320        if (!namespace.writeSQL(sqlId, connection))
     321        { return false;
     322    }
     323      }
     324    }
     325    return true;
     326  }
     327
     328  public static METSDescriptive readSQL(DocumentInterface document, GS3SQLConnection connection,
     329                    ResultSet resultSet)
     330  {
     331    try {
     332      String ID   = resultSet.getString("MetaID");
     333      String name = resultSet.getString("GroupID");
     334
     335      // create the metadata block object
     336      METSDescriptive descriptive = new METSDescriptive(ID, name);
     337     
     338      // get its metadata reference to retrieve namespaces
     339      int metadataRef = resultSet.getInt("MetadataRef");
     340
     341      // query the database for matching namespaces for this metadata block
     342      GS3SQLSelect select = new GS3SQLSelect("namespaces");
     343      select.addField("*");
     344      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(metadataRef),
     345                              GS3SQLField.INTEGER_TYPE);
     346      GS3SQLWhere where = new GS3SQLWhere(whereItem);
     347      select.setWhere(where);
     348
     349      connection.execute(select.toString());
     350
     351      // parse through the namespaces, calling the namespace class to create instances
     352      // as it will
     353      ResultSet namespaceSet = connection.getResultSet();
     354      if (namespaceSet != null && namespaceSet.first()) {
     355    do {
     356      METSNamespace namespace = METSNamespace.readSQL(connection, namespaceSet);
     357      if (namespace != null) {
     358        descriptive.addNamespace(namespace);
     359      }
     360      else {
     361        System.out.println("Null namespace output");
     362      }
     363    }
     364    while (namespaceSet.next());
     365      }
     366
     367      return descriptive;
     368    }
     369    catch (SQLException sqlEx)
     370    { System.out.println(sqlEx);
     371      System.exit(1);
     372    }
     373    return null;
    304374  }
    305375}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDescriptiveSet.java

    r5800 r5945  
    77import java.util.Map;
    88import java.util.Iterator;
     9
     10import java.sql.SQLException;
     11import java.sql.ResultSet;
    912
    1013import org.w3c.dom.Document;
     
    1821import org.xml.sax.SAXParseException;
    1922
    20 
    2123import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
    2224
    2325import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
     26import org.greenstone.gsdl3.gs3build.database.*;
    2427
    2528public class METSDescriptiveSet
    2629{ Map children;
     30  Map childrenById;
    2731
    2832  public METSDescriptiveSet()
    2933  { this.children = new HashMap();
     34    this.childrenById = new HashMap();
    3035
    3136    METSDescriptive descriptive = new METSDescriptive("DM1", "default");
     
    3540  public void addDescriptive(METSDescriptive metadata)
    3641  { this.children.put(metadata.getName(), metadata);
     42    this.childrenById.put(metadata.getID(), metadata);
    3743  }
    3844
    3945  public METSDescriptive getDescriptive(String name)
    4046  { return (METSDescriptive) this.children.get(name);
     47  }
     48
     49  public METSDescriptive getDescriptiveById(String id)
     50  { return (METSDescriptive) this.childrenById.get(id);
    4151  }
    4252
     
    101111
    102112
    103   public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
     113  public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
    104114  { Iterator groups = this.children.values().iterator();
    105115   
     
    107117    { METSDescriptive group = (METSDescriptive) groups.next();
    108118
    109       group.writeSQL(document, connection);
     119      if (!group.writeSQL(document, connection))
     120      { return false;
     121      }
    110122    }
     123    return true;
    111124  }
    112125
     
    122135    return set;
    123136  }
     137
     138  public static METSDescriptiveSet readSQL(DocumentInterface document, GS3SQLConnection connection)
     139  {
     140    METSDescriptiveSet set = new METSDescriptiveSet();
     141
     142    GS3SQLSelect select = new GS3SQLSelect("metadata");
     143    select.addField("*");
     144    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     145    select.setWhere(new GS3SQLWhere(whereItem));
     146    connection.execute(select.toString());
     147
     148    // start going through the matching metadata blocks
     149    try {
     150      ResultSet resultSet = connection.getResultSet();
     151      resultSet.first();
     152      do {
     153    METSDescriptive descriptive = METSDescriptive.readSQL(document, connection, resultSet);
     154    if (descriptive != null) {
     155      set.addDescriptive(descriptive);
     156    }
     157    else {
     158      System.out.println("Null descriptive");
     159    }
     160      } while (resultSet.next());
     161    }
     162    catch (SQLException sqlEx) {
     163      System.out.println(sqlEx);
     164      System.exit(1);
     165    }
     166
     167    return set;
     168  }
    124169}
    125170
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDivision.java

    r5800 r5945  
    1010
    1111import java.sql.SQLException;
     12import java.sql.ResultSet;
    1213
    1314import org.greenstone.gsdl3.gs3build.util.XMLTools;
     
    1819public class METSDivision
    1920{
    20   String       ID;
     21  String       ID;          // the unique identifier for this division (unique within the
     22                            // document, not necessarily unique within the collection).
    2123  String       order;       // see the METS documentation for the meaning of the different
    2224  String       orderLabel;  // label types.
     
    2729  List         fileRefs;
    2830  Map          children;
     31 
     32  public static final String STRUCTURE_PARENT = "Structure";
     33  public static final String DIVISION_PARENT  = "Division";
    2934
    3035  public METSDivision(String ID, String order, String orderLabel, String userLabel, String type)
     
    4348  }
    4449
    45     /**
    46      *  Add a new file reference to the structure
    47      *
    48      *  @param <code>METSFileRef</code> the file to be referenced.
    49      */
    50     public void addFileReference(METSFileID fileRef)
    51     {   this.fileRefs.add(fileRef);
     50  /**
     51   *  Find all the divisions that match a given list of file group
     52   *  identifiers...
     53   *
     54   *  @param <code>List</code> the list of identifiers to find
     55   *  @param <code>List</code> the list into which to place any
     56   *         matching divisions.
     57   */
     58  public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList)
     59  { Iterator iterator = this.fileRefs.iterator();
     60   
     61    // get the references on this division, and mark if necessary
     62    while (iterator.hasNext())
     63    { String reference = iterator.next().toString();
     64
     65      Iterator fileIterator = listOfFileIdentifiers.iterator();
     66      while (fileIterator.hasNext())
     67      { String fileRef = fileIterator.next().toString();
     68     
     69        if (fileRef.equals(reference))
     70    { resultList.add(this);
    5271    }
    53 
    54     /**
    55      *  Add a new metadata reference to the structure
    56      */
    57     public void addMetadataReference(String metadataRef)
    58     { // TODO: correct the type
    59       this.metadataRefs.add(metadataRef);
    60     }
    61 
    62     /**
    63      *  Add a child division, or sub-division to the document.  For example,
    64      *  sections within a chapter.
    65      *
    66      *  @param <code>METSDivision</code> the child division.
    67      */
    68     public void addSubDivision(METSDivision child)
    69     {   this.children.put(child.getID(), child);
     72      }
     73    }
     74   
     75    // get the child groups, and check each in turn...
     76    Iterator groups = this.children.values().iterator();
     77    while (groups.hasNext())
     78    { METSDivision group = (METSDivision) groups.next();
     79
     80      group.findDivisionsForFiles(listOfFileIdentifiers, resultList);
     81    }   
     82  }
     83
     84  /**
     85   *  Get the namespace from the given set of descriptive blocks...
     86   */
     87  public METSNamespace findNamespace(String namespaceName, boolean isOpenNamespace,
     88                     METSDescriptiveSet descriptiveSet)
     89  { METSNamespace namespace = null;
     90
     91    // Special case - no existing metadata references
     92    if (this.metadataRefs.size() == 0) {
     93      // Create new Descriptive and add it to the descriptive set
     94      METSDescriptive descriptive = new METSDescriptive("DM"+this.ID, this.orderLabel);
     95      descriptiveSet.addDescriptive(descriptive);
     96
     97      // Create the namespace and add it to the Descriptive
     98      namespace = NamespaceFactory.initNamespace(namespaceName);
     99      if (namespace != null)
     100      { descriptive.addNamespace(namespace);
     101      }
     102
     103      return namespace;
     104    }
     105
     106    Iterator descriptiveIterator = this.metadataRefs.iterator();
     107
     108    while (descriptiveIterator.hasNext())
     109    { String descriptiveRef = descriptiveIterator.next().toString();
     110
     111      METSDescriptive descriptive = descriptiveSet.getDescriptiveById(descriptiveRef);
     112
     113      if (isOpenNamespace) {
     114    namespace = descriptive.getOpenNamespace(namespaceName);
     115      }
     116      else {
     117    namespace = descriptive.getNamespace(namespaceName);
     118      }
     119   
     120      if (namespace != null)
     121    break;
     122    }
     123
     124    // Special case - no such namespace in any descriptive list...
     125    if (namespace == null)
     126    { // take the first descriptive area...
     127      METSDescriptive descriptive = descriptiveSet.getDescriptiveById(this.metadataRefs.get(0).toString());
     128
     129      // create the namespace, and add it to the METSDescriptive
     130      namespace = NamespaceFactory.initNamespace(namespaceName);
     131      if (namespace != null)
     132      { descriptive.addNamespace(namespace);
     133      }
     134    }
     135
     136    return namespace;
     137  }
     138
     139  /**
     140   *  Add a new file reference to the structure
     141   *
     142   *  @param <code>METSFileRef</code> the file to be referenced.
     143   */
     144  public void addFileReference(METSFileID fileRef)
     145  { this.fileRefs.add(fileRef);
     146  }
     147
     148  /**
     149   *  Add a new metadata reference to the structure
     150   */
     151  public void addMetadataReference(String metadataRef)
     152  { // TODO: correct the type
     153    this.metadataRefs.add(metadataRef);
     154  }
     155
     156  /**
     157   *  Add a child division, or sub-division to the document.  For example,
     158   *  sections within a chapter.
     159   *
     160   *  @param <code>METSDivision</code> the child division.
     161   */
     162  public void addSubDivision(METSDivision child)
     163  { this.children.put(child.getID(), child);
     164  }
     165
     166  /**
     167   *  Add a child division to a descendent of this division.
     168   *
     169   *  @param <code>String</code> the path to the division into which the
     170   *         subdivision will be added.
     171   *  @param <code>METSDivision</code> the division to be added
     172   */
     173  public void addSubDivision(String divisionID, METSDivision division)
     174  { String topDivision = METSDivision.getTopDivisionName(divisionID);
     175
     176    METSDivision child = (METSDivision) this.children.get(topDivision);
     177    if (child == null)
     178    { return;
     179    }
     180
     181    String subDivisionID = METSDivision.getSubDivisionName(divisionID);
     182    if (subDivisionID == null)
     183    { child.addSubDivision(division);
     184    }
     185    else
     186    { child.addSubDivision(subDivisionID, division);
     187    }
     188  }
     189
     190  public METSDivision getDivision(String divisionID)
     191  { String topDivision = METSDivision.getTopDivisionName(divisionID);
     192
     193    METSDivision child = (METSDivision) this.children.get(topDivision);
     194    if (child == null)
     195    { return null;
     196    }
     197
     198    String subDivision = METSDivision.getSubDivisionName(divisionID);
     199    if (subDivision != null)
     200    { child = child.getDivision(subDivision);
     201    }
     202   
     203    return child;
     204  }
     205
     206  public static String getSubDivisionName(String divisionName)
     207  { int at = divisionName.indexOf('.');
     208
     209    if (at >= 0)
     210    { return divisionName.substring(at+1);
     211    }
     212    else
     213    { return null;
     214    }
     215  }
     216
     217  public static String getTopDivisionName(String divisionName)
     218  { int at = divisionName.indexOf('.');
     219
     220    if (at >= 0)
     221    { return divisionName.substring(0, at);
     222    }
     223    else
     224    {   return divisionName;
     225    }
     226  }
     227
     228  /**
     229   *  Write the METS file in an XML to a text-output sink
     230   *
     231   *  @param <code>PrintWriter</code> the destination of the output
     232   */
     233  public void write(PrintWriter writer)
     234  { String tag = XMLTools.getOpenTag("mets", "div");
     235    tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
     236    tag = XMLTools.addAttribute(tag, "TYPE", this.type);
     237    tag = XMLTools.addAttribute(tag, "ORDER", this.order);
     238    if (this.orderLabel != null)
     239    { tag = XMLTools.addAttribute(tag, "ORDERLABEL", this.orderLabel);
     240    }
     241    if (this.userLabel != null)
     242    { tag = XMLTools.addAttribute(tag, "LABEL", this.userLabel);
     243    }
     244    writer.println(tag);
     245
     246    // write the list of file pointers for this structural element
     247    if (this.fileRefs.size() > 0)
     248    { tag = XMLTools.getOpenTag("mets", "fptr");
     249     
     250      String fileList = this.writeList(this.fileRefs);
     251
     252      tag = XMLTools.addAttribute(tag, "FILEID", fileList);
     253     
     254      tag = XMLTools.makeSingleton(tag);
     255      writer.println(tag);
     256    }
     257
     258    // ..and then the metadata - this is the same code at present,
     259    // but is replicated here in case of future amendment.
     260    if (this.metadataRefs.size() > 0)
     261    { tag = XMLTools.getOpenTag("mets", "mptr");
     262
     263      String metaList = this.writeList(this.metadataRefs);
     264      tag = XMLTools.addAttribute(tag, "METAID", metaList);
     265      tag = XMLTools.makeSingleton(tag);
     266      writer.println(tag);
     267    }
     268     
     269    // close the div element
     270    writer.println(XMLTools.getCloseTag("mets", "div"));
     271  }
     272
     273  private String writeList(List list)
     274  { StringBuffer listString = new StringBuffer();
     275     
     276    Iterator iterator = list.iterator();
     277    while (iterator.hasNext())
     278    { String item = iterator.next().toString();
     279      if (listString.length() > 0)
     280      { listString.append(" ");
     281      }
     282      listString.append(item);
     283    }
     284    return listString.toString();
     285  }
     286
     287  public boolean writeSQL(int parentRef, boolean parentIsStructure, GS3SQLConnection connection)
     288  { int sqlRef = -1;
     289
     290    // set the "where" to find the reference for this division
     291    GS3SQLSelect select = new GS3SQLSelect("divisions");
     292    select.addField("divisionRef");
     293    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(parentRef), GS3SQLField.INTEGER_TYPE);
     294    GS3SQLWhereItem parentItem = new GS3SQLWhereItem("ParentType", "=", parentIsStructure ? "Structure" : "Division");
     295    GS3SQLWhereItem item = new GS3SQLWhereItem("SectionID", "=", this.ID.toString());
     296    GS3SQLWhere where = new GS3SQLWhere(whereItem);   
     297    where.add(parentItem);
     298    where.add(item);
     299    select.setWhere(where);
     300
     301    connection.execute(select.toString());
     302
     303    // Do the actual writing
     304    GS3SQLAction action;
     305
     306    ResultSet resultSet = connection.getResultSet();
     307    try {
     308      if (resultSet == null ||
     309      !resultSet.first())
     310      { resultSet = null;
     311
     312        GS3SQLInsert insert = new GS3SQLInsert("divisions");
     313    insert.addValue("SectionID", this.ID.toString());
     314    insert.addValue("ParentRef", Integer.toString(parentRef));
     315    insert.addValue("ParentType", parentIsStructure == true ? STRUCTURE_PARENT : DIVISION_PARENT);
     316   
     317    action = insert;
     318      }
     319      else {
     320    sqlRef = resultSet.getInt("divisionRef");
     321   
     322    GS3SQLUpdate update = new GS3SQLUpdate("divisions");
     323    GS3SQLWhere  updateWhere =
     324      new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(sqlRef),
     325                          GS3SQLField.INTEGER_TYPE));
     326    update.setWhere(updateWhere);
     327    action = update;
     328      }
     329    }
     330    catch (SQLException sql) {
     331      System.err.println(sql);
     332      return false;
     333    }
     334     
     335    action.addValue("Type", this.type.toString());
     336    action.addValue("LabelOrder", this.order);
     337    action.addValue("ShortLabel", this.orderLabel);
     338    action.addValue("UserLabel", this.userLabel);
     339
     340    if (!connection.execute(action.toString()))
     341    { return false;
     342    }
     343
     344    // if doing a fresh item, get the new structure reference...
     345    if (resultSet == null)
     346    { // get the new structure reference
     347      connection.execute(select.toString());
     348
     349      // get the sql reference for the division
     350      try {
     351    // read in the structure reference
     352    resultSet = connection.getResultSet();
     353    if (resultSet == null) {
     354      return false;
    70355    }
    71 
    72     /**
    73      *  Add a child division to a descendent of this division.
    74      *
    75      *  @param <code>String</code> the path to the division into which the
    76      *         subdivision will be added.
    77      *  @param <code>METSDivision</code> the division to be added
    78      */
    79     public void addSubDivision(String divisionID, METSDivision division)
    80     {   String topDivision = METSDivision.getTopDivisionName(divisionID);
    81 
    82         METSDivision child = (METSDivision) this.children.get(topDivision);
    83         if (child == null)
    84         {   return;
    85         }
    86 
    87         String subDivisionID = METSDivision.getSubDivisionName(divisionID);
    88         if (subDivisionID == null)
    89         { child.addSubDivision(division);
    90         }
    91         else
    92         {   child.addSubDivision(subDivisionID, division);
    93         }
     356    resultSet.first();
     357    sqlRef = resultSet.getInt("divisionRef");
     358    resultSet.close();
     359    resultSet = null;
     360      }
     361      catch (SQLException sqlex) {
     362    System.err.println("Unable to retrieve reference for Division " + sqlex);
     363    return false;
     364      }
     365    }
     366    // close the open resultSet, as we don't need it any longer...
     367    else {
     368      try {
     369    resultSet.close();
     370      }
     371      catch (SQLException sql) {
     372    System.err.println(sql);
     373      }
     374    }
     375
     376    // delete the old file/metadata references
     377    GS3SQLWhere referenceWhere =
     378      new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(sqlRef),
     379                      GS3SQLField.INTEGER_TYPE));
     380
     381    GS3SQLDelete delete = new GS3SQLDelete("divisionfilerefs");
     382    delete.setWhere(referenceWhere);
     383    connection.execute(delete.toString());
     384
     385    delete = new GS3SQLDelete("divisionmetarefs");
     386    delete.setWhere(referenceWhere);
     387    connection.execute(delete.toString());
     388
     389    // write the new file references
     390    if (this.fileRefs.size() > 0)
     391    { Iterator iterator = this.fileRefs.iterator();
     392   
     393      while (iterator.hasNext())
     394      { GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs");
     395        fileinsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
     396    fileinsert.addValue("Type", "Group");
     397    fileinsert.addValue("fileRef", iterator.next().toString());
     398    connection.execute(fileinsert.toString());
     399      }
     400    }
     401
     402    // write the metadata references
     403    if (this.metadataRefs.size() > 0)
     404    { Iterator iterator = this.metadataRefs.iterator();
     405   
     406      while (iterator.hasNext())
     407      { GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs");
     408        metainsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
     409    metainsert.addValue("Type", "Group");
     410    metainsert.addValue("metadataRef", iterator.next().toString());
     411    connection.execute(metainsert.toString());
     412      }
     413    }
     414
     415    // write out any children in turn
     416    Iterator groups = this.children.values().iterator();
     417
     418    while (groups.hasNext())
     419    { METSDivision group = (METSDivision) groups.next();
     420   
     421      if (!group.writeSQL(sqlRef, false, connection)) {
     422    return false;
     423      }
     424    }
     425    return true;
     426  }
     427
     428  public static METSDivision readSQL(GS3SQLConnection connection, ResultSet resultSet)
     429  {
     430    METSLocation metsLocation = null;
     431   
     432    try {
     433      // read the basic information
     434      String ID   = resultSet.getString("SectionID");
     435      String type = resultSet.getString("Type");
     436      String order = resultSet.getString("LabelOrder");
     437      String orderLabel = resultSet.getString("ShortLabel");
     438      String userLabel  = resultSet.getString("UserLabel");
     439     
     440      // construct the division object
     441      METSDivision division = new METSDivision(ID, order, orderLabel, userLabel, type);
     442     
     443      // obtain a list of child divisions
     444      int divisionRef = resultSet.getInt("DivisionRef");
     445
     446      GS3SQLSelect select = new GS3SQLSelect("divisions");
     447      select.addField("*");
     448      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(divisionRef),
     449                              GS3SQLField.INTEGER_TYPE);
     450      GS3SQLWhere where = new GS3SQLWhere(whereItem);
     451      whereItem = new GS3SQLWhereItem("ParentType", "=", DIVISION_PARENT);
     452      where.add(whereItem);
     453      select.setWhere(where);
     454
     455      connection.execute(select.toString());
     456     
     457      // circulate through to obtain further children
     458      ResultSet childSet = connection.getResultSet();
     459      if (childSet.first())
     460      {
     461    do {
     462      METSDivision childDivision = METSDivision.readSQL(connection, childSet);
     463      division.addSubDivision(childDivision);
    94464    }
    95 
    96     public METSDivision getDivision(String divisionID)
    97     {   String topDivision = METSDivision.getTopDivisionName(divisionID);
    98 
    99         METSDivision child = (METSDivision) this.children.get(topDivision);
    100         if (child == null)
    101         {   return null;
    102         }
    103 
    104         String subDivision = METSDivision.getSubDivisionName(divisionID);
    105         if (subDivision != null)
    106         {   child = child.getDivision(subDivision);
    107         }
    108 
    109         return child;
     465    while (childSet.next());
     466      }
     467
     468      select = new GS3SQLSelect("divisionfilerefs");
     469      select.addField("*");
     470      where = new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(divisionRef),
     471                          GS3SQLField.INTEGER_TYPE));
     472      select.setWhere(where);
     473
     474      connection.execute(select.toString());
     475
     476      ResultSet fileSet = connection.getResultSet();
     477      if (fileSet != null && fileSet.first())
     478      { do
     479    { String reference = fileSet.getString("fileRef");
     480      division.fileRefs.add(reference);
    110481    }
    111 
    112     public static String getSubDivisionName(String divisionName)
    113     {   int at = divisionName.indexOf('.');
    114 
    115         if (at >= 0)
    116         {   return divisionName.substring(at+1);
    117         }
    118         else
    119         {   return null;
    120         }
     482    while (fileSet.next()); 
     483      }
     484
     485      select = new GS3SQLSelect("divisionmetarefs");
     486      select.addField("*");
     487      where = new GS3SQLWhere(new GS3SQLWhereItem("divisionRef", "=", Integer.toString(divisionRef),
     488                          GS3SQLField.INTEGER_TYPE));
     489      select.setWhere(where);
     490
     491      connection.execute(select.toString());
     492
     493      ResultSet metaSet = connection.getResultSet();
     494      if (metaSet != null && metaSet.first())
     495      { do
     496    { String reference = metaSet.getString("metadataRef");
     497      division.metadataRefs.add(reference);
    121498    }
    122 
    123     public static String getTopDivisionName(String divisionName)
    124     {   int at = divisionName.indexOf('.');
    125 
    126         if (at >= 0)
    127         {   return divisionName.substring(0, at);
    128         }
    129         else
    130         {   return divisionName;
    131         }
    132     }
    133 
    134     /**
    135      *  Write the METS file in an XML to a text-output sink
    136      *
    137      *  @param <code>PrintWriter</code> the destination of the output
    138      */
    139     public void write(PrintWriter writer)
    140     { String tag = XMLTools.getOpenTag("mets", "div");
    141       tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
    142       tag = XMLTools.addAttribute(tag, "TYPE", this.type);
    143       tag = XMLTools.addAttribute(tag, "ORDER", this.order);
    144       if (this.orderLabel != null)
    145       { tag = XMLTools.addAttribute(tag, "ORDERLABEL", this.orderLabel);
    146       }
    147       if (this.userLabel != null)
    148       { tag = XMLTools.addAttribute(tag, "LABEL", this.userLabel);
    149       }
    150       writer.println(tag);
    151 
    152       // write the list of file pointers for this structural element
    153       if (this.fileRefs.size() > 0)
    154       { tag = XMLTools.getOpenTag("mets", "fptr");
    155      
    156         String fileList = this.writeList(this.fileRefs);
    157 
    158     tag = XMLTools.addAttribute(tag, "FILEID", fileList);
    159 
    160     tag = XMLTools.makeSingleton(tag);
    161     writer.println(tag);
    162       }
    163 
    164       // ..and then the metadata - this is the same code at present,
    165       // but is replicated here in case of future amendment.
    166       if (this.metadataRefs.size() > 0)
    167       { tag = XMLTools.getOpenTag("mets", "mptr");
    168 
    169         String metaList = this.writeList(this.metadataRefs);
    170         tag = XMLTools.addAttribute(tag, "METAID", metaList);
    171     tag = XMLTools.makeSingleton(tag);
    172     writer.println(tag);
    173       }
    174      
    175       // close the div element
    176       writer.println(XMLTools.getCloseTag("mets", "div"));
    177     }
    178 
    179     private String writeList(List list)
    180     { StringBuffer listString = new StringBuffer();
    181      
    182       Iterator iterator = list.iterator();
    183       while (iterator.hasNext())
    184       { String item = iterator.next().toString();
    185     if (listString.length() > 0)
    186     { listString.append(" ");
    187     }
    188     listString.append(item);
    189       }
    190       return listString.toString();
    191     }
    192 
    193     public void writeSQL(int parentRef, boolean parentIsStructure, GS3SQLConnection connection)
    194     { Iterator groups = this.children.values().iterator();
    195 
    196       GS3SQLInsert insert = new GS3SQLInsert("divisions");
    197       insert.addValue("SectionID", this.ID.toString());
    198       insert.addValue("Type", this.type.toString());
    199       insert.addValue("LabelOrder", this.order);
    200       insert.addValue("ShortLabel", this.orderLabel);
    201       insert.addValue("UserLabel", this.userLabel);
    202       insert.addValue("ParentRef", Integer.toString(parentRef));
    203       insert.addValue("ParentType", parentIsStructure == true ? "Structure" : "Division");
    204      
    205       System.out.println(insert.toString());
    206       connection.execute(insert.toString());
    207 
    208       // TODO: write metadata references
    209       // TODO: write file references
    210      
    211       // get the new structure reference
    212       GS3SQLSelect select = new GS3SQLSelect("divisions");
    213       select.addField("divisionRef");
    214       GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(parentRef), GS3SQLField.INTEGER_TYPE);
    215       GS3SQLWhereItem parentItem = new GS3SQLWhereItem("ParentType", "=", parentIsStructure ? "Structure" : "Division");
    216       GS3SQLWhereItem item = new GS3SQLWhereItem("SectionID", "=", this.ID.toString());
    217       GS3SQLWhere where = new GS3SQLWhere(whereItem);
    218       where.add(parentItem);
    219       where.add(item);
    220       select.setWhere(where);
    221       connection.execute(select.toString());
    222 
    223       // get the sql reference for the division
    224       int sqlRef;
    225 
    226       try {
    227         // read in the structure reference
    228     connection.getResultSet().first();
    229     sqlRef = connection.getResultSet().getInt("divisionRef");
    230       }
    231       catch (SQLException sqlex) {
    232       System.out.println("Unable to retrieve reference for Division " + sqlex);
    233       return;
     499    while (metaSet.next()); 
    234500      }
    235501       
    236       // write the file references
    237       if (this.fileRefs.size() > 0)
    238       { Iterator iterator = this.fileRefs.iterator();
    239    
    240         while (iterator.hasNext())
    241     { GS3SQLInsert fileinsert = new GS3SQLInsert("divisionfilerefs");
    242       fileinsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
    243       fileinsert.addValue("Type", "Group");
    244       fileinsert.addValue("fileRef", iterator.next().toString());
    245       connection.execute(fileinsert.toString());
    246     }
    247       }
    248        
    249       // write the metadata references
    250       if (this.metadataRefs.size() > 0)
    251       { Iterator iterator = this.metadataRefs.iterator();
    252    
    253         while (iterator.hasNext())
    254     { GS3SQLInsert metainsert = new GS3SQLInsert("divisionmetarefs");
    255       metainsert.addValue("divisionRef", Integer.toString(sqlRef), GS3SQLField.INTEGER_TYPE);
    256       metainsert.addValue("Type", "Group");
    257       metainsert.addValue("metadataRef", iterator.next().toString());
    258       connection.execute(metainsert.toString());
    259     }
    260       }
    261 
    262       // write out any children in turn
    263       while (groups.hasNext())
    264       { METSDivision group = (METSDivision) groups.next();
    265 
    266         group.writeSQL(sqlRef, false, connection);
    267       }
    268     }
    269 
     502      return division;
     503    }
     504    catch (SQLException sqlEx)
     505    { System.out.println(sqlEx);
     506    }   
     507    return null;
     508  }
    270509}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFile.java

    r5800 r5945  
    88import java.net.URL;
    99import java.net.URLConnection;
     10
     11import java.sql.ResultSet;
     12import java.sql.SQLException;
    1013
    1114import org.w3c.dom.Document;
     
    2932public class METSFile
    3033{
    31     METSFileID    id;
    32     METSFilePos   location;
    33     String        MIMEType;
    34     int           sequenceNo;
    35     long          size;
    36     METSDate      created;
    37     long          checkSum;
    38     List          adminRefs;
    39     List          describeRefs;
    40     METSFileGroup group;
    41 
    42     /**
    43      *  Create a new METSFile object, with the given properties.  The remaining
    44      *  fields are filled in with default values.
    45      *
    46      *  @param <code>String</code> the id of the file
    47      *  @param <code>METSFilePos</code> the location of the file
    48      *  @param <code>String</code> the MIME type of the file
    49      */
    50     public METSFile(METSFileID id, METSFilePos location, String mimeType)
    51     {   this.id           = id;        // required
    52         this.location     = location;  // the location
    53         this.MIMEType     = mimeType;  // the MIME type;
    54         this.sequenceNo   = -1;       // -1 indicates unset
    55         this.size         = -1;        // -1 indicates unset
    56         this.checkSum     = -1;        // -1 indicates unset
    57         this.created      = null;
    58         this.adminRefs    = new ArrayList();
    59         this.describeRefs = new ArrayList();
    60         this.group        = null;
     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  }
     137       
     138  public static METSFile makeMETSFile(URL url)
     139  { String mimeType;
     140 
     141    if (url.toString().startsWith("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("ID","=", 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("ID", 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("ID");
     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  public static METSFile parseXML(Element element, METSFileGroup parentGroup)
     251  { METSFile file = null;
     252    NodeList children = element.getChildNodes();
     253       
     254    for (int c = 0; c < children.getLength(); c ++)
     255    { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
     256        continue;
     257      }
     258   
     259      Element childElement = (Element) children.item(c);
     260      if (childElement.getNodeName().equals("mets:FLocat"))
     261      { METSFilePos filePos;
     262   
     263        // get most of the information from the child FLocat node
     264        String locationType = childElement.getAttribute("LOCTYPE");
     265    String href = childElement.getAttribute("xlink:href");
     266    String id   = childElement.getAttribute("ID");
     267     
     268    // some more data from the parent node
     269    String mimeType = element.getAttribute("MIMETYPE");
     270
     271    try
     272    { filePos = new METSFilePos(href, locationType);
    61273    }
    62 
    63     /**
    64      *  Set the parent group
    65      *
    66      *  @param <code>METSFileGroup</code> the new holding file group
    67      */
    68     public void setGroup(METSFileGroup group)
    69     {   this.group = group;
     274    catch (java.net.MalformedURLException ex)
     275    { // TODO: raise error
     276      continue;
    70277    }
    71 
    72     /**
    73      *  Get the parent group of this file
    74      *
    75      *  @return <code>METSFileGroup</code> the holding file group
    76      */
    77     public METSFileGroup getGroup()
    78     {   return this.group;
    79     }
    80 
    81     /**
    82      *  Get the identifier of this file
    83      *
    84      *  @return <code>METSFileID</code> the file identifier
    85      */
    86     public METSFileID getID()
    87     {   return this.id;
    88     }
    89 
    90     /**
    91      *  Set the identifier of this file
    92      *
    93      *  @param <code>METSFileID</code> the file identifier
    94      */
    95     public void setID(METSFileID id)
    96     { this.id = id;
    97     }
    98 
    99     /**
    100      *  @return <code>String</code> the MIME (content) type of the file
    101      */
    102     public String getMIMEType()
    103     {   return this.MIMEType;
    104     }
    105 
    106     /**
    107      *  @return <code>URL</code> the location of the file.  This will often be in
    108      *          "file://" form.
    109      */
    110     public URL getLocation()
    111     {   return this.location.getLocation();
    112     }
    113 
    114     /**
    115      *  Take a url and make a METSFile object out of it - works out
    116      *  details such as the Mime type, identifiers and other necessary
    117      *  information to call the constructor for METSFile
    118      *
    119      *  @param <code>URL</code> the url of the object - this may be a
    120      *                          reference to a file on the local filestore
    121      *
    122      *  @return <code>METSFile</code> the METS file object for the corresponding
    123      *                                document component.
    124      */
    125     public static METSFile makeMETSFile(URL url, String mimeType)
    126     {   
    127         METSFile reply = new METSFile(new METSFileID(), new METSFilePos(url), mimeType);
    128         return reply;
    129     }
    130        
    131     public static METSFile makeMETSFile(URL url)
    132     { String mimeType;
    133 
    134       if (url.toString().startsWith("file://"))
    135         { // TODO: Work out the MIME type
    136             mimeType = URLConnection.getFileNameMap().getContentTypeFor(url.toString().substring(7));
    137             if (mimeType == null)
    138             { mimeType = "text/plain";
    139             }
    140         }
    141         else
    142         { // TODO: look up the MIME type through the pertinent connection
    143             mimeType = HTTPTools.getMIMEType(url);
    144         }
    145         // TODO: make the identifier...
    146 
    147         return makeMETSFile(url, mimeType);
    148     }
    149 
    150     /**
    151      *  Write the METS file in an XML to a text-output sink
    152      *
    153      *  @param <code>PrintWriter</code> the destination of the output
    154      */
    155     public void write(PrintWriter writer)
    156     { String tag = XMLTools.getOpenTag("mets", "file");
    157         tag = XMLTools.addAttribute(tag, "MIMETYPE", this.MIMEType);
    158         tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
    159         writer.println(tag);
    160 
    161         tag = XMLTools.getOpenTag("mets", "FLocat");
    162         tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
    163         tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
    164         tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
    165         tag = XMLTools.makeSingleton(tag);
    166         writer.println(tag);
    167 
    168         writer.println(XMLTools.getCloseTag("mets", "file"));
    169     }
    170 
    171    
    172     public void writeSQL(GS3SQLConnection connection)
    173     { // check if this node is in the
    174     GS3SQLInsert insert = new GS3SQLInsert("files");
    175 
    176     insert.addValue("FileLocType", this.location.getType());
    177     insert.addValue("FileLocation", this.location.getLocation().toString());
    178     insert.addValue("MIMEType", this.MIMEType);
    179     insert.addValue("ID", this.id.toString());
    180 
    181     connection.execute(insert.toString()); 
    182     }
    183    
    184     /**
    185      *  Parse an XML Element as a METS File
    186      */
    187     public static METSFile parseXML(Element element, METSFileGroup parentGroup)
    188     {   METSFile file = null;
    189         NodeList children = element.getChildNodes();
    190        
    191         for (int c = 0; c < children.getLength(); c ++)
    192         { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
    193                 continue;
    194             }
    195 
    196             Element childElement = (Element) children.item(c);
    197             if (childElement.getNodeName().equals("mets:FLocat"))
    198             {   METSFilePos filePos;
    199 
    200                 // get most of the information from the child FLocat node
    201                 String locationType = childElement.getAttribute("LOCTYPE");
    202                 String href = childElement.getAttribute("xlink:href");
    203                 String id   = childElement.getAttribute("ID");
    204 
    205                 // some more data from the parent node
    206                 String mimeType = element.getAttribute("MIMETYPE");
    207 
    208                 try
    209                 { filePos = new METSFilePos(href, locationType);
    210                 }
    211                 catch (java.net.MalformedURLException ex)
    212                 { // TODO: raise error
    213                     continue;
    214                 }
    215                 file = new METSFile(new METSFileID(id), filePos, mimeType);
    216             }
    217             else if (childElement.getNodeName().equals("mets:FContent"))
    218             {
    219             }
    220             else
    221             {   // TODO: raise an error!
    222             }
    223         }
    224         return file;
    225     }
    226 
    227 
    228     /**
    229      *  Overridden <code>toString</code> for convenience - returns the location of the file.
    230      *
    231      *  @return <code>String</code> the location of the file as a string
    232      */
    233     public String toString()
    234     {   return this.location.toString();
    235     }
     278    file = new METSFile(new METSFileID(id), filePos, mimeType);
     279      }
     280      else if (childElement.getNodeName().equals("mets:FContent"))
     281      {
     282      }
     283      else
     284      { // TODO: raise an error!
     285      }
     286    }
     287    return file;
     288  }
     289
     290
     291  /**
     292   *  Overridden <code>toString</code> for convenience - returns the location of the file.
     293   *
     294   *  @return <code>String</code> the location of the file as a string
     295   */
     296  public String toString()
     297  { return this.location.toString();
     298  }
    236299}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileGroup.java

    r5800 r5945  
    55import java.util.Iterator;
    66
     7import java.net.URL;
     8
     9import java.sql.SQLException;
     10import java.sql.ResultSet;
     11
    712import java.io.PrintWriter;
    813
     
    1318import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
    1419
     20/**
     21 *  Child groups are indicated by colons.  E.g. Chapter1:Section1 would indicate
     22 *  the group "Section1" inside the "Chapter1" group...
     23 */
    1524
    1625public class METSFileGroup
     
    1928  String id;
    2029
     30  public static final String SECTION_PARENT = "Section";
     31  public static final String GROUP_PARENT = "Group";
     32
    2133  public METSFileGroup(String id)
    2234  { this.children = new ArrayList();
     
    2537  }
    2638
    27     /**
    28      *  Add a filegroup to this group of files.
    29      *
    30      *  @param <code>METSFileGroup</code> the filegroup.
    31      */
    32     public void addGroup(METSFileGroup group)
    33     {   this.childGroups.add(group);
    34     }
    35 
    36     /**
    37      *  Add a file to this group of files.
    38      *
    39      *  @param <code>METSFile</code> the file in a METS File object.
    40      */
    41     public void addFile(METSFile file)
    42     {   this.children.add(file);
    43         file.setGroup(this);
    44         if (!file.getID().isDefined())
    45         { file.setID(new METSFileID(this.id + Integer.toString(this.children.size())));
    46         }
    47     }
    48 
    49     /**
    50      *  Get the nth child of the group
    51      *
    52      *  @param <code>int</code> the index into the group
    53      *  @return <code>METSFile</code> the corresponding child.  This may be <code>null</code>
    54      *          particularly if the index given falls outside of the valid range of child
    55      *          indexes.
    56      */
    57     public METSFile getFile(int index)
    58     { if (index < 0 || index >= this.children.size())
    59         {   return null;
    60         }
    61 
    62         return (METSFile) this.children.get(index);
    63     }
    64 
    65     /**
    66      *  Get all the files in the group as a <code>List</code>
    67      */
    68     public List getFiles()
    69     {   return this.children;
    70     }
    71 
    72     /**
    73      *  Get all the subgroups as a <code>List</code>
    74      */
    75     public List getSubgroups()
    76     {   return this.childGroups;
    77     }
    78 
    79     /**
    80      *  Get the number of files in the group
    81      *
    82      *  @return <code>int</code> the count
    83      */
    84     public int noOfFiles()
    85     {   return this.children.size();
    86     }
    87 
    88     /**
    89      *  Get the number of subgroups.
    90      */
    91     public int noOfSubgroups()
    92     {   return this.childGroups.size();
    93     }
    94 
    95     /**
    96      *  @return <code>String</code> the name of this group
    97      */
    98     public String getName()
    99     {   return this.id;
    100     }
     39  /**
     40   *  Find all the occurrences of a given file, and place them
     41   *  in a <code>List</code>.
     42   */
     43  public void findGroups(URL findFile, List resultList)
     44  {
     45    // find in this particular group
     46    Iterator childIter = children.iterator();
     47    while (childIter.hasNext())
     48    { METSFile file = (METSFile) childIter.next();
     49      if (file.equals(findFile))
     50      { resultList.add(this.id);
     51      }
     52    }
     53
     54    // iterate over the child groups
     55    childIter = childGroups.iterator();
     56    while (childIter.hasNext())
     57    { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
     58
     59      fileGroup.findGroups(findFile, resultList);
     60    }
     61  }
     62
     63  /**
     64   *  Get a sub group of this METSFileGroup.
     65   *
     66   *  @param <code>String</code> the name of the subgroup
     67   *  @return <code>METSFileGroup</code> the child group, which will
     68   *          be <code>null</code> if the group is not known.
     69   */
     70  public METSFileGroup getSubgroup(String id)
     71  { String childId;
     72 
     73    int dotAt = id.indexOf(':');
     74    if (dotAt == -1)
     75    { childId = null;
     76    }
     77    else
     78    { childId = id.substring(dotAt+1);
     79      id = id.substring(0, dotAt);
     80    }
     81   
     82    // iterate over the child groups
     83    Iterator childIter = childGroups.iterator();
     84    while (childIter.hasNext())
     85    { METSFileGroup group = (METSFileGroup) childIter.next();
     86   
     87      if (group.id.equals(id)) {
     88    if (childId == null) {
     89      return group;
     90    }
     91    else {
     92      return group.getSubgroup(childId);
     93    }
     94      }
     95    }
     96    return null;
     97  }
     98
     99  /**
     100   *  Add a filegroup to this group of files.
     101   *
     102   *    @param <code>METSFileGroup</code> the filegroup.
     103   */
     104  public void addGroup(METSFileGroup group)
     105  { this.childGroups.add(group);
     106  }
     107
     108  /**
     109   *  Add a file to this group of files.
     110   *
     111   *    @param <code>METSFile</code> the file in a METS File object.
     112   */
     113  public void addFile(METSFile file)
     114  { this.children.add(file);
     115    file.setGroup(this);
     116    if (!file.getID().isDefined())
     117    { file.setID(new METSFileID(this.id + Integer.toString(this.children.size())));
     118    }
     119  }
     120
     121  /**
     122   *  Get the nth child of the group
     123   *
     124   *  @param <code>int</code> the index into the group
     125   *  @return <code>METSFile</code> the corresponding child.  This may be <code>null</code>
     126   *          particularly if the index given falls outside of the valid range of child
     127   *          indexes.
     128   */
     129  public METSFile getFile(int index)
     130  { if (index < 0 || index >= this.children.size())
     131    { return null;
     132    }
     133 
     134    return (METSFile) this.children.get(index);
     135  }
     136
     137  /**
     138   *  Get all the files in the group as a <code>List</code>
     139   *
     140   *  @return <code>List</code> the file childen of the group.
     141   */
     142  public List getFiles()
     143  { return this.children;
     144  }
     145
     146  /**
     147   *  Get all the subgroups as a <code>List</code>
     148   */
     149  public List getSubgroups()
     150  { return this.childGroups;
     151  }
     152
     153  /**
     154   *  Get the number of files in the group
     155   *
     156   *  @return <code>int</code> the count
     157   */
     158  public int noOfFiles()
     159  { return this.children.size();
     160  }
     161
     162  /**
     163   *  Get the number of subgroups.
     164   */
     165  public int noOfSubgroups()
     166  { return this.childGroups.size();
     167  }
     168
     169  /**
     170   *  @return <code>String</code> the name of this group
     171   */
     172  public String getName()
     173  { return this.id;
     174  }
    101175
    102176  /**
     
    118192  }
    119193
    120   public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
    121   { // check if this node is in the
     194  public boolean writeSQL(DocumentInterface document, String parentRef, boolean parentIsSection,
     195              GS3SQLConnection connection)
     196  { int sqlId = -1;
     197
     198    // check if this node is in the database already
    122199    GS3SQLSelect select = new GS3SQLSelect("filegroups");
    123200    select.addField("*");
     
    128205    select.setWhere(where);
    129206
    130     System.out.println(select.toString());
    131207    connection.execute(select.toString());
    132208
    133     GS3SQLInsert insert = new GS3SQLInsert("filegroups");
    134     insert.addValue("DocID", document.getID().toString());
    135     insert.addValue("FileGroupID", this.id);
    136     insert.addValue("ParentGroup", "");
    137     System.out.println("filegroup done");
    138     connection.execute(insert.toString());
     209    ResultSet selectResult = connection.getResultSet();
     210
     211    try {
     212      if (selectResult == null ||
     213      !selectResult.first()) {
     214    GS3SQLInsert insert = new GS3SQLInsert("filegroups");
     215
     216    insert.addValue("DocID", document.getID().toString());
     217    insert.addValue("FileGroupID", this.id);
     218    insert.addValue("ParentRef", parentRef, GS3SQLField.INTEGER_TYPE);
     219    insert.addValue("ParentType", parentIsSection ? SECTION_PARENT : GROUP_PARENT);
     220   
     221    if (!connection.execute(insert.toString())) {
     222      return false;
     223    }
     224      }
     225      else {
     226    // TODO: update the data for this file group...
     227      }
     228    }
     229    catch (SQLException ex)
     230    { System.err.println(ex);
     231      return false;
     232    }
    139233       
    140     System.out.println("filegroup done");
    141 
    142     Iterator childIter = children.iterator();
     234    // get the filegroup reference now
     235    connection.execute(select.toString());
     236   
     237    try {
     238      ResultSet results = connection.getResultSet();
     239      if (results == null) {
     240    return false;
     241      }
     242      results.first();
     243      sqlId = results.getInt("FileGroupRef");
     244    }
     245    catch (SQLException sqlex) {
     246      System.out.println(sqlex);
     247      return false;
     248    }
     249
     250    // iterate over the child groups
     251    Iterator childIter = childGroups.iterator();
     252    while (childIter.hasNext())
     253    { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
     254
     255      if (!fileGroup.writeSQL(document, Integer.toString(sqlId), false, connection))
     256      { return false;
     257      }
     258    }
     259
     260    // iterate over the child files
     261    childIter = children.iterator();
    143262    while (childIter.hasNext())
    144263    { METSFile file = (METSFile) childIter.next();
    145       file.writeSQL(connection);
    146     }
     264   
     265      if (!file.writeSQL(sqlId, connection))
     266      { return false;
     267      }
     268    }
     269
     270    return true;
     271  }
     272
     273  public static METSFileGroup readSQL(DocumentInterface document, GS3SQLConnection connection,
     274                      ResultSet resultSet)
     275  {
     276    try {
     277      String ID         = resultSet.getString("FileGroupID");
     278      String parentType = resultSet.getString("ParentType");
     279      String parentRef  = resultSet.getString("ParentRef");
     280
     281      // create the metadata block object
     282      METSFileGroup group = new METSFileGroup(ID);
     283     
     284      // get its metadata reference to retrieve divisions
     285      int groupRef = resultSet.getInt("FileGroupRef");
     286
     287      // query the database for matching groups which are children of this one
     288      GS3SQLSelect select = new GS3SQLSelect("filegroups");
     289      select.addField("*");
     290      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(groupRef),
     291                              GS3SQLField.INTEGER_TYPE);
     292      GS3SQLWhere where = new GS3SQLWhere(whereItem);
     293      whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.GROUP_PARENT);
     294      where.add(whereItem);
     295      select.setWhere(where);
     296
     297      connection.execute(select.toString());
     298
     299      // parse through the child groups
     300      ResultSet childSet = connection.getResultSet();
     301      if (childSet.first()) {
     302    do {
     303      METSFileGroup fileGroup = METSFileGroup.readSQL(document, connection, childSet);
     304      if (fileGroup != null) {
     305        group.addGroup(fileGroup);
     306      }
     307    }
     308    while (childSet.next());
     309      }
     310
     311      // now scan for file members
     312      select = new GS3SQLSelect("files");
     313      select.addField("*");
     314      whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupRef),
     315                      GS3SQLField.INTEGER_TYPE);
     316      where = new GS3SQLWhere(whereItem);
     317      select.setWhere(where);
     318      connection.execute(select.toString());
     319
     320      ResultSet childFileSet = connection.getResultSet();
     321      if (childFileSet != null && childFileSet.first()) {
     322    do {
     323      METSFile file = METSFile.readSQL(connection, childFileSet);
     324      if (file != null) {
     325        group.addFile(file);
     326      }
     327    }
     328    while (childFileSet.next());
     329      }
     330
     331      return group;
     332    }
     333    catch (SQLException sqlEx)
     334    { System.out.println(sqlEx);
     335      System.exit(1);
     336    }
     337    return null;
    147338  }
    148339}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFilePos.java

    r5800 r5945  
    66public class METSFilePos
    77{
    8     METSLocation location;
     8  METSLocation location;
    99
    10     public METSFilePos(File file) throws java.net.MalformedURLException
    11     {   this.location = new METSLocation(file);
    12     }
     10  public METSFilePos(File file) throws java.net.MalformedURLException
     11  { this.location = new METSLocation(file);
     12  }
    1313
    14     public METSFilePos(URL url)
    15     {   this.location = new METSLocation(url);
    16     }
     14  public METSFilePos(URL url)
     15  { this.location = new METSLocation(url);
     16  }
    1717
    18     public METSFilePos(String location, String locationType) throws java.net.MalformedURLException
    19     {   this.location = new METSLocation(location, locationType);
    20     }
     18  public METSFilePos(String locationType, String location) throws java.net.MalformedURLException
     19  { this.location = new METSLocation(locationType, location);
     20  }
     21 
     22  public URL getLocation()
     23  { return this.location.getLocation();
     24  }
    2125
    22     public URL getLocation()
    23     {   return this.location.getLocation();
    24     }
     26  public String getType()
     27  { return this.location.getType();
     28  }
    2529
    26     public String getType()
    27     {   return this.location.getType();
    28     }
     30  public String toString()
     31  { return this.location.getLocation().toString();
     32  }
    2933
    30     public String toString()
    31     {   return this.location.getLocation().toString();
    32     }
     34  public boolean equals(URL filePos)
     35  { return this.location.equals(filePos);
     36  }
    3337}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileSet.java

    r5800 r5945  
    88import java.util.Map;
    99import java.util.HashMap;
     10import java.util.List;
     11import java.util.ArrayList;
    1012import java.util.Iterator;
    1113
    1214import java.io.File;
    1315import java.io.PrintWriter;
     16
     17import java.sql.SQLException;
     18import java.sql.ResultSet;
     19
    1420import java.net.URL;
    1521
     
    2026
    2127public class METSFileSet
    22 { Map fileGroups;
     28{ Map    fileGroups;
     29  String reference;
    2330
    2431  public METSFileSet()
    2532  { this.fileGroups = new HashMap();
    2633    this.fileGroups.put("default", new METSFileGroup("default"));
    27   }
    28 
     34    this.reference = null;
     35  }
     36 
     37  /**
     38   *  Get the group that corresponds to the given name...
     39   *
     40   *  @param <code>String</code> the name of the group.
     41   *  @return <code>METSFileGroup</code> the group object - this will be
     42   *          <code>null</code> if the group is not found
     43   */
    2944  public METSFileGroup getGroup(String name)
    3045  { if (!this.fileGroups.containsKey(name))
     
    3449  }
    3550
     51  /**
     52   *  Get the Nth file from the default group...
     53   *
     54   *  @param <code>int</code> the index into the default group to use...
     55   *  @return <code>METSFile</code> the file.
     56   */
    3657  public METSFile getFile(int index)
    3758  { METSFileGroup group = this.getGroup("default");
     
    7091  }
    7192
     93  /**
     94   *  Add a group at the top level of the METS File set.
     95   *
     96   *  @param <code>METSFileGroup</code> the file group to add.
     97   */
    7298  public void addGroup(METSFileGroup group)
    7399  { this.fileGroups.put(group.getName(), group);
     100  }
     101
     102  /**
     103   *  Get all the groups that contain a given URL/file.
     104   *
     105   *  @param <code>URL</code> the location of the file.
     106   *  @return <code>List</code> the list of group references that contain the file.
     107   */
     108  public List findGroups(URL file)
     109  { List resultList = new ArrayList();
     110
     111    Iterator groups = this.fileGroups.values().iterator();
     112
     113    while (groups.hasNext())
     114    { METSFileGroup group = (METSFileGroup) groups.next();
     115     
     116      group.findGroups(file, resultList);
     117    }
     118    return resultList;
    74119  }
    75120
     
    86131  }
    87132
    88   public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
     133  public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
    89134  { Iterator groups = this.fileGroups.values().iterator();
    90135
    91     GS3SQLInsert insert = new GS3SQLInsert("filesection");
    92     insert.addValue("DocID", document.getID().toString());
    93     insert.addValue("FileSecID", "test"); // TODO: remove magic string
    94 
    95     connection.execute(insert.toString());
     136    // write the set if the reference does not already exist...
     137    if (this.reference == null)
     138    { // Insert the file section into the database
     139      GS3SQLInsert insert = new GS3SQLInsert("filesection");
     140      insert.addValue("DocID", document.getID().toString());
     141      insert.addValue("FileSecID", "test"); // TODO: remove magic string
     142
     143      if (!connection.execute(insert.toString()))
     144      { return false;
     145      }
     146
     147      // find the file section number
     148      GS3SQLSelect select = new GS3SQLSelect("filesection");
     149      select.addField("*");
     150      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileSecID", "=", "test");
     151      GS3SQLWhereItem whereDoc  = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     152      GS3SQLWhere where = new GS3SQLWhere(whereItem);
     153      where.add(whereDoc);
     154      select.setWhere(where);
     155
     156      try {
     157    connection.execute(select.toString());
     158   
     159    ResultSet set = connection.getResultSet();
     160    set.first();
     161    int sectionRef = set.getInt("FileSectionRef");
     162
     163    this.reference = Integer.toString(sectionRef);
     164      }
     165      catch (SQLException ex)
     166      { System.out.println(ex);
     167        return false;
     168      }
     169    }
    96170   
     171    // write out the children
    97172    while (groups.hasNext())
    98173    { METSFileGroup group = (METSFileGroup) groups.next();
    99 
    100       group.writeSQL(document, connection);
    101     }
    102   }
     174     
     175      if (!group.writeSQL(document, this.reference, true, connection))
     176      { return false;
     177      }
     178    }
     179
     180    return true;
     181  }
     182
     183  public static METSFileSet readSQL(DocumentInterface document, GS3SQLConnection connection)
     184  {
     185    METSFileSet set = new METSFileSet();   
     186
     187    // Get file sections from the filesection table (currently redundant)
     188    GS3SQLSelect select = new GS3SQLSelect("filesection");
     189    select.addField("*");
     190    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     191    select.setWhere(new GS3SQLWhere(whereItem));
     192    connection.execute(select.toString());
     193   
     194    // Get the identifier for this file set, etc.
     195    ResultSet sections = connection.getResultSet();
     196    int fileSetRef;
     197    try {
     198      sections.first();
     199      fileSetRef     = sections.getInt("FileSectionRef");
     200      set.reference = Integer.toString(fileSetRef);
     201    }
     202    catch (SQLException ex)
     203    { System.out.println(ex);
     204      return null;
     205    }
     206   
     207    // Get child file groups
     208    select = new GS3SQLSelect("filegroups");
     209    select.addField("*");
     210    whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());   
     211    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     212    whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(fileSetRef),
     213                    GS3SQLField.INTEGER_TYPE);
     214    where.add(whereItem);
     215    whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.SECTION_PARENT);
     216    where.add(whereItem);
     217    select.setWhere(where);
     218    connection.execute(select.toString());
     219
     220    // start going through the matching file groups
     221    try {
     222      ResultSet resultSet = connection.getResultSet();
     223      resultSet.first();
     224      do {
     225    METSFileGroup filegroup = METSFileGroup.readSQL(document, connection, resultSet);
     226    if (filegroup != null) {
     227      set.addGroup(filegroup);
     228    }
     229      } while (resultSet.next());
     230    }
     231    catch (SQLException sqlEx) {
     232      System.out.println(sqlEx);
     233      System.exit(1);
     234    }
     235
     236    return set;
     237  }
     238
    103239}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSLocation.java

    r5800 r5945  
    66public class METSLocation
    77{
    8     String type;
    9     URL    location;
     8  String type;
     9  URL    location;
    1010
    11     public METSLocation(String type, String location) throws java.net.MalformedURLException
    12     {   this.type = type;
    13         this.location = new URL(location);
    14     }
     11  public METSLocation(String type, String location) throws java.net.MalformedURLException
     12  { this.type = type;
     13    this.location = new URL(location);
     14  }
    1515
    16     public METSLocation(URL url)
    17     {   this.type = "URL";
    18         this.location = url;
    19     }
     16  public METSLocation(URL url)
     17  { this.type = "URL";
     18    this.location = url;
     19  }
    2020
    21     public METSLocation(File file) throws java.net.MalformedURLException
    22     { this.type = "URL";
    23         this.location = new URL("file://" + file.toString());
    24     }
     21  public METSLocation(File file) throws java.net.MalformedURLException
     22  { this.type = "URL";
     23    this.location = new URL("file://" + file.toString());
     24  }
    2525
    26     public URL getLocation()
    27     {   return this.location;
    28     }
     26  public boolean equals(URL url)
     27  { return this.location.equals(url);
     28  }
    2929
    30     public String getType()
    31     {   return this.type;
    32     }
     30  public URL getLocation()
     31  { return this.location;
     32  }
     33
     34  public String getType()
     35  { return this.type;
     36  }
    3337}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSNamespace.java

    r5800 r5945  
    44
    55import java.util.List;
     6
     7import java.sql.SQLException;
     8import java.sql.ResultSet;
    69
    710import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
     
    3639  }
    3740
    38     /**
    39     *  Get the name of the namespace...
    40     *
    41     *  @return <code>String</code> the name of the namespace
    42     */
    43     public String getName()
    44     {   return this.name;
    45     }
    46 
    47     /**
    48     *  Get the group of namespaces to which this namespace belongs.
    49     *
    50     *  @return <code>String</code> the name of the namespace group
    51     */
    52     public String getGroup()
    53     {   return this.group;
    54     }
     41  /**
     42  *  Get the name of the namespace...
     43  *
     44  *  @return <code>String</code> the name of the namespace
     45  */
     46  public String getName()
     47  { return this.name;
     48  }
     49
     50  /**
     51  *  Get the group of namespaces to which this namespace belongs.
     52  *
     53  *  @return <code>String</code> the name of the namespace group
     54  */
     55  public String getGroup()
     56  { return this.group;
     57  }
    5558   
    56     /**
    57     *  Set the group of namespaces to which this namespace belongs.
    58     *
    59     *  @param <code>String</code> the name of the namespace group
    60     */
    61     public void setGroup(String group)
    62     {   this.group = group;
    63     }
    64 
    65     /**
    66     *  Get the ID for this namespace.
    67     *
    68     *  @return <code>String</code> the id of the namespace
    69     */
    70     public String getID()
    71     { return this.id;
    72     }
    73 
    74     /**
    75     *  Set the id of this namespace.
    76     *
    77     *  @param <code>String</code> the name of the namespace group
    78     */
    79     public void setID(String id)
    80     {   this.id = id;
    81     }
     59  /**
     60  *  Set the group of namespaces to which this namespace belongs.
     61  *
     62  *  @param <code>String</code> the name of the namespace group
     63  */
     64  public void setGroup(String group)
     65  { this.group = group;
     66  }
     67
     68  /**
     69  *  Get the ID for this namespace.
     70  *
     71  *  @return <code>String</code> the id of the namespace
     72  */
     73  public String getID()
     74  { return this.id;
     75  }
     76
     77  /**
     78  *  Set the id of this namespace.
     79  *
     80  *  @param <code>String</code> the name of the namespace group
     81  */
     82  public void setID(String id)
     83  { this.id = id;
     84  }
    8285
    8386  /**
     
    107110  public boolean writeSQL(int parentId, GS3SQLConnection connection)
    108111  {
    109     GS3SQLInsert insert = new GS3SQLInsert("namespaces");
    110 
     112    GS3SQLAction action;
     113
     114    // If the namespace is not null, then set it...
    111115    if (this.id != null) {
    112     insert.addValue("NamespaceID", this.id);
     116      // use an update action in this case...
     117      GS3SQLUpdate update = new GS3SQLUpdate("namespaces");
     118      update.addValue("NamespaceID", this.id);
     119
     120      // set up the where clause
     121      GS3SQLWhere where =
     122    new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", this.id,
     123                        GS3SQLField.INTEGER_TYPE));
     124      update.setWhere(where);
     125      action = update;
     126    }
     127    else {
     128      GS3SQLInsert insert = new GS3SQLInsert("namespaces");
     129      action = insert;
    113130    }
    114131
    115132    if (this.location != null) {
    116       insert.addValue("fileLoc", this.location.getLocation().toString());
    117       insert.addValue("fileType", "URL");
     133      action.addValue("fileLoc", this.location.getLocation().toString());
     134      action.addValue("fileType", "URL");
    118135    }
    119136    else {
    120137      // no location stuff
    121138    }
    122     insert.addValue("MetadataRef", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE);
    123     insert.addValue("NamespaceType", this.name);
    124 
    125     //    System.out.println(insert.toString());
    126     connection.execute(insert.toString());
     139    action.addValue("MetadataRef", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE);
     140    action.addValue("NamespaceType", this.name);
     141
     142    // Execute the action
     143    connection.execute(action.toString());
     144
     145    // then get the namespace reference number if needsbe...
     146    if (this.id == null) {
     147      GS3SQLSelect select = new GS3SQLSelect("namespaces");
     148      select.addField("NamespaceRef");
     149      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE);
     150      GS3SQLWhere where = new GS3SQLWhere(whereItem);
     151      whereItem = new GS3SQLWhereItem("NamespaceType", "=", this.name);
     152      where.add(where);
     153      try {
     154    connection.execute(select.toString());
     155     
     156    ResultSet result = connection.getResultSet();
     157    result.last();
     158    this.id = Integer.toString(result.getInt("NamespaceRef"));
     159      }
     160      catch (SQLException sqlex)
     161      { this.id = null;
     162        System.err.println(sqlex);
     163    return false;
     164      }
     165    }
    127166
    128167    return true;
    129168  }
     169
     170  public static METSNamespace readSQL(GS3SQLConnection connection, ResultSet resultSet)
     171  {
     172    METSLocation metsLocation = null;
     173   
     174    try {
     175      String name = resultSet.getString("NamespaceType");
     176      String id = resultSet.getString("NamespaceRef");
     177      String location = resultSet.getString("fileLoc");
     178      String type = resultSet.getString("fileType");
     179      if (location != null && type != null) {
     180    metsLocation = new METSLocation(type, location);
     181      }
     182
     183      METSNamespace namespace = NamespaceFactory.initNamespace(name, metsLocation);
     184      namespace.id = id;
     185
     186      int namespaceRef = resultSet.getInt("NamespaceRef");
     187     
     188      GS3SQLSelect select = new GS3SQLSelect("mdvalues");
     189      select.addField("*");
     190      GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", Integer.toString(namespaceRef),
     191                                  GS3SQLField.INTEGER_TYPE));
     192      select.setWhere(where);
     193
     194      connection.execute(select.toString());
     195     
     196      ResultSet valuesSet = connection.getResultSet();
     197      if (valuesSet != null && valuesSet.first()) {
     198    do {
     199      String label = valuesSet.getString("label");
     200      String value = valuesSet.getString("value");
     201
     202      namespace.addMetadata(label, value);
     203    }
     204    while (valuesSet.next());
     205      }
     206
     207      return namespace;
     208    }
     209    catch (java.net.MalformedURLException urlEx)
     210    { System.out.println(urlEx);
     211    }
     212    catch (SQLException sqlEx)
     213    { System.out.println(sqlEx);
     214    }   
     215    return null;
     216  }
    130217}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructure.java

    r5800 r5945  
    1010
    1111import java.sql.SQLException;
     12import java.sql.ResultSet;
    1213
    1314import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
     
    1920public class METSStructure
    2021{
    21     Map children;
    22     String ID;
    23     String label;
    24     String type;
    25 
    26     public METSStructure(String id, String label, String type)
    27     {   this.children = new HashMap();
    28         this.ID       = id;
    29         this.label    = label;
    30         this.type     = type;
    31     }
    32 
    33     public String getID()
    34     {   return this.ID;
    35     }
    36 
    37     public void addDivision(METSDivision division)
    38     {   this.children.put(division.getID(), division);
    39     }
    40 
    41     public void addSubDivision(String divisionID, METSDivision division)
    42     {   String topDivision = METSDivision.getTopDivisionName(divisionID);
    43 
    44         METSDivision child = (METSDivision) this.children.get(topDivision);
    45         if (child == null)
    46         {   return;
    47         }
    48 
    49         String subDivisionID = METSDivision.getSubDivisionName(divisionID);
    50         if (subDivisionID == null)
    51         { child.addSubDivision(division);
    52         }
    53         else
    54         {   child.addSubDivision(subDivisionID, division);
    55         }
    56     }
    57 
    58     public METSDivision getDivision(String divisionID)
    59     {   String topDivision = METSDivision.getTopDivisionName(divisionID);
    60 
    61         METSDivision child = (METSDivision) this.children.get(topDivision);
    62         if (child == null)
    63         {   return null;
    64         }
    65 
    66         String subDivision = METSDivision.getSubDivisionName(divisionID);
    67         if (subDivision != null)
    68         {   child = child.getDivision(subDivision);
    69         }
    70 
    71         return child;
    72     }
    73 
    74     public void write(PrintWriter writer)
    75     { Iterator groups = this.children.values().iterator();
    76 
    77         String tag = XMLTools.getOpenTag("mets", "structMap");
    78         tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
    79         tag = XMLTools.addAttribute(tag, "TYPE", this.type.toString());
    80         tag = XMLTools.addAttribute(tag, "LABEL", this.label.toString());
     22  Map children;
     23  String ID;
     24  String label;
     25  String type;
     26 
     27  public METSStructure(String id, String label, String type)
     28  { this.children = new HashMap();
     29    this.ID       = id;
     30    this.label    = label;
     31    this.type     = type;
     32  }
     33 
     34  /**
     35   *  Get the identifer of this structure
     36   *
     37   *  @param <code>String</code> the identifier.
     38   */
     39  public String getID()
     40  { return this.ID;
     41  }
     42
     43  /**
     44   *  Find all the divisions that match a given list of file group
     45   *  identifiers...
     46   *
     47   *  @param <code>List</code> the list of identifiers to find
     48   *  @param <code>List</code> the list into which to place any
     49   *         matching identifiers.
     50   */
     51  public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList)
     52  { Iterator groups = this.children.values().iterator();
     53    while (groups.hasNext())
     54    { METSDivision group = (METSDivision) groups.next();
     55
     56      group.findDivisionsForFiles(listOfFileIdentifiers, resultList);
     57    }   
     58  }
     59
     60  /**
     61   *  Add a division directly underneath this structure.
     62   *
     63   *  @param <code>METSDivision</code> the division to add.
     64   */
     65  public void addDivision(METSDivision division)
     66  { this.children.put(division.getID(), division);
     67  }
     68
     69  /**
     70   *  Add a division further into this structure.
     71   *
     72   *  @param <code>String</code> the identifier for the division
     73   *         within which the division is to be added.
     74   *  @param <code>METSDivision</code> the division to add.   
     75   */
     76  public void addSubDivision(String divisionID, METSDivision division)
     77  { String topDivision = METSDivision.getTopDivisionName(divisionID);
     78
     79    METSDivision child = (METSDivision) this.children.get(topDivision);
     80    if (child == null)
     81    { return;
     82    }
     83
     84    String subDivisionID = METSDivision.getSubDivisionName(divisionID);
     85    if (subDivisionID == null)
     86    { child.addSubDivision(division);
     87    }
     88    else
     89    { child.addSubDivision(subDivisionID, division);
     90    }
     91  }
     92
     93  /**
     94   *  Get a subdivision of a given identifier...
     95   *
     96   *  @param <code>String</code> the identifier of the division
     97   *  @return <code>METSDivision</code> the division, which will be
     98   *          <code>null</code> if it is not found.
     99   */
     100  public METSDivision getDivision(String divisionID)
     101  { String topDivision = METSDivision.getTopDivisionName(divisionID);
     102
     103    METSDivision child = (METSDivision) this.children.get(topDivision);
     104    if (child == null)
     105    { return null;
     106    }
     107
     108    String subDivision = METSDivision.getSubDivisionName(divisionID);
     109    if (subDivision != null)
     110    { child = child.getDivision(subDivision);
     111    }
     112
     113    return child;
     114  }
     115
     116  public void write(PrintWriter writer)
     117  { Iterator groups = this.children.values().iterator();
     118
     119    String tag = XMLTools.getOpenTag("mets", "structMap");
     120    tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
     121    tag = XMLTools.addAttribute(tag, "TYPE", this.type);
     122    tag = XMLTools.addAttribute(tag, "LABEL", this.label);
    81123       
    82         writer.println(tag);
    83         while (groups.hasNext())
    84         {   METSDivision group = (METSDivision) groups.next();
    85 
    86             group.write(writer);
    87         }
    88         writer.println("</mets:structMap>");
    89     }
    90 
    91 
    92     public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
    93     { Iterator groups = this.children.values().iterator();
    94 
     124    writer.println(tag);
     125    while (groups.hasNext())
     126    { METSDivision group = (METSDivision) groups.next();
     127
     128      group.write(writer);
     129    }
     130    writer.println("</mets:structMap>");
     131  }
     132
     133
     134  /**
     135   *  Write this METSStructure to an SQL database....
     136   *
     137   *  @param <code>DocumentInterface</code> the enclosing document.
     138   *  @param <code>GS3SQLConnection</code> the SQL database connection.
     139   */
     140  public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
     141  { int sqlRef = -1;
     142    ResultSet results;
     143
     144    // Prepare query to see if this structure has already been written
     145    GS3SQLSelect select = new GS3SQLSelect("structure");
     146    select.addField("StructureRef");
     147    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     148    GS3SQLWhereItem item = new GS3SQLWhereItem("StructureID", "=", this.ID.toString());
     149    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     150    where.add(item);
     151    select.setWhere(where);
     152
     153    // attempt to execute the query & get the current structure's reference
     154    try {
     155      connection.execute(select.toString());
     156      results = connection.getResultSet();
     157      if (results != null &&
     158      results.first())
     159      { sqlRef = results.getInt("structureRef");
     160      }
     161      else
     162      { results = null;
     163      }
     164    }
     165    catch (SQLException sqlEx) {
     166      System.err.print(sqlEx);
     167      return false;
     168    }
     169
     170    // insert a new structure if it wasn't there previously
     171    if (results == null) {
    95172      GS3SQLInsert insert = new GS3SQLInsert("structure");
    96173      insert.addValue("DocID", document.getID().toString());
    97174      insert.addValue("StructureID", this.ID.toString());
    98       insert.addValue("Type", this.type.toString());
    99       insert.addValue("Label", this.label.toString());
     175      insert.addValue("Type", this.type);
     176      insert.addValue("Label", this.label);
    100177     
    101       System.out.println(insert.toString());
    102       connection.execute(insert.toString());
     178      if (!connection.execute(insert.toString())) {
     179    return false;
     180      }
     181
     182      // get the new structure reference by re-running the original select object
     183      connection.execute(select.toString());
     184
     185      try {
     186    results = connection.getResultSet();
     187    results.first();
     188    sqlRef = results.getInt("StructureRef");
     189      }
     190      catch (SQLException sql) {
     191    System.err.println(sql);
     192    return false;
     193      }
     194    }
     195    else {
     196      GS3SQLUpdate update = new GS3SQLUpdate("structure");
     197      update.setWhere(where);
     198
     199      update.addValue("Type", this.type);
     200      update.addValue("Label", this.label);
     201
     202      connection.execute(update.toString());
     203    }
    103204     
    104       // get the new structure reference
    105       GS3SQLSelect select = new GS3SQLSelect("structure");
    106       select.addField("StructureRef");
    107       GS3SQLWhereItem whereItem = new GS3SQLWhereItem("StructureID", "=", this.ID);
    108       GS3SQLWhereItem item = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     205    // write out the child groups (Divisions) now...   
     206    Iterator groups = this.children.values().iterator();
     207
     208    while (groups.hasNext())
     209    { METSDivision group = (METSDivision) groups.next();
     210
     211      if (!group.writeSQL(sqlRef, true, connection))
     212      { return false;
     213      }
     214    }
     215    return true;
     216  }
     217
     218
     219  public static METSStructure readSQL(DocumentInterface document, GS3SQLConnection connection,
     220                      ResultSet resultSet)
     221  {
     222    try {
     223      String ID    = resultSet.getString("StructureID");
     224      String type  = resultSet.getString("Type");
     225      String label = resultSet.getString("Label");
     226
     227      // create the metadata block object
     228      METSStructure structure = new METSStructure(ID, label, type);
     229     
     230      // get its metadata reference to retrieve divisions
     231      int structureRef = resultSet.getInt("StructureRef");
     232
     233      // query the database for matching division for this structure block
     234      GS3SQLSelect select = new GS3SQLSelect("divisions");
     235      select.addField("*");
     236      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(structureRef),
     237                              GS3SQLField.INTEGER_TYPE);
    109238      GS3SQLWhere where = new GS3SQLWhere(whereItem);
    110       where.add(item);
     239      whereItem = new GS3SQLWhereItem("ParentType", "=", METSDivision.STRUCTURE_PARENT);
     240      where.add(whereItem);
    111241      select.setWhere(where);
    112       System.out.println(select.toString());
     242
    113243      connection.execute(select.toString());
    114244
    115       try {
    116         // read in the structure reference
    117         connection.getResultSet().first();
    118     int sqlRef = connection.getResultSet().getInt("StructureRef");
    119        
    120     while (groups.hasNext())
    121     { METSDivision group = (METSDivision) groups.next();
    122 
    123           group.writeSQL(sqlRef, true, connection);
     245      // parse through the divisions
     246      ResultSet divisionSet = connection.getResultSet();
     247      divisionSet.first();
     248      do {
     249    METSDivision division = METSDivision.readSQL(connection, divisionSet);
     250    if (division != null) {
     251      structure.addDivision(division);
    124252    }
    125253      }
    126       catch (SQLException sql) {
    127       System.out.println(sql);
    128       }
    129     }
     254      while (divisionSet.next());
     255
     256      return structure;
     257    }
     258    catch (SQLException sqlEx)
     259    { System.out.println(sqlEx);
     260      System.exit(1);
     261    }
     262    return null;
     263  }
    130264}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructureSet.java

    r5800 r5945  
    55import java.util.HashMap;
    66import java.util.Map;
     7import java.util.List;
     8import java.util.ArrayList;
    79import java.util.Iterator;
     10
     11import java.sql.SQLException;
     12import java.sql.ResultSet;
    813
    914import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
    1015
    1116import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
     17import org.greenstone.gsdl3.gs3build.database.*;
    1218
    1319public class METSStructureSet
    14 {   Map children;
     20{ Map children;
    1521
    16     public METSStructureSet()
    17     {   this.children = new HashMap();
    18     }
     22  public METSStructureSet()
     23  { this.children = new HashMap();
     24  }
    1925
    20     public void addStructure(METSStructure structure)
    21     {   this.children.put(structure.getID(), structure);
    22     }
     26  public void addStructure(METSStructure structure)
     27  { this.children.put(structure.getID(), structure);
     28  }
    2329
    24     public METSStructure getStructure(String name)
    25     {   return (METSStructure) this.children.get(name);
    26     }
     30  public METSStructure getStructure(String name)
     31  { return (METSStructure) this.children.get(name);
     32  }
    2733
    28     public void addDivision(String structureName, METSDivision division)
    29     {   METSStructure structure = (METSStructure) this.children.get(structureName);
     34  /**
     35   *  Find the divisions that contain a list of given files
     36   *
     37   *  @param <code>List</code> the list of files to find...
     38   *  @return <code>List</code> the matching divisions.
     39   */
     40  public List findDivisionsForFiles(List listOfFileIdentifiers)
     41  { List reply = new ArrayList();
    3042
    31         if (structure != null)
    32         { structure.addDivision(division);
    33         }
    34     }
     43    // iterate across all the children...
     44    Iterator structures = this.children.values().iterator();
     45   
     46    while (structures.hasNext())
     47    { METSStructure structure = (METSStructure) structures.next();
    3548
    36     public void addSubDivision(String structureName, String divisionID, METSDivision division)
    37     {   METSStructure structure = (METSStructure) this.children.get(structureName);
     49      structure.findDivisionsForFiles(listOfFileIdentifiers, reply);     
     50    }   
     51    return reply;
     52  }
    3853
    39         if (structure != null)
    40         { structure.addSubDivision(divisionID, division);
    41         }
    42     }
     54  /**
     55   *  Add a division to an indicated structure.
     56   *
     57   *  @param <code>String</code> the structure identifier
     58   *  @param <code>METSDivision</code> the division to be added.
     59   */
     60  public void addDivision(String structureName, METSDivision division)
     61  { METSStructure structure = (METSStructure) this.children.get(structureName);
    4362
    44     public METSDivision getDivision(String structureName, String divisionID)
    45     {   METSStructure structure = (METSStructure) this.children.get(structureName);
    46 
    47         if (structure != null)
    48         { return structure.getDivision(divisionID);
    49         }
    50         return null;
    51     }
    52 
    53     public void write(PrintWriter writer)
    54     { Iterator groups = this.children.values().iterator();
    55 
    56         while (groups.hasNext())
    57         {   METSStructure group = (METSStructure) groups.next();
    58 
    59             group.write(writer);
    60         }
    61     }
    62 
    63   public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
    64   { Iterator groups = this.children.values().iterator();
    65    
    66     while (groups.hasNext())
    67     { METSStructure group = (METSStructure) groups.next();
    68 
    69       group.writeSQL(document, connection);
     63    if (structure != null)
     64    { structure.addDivision(division);
    7065    }
    7166  }
    7267
     68  /**
     69   *  Add a division to an indicated division within structure.
     70   *
     71   *  @param <code>String</code> the structure identifier
     72   *  @param <code>String</code> the division identifer, within which the division
     73   *         is to be placed.
     74   *  @param <code>METSDivision</code> the division to be added.
     75   */
     76  public void addSubDivision(String structureName, String divisionID, METSDivision division)
     77  { METSStructure structure = (METSStructure) this.children.get(structureName);
     78
     79    if (structure != null)
     80    { structure.addSubDivision(divisionID, division);
     81    }
     82  }
     83
     84  public METSDivision getDivision(String structureName, String divisionID)
     85  { METSStructure structure = (METSStructure) this.children.get(structureName);
     86
     87    if (structure != null)
     88    { return structure.getDivision(divisionID);
     89    }
     90    return null;
     91  }
     92
     93  public void write(PrintWriter writer)
     94  { Iterator structures = this.children.values().iterator();
     95
     96    while (structures.hasNext())
     97    { METSStructure group = (METSStructure) structures.next();
     98
     99      group.write(writer);
     100    }
     101  }
     102
     103  public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
     104  { Iterator structures = this.children.values().iterator();
     105   
     106    while (structures.hasNext())
     107    { METSStructure group = (METSStructure) structures.next();
     108
     109      if (!group.writeSQL(document, connection))
     110      { return false;
     111      }
     112    }
     113    return true;
     114  }
     115
     116  public static METSStructureSet readSQL(DocumentInterface document, GS3SQLConnection connection)
     117  {
     118    METSStructureSet set = new METSStructureSet();
     119
     120    GS3SQLSelect select = new GS3SQLSelect("structure");
     121    select.addField("*");
     122    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     123    select.setWhere(new GS3SQLWhere(whereItem));
     124    connection.execute(select.toString());
     125
     126    // start going through the matching metadata blocks
     127    try {
     128      ResultSet resultSet = connection.getResultSet();
     129      resultSet.first();
     130      do {
     131    METSStructure structure = METSStructure.readSQL(document, connection, resultSet);
     132    if (structure != null) {
     133      set.addStructure(structure);
     134    }
     135      } while (resultSet.next());
     136    }
     137    catch (SQLException sqlEx) {
     138      System.out.println(sqlEx);
     139      System.exit(1);
     140    }
     141
     142    return set;
     143  }
     144
    73145}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/OrderedNamespace.java

    r5800 r5945  
    1919public class OrderedNamespace extends METSNamespace
    2020{
    21     String  name;
    22     List metadataList;
    23 
    24     public OrderedNamespace(String name)
    25     { super(name);
    26         this.metadataList = new ArrayList();
    27     }
    28 
    29     public OrderedNamespace(String name, METSLocation location)
    30     { super(name, location);
    31         this.metadataList = new ArrayList();
    32     }
    33 
    34     public boolean validate(String field, String value)
    35     {   return true;
    36     }
    37 
    38     /**
    39      *  Add a metadata item.  Whether the field and value validate for this
    40      *  schema will also be tested.
    41      *
    42      *  @param <code>String</code> the name of the field to be given the value
    43      *  @param <code>String</code> the value to be assigned
    44      *
    45      *  @return <code>boolean</code> whether the field value was added.  This
    46      *          would return <code>false</code> if the values did not validate,
    47      *          for example.
    48      */
    49     public boolean addMetadata(String label, String value)
    50     {   if (!this.validate(label, value)) {
    51             return false;
    52         }
    53 
    54         this.metadataList.add(new NamespaceItem(label, value));
    55         return true;
    56     }
    57 
    58     /**
    59      *  Assign a metadata item.  Whether the field and value validate for this
    60      *  schema will also be tested.  Any existing metadata for that field will
    61      *  be destroyed if the new value validates.
    62      *
    63      *  @param <code>String</code> the name of the field to be given the value
    64      *  @param <code>String</code> the value to be assigned
    65      *
    66      *  @return <code>boolean</code> whether the field value was added.  This
    67      *          would return <code>false</code> if the values did not validate,
    68      *          for example.
    69      */
    70     public boolean setMetadata(String label, String value)
    71     { if (!this.validate(label, value))
    72         { return false;
    73         }
    74 
    75 // TODO:        this.metadataMap.setOnly(label, value);
    76       int itemNo = this.findItem(label);
    77         if (itemNo >= 0)
    78         {   this.metadataList.set(itemNo, new NamespaceItem(label, value));
    79             return true;
    80         }
    81         return false;
    82     }
    83 
    84     /**
    85      *  Remove all metadata values for a given field name
    86      *
    87      *  @param <code>String</code> the field to delete
    88      *
    89      *  @return <code>boolean</code> whether the field was actually deleted;
    90      *          will return <code>true</code> if the field was already empty.
    91      */
    92     public boolean removeMetadata(String label)
    93     { int itemNo;
    94 
    95         itemNo = this.findItem(label);
    96         while (itemNo >= 0)
    97         {   this.metadataList.remove(itemNo);
    98             itemNo = this.findItem(label);
    99         }
    100         return true;
    101     }
    102 
    103     /**
    104      *  Remove a particular incidence of a given metadata field for a document.
    105      *  If an exact match for the given value is not found, nothing changes.
    106      *  N.B. if a value occurs twice, only the first incidence of it will be
    107      *  deleted from the list.
    108      *
    109      *  @param <code>String</code> the field to have the value removed
    110      *  @param <code>String</code> the value to be removed from a given field
    111      *
    112      *  @return <code>boolean</code> <code>true</code> if an actual metadata text
    113      *          is matched against the given value and is thus deleted.
    114      */
    115     public boolean removeMetadata(String label, String value)
    116     { int itemNo;
    117 
    118         itemNo = this.findItem(label);
    119         if (itemNo >= 0)
    120         {   this.metadataList.remove(itemNo);
    121             return true;
    122         }
    123         return false;
    124     }
    125 
    126     /**
    127      *  Get the metadata items for a particular label
    128      *
    129      *  @param <code>String</code> the label to fetch values for - must be devoid
    130      *                             of namespace prologue (i.e. "title" rather than
    131      *                             e.g. "dc:title").
    132      * 
    133      *  @return <code>List</code> the list of corresponding values.  May be
    134      *          <code>null</code> if no values are found for the metadata
    135      */
    136     public List getMetadata(String label)
    137     {   List resultList = new ArrayList();
    138 
    139         for (int i = 0; i < this.metadataList.size(); i ++)
    140         {   NamespaceItem item = (NamespaceItem) this.metadataList.get(i);
    141 
    142             if (item.getLabel().equals(label))
    143             {   resultList.add(item.getValue());
    144             }
    145         }
    146         if (resultList.size() > 0)
    147         {   return resultList;
    148         }
    149         return null;
    150     }
    151 
    152     private int findItem(String label)
    153     {   for (int i = 0; i < this.metadataList.size(); i ++)
    154         {   if (((NamespaceItem) this.metadataList.get(i)).getLabel().equals(label))
    155             { return i;
    156             }
    157         }
    158         return -1;
    159     }
    160 
    161     private int findItem(String label, String value)
    162     {   for (int i = 0; i < this.metadataList.size(); i ++)
    163         {   NamespaceItem item = (NamespaceItem) this.metadataList.get(i);
    164             if (item.getLabel().equals(label) && item.getValue().equals(value))
    165             { return i;
    166             }
    167         }
    168         return -1;
    169     }
    170 
    171     /**
    172      *  Write out the metadata to an XML file through a <code>PrintWriter</code>.
    173      *
    174      *  @param <code>PrintWriter</code> the writer to use.
    175      */
    176     public boolean write(PrintWriter writer)
    177     { // if this is a non-file block of metadata, write it out in long hand
    178         if (this.location == null)
    179         {   String tag = XMLTools.getOpenTag("mets", "mdWrap");
    180             tag = XMLTools.addAttribute(tag, "MDType", this.name);
    181             writer.println(tag);
    182             Iterator items = this.metadataList.iterator();
    183 
    184             while (items.hasNext())
    185             {   NamespaceItem item = (NamespaceItem) items.next();
    186                 this.writeItem(writer, item);
    187             }
    188             writer.println("</mets:mdWrap>");
    189         }
    190         // otherwise, drop the metadata out in a simplified file-reference
    191         // form only
    192         else
    193         {   String tag = XMLTools.getOpenTag("mets", "mdRef");
    194             tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
    195             tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
    196             tag = XMLTools.addAttribute(tag, "MDTYPE", this.getName());
    197             tag = XMLTools.makeSingleton(tag);
    198         }
    199         return true;
    200     }
    201 
    202     /**
    203      *  Write out a single element - this may be overloaded to provide for the
    204      *  appropriate formatting for this metadata.
    205      */
    206     protected boolean writeItem(PrintWriter writer, NamespaceItem item)
    207     {   // Do some default sillinesses
    208         writer.write(XMLTools.getOpenTag(this.name, item.getLabel()));
    209 
    210         writer.write(item.getValue());
    211 
    212         writer.write(XMLTools.getCloseTag(this.name, item.getLabel()));
    213         return true;
    214     }
    215 
    216     /**
    217      *  <p>Indicate whether this metadata is open to being changed or not.</p>
    218      *  <p>Metadata which is created from a distinct file cannot be changed,
    219      *  only those which have no associated file can be modified.
    220      *
    221      *  @return <code>boolean</code> whether this namespace can be altered.
    222      */
    223     public boolean isEditable()
    224     {   return (this.location == null);
    225     }
     21  List metadataList;
     22
     23  public OrderedNamespace(String name)
     24  { super(name);
     25    this.metadataList = new ArrayList();
     26  }
     27
     28  public OrderedNamespace(String name, METSLocation location)
     29  { super(name, location);
     30    this.metadataList = new ArrayList();
     31  }
     32
     33  public boolean validate(String field, String value)
     34  { return true;
     35  }
     36
     37  /**
     38   *  Add a metadata item.  Whether the field and value validate for this
     39   *  schema will also be tested.
     40   *
     41   *  @param <code>String</code> the name of the field to be given the value
     42   *  @param <code>String</code> the value to be assigned
     43   *
     44   *  @return <code>boolean</code> whether the field value was added.  This
     45   *          would return <code>false</code> if the values did not validate,
     46   *          for example.
     47   */
     48  public boolean addMetadata(String label, String value)
     49  { if (!this.validate(label, value)) {
     50      return false;
     51    }
     52
     53    this.metadataList.add(new NamespaceItem(label, value));
     54    return true;
     55  }
     56
     57  /**
     58   *  Assign a metadata item.  Whether the field and value validate for this
     59   *  schema will also be tested.  Any existing metadata for that field will
     60   *  be destroyed if the new value validates.
     61   *
     62   *  @param <code>String</code> the name of the field to be given the value
     63   *  @param <code>String</code> the value to be assigned
     64   *
     65   *  @return <code>boolean</code> whether the field value was added.  This
     66   *          would return <code>false</code> if the values did not validate,
     67   *          for example.
     68   */
     69  public boolean setMetadata(String label, String value)
     70  { if (!this.validate(label, value))
     71    { return false;
     72    }
     73
     74    // TODO:        this.metadataMap.setOnly(label, value);
     75    int itemNo = this.findItem(label);
     76    if (itemNo >= 0)
     77    { this.metadataList.set(itemNo, new NamespaceItem(label, value));
     78      return true;
     79    }
     80    return false;
     81  }
     82
     83  /**
     84   *  Remove all metadata values for a given field name
     85   *
     86   *  @param <code>String</code> the field to delete
     87   *
     88   *  @return <code>boolean</code> whether the field was actually deleted;
     89   *          will return <code>true</code> if the field was already empty.
     90   */
     91  public boolean removeMetadata(String label)
     92  { int itemNo;
     93
     94    itemNo = this.findItem(label);
     95    while (itemNo >= 0)
     96    { this.metadataList.remove(itemNo);
     97      itemNo = this.findItem(label);
     98    }
     99    return true;
     100  }
     101
     102  /**
     103   *  Remove a particular incidence of a given metadata field for a document.
     104   *  If an exact match for the given value is not found, nothing changes.
     105   *  N.B. if a value occurs twice, only the first incidence of it will be
     106   *  deleted from the list.
     107   *
     108   *  @param <code>String</code> the field to have the value removed
     109   *  @param <code>String</code> the value to be removed from a given field
     110   *
     111   *  @return <code>boolean</code> <code>true</code> if an actual metadata text
     112   *          is matched against the given value and is thus deleted.
     113   */
     114  public boolean removeMetadata(String label, String value)
     115  { int itemNo;
     116
     117    itemNo = this.findItem(label);
     118    if (itemNo >= 0)
     119    { this.metadataList.remove(itemNo);
     120      return true;
     121    }
     122    return false;
     123  }
     124
     125  /**
     126   *  Get the metadata items for a particular label
     127   *
     128   *  @param <code>String</code> the label to fetch values for - must be devoid
     129   *                             of namespace prologue (i.e. "title" rather than
     130   *                             e.g. "dc:title").
     131   * 
     132   *  @return <code>List</code> the list of corresponding values.  May be
     133   *          <code>null</code> if no values are found for the metadata
     134   */
     135  public List getMetadata(String label)
     136  { List resultList = new ArrayList();
     137
     138    for (int i = 0; i < this.metadataList.size(); i ++)
     139    { NamespaceItem item = (NamespaceItem) this.metadataList.get(i);
     140
     141      if (item.getLabel().equals(label))
     142      { resultList.add(item.getValue());
     143      }
     144    }
     145    if (resultList.size() > 0)
     146    { return resultList;
     147    }
     148    return null;
     149  }
     150
     151  private int findItem(String label)
     152  { for (int i = 0; i < this.metadataList.size(); i ++)
     153    { if (((NamespaceItem) this.metadataList.get(i)).getLabel().equals(label))
     154      { return i;
     155      }
     156    }
     157    return -1;
     158  }
     159
     160  private int findItem(String label, String value)
     161  { for (int i = 0; i < this.metadataList.size(); i ++)
     162    { NamespaceItem item = (NamespaceItem) this.metadataList.get(i);
     163      if (item.getLabel().equals(label) && item.getValue().equals(value))
     164      { return i;
     165      }
     166    }
     167    return -1;
     168  }
     169
     170  /**
     171   *  Write out the metadata to an XML file through a <code>PrintWriter</code>.
     172   *
     173   *  @param <code>PrintWriter</code> the writer to use.
     174   */
     175  public boolean write(PrintWriter writer)
     176  { // if this is a non-file block of metadata, write it out in long hand
     177    if (this.location == null)
     178    { String tag = XMLTools.getOpenTag("mets", "mdWrap");
     179      tag = XMLTools.addAttribute(tag, "MDType", this.name);
     180      if (this.id != null) {
     181    tag = XMLTools.addAttribute(tag, "ID", this.id);
     182      }
     183      writer.println(tag);
     184
     185      Iterator items = this.metadataList.iterator();
     186     
     187      while (items.hasNext())
     188      { NamespaceItem item = (NamespaceItem) items.next();
     189        this.writeItem(writer, item);
     190      }
     191      writer.println("</mets:mdWrap>");
     192    }
     193    // otherwise, drop the metadata out in a simplified file-reference
     194    // form only
     195    else
     196    { String tag = XMLTools.getOpenTag("mets", "mdRef");
     197      tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
     198      tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
     199      tag = XMLTools.addAttribute(tag, "MDTYPE", this.name);
     200      if (this.id != null) {
     201    tag = XMLTools.addAttribute(tag, "ID", this.id);
     202      }
     203      tag = XMLTools.makeSingleton(tag);
     204    }
     205    return true;
     206  }
     207
     208  /**
     209   *  Write out a single element - this may be overloaded to provide for the
     210   *  appropriate formatting for this metadata.
     211   */
     212  protected boolean writeItem(PrintWriter writer, NamespaceItem item)
     213  { // Do some default sillinesses
     214    writer.write(XMLTools.getOpenTag(this.name, item.getLabel()));
     215   
     216    writer.write(item.getValue());
     217   
     218    writer.write(XMLTools.getCloseTag(this.name, item.getLabel()));
     219    return true;
     220  }
     221 
     222  /**
     223   *  <p>Indicate whether this metadata is open to being changed or not.</p>
     224   *  <p>Metadata which is created from a distinct file cannot be changed,
     225   *  only those which have no associated file can be modified.
     226   *
     227   *    @return <code>boolean</code> whether this namespace can be altered.
     228   */
     229  public boolean isEditable()
     230  { return (this.location == null);
     231  }
    226232}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/SimpleNamespace.java

    r5800 r5945  
    99
    1010import java.sql.SQLException;
     11import java.sql.ResultSet;
    1112
    1213import org.w3c.dom.Document;
     
    3031public class SimpleNamespace extends METSNamespace
    3132{
    32     MultiMap metadataMap;
    33 
    34     public SimpleNamespace(String name)
    35     { super(name);
    36         this.metadataMap = new MultiMap();
    37         System.out.println("Namespace " + name);
     33  MultiMap metadataMap;
     34
     35  public SimpleNamespace(String name)
     36  { super(name);
     37    this.metadataMap = new MultiMap();
     38    System.out.println("Namespace " + name);
     39  }
     40
     41  public SimpleNamespace(String name, METSLocation location)
     42  { super(name, location);
     43    this.metadataMap = new MultiMap();
     44  }
     45
     46  public SimpleNamespace(String name, Element mdWrapTag)
     47  { super(name);
     48    this.metadataMap = new MultiMap();
     49   
     50    NodeList childNodes = mdWrapTag.getChildNodes();
     51    for (int c = 0; c < childNodes.getLength(); c ++)
     52    { // a metadata node
     53      if (childNodes.item(c).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
     54      { // get the name of the metadata from the node
     55    String metadataLabel = childNodes.item(c).getNodeName();
     56
     57    // skip blank labels
     58    // TODO: raise an error: metadata item without metadata label set
     59    if (metadataLabel == null || metadataLabel.length() == 0)
     60    { continue;
    3861    }
    3962
    40     public SimpleNamespace(String name, METSLocation location)
    41     { super(name, location);
    42         this.metadataMap = new MultiMap();
     63    // build the value
     64    String metadataValue = "";
     65
     66    Element childElement = (Element) childNodes.item(c);
     67    for (int i = 0; i < childElement.getChildNodes().getLength(); i ++)
     68    { metadataValue = metadataValue + childElement.getChildNodes().item(i).toString();
    4369    }
    4470
    45     public SimpleNamespace(String name, Element mdWrapTag)
    46     {   super(name);
    47         this.metadataMap = new MultiMap();
    48 
    49         NodeList childNodes = mdWrapTag.getChildNodes();
    50         for (int c = 0; c < childNodes.getLength(); c ++)
    51         {   // a metadata node
    52             if (childNodes.item(c).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
    53             {   // get the name of the metadata from the node
    54                 String metadataLabel = childNodes.item(c).getNodeName();
    55 
    56                 // skip blank labels
    57                 // TODO: raise an error: metadata item without metadata label set
    58                 if (metadataLabel == null || metadataLabel.length() == 0)
    59                 {   continue;
    60                 }
    61 
    62                 // build the value
    63                 String metadataValue = "";
    64 
    65                 Element childElement = (Element) childNodes.item(c);
    66                 for (int i = 0; i < childElement.getChildNodes().getLength(); i ++)
    67                 {   metadataValue = metadataValue + childElement.getChildNodes().item(i).toString();
    68                 }
    69 
    70                 // trim any leading namespace identifiers in "namespace:label" metadata
    71                 if (metadataLabel.startsWith(this.name + ":"))
    72                 {   metadataLabel = metadataLabel.substring(this.name.length() + 1);
    73                 }
    74 
    75                 this.addMetadata(metadataLabel, metadataValue);
    76             }
    77         }
     71    // trim any leading namespace identifiers in "namespace:label" metadata
     72    if (metadataLabel.startsWith(this.name + ":"))
     73    { metadataLabel = metadataLabel.substring(this.name.length() + 1);
    7874    }
    7975
    80     public boolean validate(String field, String value)
    81     {   return true;
    82     }
    83 
    84     /**
    85      *  Add a metadata item.  Whether the field and value validate for this
    86      *  schema will also be tested.
    87      *
    88      *  @param <code>String</code> the name of the field to be given the value
    89      *  @param <code>String</code> the value to be assigned
    90      *
    91      *  @return <code>boolean</code> whether the field value was added.  This
    92      *          would return <code>false</code> if the values did not validate,
    93      *          for example.
    94      */
    95     public boolean addMetadata(String label, String value)
    96     {   if (!this.validate(label, value)) {
    97             return false;
    98         }
    99 
    100         this.metadataMap.put(label, value);
    101         return true;
    102     }
    103 
    104     /**
    105      *  Assign a metadata item.  Whether the field and value validate for this
    106      *  schema will also be tested.  Any existing metadata for that field will
    107      *  be destroyed if the new value validates.
    108      *
    109      *  @param <code>String</code> the name of the field to be given the value
    110      *  @param <code>String</code> the value to be assigned
    111      *
    112      *  @return <code>boolean</code> whether the field value was added.  This
    113      *          would return <code>false</code> if the values did not validate,
    114      *          for example.
    115      */
    116     public boolean setMetadata(String label, String value)
    117     { if (!this.validate(label, value))
    118         { return false;
    119         }
    120 
    121         this.metadataMap.setOnly(label, value);
    122         return true;
    123     }
    124 
    125     /**
    126      *  Remove all metadata values for a given field name
    127      *
    128      *  @param <code>String</code> the field to delete
    129      *
    130      *  @return <code>boolean</code> whether the field was actually deleted;
    131      *          will return <code>true</code> if the field was already empty.
    132      */
    133     public boolean removeMetadata(String label)
    134     { this.metadataMap.remove(label);
    135         return true;
    136     }
    137 
    138     /**
    139      *  Remove a particular incidence of a given metadata field for a document.
    140      *  If an exact match for the given value is not found, nothing changes.
    141      *  N.B. if a value occurs twice, only the first incidence of it will be
    142      *  deleted from the list.
    143      *
    144      *  @param <code>String</code> the field to have the value removed
    145      *  @param <code>String</code> the value to be removed from a given field
    146      *
    147      *  @return <code>boolean</code> <code>true</code> if an actual metadata text
    148      *          is matched against the given value and is thus deleted.
    149      */
    150     public boolean removeMetadata(String label, String value)
    151     { return this.metadataMap.remove(label, value);
    152     }
    153 
    154     /**
    155      *  Get the metadata items for a particular label
    156      *
    157      *  @param <code>String</code> the label to fetch values for
    158      * 
    159      *  @return <code>List</code> the list of corresponding values.  May be
    160      *          <code>null</code>
    161      */
    162     public List getMetadata(String label)
    163     {   return (List) this.metadataMap.get(label);
    164     }
     76    this.addMetadata(metadataLabel, metadataValue);
     77      }
     78    }
     79  }
     80
     81  public boolean validate(String field, String value)
     82  { return true;
     83  }
     84
     85  /**
     86   *  Add a metadata item.  Whether the field and value validate for this
     87   *  schema will also be tested.
     88   *
     89   *  @param <code>String</code> the name of the field to be given the value
     90   *  @param <code>String</code> the value to be assigned
     91   *
     92   *  @return <code>boolean</code> whether the field value was added.  This
     93   *          would return <code>false</code> if the values did not validate,
     94   *          for example.
     95   */
     96  public boolean addMetadata(String label, String value)
     97  { if (!this.validate(label, value)) {
     98      return false;
     99    }
     100
     101    this.metadataMap.put(label, value);
     102    return true;
     103  }
     104
     105  /**
     106   *  Assign a metadata item.  Whether the field and value validate for this
     107   *  schema will also be tested.  Any existing metadata for that field will
     108   *  be destroyed if the new value validates.
     109   *
     110   *  @param <code>String</code> the name of the field to be given the value
     111   *  @param <code>String</code> the value to be assigned
     112   *
     113   *  @return <code>boolean</code> whether the field value was added.  This
     114   *          would return <code>false</code> if the values did not validate,
     115   *          for example.
     116   */
     117  public boolean setMetadata(String label, String value)
     118  { if (!this.validate(label, value))
     119    { return false;
     120    }
     121
     122    this.metadataMap.setOnly(label, value);
     123    return true;
     124  }
     125
     126  /**
     127   *  Remove all metadata values for a given field name
     128   *
     129   *  @param <code>String</code> the field to delete
     130   *
     131   *  @return <code>boolean</code> whether the field was actually deleted;
     132   *          will return <code>true</code> if the field was already empty.
     133   */
     134  public boolean removeMetadata(String label)
     135  { this.metadataMap.remove(label);
     136    return true;
     137  }
     138
     139  /**
     140   *  Remove a particular incidence of a given metadata field for a document.
     141   *  If an exact match for the given value is not found, nothing changes.
     142   *  N.B. if a value occurs twice, only the first incidence of it will be
     143   *  deleted from the list.
     144   *
     145   *  @param <code>String</code> the field to have the value removed
     146   *  @param <code>String</code> the value to be removed from a given field
     147   *
     148   *  @return <code>boolean</code> <code>true</code> if an actual metadata text
     149   *          is matched against the given value and is thus deleted.
     150   */
     151  public boolean removeMetadata(String label, String value)
     152  { return this.metadataMap.remove(label, value);
     153  }
     154
     155  /**
     156   *  Get the metadata items for a particular label
     157   *
     158   *  @param <code>String</code> the label to fetch values for
     159   * 
     160   *  @return <code>List</code> the list of corresponding values.  May be
     161   *          <code>null</code>
     162   */
     163  public List getMetadata(String label)
     164  { return this.metadataMap.getAll(label);
     165  }
    165166
    166167  /**
     
    175176     
    176177      tag = XMLTools.addAttribute(tag, "MDType", this.name);
     178      if (this.id != null) {
     179    tag = XMLTools.addAttribute(tag, "ID", this.id);
     180      }
    177181      writer.println(tag);
    178182           
     
    197201      tag = XMLTools.addAttribute(tag, "xlink:href", location.getLocation().toString());
    198202      tag = XMLTools.addAttribute(tag, "MDType", this.name);
     203      if (this.id != null) {
     204    tag = XMLTools.addAttribute(tag, "ID", this.id);
     205      }
    199206      writer.println(tag);
    200207
     
    204211  }
    205212
    206     /**
    207     *  Write out a single element - this may be overloaded to provide for the
    208     *  appropriate formatting for this metadata.
    209     */
    210     protected boolean writeItem(PrintWriter writer, String label)
    211     {   if (this.location == null)
    212         { // just place the metadata in a simple wrapper
    213             Iterator values = this.metadataMap.getAll(label).iterator();
    214 
    215             while (values.hasNext())
    216             {   String value = values.next().toString();
    217 
    218                 writer.write(XMLTools.getOpenTag(this.name, label));
    219 
    220                 writer.write(value);
    221 
    222                 writer.println(XMLTools.getCloseTag(this.name, label));
    223             }
    224         }
    225         else
    226         {   String tag = XMLTools.getOpenTag("mets", "mdRef");
    227             tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
    228             tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
    229             tag = XMLTools.addAttribute(tag, "MDTYPE", this.getName());
    230             tag = XMLTools.makeSingleton(tag);
    231             writer.println(tag);
    232         }
    233         return true;
    234     }
     213  /**
     214  *  Write out a single element - this may be overloaded to provide for the
     215  *  appropriate formatting for this metadata.
     216  */
     217  protected boolean writeItem(PrintWriter writer, String label)
     218  { if (this.location == null)
     219    { // just place the metadata in a simple wrapper
     220      Iterator values = this.metadataMap.getAll(label).iterator();
     221
     222      while (values.hasNext())
     223      { String value = values.next().toString();
     224
     225        writer.write(XMLTools.getOpenTag(this.name, label));
     226   
     227    writer.write(value);
     228   
     229    writer.println(XMLTools.getCloseTag(this.name, label));
     230      }
     231    }
     232    else
     233    { String tag = XMLTools.getOpenTag("mets", "mdRef");
     234      tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
     235      tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
     236      tag = XMLTools.addAttribute(tag, "MDTYPE", this.getName());
     237      tag = XMLTools.makeSingleton(tag);
     238      writer.println(tag);
     239    }
     240    return true;
     241  }
    235242
    236243  /**
     
    241248  public boolean writeSQL(int parentId, GS3SQLConnection connection)
    242249  { // write the general stuff
    243     int sqlId;
    244     super.writeSQL(parentId, connection);
     250    String sqlId;
     251
     252    if (!super.writeSQL(parentId, connection)) {
     253      return false;
     254    }
    245255
    246256    try {
    247 
     257      if (this.id == null) {
    248258    GS3SQLSelect select = new GS3SQLSelect("namespaces");
    249259    select.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE)));
    250260    select.addField("NamespaceRef");
    251     System.out.println(select.toString());
    252261    connection.execute(select.toString());
    253262   
    254     connection.getResultSet().first();
    255     sqlId = connection.getResultSet().getInt("NamespaceRef");
    256 
    257     Iterator keys = this.metadataMap.keySet().iterator();
    258     while (keys.hasNext())
    259     { String thisKey = keys.next().toString();
     263    ResultSet results = connection.getResultSet();
     264    results.first();
     265    sqlId = Integer.toString(results.getInt("NamespaceRef"));
     266      }
     267      else {
     268    sqlId = this.id;
     269      }
     270
     271      // clear all the existing metadata items for this namespace
     272      GS3SQLDelete delete = new GS3SQLDelete("mdvalues");
     273      GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", sqlId, GS3SQLField.INTEGER_TYPE));
     274      delete.setWhere(where);
     275      connection.execute(delete.toString());
     276
     277      // write out the metadata for this namespace
     278      Iterator keys = this.metadataMap.keySet().iterator();
     279      while (keys.hasNext())
     280      { String thisKey = keys.next().toString();
    260281   
    261       Iterator values = this.metadataMap.getAll(thisKey).iterator();
     282        Iterator values = this.metadataMap.getAll(thisKey).iterator();
    262283     
    263       while (values.hasNext())
    264       { String value = values.next().toString();
     284    while (values.hasNext())
     285    { String value = values.next().toString();
    265286     
    266 
    267         GS3SQLInsert insert = new GS3SQLInsert("mdvalues");
    268         insert.addValue("NamespaceRef", Integer.toString(sqlId), GS3SQLField.INTEGER_TYPE);
    269         insert.addValue("label", thisKey);
    270         insert.addValue("value", value);
    271         System.out.println(insert.toString());
    272         connection.execute(insert.toString());
    273       }
     287      GS3SQLInsert insert = new GS3SQLInsert("mdvalues");
     288      insert.addValue("NamespaceRef", sqlId, GS3SQLField.INTEGER_TYPE);
     289      insert.addValue("label", thisKey);
     290      insert.addValue("value", value);
     291      connection.execute(insert.toString());
    274292    }
     293      }
    275294    }
    276295    catch (SQLException sql) {
    277     System.out.println(sql);
    278     }
    279     return true;
    280     }
    281 
    282 
    283     /**
    284     *  <p>Indicate whether this metadata is open to being changed or not.</p>
    285     *  <p>Metadata which is created from a distinct file cannot be changed,
    286     *  only those which have no associated file can be modified.
    287     *
    288     *  @return <code>boolean</code> whether this namespace can be altered.
    289     */
    290     public boolean isEditable()
    291     {   return (this.location == null);
    292     }
     296      System.out.println(sql);
     297    }
     298    return true;
     299  }
     300
     301
     302  /**
     303  *  <p>Indicate whether this metadata is open to being changed or not.</p>
     304  *  <p>Metadata which is created from a distinct file cannot be changed,
     305  *  only those which have no associated file can be modified.
     306  *
     307  *    @return <code>boolean</code> whether this namespace can be altered.
     308  */
     309  public boolean isEditable()
     310  { return (this.location == null);
     311  }
    293312}
Note: See TracChangeset for help on using the changeset viewer.