package org.greenstone.gsdl3.gs3build.metadata; import java.io.PrintWriter; import java.util.List; import java.util.Iterator; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import org.greenstone.gsdl3.gs3build.database.*; /** * Implement a single namespace */ public abstract class METSNamespace { protected String type; // the type of the metadata protected String otherType; // if a non-standard type, then... protected String name; // e.g. "DC" for Dublin Core protected METSLocation location; // the location of the corresponding file protected String group; // the group of metadata namespaces to which // this namespace belongs protected String id; // the ID for this namespace public METSNamespace(String name) { this.name = name; this.location = null; this.group = null; this.id = null; } public METSNamespace(String name, METSLocation location) { this.location = location; this.name = name; this.group = null; this.id = null; } /** * Get the name of the namespace... * * @return String the name of the namespace */ public String getName() { return this.name; } /** * Get the group of namespaces to which this namespace belongs. * * @return String the name of the namespace group */ public String getGroup() { return this.group; } /** * Set the group of namespaces to which this namespace belongs. * * @param String the name of the namespace group */ public void setGroup(String group) { this.group = group; } /** * Get the ID for this namespace. * * @return String the id of the namespace */ public String getID() { return this.id; } /** * Set the id of this namespace. * * @param String the name of the namespace group */ public void setID(String id) { this.id = id; } /** *

Indicate whether this namespace is open to being changed or not.

*

Metadata which is created from a distinct file cannot be changed, * only those which have no associated file can be modified. * * @return boolean whether this namespace can be altered. */ public boolean isEditable() { return (this.location == null); } public abstract boolean addMetadata(String label, String value); public abstract boolean setMetadata(String label, String value); public abstract boolean removeMetadata(String label); public abstract boolean removeMetadata(String label, String value); public abstract List getMetadata(String label); public abstract Iterator getMetadataNames(); public abstract boolean write(PrintWriter writer); // public abstract boolean writeSQL(GS3SQLConnection connection); /** * Write out the metadata to an SQL database through a GS3SQLConnection. * * @param int the sql identifier of the parent object (in "metadata") table. * @param GS3SQLConnection the SQL database to use. */ public boolean writeSQL(int parentId, GS3SQLConnection connection) { GS3SQLAction action; // If the namespace is not null, then set it... if (this.id != null) { // use an update action in this case... GS3SQLUpdate update = new GS3SQLUpdate("namespaces"); update.addValue("NamespaceID", this.id); // set up the where clause GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", this.id, GS3SQLField.INTEGER_TYPE)); update.setWhere(where); action = update; } else { GS3SQLInsert insert = new GS3SQLInsert("namespaces"); action = insert; } if (this.location != null) { action.addValue("FileLoc", this.location.getLocation().toString()); action.addValue("FileType", "URL"); } else { // no location stuff } action.addValue("MetadataRef", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE); action.addValue("NamespaceType", this.name); // Execute the action Statement statement = null; try { statement = connection.createStatement(); statement.execute(action.toString()); } catch (SQLException e) { System.err.println("METSNamespace.writeSQL(): "+e); return false; } // then get the namespace reference number if needsbe... if (this.id == null) { GS3SQLSelect select = new GS3SQLSelect("namespaces"); select.addField("NamespaceRef"); GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE); GS3SQLWhere where = new GS3SQLWhere(whereItem); whereItem = new GS3SQLWhereItem("NamespaceType", "=", this.name); where.add(where); try { ResultSet result = statement.executeQuery(select.toString()); if (result.last()) { this.id = Integer.toString(result.getInt("NamespaceRef")); } statement.close(); } catch (SQLException sqlex){ this.id = null; System.err.println("METSNamespace.writeSQL(): "+sqlex); return false; } } return true; } public static METSNamespace readSQL(GS3SQLConnection connection, ResultSet resultSet) { METSLocation metsLocation = null; try { String name = resultSet.getString("NamespaceType"); String id = resultSet.getString("NamespaceRef"); String location = resultSet.getString("FileLoc"); String type = resultSet.getString("FileType"); if (location != null && type != null) { metsLocation = new METSLocation(type, location); } METSNamespace namespace = NamespaceFactory.initNamespace(name, metsLocation); namespace.id = id; int namespaceRef = resultSet.getInt("NamespaceRef"); GS3SQLSelect select = new GS3SQLSelect("mdvalues"); select.addField("*"); GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", Integer.toString(namespaceRef), GS3SQLField.INTEGER_TYPE)); select.setWhere(where); Statement statement = connection.createStatement(); ResultSet valuesSet = statement.executeQuery(select.toString()); if (valuesSet.first()) { do { String label = valuesSet.getString("Label"); String value = valuesSet.getString("Value"); namespace.addMetadata(label, value); } while (valuesSet.next()); } statement.close(); return namespace; } catch (java.net.MalformedURLException urlEx){ System.err.println("METSNamespace.readSQL(): "+urlEx); } catch (SQLException sqlEx){ System.err.println("METSNamespace.readSQL(): "+sqlEx); } return null; } }