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

modifications for the validated METS format and program layout change

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

Legend:

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

    r8461 r8700  
    2323public class METSStructure extends AbstractStructure
    2424{
    25   String label;
    26   String type;
     25    String label;
     26    String type;
    2727    METSDivision divGroup;
    2828
    29   public static final String STRUCTURE_TYPE = "Structure";
     29    public static final String STRUCTURE_TYPE = "Structure";
    3030 
    31   public METSStructure(String id, String label, String type)
    32   { super(id);
    33 
    34     this.label    = label;
    35     this.type     = type;
    36     this.divGroup = null;
    37   }
    38 
    39   /**
    40    *  Show what sort of <code>AbstractStructure</code> this structure is...
    41    *
    42    *  @return <code>String The identifying string for a <code>METSStructure</code> type.
    43    */
    44   public String getStructureType()
    45   { return STRUCTURE_TYPE;
    46   }
     31    public METSStructure(String id, String label, String type)
     32    {
     33    super(id);
     34   
     35    this.label    = label;
     36    this.type     = type;
     37    this.divGroup = null;
     38    }
     39
     40    /**
     41     *  Show what sort of <code>AbstractStructure</code> this structure is...
     42     *
     43     *  @return <code>String The identifying string for a <code>METSStructure</code> type.
     44     */
     45    public String getStructureType()
     46    { return STRUCTURE_TYPE;
     47    }
    4748 
    48   /**
    49    *  Find all the divisions that match a given list of file group
    50    *  identifiers...
    51    *
    52    *  @param <code>List</code> the list of identifiers to find
    53    *  @param <code>List</code> the list into which to place any
    54    *         matching identifiers.
    55    */
    56   public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList)
    57   { Iterator groups = this.children.values().iterator();
     49    /**
     50     *  Find all the divisions that match a given list of file group
     51     *  identifiers...
     52     *
     53     *  @param <code>List</code> the list of identifiers to find
     54     *  @param <code>List</code> the list into which to place any
     55     *         matching identifiers.
     56     */
     57    public void findDivisionsForFiles(List listOfFileIdentifiers, List resultList)
     58    { Iterator groups = this.children.values().iterator();
    5859    while (groups.hasNext())
    59     { METSDivision group = (METSDivision) groups.next();
    60 
    61       group.findDivisionsForFiles(listOfFileIdentifiers, resultList);
    62     }   
    63   }
    64 
    65   /**
    66    *  Add a division further into this structure.
    67    *
    68    *  @param <code>String</code> the identifier label for the division
    69    *         within which the division is to be added.
    70    *  @param <code>METSDivision</code> the division to add.   
    71    */
    72   public void addSubDivision(String divisionLabel, METSDivision division)
    73   { String topDivision = METSDivision.getTopDivisionName(divisionLabel);
     60    { METSDivision group = (METSDivision) groups.next();
     61
     62    group.findDivisionsForFiles(listOfFileIdentifiers, resultList);
     63    }   
     64    }
     65
     66    /**
     67     *  Add a division further into this structure.
     68     *
     69     *  @param <code>String</code> the identifier label for the division
     70     *         within which the division is to be added.
     71     *  @param <code>METSDivision</code> the division to add.   
     72     */
     73    public void addSubDivision(String divisionLabel, METSDivision division)
     74    { String topDivision = METSDivision.getTopDivisionName(divisionLabel);
    7475
    7576    METSDivision child = (METSDivision) this.children.get(topDivision);
    7677    if (child == null)
    77     { return;
    78     }
     78    { return;
     79    }
    7980
    8081    child = child.getDivision(divisionLabel);
    8182
    8283    if (child != null) {
    83       child.addDivision(division);
    84     }
    85   }
    86 
    87   /**
    88    *  Get a subdivision of a given identifier...
    89    *
    90    *  @param <code>String</code> the identifier of the division
    91    *  @return <code>METSDivision</code> the division, which will be
    92    *          <code>null</code> if it is not found.
    93    */
    94   public METSDivision getDivision(String divisionLabel)
    95   { String topDivision = METSDivision.getTopDivisionName(divisionLabel);
    96 
    97   if (!topDivision.equals("All"))
    98     System.out.println("Top division is " + topDivision);
     84    child.addDivision(division);
     85    }
     86    }
     87
     88    /**
     89     *  Get a subdivision of a given identifier...
     90     *
     91     *  @param <code>String</code> the identifier of the division
     92     *  @return <code>METSDivision</code> the division, which will be
     93     *          <code>null</code> if it is not found.
     94     */
     95    public METSDivision getDivision(String divisionLabel)
     96    { String topDivision = METSDivision.getTopDivisionName(divisionLabel);
     97
     98    if (!topDivision.equals("All"))
     99    System.out.println("Top division is " + topDivision);
    99100    METSDivision child = (METSDivision) this.children.get(topDivision);
    100101    if (child == null)
    101       { System.out.println("No child found");
    102       Iterator groups = this.children.keySet().iterator();
    103       while (groups.hasNext()) {
    104     System.out.println(groups.next().toString());
    105       }
    106       return null;
    107     }
     102    { System.out.println("No child found");
     103    Iterator groups = this.children.keySet().iterator();
     104    while (groups.hasNext()) {
     105        System.out.println(groups.next().toString());
     106    }
     107    return null;
     108    }
    108109
    109110
     
    111112
    112113    return child;
    113   }
    114 
    115 
    116      /*
    117       *
    118       * parse an XML Element as a METSStructure Section
    119       *
    120       * @param <code>Element</code> the XML element which represents
    121       *  the mets:structMap itself
    122       */
     114    }
     115
     116
     117    /*
     118     *
     119     * parse an XML Element as a METSStructure Section
     120     *
     121     * @param <code>Element</code> the XML element which represents
     122     *  the mets:structMap itself
     123     */
    123124   
    124      public static METSStructure parseXML(Element element)
    125      {
     125    public static METSStructure parseXML(Element element)
     126    {
    126127    String structId = element.getAttribute("ID");
    127     String structType = element.getAttribute("TYPE");
    128128    String structLabel = element.getAttribute("LABEL");
     129    String structType = element.getAttribute("TYPE");
    129130 
    130     METSStructure thisStruct = new METSStructure (structId, structType, structLabel);
     131    METSStructure thisStruct = new METSStructure (structId, structLabel, structType);
    131132   
    132133    NodeList divSecs = element.getChildNodes();
     
    147148    }
    148149    return thisStruct;
    149      }
     150    }
    150151 
    151   /**
    152    *  Write this structure in XML(METS) format to a <code>PrintWriter</code>
    153    */
    154   public void write(PrintWriter writer)
    155   { Iterator groups = this.children.values().iterator();
    156 
    157     String tag = XMLTools.getOpenTag("mets", "structMap");
    158     tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
    159     tag = XMLTools.addAttribute(tag, "TYPE", this.type);
    160     tag = XMLTools.addAttribute(tag, "LABEL", this.label);
    161        
    162     writer.println(tag);
    163     while (groups.hasNext())
    164     { METSDivision group = (METSDivision) groups.next();
    165 
    166       group.write(writer);
    167     }
    168     writer.println("</mets:structMap>");
    169   }
    170 
    171 
    172   /**
    173    *  Write this METSStructure to an SQL database....
    174    *
    175    *  @param <code>DocumentInterface</code> the enclosing document.
    176    *  @param <code>GS3SQLConnection</code> the SQL database connection.
    177    */
    178   public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
    179   { int sqlRef = -1;
    180     ResultSet results;
    181 
    182     // Prepare query to see if this structure has already been written
    183     GS3SQLSelect select = new GS3SQLSelect("structure");
    184     select.addField("StructureRef");
    185     GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
    186     GS3SQLWhereItem item = new GS3SQLWhereItem("StructureID", "=", this.ID.toString());
    187     GS3SQLWhere where = new GS3SQLWhere(whereItem);
    188     where.add(item);
    189     select.setWhere(where);
    190 
    191     // attempt to execute the query & get the current structure's reference
    192     try {
    193       connection.execute(select.toString());
    194       results = connection.getResultSet();
    195       if (results != null &&
    196       results.first())
    197       { sqlRef = results.getInt("structureRef");
    198       }
    199       else
    200       { results = null;
    201       }
     152    /**
     153     *  Write this structure in XML(METS) format to a <code>PrintWriter</code>
     154     */
     155    public void write(PrintWriter writer)
     156    {
     157    Iterator groups = this.children.values().iterator();
     158   
     159    String tag = XMLTools.getOpenTag("mets", "structMap");
     160    tag = XMLTools.addAttribute(tag, "ID", this.ID.toString());
     161    tag = XMLTools.addAttribute(tag, "TYPE", this.type);
     162    tag = XMLTools.addAttribute(tag, "LABEL", this.label);
     163   
     164    writer.println(tag);
     165    while (groups.hasNext()){
     166        METSDivision group = (METSDivision) groups.next();
     167       
     168        group.write(writer);
     169    }
     170    writer.println("</mets:structMap>");
     171    }
     172   
     173   
     174    /**
     175     *  Write this METSStructure to an SQL database....
     176     *
     177     *  @param <code>DocumentInterface</code> the enclosing document.
     178     *  @param <code>GS3SQLConnection</code> the SQL database connection.
     179     */
     180    public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
     181    {
     182    int sqlRef = -1;
     183    ResultSet results;
     184
     185    // Prepare query to see if this structure has already been written
     186    GS3SQLSelect select = new GS3SQLSelect("structure");
     187    select.addField("StructureRef");
     188    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     189    GS3SQLWhereItem item = new GS3SQLWhereItem("StructureID", "=", this.ID.toString());
     190    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     191    where.add(item);
     192    select.setWhere(where);
     193
     194    // attempt to execute the query & get the current structure's reference
     195    try {
     196        connection.execute(select.toString());
     197        results = connection.getResultSet();
     198        if (results != null &&
     199        results.first()){
     200        sqlRef = results.getInt("structureRef");
     201        }
     202        else {
     203        results = null;
     204        }
     205       
     206        if (results != null) {
     207        results.close();
     208        }
     209    }
     210    catch (SQLException sqlEx) {
     211        System.err.print(sqlEx);
     212        return false;
     213    }
     214   
     215    // insert a new structure if it wasn't there previously
     216    if (results == null) {
     217        GS3SQLInsert insert = new GS3SQLInsert("structure");
     218        insert.addValue("DocID", document.getID().toString());
     219        insert.addValue("StructureID", this.ID.toString());
     220        insert.addValue("StructureType", this.type);
     221        insert.addValue("Label", this.label);
     222       
     223        if (!connection.execute(insert.toString())) {
     224        return false;
     225        }
     226       
     227        // get the new structure reference by re-running the original select object
     228        connection.execute(select.toString());
     229
     230        try {
     231        results = connection.getResultSet();
     232        results.first();
     233        sqlRef = results.getInt("StructureRef");
     234        }
     235        catch (SQLException sql) {
     236        System.err.println(sql);
     237        return false;
     238        }
     239    }
     240    else {
     241        GS3SQLUpdate update = new GS3SQLUpdate("structure");
     242        update.setWhere(where);
     243       
     244        update.addValue("StructureType", this.type);
     245        update.addValue("Label", this.label);
     246       
     247        connection.execute(update.toString());
     248    }
     249   
     250    // write out the child groups (Divisions) now...   
     251    Iterator groups = this.children.values().iterator();
     252   
     253    while (groups.hasNext()){
     254        METSDivision group = (METSDivision) groups.next();
     255       
     256        if (!group.writeSQL(document.getID(), sqlRef, true, connection)){
     257        return false;
     258        }
     259    }
     260    return true;
     261    }
     262   
     263    /**
     264     *  Read a METSStructure from a database - on entry, the current item in the
     265     *  <code>ResultSet</code> holds the row in the database with the structure's
     266     *  data.
     267     *
     268     *  @param <code>DocumentInterface</code> the document which owns the structure
     269     *  @param <code>GS3SQLConnection</code> the database connection itself, to load
     270     *         child objects through, etc.
     271     *  @param <code>ResultSet</code> pointing to the current row in the database.
     272     */
     273    public static METSStructure readSQL(DocumentInterface document, GS3SQLConnection connection,
     274                    ResultSet resultSet)
     275    {
     276    GS3SQLSelect select = null;
     277   
     278    try {
     279        String ID    = resultSet.getString("StructureID");
     280        String type  = resultSet.getString("StructureType");
     281        String label = resultSet.getString("Label");
     282
     283        // create the metadata block object
     284        METSStructure structure = new METSStructure(ID, label, type);
    202285     
    203       if (results != null) {
    204     results.close();
    205       }
    206     }
    207     catch (SQLException sqlEx) {
    208       System.err.print(sqlEx);
    209       return false;
    210     }
    211 
    212     // insert a new structure if it wasn't there previously
    213     if (results == null) {
    214       GS3SQLInsert insert = new GS3SQLInsert("structure");
    215       insert.addValue("DocID", document.getID().toString());
    216       insert.addValue("StructureID", this.ID.toString());
    217       insert.addValue("StructureType", this.type);
    218       insert.addValue("Label", this.label);
    219      
    220       if (!connection.execute(insert.toString())) {
    221     return false;
    222       }
    223 
    224       // get the new structure reference by re-running the original select object
    225       connection.execute(select.toString());
    226 
    227       try {
    228     results = connection.getResultSet();
    229     results.first();
    230     sqlRef = results.getInt("StructureRef");
    231       }
    232       catch (SQLException sql) {
    233     System.err.println(sql);
    234     return false;
    235       }
    236     }
    237     else {
    238       GS3SQLUpdate update = new GS3SQLUpdate("structure");
    239       update.setWhere(where);
    240 
    241       update.addValue("StructureType", this.type);
    242       update.addValue("Label", this.label);
    243 
    244       connection.execute(update.toString());
    245     }
    246      
    247     // write out the child groups (Divisions) now...   
    248     Iterator groups = this.children.values().iterator();
    249 
    250     while (groups.hasNext())
    251     { METSDivision group = (METSDivision) groups.next();
    252 
    253       if (!group.writeSQL(document.getID(), sqlRef, true, connection))
    254       { return false;
    255       }
    256     }
    257     return true;
    258   }
    259 
    260   /**
    261    *  Read a METSStructure from a database - on entry, the current item in the
    262    *  <code>ResultSet</code> holds the row in the database with the structure's
    263    *  data.
    264    *
    265    *  @param <code>DocumentInterface</code> the document which owns the structure
    266    *  @param <code>GS3SQLConnection</code> the database connection itself, to load
    267    *         child objects through, etc.
    268    *  @param <code>ResultSet</code> pointing to the current row in the database.
    269    */
    270   public static METSStructure readSQL(DocumentInterface document, GS3SQLConnection connection,
    271                       ResultSet resultSet)
    272   {
    273     GS3SQLSelect select = null;
    274 
    275     try {
    276       String ID    = resultSet.getString("StructureID");
    277       String type  = resultSet.getString("StructureType");
    278       String label = resultSet.getString("Label");
    279 
    280       // create the metadata block object
    281       METSStructure structure = new METSStructure(ID, label, type);
    282      
    283       // get its metadata reference to retrieve divisions
    284       int structureRef = resultSet.getInt("StructureRef");
    285 
    286       // query the database for matching division for this structure block
    287       select = new GS3SQLSelect("divisions");
    288       select.addField("*");
    289       GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(structureRef),
    290                               GS3SQLField.INTEGER_TYPE);
    291       GS3SQLWhere where = new GS3SQLWhere(whereItem);
    292       whereItem = new GS3SQLWhereItem("ParentType", "=", METSStructure.STRUCTURE_TYPE);
    293       where.add(whereItem);
    294       select.setWhere(where);
    295 
    296       connection.execute(select.toString());
    297 
    298       // parse through the divisions
    299       ResultSet divisionSet = connection.getResultSet();
    300       if (divisionSet != null && divisionSet.first()) {
    301     do {
    302       METSDivision division = METSDivision.readSQL(connection, divisionSet);
    303       if (division != null) {
    304         structure.addDivision(division);
    305       }
    306     }
    307     while (divisionSet.next());
    308       }
    309 
    310       return structure;
    311     }
    312     catch (SQLException sqlEx)
    313     { System.out.println(sqlEx + " " + select.toString());
    314       System.exit(1);
    315     }
    316     return null;
    317   }
     286        // get its metadata reference to retrieve divisions
     287        int structureRef = resultSet.getInt("StructureRef");
     288
     289        // query the database for matching division for this structure block
     290        select = new GS3SQLSelect("divisions");
     291        select.addField("*");
     292        GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(structureRef),
     293                                GS3SQLField.INTEGER_TYPE);
     294        GS3SQLWhere where = new GS3SQLWhere(whereItem);
     295        whereItem = new GS3SQLWhereItem("ParentType", "=", METSStructure.STRUCTURE_TYPE);
     296        where.add(whereItem);
     297        select.setWhere(where);
     298
     299        connection.execute(select.toString());
     300
     301        // parse through the divisions
     302        ResultSet divisionSet = connection.getResultSet();
     303        if (divisionSet != null && divisionSet.first()) {
     304        do {
     305            METSDivision division = METSDivision.readSQL(connection, divisionSet);
     306            if (division != null) {
     307            structure.addDivision(division);
     308            }
     309        }
     310        while (divisionSet.next());
     311        }
     312
     313        return structure;
     314    }
     315    catch (SQLException sqlEx)
     316        { System.out.println(sqlEx + " " + select.toString());
     317        System.exit(1);
     318        }
     319    return null;
     320    }
    318321}
    319322
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/OrderedNamespace.java

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

    r8461 r8700  
    3131public class SimpleNamespace extends METSNamespace
    3232{
    33   MultiMap metadataMap;
    34 
    35   public SimpleNamespace(String name)
    36   { super(name);
    37     this.metadataMap = new MultiMap();
    38   }
    39 
    40   public SimpleNamespace(String name, METSLocation location)
    41   { super(name, location);
    42     this.metadataMap = new MultiMap();
    43   }
    44 
    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     }
    78   }
    79 
    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 this.metadataMap.getAll(label);
    164   }
    165 
     33    MultiMap metadataMap;
     34   
     35    public SimpleNamespace(String name)
     36    {
     37    super(name);
     38    this.metadataMap = new MultiMap();
     39    }
     40   
     41    public SimpleNamespace(String name, METSLocation location)
     42    {
     43    super(name, location);
     44    this.metadataMap = new MultiMap();
     45    }
     46   
     47    public SimpleNamespace(String name, Element mdWrapTag)
     48    {
     49    super(name);
     50    this.metadataMap = new MultiMap();
     51   
     52    NodeList childNodes = mdWrapTag.getChildNodes();
     53    for (int c = 0; c < childNodes.getLength(); c ++) {
     54        // a metadata node
     55        if (childNodes.item(c).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
     56        // get the name of the metadata from the node
     57        String metadataLabel = childNodes.item(c).getNodeName();
     58       
     59        // skip blank labels
     60        // TODO: raise an error: metadata item without metadata label set
     61        if (metadataLabel == null || metadataLabel.length() == 0) {
     62            continue;
     63        }
     64       
     65        // build the value
     66        String metadataValue = "";
     67       
     68        Element childElement = (Element) childNodes.item(c);
     69        for (int i = 0; i < childElement.getChildNodes().getLength(); i ++) {
     70            metadataValue = metadataValue + childElement.getChildNodes().item(i).toString();
     71        }
     72       
     73        // trim any leading namespace identifiers in "namespace:label" metadata
     74        if (metadataLabel.startsWith(this.name + ":")) {
     75            metadataLabel = metadataLabel.substring(this.name.length() + 1);
     76        }
     77       
     78        this.addMetadata(metadataLabel, metadataValue);
     79        }
     80    }
     81    }
     82   
     83    public boolean validate(String field, String value)
     84    {
     85    return true;
     86    }
     87   
     88    /**
     89     *  Add a metadata item.  Whether the field and value validate for this
     90     *  schema will also be tested.
     91     *
     92     *  @param <code>String</code> the name of the field to be given the value
     93     *  @param <code>String</code> the value to be assigned
     94     *
     95     *  @return <code>boolean</code> whether the field value was added.  This
     96     *          would return <code>false</code> if the values did not validate,
     97     *          for example.
     98     */
     99    public boolean addMetadata(String label, String value)
     100    {
     101    if (!this.validate(label, value)) {
     102        return false;
     103    }
     104   
     105    this.metadataMap.put(label, value);
     106    return true;
     107    }
     108   
     109    /**
     110     *  Assign a metadata item.  Whether the field and value validate for this
     111     *  schema will also be tested.  Any existing metadata for that field will
     112     *  be destroyed if the new value validates.
     113     *
     114     *  @param <code>String</code> the name of the field to be given the value
     115     *  @param <code>String</code> the value to be assigned
     116     *
     117     *  @return <code>boolean</code> whether the field value was added.  This
     118     *          would return <code>false</code> if the values did not validate,
     119     *          for example.
     120     */
     121    public boolean setMetadata(String label, String value)
     122    {
     123    if (!this.validate(label, value)) {
     124        return false;
     125    }
     126   
     127    this.metadataMap.setOnly(label, value);
     128    return true;
     129    }
     130   
     131    /**
     132     *  Remove all metadata values for a given field name
     133     *
     134     *  @param <code>String</code> the field to delete
     135     *
     136     *  @return <code>boolean</code> whether the field was actually deleted;
     137     *          will return <code>true</code> if the field was already empty.
     138     */
     139    public boolean removeMetadata(String label)
     140    {
     141    this.metadataMap.remove(label);
     142    return true;
     143    }
     144   
     145    /**
     146     *  Remove a particular incidence of a given metadata field for a document.
     147     *  If an exact match for the given value is not found, nothing changes.
     148     *  N.B. if a value occurs twice, only the first incidence of it will be
     149     *  deleted from the list.
     150     *
     151     *  @param <code>String</code> the field to have the value removed
     152     *  @param <code>String</code> the value to be removed from a given field
     153     *
     154     *  @return <code>boolean</code> <code>true</code> if an actual metadata text
     155     *          is matched against the given value and is thus deleted.
     156     */
     157    public boolean removeMetadata(String label, String value)
     158    {
     159    return this.metadataMap.remove(label, value);
     160    }
     161   
     162    /**
     163     *  Get the metadata items for a particular label
     164     *
     165     *  @param <code>String</code> the label to fetch values for
     166     * 
     167     *  @return <code>List</code> the list of corresponding values.  May be
     168     *          <code>null</code>
     169     */
     170    public List getMetadata(String label)
     171    {
     172    return this.metadataMap.getAll(label);
     173    }
     174   
    166175    public Iterator getMetadataNames()
    167176    {
    168177    return this.metadataMap.keySet().iterator();
    169178    }
    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      
    181       tag = XMLTools.addAttribute(tag, "MDType", this.name);
    182       if (this.id != null) {
    183     tag = XMLTools.addAttribute(tag, "ID", this.id);
    184       }
    185       writer.println(tag);
    186            
    187       tag = XMLTools.getOpenTag("mets", "xmlData");
    188       writer.println(tag);
    189      
    190       Iterator keys = this.metadataMap.keySet().iterator();
    191       while (keys.hasNext())
    192       { String thisKey = keys.next().toString();
    193         this.writeItem(writer, thisKey);
    194       }
    195 
    196       writer.println(XMLTools.getCloseTag("mets", "xmlData"));
    197      
    198       writer.println(XMLTools.getCloseTag("mets", "mdWrap"));
    199     }
    200     // otherwise, drop the metadata out in a simplified file-reference
    201     // form only
    202     else
    203     { String tag = XMLTools.getOpenTag("mets","mdRef");
    204       tag = XMLTools.addAttribute(tag, "LOCTYPE", "URL");
    205       tag = XMLTools.addAttribute(tag, "xlink:href", location.getLocation().toString());
    206       tag = XMLTools.addAttribute(tag, "MDType", this.name);
    207       if (this.id != null) {
    208     tag = XMLTools.addAttribute(tag, "ID", this.id);
    209       }
    210       writer.println(tag);
    211 
    212       writer.println("</mets:mdRef>");
    213     }
    214     return true;
    215   }
    216 
    217   /**
    218    *  Write out a single element - this may be overloaded to provide for the
    219    *  appropriate formatting for this metadata.
    220    */
    221   protected boolean writeItem(PrintWriter writer, String label)
    222   { if (this.location == null)
    223     { // just place the metadata in a simple wrapper
    224       Iterator values = this.metadataMap.getAll(label).iterator();
    225 
    226       while (values.hasNext())
    227       { String value = values.next().toString();
    228 String metaTagName = "Metadata name=" +'"'+ label +'"';
    229       //writer.write(XMLTools.getOpenTag(this.name, label));
    230     writer.write("         ");
    231     writer.write(XMLTools.getOpenTag(this.name, metaTagName));
    232     writer.write(value);
    233    
    234     //writer.println(XMLTools.getCloseTag(this.name, label));
    235 writer.println(XMLTools.getCloseTag(this.name, "Metadata"));
    236       }
    237     }
    238     else
    239     { String tag = XMLTools.getOpenTag("mets", "mdRef");
    240       tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
    241       tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
    242       tag = XMLTools.addAttribute(tag, "MDTYPE", this.getName());
    243       tag = XMLTools.makeSingleton(tag);
    244       writer.println(tag);
    245     }
    246     return true;
    247   }
    248 
    249   /**
    250    *  Write out the metadata to an SQL database through a <code>GS3SQLConnection</code>.
    251    *
    252    *  @param <code>GS3SQLConnection</code> the SQL database to use.
    253    */
    254   public boolean writeSQL(int parentId, GS3SQLConnection connection)
    255   { // write the general stuff
    256     String sqlId;
    257 
    258     if (!super.writeSQL(parentId, connection)) {
    259       return false;
    260     }
    261 
    262     try {
    263       if (this.id == null) {
    264     GS3SQLSelect select = new GS3SQLSelect("namespaces");
    265     select.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE)));
    266     select.addField("NamespaceRef");
    267     connection.execute(select.toString());
    268    
    269     ResultSet results = connection.getResultSet();
    270     results.first();
    271     sqlId = Integer.toString(results.getInt("NamespaceRef"));
    272       }
    273       else {
    274     sqlId = this.id;
    275       }
    276 
    277       // clear all the existing metadata items for this namespace
    278       GS3SQLDelete delete = new GS3SQLDelete("mdvalues");
    279       GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", sqlId, GS3SQLField.INTEGER_TYPE));
    280       delete.setWhere(where);
    281       connection.execute(delete.toString());
    282 
    283       // write out the metadata for this namespace
    284       Iterator keys = this.metadataMap.keySet().iterator();
    285       while (keys.hasNext())
    286       { String thisKey = keys.next().toString();
    287    
    288         Iterator values = this.metadataMap.getAll(thisKey).iterator();
    289      
    290     while (values.hasNext())
    291     { String value = values.next().toString();
    292      
    293       GS3SQLInsert insert = new GS3SQLInsert("mdvalues");
    294       insert.addValue("NamespaceRef", sqlId, GS3SQLField.INTEGER_TYPE);
    295       insert.addValue("Label", thisKey);
    296       insert.addValue("Value", value);
    297       connection.execute(insert.toString());
    298     }
    299       }
    300     }
    301     catch (SQLException sql) {
    302       System.out.println(sql);
    303     }
    304     return true;
    305   }
    306 
    307 
    308   /**
    309    *  <p>Indicate whether this metadata is open to being changed or not.</p>
    310    *  <p>Metadata which is created from a distinct file cannot be changed,
    311    *  only those which have no associated file can be modified.
    312    *
    313    *    @return <code>boolean</code> whether this namespace can be altered.
    314    */
    315   public boolean isEditable()
    316   { return (this.location == null);
    317   }
     179   
     180    /**
     181     *  Write out the metadata to an XML file through a <code>PrintWriter</code>.
     182     *
     183     *  @param <code>PrintWriter</code> the writer to use.
     184     */
     185    public boolean write(PrintWriter writer)
     186    {
     187    // if this is a non-file block of metadata, write it out in long hand
     188    if (this.location == null){
     189        String tag = XMLTools.getOpenTag("mets", "mdWrap");
     190       
     191        tag = XMLTools.addAttribute(tag, "MDTYPE", "OTHER");
     192        tag = XMLTools.addAttribute(tag, "OTHERMDTYPE", this.name);
     193        if (this.id != null) {
     194        this.id = "gsdl"+this.id;
     195        //System.err.println("###Simple ID=" +this.id);
     196        tag = XMLTools.addAttribute(tag, "ID", this.id);
     197        }
     198        writer.println(tag);
     199       
     200        tag = XMLTools.getOpenTag("mets", "xmlData");
     201        writer.println(tag);
     202       
     203        Iterator keys = this.metadataMap.keySet().iterator();
     204        while (keys.hasNext()){
     205        String thisKey = keys.next().toString();
     206        this.writeItem(writer, thisKey);
     207        }
     208       
     209        writer.println(XMLTools.getCloseTag("mets", "xmlData"));
     210       
     211        writer.println(XMLTools.getCloseTag("mets", "mdWrap"));
     212    }
     213    // otherwise, drop the metadata out in a simplified file-reference
     214    // form only
     215    else
     216        {
     217        String tag = XMLTools.getOpenTag("mets","mdRef");
     218        tag = XMLTools.addAttribute(tag, "LOCTYPE", "URL");
     219        tag = XMLTools.addAttribute(tag, "xlink:href", location.getLocation().toString());
     220        //tag = XMLTools.addAttribute(tag, "MDTYPE", "OTHER");
     221        //tag = XMLTools.addAttribute(tag, "OTHERMDTYPE", this.name);
     222        tag = XMLTools.addAttribute(tag, "MDTYPE", this.name); 
     223        if (this.id != null) {
     224            tag = XMLTools.addAttribute(tag, "ID", this.id);
     225        }
     226        writer.println(tag);
     227       
     228        writer.println("</mets:mdRef>");
     229        }
     230    return true;
     231    }
     232   
     233    /**
     234     *  Write out a single element - this may be overloaded to provide for the
     235     *  appropriate formatting for this metadata.
     236     */
     237    protected boolean writeItem(PrintWriter writer, String label)
     238    {
     239    if (this.location == null) {
     240        // just place the metadata in a simple wrapper
     241        Iterator values = this.metadataMap.getAll(label).iterator();
     242       
     243        while (values.hasNext()) {
     244        String value = values.next().toString();
     245        String metaTagName = "Metadata name=" +'"'+ label +'"';
     246        //writer.write(XMLTools.getOpenTag(this.name, label));
     247        writer.write("         ");
     248        writer.write(XMLTools.getOpenTag(this.name, metaTagName));
     249        writer.write(value);
     250       
     251        //writer.println(XMLTools.getCloseTag(this.name, label));
     252        writer.println(XMLTools.getCloseTag(this.name, "Metadata"));
     253        }
     254    }
     255    else {
     256        String tag = XMLTools.getOpenTag("mets", "mdRef");
     257        tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
     258        tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
     259        tag = XMLTools.addAttribute(tag, "MDTYPE", this.getName());
     260        tag = XMLTools.makeSingleton(tag);
     261        writer.println(tag);
     262    }
     263    return true;
     264    }
     265   
     266    /**
     267     *  Write out the metadata to an SQL database through a <code>GS3SQLConnection</code>.
     268     *
     269     *  @param <code>GS3SQLConnection</code> the SQL database to use.
     270     */
     271    public boolean writeSQL(int parentId, GS3SQLConnection connection)
     272    {
     273    // write the general stuff
     274    String sqlId;
     275   
     276    if (!super.writeSQL(parentId, connection)) {
     277        return false;
     278    }
     279   
     280    try {
     281        if (this.id == null) {
     282        GS3SQLSelect select = new GS3SQLSelect("namespaces");
     283        select.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE)));
     284        select.addField("NamespaceRef");
     285        connection.execute(select.toString());
     286       
     287        ResultSet results = connection.getResultSet();
     288        results.first();
     289        sqlId = Integer.toString(results.getInt("NamespaceRef"));
     290        }
     291        else {
     292        sqlId = this.id;
     293        }
     294       
     295        // clear all the existing metadata items for this namespace
     296        GS3SQLDelete delete = new GS3SQLDelete("mdvalues");
     297        GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", sqlId, GS3SQLField.INTEGER_TYPE));
     298        delete.setWhere(where);
     299        connection.execute(delete.toString());
     300       
     301        // write out the metadata for this namespace
     302        Iterator keys = this.metadataMap.keySet().iterator();
     303        while (keys.hasNext()){
     304        String thisKey = keys.next().toString();
     305       
     306        Iterator values = this.metadataMap.getAll(thisKey).iterator();
     307       
     308        while (values.hasNext()){
     309            String value = values.next().toString();
     310           
     311            GS3SQLInsert insert = new GS3SQLInsert("mdvalues");
     312            insert.addValue("NamespaceRef", sqlId, GS3SQLField.INTEGER_TYPE);
     313            insert.addValue("Label", thisKey);
     314            insert.addValue("Value", value);
     315            connection.execute(insert.toString());
     316        }
     317        }
     318    }
     319    catch (SQLException sql) {
     320        System.out.println(sql);
     321    }
     322    return true;
     323    }
     324   
     325   
     326    /**
     327     *  <p>Indicate whether this metadata is open to being changed or not.</p>
     328     *  <p>Metadata which is created from a distinct file cannot be changed,
     329     *  only those which have no associated file can be modified.
     330     *
     331     *  @return <code>boolean</code> whether this namespace can be altered.
     332     */
     333    public boolean isEditable()
     334    {
     335    return (this.location == null);
     336    }
    318337}
Note: See TracChangeset for help on using the changeset viewer.