Index: src/edu/ucsb/nceas/metacat/QuerySpecification.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 3354)
+++ src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 3355)
@@ -794,46 +794,7 @@
         return queryString.toString();
     }
 
-    /**
-     * This sql command will selecet startnodeid and endnodeid that user can
-     * NOT access
-     */
-    /*public String printAccessControlSQLForReturnField(String doclist)
-    {
-        StringBuffer sql = new StringBuffer();
-        String allowString = constructAllowString();
-        String denyString = constructDenyString();
-        sql.append("SELECT distinct startnodeid, endnodeid from xml_access ");
-        sql.append("WHERE docid in (");
-        sql.append(doclist);
-        sql.append(") AND startnodeid IS NOT NULL AND ");
-        sql.append("(");
-        sql.append("(");
-        sql
-                .append("startnodeid NOT IN (SELECT startnodeid from xml_access, xml_documents ");
-        sql.append(" WHERE xml_access.docid = xml_documents.docid");
-        sql.append(" AND lower(xml_documents.user_owner) ='");
-        sql.append(userName);
-        sql.append("' AND xml_access.startnodeid IS NOT NULL)");
-        sql.append(")");
-        sql.append(" AND ");
-        sql.append("(");
-        sql
-                .append("(startnodeid NOT IN (SELECT startnodeid from xml_access where( ");
-        sql.append(allowString);
-        sql.append(") AND (startnodeid IS NOT NULL))");
-        sql.append(")");
-        sql
-                .append(" OR (startnodeid IN (SELECT startnodeid from xml_access where( ");
-        sql.append(denyString);
-        sql.append(") AND (startnodeid IS NOT NULL))");
-        sql.append(")");
-        sql.append(")");
-        sql.append(")");
-        logMetacat.info("accessControlSQLForReturnField: "
-                + sql.toString());
-        return sql.toString();
-    }*/
+   
 
     /**
      * This method prints sql based upon the &lt;returnfield&gt; tag in the
@@ -901,10 +862,12 @@
      * @param unaccessableNodePair the node pairs (start id and end id)
      *            which this user should not access
      */
-    public String printExtendedSQL(String doclist)
+    private String printExtendedSQL(String doclist)
     {
         logMetacat.info("querySpecification.printExtendedSQL called\n");
         StringBuffer self = new StringBuffer();
+        Vector elementVector = new Vector();
+        Vector attributeVector = new Vector();
 
         boolean usePathIndex = true;
 
@@ -914,16 +877,29 @@
         }
 
         for (int i = 0; i < returnFieldList.size(); i++) {
-            if(!MetaCatUtil.pathsForIndexing.contains
-               (returnFieldList.elementAt(i))){
-                usePathIndex = false;
-                break;
+        	String path = (String)returnFieldList.elementAt(i);
+        	if (path != null && path.contains(ATTRIBUTESYMBOL))
+        	{
+        		attributeVector.add(path);
+        	}
+        	else 
+        	{
+        		elementVector.add(path);
+        	}       	
+            if(!MetaCatUtil.pathsForIndexing.contains(path)){
+                usePathIndex = false;              
             }
+         
         }
+        // check if has return field
+        if (elementVector.size() == 0 && attributeVector.size()==0)
+        {
+        	return null;
+        }
 
         if(usePathIndex){
             self.append("select docid, path, nodedata, parentnodeid ");
-            self.append("from xml_path_index where (path like '");
+            self.append("from xml_path_index where path in( '");
 
             boolean firstfield = true;
             //put the returnfields into the query
@@ -935,7 +911,7 @@
                     self.append("' ");
                 }
                 else {
-                    self.append("or path like '");
+                    self.append(", '");
                     self.append( (String) returnFieldList.elementAt(i));
                     self.append("' ");
                 }
@@ -947,31 +923,66 @@
         } else {
             self.append("select xml_nodes.docid, xml_index.path, xml_nodes.nodedata,  ");
             self.append("xml_nodes.parentnodeid ");
-            self.append("from xml_index, xml_nodes where xml_index.nodeid=");
-            self.append("xml_nodes.parentnodeid and (xml_index.path like '");
+            self.append("FROM xml_index, xml_nodes WHERE (");
+           
 
-            boolean firstfield = true;
+            boolean firstElement = true;
+            boolean firstAttribute = true;
             //put the returnfields into the query
             //the for loop allows for multiple fields
-            for (int i = 0; i < returnFieldList.size(); i++) {
-                if (firstfield) {
-                    firstfield = false;
-                    self.append((String) returnFieldList.elementAt(i));
-                    self.append("' ");
-                } else {
-                    self.append("or xml_index.path like '");
-                    self.append((String) returnFieldList.elementAt(i));
-                    self.append("' ");
-                }
+            if (elementVector.size() != 0)
+            {
+	            for (int i = 0; i < elementVector.size(); i++) {
+	            	String path = (String) elementVector.elementAt(i);
+	                if (firstElement) {
+	                	firstElement = false;
+	                	self.append(" (xml_index.nodeid=xml_nodes.parentnodeid AND xml_index.path IN ('");
+	                    self.append(path);
+	                    self.append("'");
+	                 }
+	                else 
+	                {
+	                    self.append(", '");
+	                    self.append(path);
+	                    self.append("' ");
+	                }
+	            }
+	            self.append(") AND xml_nodes.nodetype = 'TEXT')");
             }
+            
+            if (attributeVector.size() != 0)
+            {
+            	for (int j=0; j<attributeVector.size(); j++)
+            	{
+            		String path = (String) attributeVector.elementAt(j);
+            		if (firstAttribute)
+            		{
+            			firstAttribute = false;
+            			if (!firstElement)
+                		{
+                			self.append(" OR ");
+                		}
+            			self.append(" (xml_index.nodeid=xml_nodes.nodeid AND ( xml_index.path IN ( '");
+	                    self.append(path);
+	                    self.append("'");
+            		}
+            		else 
+	                {
+	                    self.append(", '");
+	                    self.append(path);
+	                    self.append("' ");
+	                }
+            	}
+            	self.append(") AND xml_nodes.nodetype = 'ATTRIBUTE'))");
+            }
+            
+          
             self.append(") AND xml_nodes.docid in (");
             self.append(doclist);
-            self.append(") AND xml_nodes.nodetype = 'TEXT'");
+            self.append(")");
 
         }
 
-        //addAccessRestrictionSQL(unaccessableNodePair, self);
-
         return self.toString();
     }
 
@@ -1022,45 +1033,9 @@
     }
 
 
-    /**
-     * 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();
+  
 
-        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(")");
-        }
-    }*/
 
-   
-
-
-
     public static String printRelationSQL(String docid)
     {
         StringBuffer self = new StringBuffer();
