package org.greenstone.gsdl3.gs3build.metadata; import java.io.PrintWriter; import java.util.List; import java.sql.SQLException; import java.sql.ResultSet; import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection; 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 boolean write(PrintWriter writer); // public abstract boolean writeSQL(GS3SQLConnection connection); /** * Write out the metadata to an SQL database through a GS3SQLConnection. * * @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 connection.execute(action.toString()); // 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 { connection.execute(select.toString()); ResultSet result = connection.getResultSet(); result.last(); this.id = Integer.toString(result.getInt("NamespaceRef")); } catch (SQLException sqlex) { this.id = null; System.err.println(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); connection.execute(select.toString()); ResultSet valuesSet = connection.getResultSet(); if (valuesSet != null && valuesSet.first()) { do { String label = valuesSet.getString("label"); String value = valuesSet.getString("value"); namespace.addMetadata(label, value); } while (valuesSet.next()); } return namespace; } catch (java.net.MalformedURLException urlEx) { System.out.println(urlEx); } catch (SQLException sqlEx) { System.out.println(sqlEx); } return null; } }