Index: src/edu/ucsb/nceas/metacat/QueryGroup.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QueryGroup.java	(revision 3236)
+++ src/edu/ucsb/nceas/metacat/QueryGroup.java	(revision 3237)
@@ -49,6 +49,7 @@
     private Vector queryTerms = null;//this vector only holds query terms without same search value.
     private Vector queryGroupsChildren = null;
     private static Logger logMetacat = Logger.getLogger(QueryGroup.class);
+    private static String UNION = "UNION";
 
     /**
      * construct a new QueryGroup
@@ -70,7 +71,7 @@
      * @param qgroup the query group to be added to the list of terms
      */
     public void addChild(QueryGroup qgroup) {
-      //children.add((Object)qgroup);
+      children.add((Object)qgroup);
       queryGroupsChildren.add(qgroup);
     }
 
@@ -80,15 +81,15 @@
      * @param qterm the query term to be added to the list of terms
      */
     public void addChild(QueryTerm qterm) {
-      // children.add((Object)qterm);
+      children.add((Object)qterm);
       handleNewQueryTerms(qterm);
       
     }
 
-    /**
+    /*
      * Retrieve an Enumeration of query terms for this QueryGroup
      */
-    public Enumeration getChildren() {
+    private Enumeration getChildren() {
       return children.elements();
     }
 
@@ -106,15 +107,19 @@
 
       boolean first = true;
       
-      Enumeration en= getChildren();
-      while (en.hasMoreElements()) {
-        Object qobject = en.nextElement();
-        if (qobject instanceof QueryGroup) {
-            QueryGroup qg = (QueryGroup)qobject;
+      if (!queryTermsWithSameValue.isEmpty())
+      {
+    	  printSQLStringWithSameSearchValue();
+      }
+      
+      for (int i=0; i<queryGroupsChildren.size(); i++)
+      {
+      
+        
+            QueryGroup qg = (QueryGroup)queryGroupsChildren.elementAt(i);
         	String queryGroupSQL = qg.printSQL(useXMLIndex);
         	logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
-        			+ queryGroupSQL);
-        	
+        			+ queryGroupSQL);       	
         	if (first) {
         		first = false;
         	} else {
@@ -127,8 +132,11 @@
    		  	// count percerntage number
    		  	int count = qg.getPercentageSymbolCount();
    		  	countPercentageSearchItem = countPercentageSearchItem + count;
-        } else if (qobject instanceof QueryTerm) {
-           QueryTerm qt = (QueryTerm)qobject;
+      }
+      
+      for (int i=0; i<queryTerms.size(); i++)
+      {
+           QueryTerm qt = (QueryTerm)queryTerms.elementAt(i);
            String termQueryString = qt.printSQL(useXMLIndex);
        	   logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
         			+ termQueryString);
@@ -141,13 +149,11 @@
                    }
                }
                queryString.append(termQueryString);
-           }
+           
            // count percerntage number
            int count = qt.getPercentageSymbolCount();
            countPercentageSearchItem = countPercentageSearchItem + count;
-        } else {
-          System.err.println("qobject wrong type: fatal error");
-        }
+        } 
       }
 
       if(!queryString.toString().equals("")){
@@ -162,6 +168,8 @@
     }
     
     
+    
+    
     /*
      * If every query term in a queryGroup share a search value, we should
      * use a new query to replace the original query term query in order to
@@ -177,39 +185,57 @@
     	
     	if (queryTermsWithSameValue != null)
     	{
-    		QueryTerm term1 = (QueryTerm)queryTermsWithSameValue.elementAt(0);
-        	value = term1.getValue();
-        	boolean first = true;
-			sqlBuff.append("SELECT DISTINCT docid FROM xml_path_index WHERE UPPER(nodedata) LIKE '%");
-			sqlBuff.append(value);
-			sqlBuff.append("%' AND path IN (");
-    		//gets every path in query term object
-    		for (int i=0; i<queryTermsWithSameValue.size(); i++)
+    		boolean firstVector = true;
+    		for (int j=0; j<queryTermsWithSameValue.size(); j++)
     		{
-    			QueryTerm term = (QueryTerm)queryTermsWithSameValue.elementAt(i);
-    			value = term.getValue();
-    			String path = term.getPathExpression();
-    			if (path != null && !path.equals(""))
-    			{
-    				if (first)
-    				{
-    					first = false;
-    					sqlBuff.append("'");
-    					sqlBuff.append(path);
-    					sqlBuff.append("'");
-    					
-    				}
-    				else
-    				{
-    					sqlBuff.append(",'");
-    					sqlBuff.append(path);
-    					sqlBuff.append("'");
-    				}
-    				index++;
-    			}
-    		}
-    		sqlBuff.append(")");
+    			
+	    		Vector queryTermVector = (Vector)queryTermsWithSameValue.elementAt(j);
+	    		QueryTerm term1 = (QueryTerm)queryTermVector.elementAt(0);
+	        	value = term1.getValue();
+	        	boolean first = true;
+	        	if (firstVector)
+	        	{
+				  firstVector = false;
+	        	}
+	        	else
+	        	{
+	        		sqlBuff.append(" "+operator+" ");
+	        	}
+	        	sqlBuff.append("SELECT DISTINCT docid FROM xml_path_index WHERE UPPER(nodedata) LIKE '%");
+	        	sqlBuff.append(value);
+				sqlBuff.append("%' AND path IN (");
+	    		//gets every path in query term object
+	    		for (int i=0; i<queryTermVector.size(); i++)
+	    		{
+	    			QueryTerm term = (QueryTerm)queryTermVector.elementAt(i);
+	    			value = term.getValue();
+	    			String path = term.getPathExpression();
+	    			if (path != null && !path.equals(""))
+	    			{
+	    				if (first)
+	    				{
+	    					first = false;
+	    					sqlBuff.append("'");
+	    					sqlBuff.append(path);
+	    					sqlBuff.append("'");
+	    					
+	    				}
+	    				else
+	    				{
+	    					sqlBuff.append(",'");
+	    					sqlBuff.append(path);
+	    					sqlBuff.append("'");
+	    				}
+	    				index++;
+	     				if (value != null && (value.equals("%") || value.equals("%%%")))
+	                    {
+	    				  countPercentageSearchItem++;
+	                    }
+    			     }
+    		    }
+    		    sqlBuff.append(")");
     	
+    	    }
     	}
     	if (index >0)
     	{
@@ -250,35 +276,40 @@
      */
     private void handleNewQueryTerms(QueryTerm newTerm)
     {
-    	if (newTerm != null)
+    	// currently we only handle UNION group
+    	if (newTerm != null )
     	{
-    		for (int i=0; i<queryTerms.size(); i++)
-    		{
-    			QueryTerm term = (QueryTerm)queryTerms.elementAt(i);
-    			if (term != null && term.hasSameSearchValue(newTerm))
-    			{
-    				// find a target which has same search value
-    				Vector newSameValueVector = new Vector();
-    				newSameValueVector.add(term);
-    				newSameValueVector.addElement(newTerm);
-    				queryTermsWithSameValue.add(newSameValueVector);
-    				queryTerms.remove(i);
-    				return;
-    			}
-    		}
-    		// no same search value was found in queryTerms.
-    		// then we need search queryTermsWithSameValue
-    		for (int i=0; i<queryTermsWithSameValue.size(); i++)
-    		{
-    			Vector sameValueVec = (Vector)queryTermsWithSameValue.elementAt(i);
-    			// we only compare the first query term
-    			QueryTerm term = (QueryTerm)sameValueVec.elementAt(0);
-    			if (term != null && term.hasSameSearchValue(newTerm))
-    			{
-    				sameValueVec.add(newTerm);
-    				return;
-    			}
-    		}
+    		//we only handle union operator now.
+    		if (operator != null && operator.equalsIgnoreCase(UNION))
+    	    {
+	    		for (int i=0; i<queryTerms.size(); i++)
+	    		{
+	    			QueryTerm term = (QueryTerm)queryTerms.elementAt(i);
+	    			if (term != null && term.hasSameSearchValue(newTerm))
+	    			{
+	    				// find a target which has same search value
+	    				Vector newSameValueVector = new Vector();
+	    				newSameValueVector.add(term);
+	    				newSameValueVector.addElement(newTerm);
+	    				queryTermsWithSameValue.add(newSameValueVector);
+	    				queryTerms.remove(i);
+	    				return;
+	    			}
+	    		}
+	    		// no same search value was found in queryTerms.
+	    		// then we need search queryTermsWithSameValue
+	    		for (int i=0; i<queryTermsWithSameValue.size(); i++)
+	    		{
+	    			Vector sameValueVec = (Vector)queryTermsWithSameValue.elementAt(i);
+	    			// we only compare the first query term
+	    			QueryTerm term = (QueryTerm)sameValueVec.elementAt(0);
+	    			if (term != null && term.hasSameSearchValue(newTerm))
+	    			{
+	    				sameValueVec.add(newTerm);
+	    				return;
+	    			}
+	    		}
+    	    }
     		// both queryTerms and queryTermsWithSameValue don't have the search value,
     		// add this newTerm to queryTerms
     		queryTerms.add(newTerm);
