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

Extensive additions to metadata

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.