Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/AbstractHierarchyNode.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/AbstractHierarchyNode.java	(revision 7267)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/AbstractHierarchyNode.java	(revision 7268)
@@ -51,8 +51,8 @@
    */
   public AbstractHierarchyNode()
-  { this.descriptor = null;
-    this.name       = null;
-    this.id         = null;
-    this.prefix     = null;
+  { this.descriptor = "";
+    this.name       = "";
+    this.id         = "";
+    this.prefix     = "";
     this.childNodes = new ArrayList();
     this.childDocs  = new ArrayList();
@@ -78,5 +78,5 @@
     child.setParent(this);
 
-    /*
+    /**
     if (this.id == null) {
       System.out.println(child.id.toString() + " added to root");
@@ -89,5 +89,5 @@
 
   public boolean add(AbstractHierarchyNode child)
-  { if (this.id != null &&
+  { if (this.id != null && this.id.length() > 0 &&
         !child.id.startsWith(this.id + "."))
     { return false;
@@ -256,12 +256,15 @@
   {
     int classifyRef;
-    GS3SQLAction action;
+    GS3SQLAction action = null;
     GS3SQLSelect select;
     GS3SQLInsert insert;
+
+    // Get own full id
+    String fullId = this.id.length() > 0 ? this.prefix+"."+this.id : this.prefix;
 
     // check for an existing instance of this classifier
     select = new GS3SQLSelect("classifiers");
     select.addField("ClassifyRef");
-    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ClassifyID", "=", this.prefix+"."+this.id);
+    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ClassifyID", "=", fullId);
     GS3SQLWhere where = new GS3SQLWhere(whereItem);
     select.setWhere(where);
@@ -282,19 +285,35 @@
 	insert = new GS3SQLInsert("classifiers");
 
-	String parentId = this.getParentId();
-
-	if (parentId.length() > 0) {
-	  insert.addValue("ParentID", this.prefix+"."+this.getParentId());
+	if (this.id == null || this.id.length() == 0) {
+	  insert.addValue("ParentID", "");
 	}
 	else {
-	  insert.addValue("ParentID", this.prefix);
+	  String parentId = this.getParentId();
+
+	  if (parentId.length() > 0) {
+	    insert.addValue("ParentID", this.prefix+"."+this.getParentId());
+	  }
+	  else {
+	    insert.addValue("ParentID", this.prefix);
+	  }
 	}
 
 	action = insert;
       }
-      action.addValue("ClassifyID", this.prefix+"."+this.id);
+      action.addValue("ClassifyID", fullId);
       action.addValue("Name", this.name);
       action.addValue("Description", this.descriptor);
-      action.addValue("ClassifyOrder", this.id, GS3SQLField.INTEGER_TYPE);
+
+      String tailId = "0";
+      if (this.id.length() > 0) {
+	int lastDot = this.id.lastIndexOf('.');
+	if (lastDot >= 0) {
+	  tailId = this.id.substring(lastDot+1);
+	}
+	else {
+	  tailId = this.id;
+	}
+      }
+      action.addValue("ClassifyOrder", tailId, GS3SQLField.INTEGER_TYPE);
       action.addValue("NumLeafDocs", Integer.toString(this.noOfLeafDocs()), GS3SQLField.INTEGER_TYPE);
 
@@ -303,5 +322,10 @@
     }
     catch (SQLException sqlEx) {
-      System.err.println(sqlEx);
+      if (action == null) {
+	System.err.println(sqlEx);
+      }
+      else {
+	System.err.println(sqlEx + " " + action.toString());
+      }
       return false;
     }
@@ -341,4 +365,5 @@
 
       if (!childNode.writeSQL(connection)) {
+	  System.out.println("Failed to write " );
 	return false;
       }
Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/GS2HierarchyClassifier.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/GS2HierarchyClassifier.java	(revision 7267)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/GS2HierarchyClassifier.java	(revision 7268)
@@ -97,6 +97,6 @@
     { super(content);
  
-      this.rootNode = new GS2HierarchyNode();
       String prefix = "CL"+hierarchyName;
+      this.rootNode = new GS2HierarchyNode(prefix, hierarchyName, "", hierarchyName);
 
       while (this.hasMoreLines()) {
Index: /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/HierarchyClassifier.java
===================================================================
--- /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/HierarchyClassifier.java	(revision 7267)
+++ /trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/HierarchyClassifier.java	(revision 7268)
@@ -47,4 +47,8 @@
     public HierarchyNode()
     { super();
+    }
+
+    public HierarchyNode(String prefix, String name, String id, String descriptor)
+    { super(prefix, name, id, descriptor);
     }
     
@@ -77,10 +81,10 @@
     StringBuffer path;
     StringBuffer match;
+    String       prefix;
     boolean inElement;
     AbstractHierarchyNode rootNode = null;
     AbstractHierarchyNode currentNode = null;
-    List          rootNodes = null;
-
-    HierarchyHandler()
+
+    HierarchyHandler(String classifierName)
     { super();
  
@@ -89,11 +93,18 @@
       this.path        = null;
       this.match       = null;
-      this.rootNodes   = new ArrayList();
+      this.prefix      = classifierName;
     }
 
     public void startElement(String URI, String localName, String qName, Attributes attributes)
-    { if (localName.equals("Classification"))
+    { if (localName.equals("Hierarchy"))
       {	// create a new node in the hierarchy
-	HierarchyNode node = new HierarchyNode();
+	HierarchyNode node = new HierarchyNode(prefix, "", "", "Classifier");
+
+	this.rootNode = node;
+	this.currentNode = node;
+      }
+      else if (localName.equals("Classification"))
+      {	// create a new node in the hierarchy
+	HierarchyNode node = new HierarchyNode(this.prefix, "", "", "");
 
 	// Initialise the tree
@@ -127,5 +138,6 @@
     { if (localName.equals("Classification"))
       {	if (this.currentNode.getParent() == null) {
-          this.rootNodes.add(this.currentNode);
+	  // TODO: Error state
+          // this.rootNodes.add(this.currentNode);
         }
 	this.currentNode = this.currentNode.getParent();
@@ -133,4 +145,5 @@
       else if (localName.equals("Name"))
       { this.currentNode.setName(XMLTools.cleanString(this.name.toString()));
+      System.out.println("Name is " + this.name);
         this.name = null;
       }
@@ -177,10 +190,11 @@
   private List             fields;
   private String           sortBy;
-
-  public HierarchyClassifier(URL basefile, List fields, String sortBy)
+  private String           name;
+
+  public HierarchyClassifier(String name, URL basefile, List fields, String sortBy)
   {	
     try 
-      { SAXParser parser = new SAXParser();
-      HierarchyHandler handler = new HierarchyHandler();
+    { SAXParser parser = new SAXParser();
+      HierarchyHandler handler = new HierarchyHandler(name);
       /*
       XMLReader reader = XMLReaderFactory.createXMLReader();
