Index: src/edu/ucsb/nceas/metacat/QuerySpecification.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 169)
+++ src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 170)
@@ -28,8 +28,8 @@
 
 
 /** 
- * A Class that represents a structuredd query,and can be constructed from an
- * XML serialization
+ * A Class that represents a structured query,and can be constructed from an
+ * XML serialization conforming to "pathquery.dtd"
  */
 public class QuerySpecification extends HandlerBase {
  
@@ -93,7 +93,7 @@
        try {
          FileReader xml = new FileReader(new File(xmlfile));
          QuerySpecification qspec = new QuerySpecification(xml);
-         System.out.println(qspec);
+         System.out.println(qspec.toSQL());
        } catch (IOException e) {
          System.err.println(e.getMessage());
        }
@@ -189,6 +189,15 @@
     }
   }
 
+  public String toSQL() {
+    StringBuffer self = new StringBuffer();
+
+    self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
+    self.append(query.toSQL());
+
+    return self.toString();
+  }
+
   public String toString() {
     return "meta_file_id=" + meta_file_id + "\n" + 
            "querytitle=" + querytitle + "\n" + query;
@@ -216,6 +225,34 @@
       return children.elements();
     }
    
+    public String toSQL() {
+      StringBuffer self = new StringBuffer();
+      boolean first = true;
+
+      self.append("(");
+
+      Enumeration en= getChildren();
+      while (en.hasMoreElements()) {
+        Object qobject = en.nextElement();
+        if (first) {
+          first = false;
+        } else {
+          self.append(" " + booleantype + " ");
+        }
+        if (qobject instanceof QueryGroup) {
+          QueryGroup qg = (QueryGroup)qobject;
+          self.append(qg.toSQL());
+        } else if (qobject instanceof QueryTerm) {
+          QueryTerm qt = (QueryTerm)qobject;
+          self.append(qt.toSQL());
+        } else {
+          System.err.println("qobject wrong type: fatal error");
+        }
+      }
+      self.append(") \n");
+      return self.toString();
+    }
+
     public String toString() {
       StringBuffer self = new StringBuffer();
 
@@ -266,6 +303,51 @@
       return pathexpr;
     }
 
+    public String toSQL() {
+      StringBuffer self = new StringBuffer();
+
+      //self.append("    Query Term iscasesensitive=" + casesensitive + "\n");
+
+      // Uppercase the search string if case match is not important
+      String casevalue = null;
+      String nodedataterm = null;
+
+      if (casesensitive) {
+        nodedataterm = "nodedata";
+        casevalue = value;
+      } else {
+        nodedataterm = "UPPER(nodedata)";
+        casevalue = value.toUpperCase();
+      }
+
+      // Add appropriate wildcards to search string
+      String searchvalue = null;
+      if (searchmode.equals("starts-with")) {
+        searchvalue = casevalue + "%";
+      } else if (searchmode.equals("ends-with")) {
+        searchvalue = "%" + casevalue;
+      } else if (searchmode.equals("contains")) {
+        searchvalue = "%" + casevalue + "%";
+      } else {
+        searchvalue = casevalue;
+      }
+
+      self.append("(");
+
+      if (pathexpr != null) {
+        self.append(nodedataterm + " LIKE " + "'" + searchvalue + "' ");
+        self.append("AND parentnodeid IN ");
+        self.append("(SELECT nodeid FROM xml_index WHERE path LIKE " + 
+                    "'" +  pathexpr + "') " );
+      } else {
+        self.append(nodedataterm + " LIKE " + "'" + searchvalue + "' ");
+      }
+
+      self.append(") \n");
+
+      return self.toString();
+    }
+
     public String toString() {
       StringBuffer self = new StringBuffer();
 
Index: src/edu/ucsb/nceas/metacat/DBQuery.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 169)
+++ src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 170)
@@ -34,9 +34,9 @@
   /**
    * the main routine used to test the DBQuery utility.
    *
-   * Usage: java DBQuery <query> <user> <password> [dbstring]
+   * Usage: java DBQuery <xmlfile> <user> <password> [dbstring]
    *
-   * @param query the text to search for in the element and attribute content
+   * @param xmlfile the filename of the xml file containing the query
    * @param user the username to use for the database connection
    * @param password the password to use for the database connection
    * @param dbstring the connection info to use for the database connection
@@ -43,25 +43,24 @@
    */
   static public void main(String[] args) {
      
-     if (args.length < 4)
+     if (args.length < 3)
      {
         System.err.println("Wrong number of arguments!!!");
         System.err.println("USAGE: java DBQuery " +
-                           "<query> <doctype> <user> <password> [dbstring]");
+                           "<xmlfile> <user> <password> [dbstring]");
         return;
      } else {
         try {
                     
-          String query    = args[0];
-          String doctype  = args[1];
-          String user     = args[2];
-          String password = args[3];
+          String xmlfile  = args[0];
+          String user     = args[1];
+          String password = args[2];
           String dbstring = null;
 
-          if (args.length <= 4) {
+          if (args.length <= 3) {
             dbstring = defaultDB;
           } else {
-            dbstring = args[4];
+            dbstring = args[3];
           }
 
           // Open a connection to the database
@@ -68,21 +67,26 @@
           Connection dbconn = MetaCatUtil.openDBConnection( 
                               "oracle.jdbc.driver.OracleDriver",
                               dbstring, user, password);
-          // Execute the simple query
+          // Execute the query
           DBQuery rd = new DBQuery(dbconn);
+
+          FileReader xml = new FileReader(new File(xmlfile));
+          //QuerySpecification qspec = new QuerySpecification(xml);
+          //System.out.println(qspec.toSQL());
+
           Hashtable nodelist = null;
-          nodelist = rd.findDocuments(query);
+          nodelist = rd.findDocuments(xml);
 
           // Print the reulting root nodes
           StringBuffer result = new StringBuffer();
           String document = null;
-          Long docid = null;
+          String docid = null;
           result.append("<?xml version=\"1.0\"?>\n");
           result.append("<resultset>\n");
-          result.append("  <query>" + query + "</query>\n");
+          result.append("  <query>" + xmlfile + "</query>\n");
           Enumeration doclist = nodelist.keys(); 
           while (doclist.hasMoreElements()) {
-            docid = (Long)doclist.nextElement();
+            docid = (String)doclist.nextElement();
             document = (String)nodelist.get(docid);
             result.append("  <document>\n    " + document + 
                           "\n  </document>\n");
@@ -118,21 +122,15 @@
   /** 
    * routine to search the elements and attributes looking to match query
    *
-   * @param queryspec the xml serialization of the query
-   * @param requestedDoctype the type of documents to return from the query
+   * @param queryspec the xml serialization of the query (pathquery.dtd)
    */
-  public Hashtable findDocuments(String queryspec) {
+  public Hashtable findDocuments(Reader queryspec) {
       Hashtable	 docListResult = new Hashtable();
 
-
-      //QuerySpecification qspec = new QuerySpecification(queryspec);
-
- 
-/*
       PreparedStatement pstmt;
 
       // Now look up the document id
-      long docid = 0;
+      String docid = null;
       String docname = null;
       String doctype = null;
       String doctitle = null;
@@ -139,37 +137,18 @@
       StringBuffer document = null; 
 
       try {
-        if (requestedDoctype == null || 
-            requestedDoctype.equals("any") || 
-            requestedDoctype.equals("ANY")) {
-          pstmt = conn.prepareStatement(
-                "SELECT docid,docname,doctype,doctitle " +
-                "FROM xml_documents " +
-                "WHERE docid IN " +
-                "(SELECT docid " +
-                "FROM xml_nodes WHERE nodedata LIKE ? )");
+        QuerySpecification qspec = new QuerySpecification(queryspec);
+ 
+        pstmt = conn.prepareStatement(
+              "SELECT docid,docname,doctype,doctitle " +
+              "FROM xml_documents WHERE docid IN (" +
+              qspec.toSQL() + ")");
 
-                // Bind the values to the query
-                pstmt.setString(1, query);
-        } else {
-          pstmt = conn.prepareStatement(
-                "SELECT docid,docname,doctype,doctitle " +
-                "FROM xml_documents " +
-                "WHERE docid IN " +
-                "(SELECT docid " +
-                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
-                "AND doctype = ?");
-
-                // Bind the values to the query
-                pstmt.setString(1, query);
-                pstmt.setString(2, requestedDoctype);
-        }
-
         pstmt.execute();
         ResultSet rs = pstmt.getResultSet();
         boolean tableHasRows = rs.next();
         while (tableHasRows) {
-          docid = rs.getLong(1);
+          docid = rs.getString(1);
           docname = rs.getString(2);
           doctype = rs.getString(3);
           doctitle = rs.getString(4);
@@ -187,7 +166,7 @@
           }
 
           // Store the document id and the root node id
-          docListResult.put(new Long(docid),(String)document.toString());
+          docListResult.put(docid,(String)document.toString());
 
           // Advance to the next record in the cursor
           tableHasRows = rs.next();
@@ -195,8 +174,10 @@
         pstmt.close();
       } catch (SQLException e) {
         System.out.println("Error getting id: " + e.getMessage());
+      } catch (IOException ioe) {
+        System.err.println("Error printing qspec:");
+        System.err.println(ioe.getMessage());
       }
-*/
     return docListResult;
   }
 }
