Index: src/edu/ucsb/nceas/metacat/QuerySpecification.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 2072)
+++ src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 2073)
@@ -813,12 +813,11 @@
      * xml_nodes to find the fields to be included by their path expression, and 
      * avoids the use of the xml_index table.
      * 
-     * @param doclist 
-     *            the list of document ids to search by
-     * @param unaccessableNodePair
-     *            the node pair(start id and end id) which this user could not
-     *            access it
-     * @param useXMLIndex a boolean flag indicating whether to search using xml_index
+     * @param doclist the list of document ids to search
+     * @param unaccessableNodePair the node pairs (start id and end id) which 
+     *            this user should not access
+     * @param useXMLIndex a boolean flag indicating whether to search using 
+     *            xml_index
      */
     public String printExtendedSQL(String doclist,
             Hashtable unaccessableNodePair, boolean useXMLIndex)
@@ -840,8 +839,7 @@
                 }
                 String path  = (String) returnFieldList.elementAt(i);
                 self.append("select xml_nodes.docid, ");
-                self.append("'"+ path  + "' as path, ");
-                self.append("xml_nodes.nodedata, ");
+                self.append("'"+ path  + "' as path, xml_nodes.nodedata, ");
                 self.append("xml_nodes.parentnodeid ");
                 self.append("from xml_nodes, xml_documents ");
                 self.append("where parentnodeid IN ");
@@ -849,28 +847,10 @@
                 
                 self.append(" AND xml_nodes.docid in (");
                 self.append(doclist);
-                self.append(")");
-                self.append(" AND xml_nodes.nodetype = 'TEXT'");
+                self.append(") AND xml_nodes.nodetype = 'TEXT'");
                 self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
                 
-                // add control part for extended query
-                Enumeration en = unaccessableNodePair.keys();
-
-                while (en.hasMoreElements()) {
-                    // Get control pairs in object
-                    Long startNodeIdObject = (Long) en.nextElement();
-                    Long endNodeIdObject = (Long) unaccessableNodePair
-                            .get(startNodeIdObject);
-                    // change it to long
-                    long startNodeId = startNodeIdObject.longValue();
-                    long endNodeId = endNodeIdObject.longValue();
-                    // add into query
-                    self.append(" AND( xml_nodes.nodeid < ");
-                    self.append(startNodeId);
-                    self.append(" OR xml_nodes.nodeid > ");
-                    self.append(endNodeId);
-                    self.append(")");
-                }
+                addAccessRestrictionSQL(unaccessableNodePair, self);
                 self.append(")");
             }
 
@@ -884,11 +864,9 @@
      * It uses the xml_index table and so assumes that this table has been
      * built.
      * 
-     * @param doclist
-     *            the list of document ids to search by
-     * @param unaccessableNodePair
-     *            the node pair(start id and end id) which this user could not
-     *            access it
+     * @param doclist the list of document ids to search
+     * @param unaccessableNodePair the node pairs (start id and end id) 
+     *            which this user should not access
      */
     public String printExtendedSQL(String doclist, 
             Hashtable unaccessableNodePair)
@@ -913,11 +891,29 @@
             }
         }
         self.append(") AND xml_nodes.docid in (");
-        //self.append(query.printSQL());
         self.append(doclist);
-        self.append(")");
-        self.append(" AND xml_nodes.nodetype = 'TEXT'");
+        self.append(") AND xml_nodes.nodetype = 'TEXT'");
 
+        addAccessRestrictionSQL(unaccessableNodePair, self);
+
+        return self.toString();
+    }
+
+    /**
+     * Create the SQl necessary to restrict access to allowed nodes.  This is
+     * accomplished by restricting the nodes that are returned to include
+     * only those whose IDs fall outside of a set of start/stop pairs of
+     * nodeid values.  These pairs are passed in as a hash, with the key
+     * containing the start nodeid and the value containing the end nodeid.
+     * Any nodes between these start and end nodeid values will be excluded
+     * from the results.
+     * 
+     * @param unaccessableNodePair hash of start/end nodeid pairs to restrict
+     * @param self a stringbuffer to which the genrated SQL is appended 
+     */
+    private void addAccessRestrictionSQL(Hashtable unaccessableNodePair, 
+            StringBuffer self)
+    {
         // add control part for extended query
         Enumeration en = unaccessableNodePair.keys();
 
@@ -930,20 +926,18 @@
             long startNodeId = startNodeIdObject.longValue();
             long endNodeId = endNodeIdObject.longValue();
             // add into query
-            self.append(" AND( xml_nodes.nodeid < ");
+            self.append(" AND ( xml_nodes.nodeid < ");
             self.append(startNodeId);
             self.append(" OR xml_nodes.nodeid > ");
             self.append(endNodeId);
             self.append(")");
         }
-
-        return self.toString();
     }
-
+    
     /**
      * This method prints sql based upon the returnfield tag in the pathquery
      * document has an attribute. This allows for customization of the returned
-     * fields
+     * fields.
      * 
      * @param doclist
      *            the list of document ids to search by
@@ -1019,7 +1013,7 @@
         //"querytitle=" + querytitle + "\n" + query;
     }
 
-    /* A method to get rid of attribute part in path expression */
+    /** A method to get rid of attribute part in path expression */
     public static String newPathExpressionWithOutAttribute(String pathExpression)
     {
         if (pathExpression == null) { return null; }
@@ -1033,7 +1027,7 @@
         return newExpression;
     }
 
-    /* A method to get attribute name from path */
+    /** A method to get attribute name from path */
     public static String getAttributeName(String path)
     {
         if (path == null) { return null; }
