/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.xml;

import java.text.Collator;
import java.util.*;
import java.sql.*;

import org.apache.log4j.Logger;


public class XMLQuery extends java.util.Vector
	{
    
    private static Logger log = Logger.getLogger(XMLQuery.class);
    
	// if multiple sub queries, how to combine...
	public static final int COMBINE_UNION					= 0;
	public static final int COMBINE_INTERCEPT				= 1;

	// type of descent...
	public static final int RELATE_FAMILY				= 0;
	public static final int RELATE_DESCENDENT			= 1;
	public static final int RELATE_CHILD				= 2;
	// not implemented...
	public static final int RELATE_ANCESTOR		   = 3;
	public static final int RELATE_PARENT				= 4;
	
	
	private XMLRepository repository;
	private String o_table = "xml_objects";
	private String e_table = "xml_elements";
	private String a_table = "xml_attributes";
	private String c_table = "xml_cdata";
	private String w_table = "xml_words";
	
	public int combination;
	public int relationship;
	
	public Integer xml_object_id;
	public Integer reference;
	public String title_criterion;
	
	public String element_name=null;
	public boolean root_element=false;
	public String attribute_name=null;
	public String attribute_criterion=null;
	public String text_criterion=null;
	public Vector words=new Vector();
	public int word_strength=Collator.IDENTICAL;
	
	private String element_criterion;

	
	private int[] last_position;
	private static final int MAX_DEPTH=8;
	
	XMLQuery container;
	
	// package access to constructor because apps must use XMLRepository
	XMLQuery( XMLRepository rep )
		{
		repository = rep;
		o_table = rep.o_table;
		e_table = rep.e_table;
		a_table = rep.a_table;
		c_table = rep.c_table;
		w_table = rep.w_table;
		
		relationship = RELATE_DESCENDENT;
		combination = COMBINE_UNION;
		last_position = new int[MAX_DEPTH];
		for ( int i=0; i<MAX_DEPTH; i++ )
			last_position[i]=0;
		
		container=null;
		}
	
	public void addElement( XMLQuery xml_query )
		{
		super.addElement( xml_query );
		xml_query.container = this;
		}

	private XMLQuery topQuery()
		{
		XMLQuery q = this;
		while ( q.container!=null )
			q = q.container;
		return q;
		}

	public Vector getXMLObjectRecords( Connection con, Integer max )
		throws SQLException
		{
		Integer ref;
		XMLObjectRecord record;
		Vector v = new Vector();

		repository.initWordCache( con );
		
		String sql = sqlObjects();
		log.debug( sql );
		Statement st = con.createStatement();
		ResultSet results = st.executeQuery( sql );
		while ( results.next() )
			{
			ref = new Integer( results.getInt( 6 ) );
			if ( results.wasNull() )
				ref=null;
			record = new XMLObjectRecord( results.getInt( 1 ), 
													results.getInt( 2 ),
													results.getString( 3 ),
													results.getString( 4 ),
													results.getString( 5 ),
													ref );
			v.addElement( record );
			if ( max !=null && v.size() >= max.intValue() )
				break;
			}
		results.close();
		st.close();
		
		return v;
		}
	
	public Vector getXMLElementRecords( Connection con, Integer max )
		throws SQLException
		{
		Integer parent;
		XMLElementRecord record;
		Vector v = new Vector();
		
		repository.initWordCache( con );

		String sql = sqlElements();
		log.debug( sql );
		
		Statement st = con.createStatement();
		ResultSet results = st.executeQuery( sql );
		while ( results.next() )
			{
			parent = new Integer( results.getInt( 6 ) );
			if ( results.wasNull() )
				parent=null;
			record = new XMLElementRecord( results.getInt( 1 ), 
													results.getInt( 2 ),
													results.getInt( 3 ),
													results.getInt( 4 ),
													results.getString( 5 ),
													parent );
			v.addElement( record );
			if ( max !=null && v.size() >= max.intValue() )
				break;
			}
		results.close();
		st.close();
		
		return v;
		}
	
	private String sqlElements()
		throws IllegalArgumentException 
		{
		StringBuffer select = new StringBuffer();
		for ( int i=0; i<MAX_DEPTH; i++ )
			topQuery().last_position[i]=0;
		sqlSelect( select, 0, false, null );
		return select.toString();
		}

	private String sqlObjects()
		throws IllegalArgumentException 
		{
		StringBuffer select = new StringBuffer();
		for ( int i=0; i<MAX_DEPTH; i++ )
			topQuery().last_position[i]=0;
		sqlSelect( select, 0, true, null );
		return select.toString();
		}

	private String sqlTable( int depth, int position )
		{
		return "el_" + depth + "_" + position;
		}

	private String sqlFrom( int depth, int position )
		{
		return e_table + " " + sqlTable( depth, position );
		}

	private String sqlObjectTable( int depth, int position )
		{
		return "ob_" + depth + "_" + position;
		}

	private String sqlObjectFrom( int depth, int position )
		{
		return o_table + " " + sqlObjectTable( depth, position );
		}

	private String sqlTextTable( int depth, int position )
		{
		return "tx_" + depth + "_" + position;
		}

	private String sqlTextFrom( int depth, int position )
		{
		return c_table + " " + sqlTextTable( depth, position );
		}

	private String sqlWordTable( int depth, int position )
		{
		return "w_"  + depth + "_" + position;
		}

	private String sqlWordFrom( int depth, int position )
		{
		return w_table + " " + sqlWordTable( depth, position );
		}

	private String sqlAttTable( int depth, int position )
		{
		return "at_" + depth + "_" + position;
		}

	private String sqlAttFrom( int depth, int position )
		{
		return a_table + " " + sqlAttTable( depth, position );
		}

	private String sqlInsert( String expression, String ins )
		{
		StringBuffer expanded = new StringBuffer();
		for ( int i=0; i< expression.length(); i++ )
			{
			if ( expression.charAt( i ) == '?' )
				expanded.append( ins );
			else
				expanded.append( expression.charAt( i ) );
			}
		return expanded.toString();
		}


	private boolean hasCriteria()
		{
		return !( reference==null && title_criterion == null && 
		            xml_object_id == null && element_name==null &&
					attribute_name == null && attribute_criterion == null &&
					words.size() == 0 );
		}


	private void sqlSelect( StringBuffer sql, int depth, boolean objects, String sql_connection )
		throws IllegalArgumentException 
		{
		int i, position = ++topQuery().last_position[depth];
		
		int last_deeper_position;
		XMLQuery current;
		StringBuffer connect;
        Vector all_token_ids = new Vector();
		int useable_words = 0;

        // TODO method could do with refactoring...
        
		if ( !hasCriteria() && size()== 0 )
			throw new IllegalArgumentException( "A query with no search criteria were specified." );
		
		//look up token ids first, only want the words for which tokens exist
        //TODO return if COMBINE_INTERCEPT and no tokens for a word?
        
		for ( i=0; i<words.size(); i++ )
		{
		    // for each of the query terms supplied, get an array of token ids
            // that match (possibly via SQL wildcard)
		    Integer[] token_ids = repository.getMatchingTokenIds( (String)words.elementAt( i ), word_strength );
		    if ( token_ids.length > 0 )
		    {
                // add this array of token ids to a Vector of arrays:
		        all_token_ids.add( token_ids );
                // This query term matched at least one token,
                // useful to keep track of total (is equivalent to all_token_ids.size()):
		        useable_words ++;
		    }
		    
		}
        
		// if there are no tokens that match at all
        // return a query that is guaranteed to return no records.
        if ( words.size() > 0 && useable_words == 0 )
        {
            sql.append( "SELECT * FROM xml_objects WHERE xml_object_id IS NULL" );
            return;
        }
        
        // or if there are no tokens for any one of the terms in an AND query
        // return a query that is guaranteed to return no records.
        if ( useable_words < words.size() && combination == COMBINE_INTERCEPT )
        {
            sql.append( "SELECT * FROM xml_objects WHERE xml_object_id IS NULL" );
            return;
        }        

			
		// a select statement is only need if this node in the query tree
		// has search criteria
		if ( hasCriteria() )
			{
			// sort out which tables to put in query and how to join them
			sql.append( "SELECT " );
			sql.append( "DISTINCT " );
			if ( objects )
				sql.append( sqlObjectTable( depth, position ) );
			else
				sql.append( sqlTable( depth, position ) );
			sql.append( ".* FROM " );
					
			if ( objects || reference!=null || title_criterion!=null )
				{
				sql.append( sqlObjectFrom( depth, position ) );
				sql.append( ", " );
				sql.append( sqlFrom( depth, position ) );
				sql.append( ", " );
				}
			else
				{
				sql.append( sqlFrom( depth, position ) );
				sql.append( ", " );
				}

			// must include the attributes table if there is a search on it.
			if ( attribute_name !=null && attribute_criterion != null )
				{
				sql.append( sqlAttFrom( depth, position ) );
				sql.append( ", " );
				}

			// if there is a word match then word tables are
			if ( useable_words > 0 )
				{
				if ( combination == COMBINE_UNION )
					{
					sql.append( sqlWordFrom( depth, position ) );
					sql.append( "_0, " );
					}
				else
					{
					for ( i=0; i<useable_words; i++ )
						{
						sql.append( sqlWordFrom( depth, position ) );
						sql.append( "_" );
						sql.append( i );
						sql.append( ", " );
						}
					}
				}

			// there will be ", " at the end so chop it off...
			sql.setLength( sql.length()-2 );
			
			// ready for where clause
			sql.append( " WHERE " );

			// first table joining expressions
			if ( objects || reference!=null || title_criterion!=null )
				{
				sql.append( this.sqlObjectTable( depth, position ) );
				sql.append( ".xml_object_id = " );
				sql.append( this.sqlTable( depth, position ) );
				sql.append( ".xml_object_id AND " );
				}

			// must include the attributes table if there is a search on it.
			if ( attribute_name !=null && attribute_criterion != null )
				{
				sql.append( sqlAttTable( depth, position ) );
				sql.append( ".xml_element_id = " );
				sql.append( sqlTable( depth, position ) );
				sql.append( ".xml_element_id AND " );
				}

			// if there is a word match then word tables are
			if ( useable_words > 0 )
				{
				if ( combination == COMBINE_UNION )
					{
					sql.append( sqlWordTable( depth, position ) );
					sql.append( "_0.xml_element_id = " );
					sql.append( sqlTable( depth, position ) );
					sql.append( ".xml_element_id AND " );
					}
				else
					{
					for ( i=0; i<useable_words; i++ )
						{
						sql.append( sqlWordTable( depth, position ) );
						sql.append( "_" );
						sql.append( i );
						sql.append( ".xml_element_id = " );
						sql.append( sqlTable( depth, position ) );
						sql.append( ".xml_element_id AND " );
						}
					}
				}

			// now the expressions to find the desired records
			if ( reference!=null )
				{
				sql.append( this.sqlObjectTable( depth, position ) );
				sql.append( ".reference = " );
				sql.append( reference.toString() );
				sql.append( " AND " );
				}
			if ( title_criterion!=null )
				{
				sql.append( "(" );
				sql.append( sqlInsert( title_criterion, this.sqlObjectTable( depth, position ) + ".title" ) );
				sql.append( ") AND " );
				}
			
			if ( xml_object_id != null )
			    {
				sql.append( this.sqlTable( depth, position ) );
				sql.append( ".xml_object_id = " );
				sql.append( xml_object_id.toString() );
				sql.append( " AND " );
			    }
			    
			if ( element_name!=null )
				{
				sql.append( this.sqlTable( depth, position ) );
				sql.append( ".element_name = '" );
				sql.append( element_name );
				sql.append( "'" );
				sql.append( " AND " );
				}
								
			if ( root_element )
				{
				sql.append( sqlTable( depth, position ) );
				sql.append( ".left_index = 0" );
				sql.append( " AND " );
				}

								
			if ( attribute_name !=null && attribute_criterion != null )
				{
				sql.append( this.sqlAttTable( depth, position ) + ".name = '" );
				sql.append( attribute_name );
				sql.append( "' AND (" );
				sql.append( sqlInsert( attribute_criterion, this.sqlAttTable( depth, position ) + ".value" ) );
				sql.append( ") AND " );
				}
								
			if ( useable_words > 0 )
			{
			    if ( combination == COMBINE_UNION )
			    {
			    
			            sql.append( "( " );
                    
                    for ( int j = 0; j < all_token_ids.size(); j++ )
                    {
                        Integer[] token_ids = (Integer[])all_token_ids.get( j );
                        
			            for ( int k=0; k<token_ids.length; k++ )
			            {
			                if ( j>0 || k>0 )
			                    sql.append( " OR " );
			                sql.append( sqlWordTable( depth, position ) );
			                sql.append( "_0.xml_token_id = " );
			                sql.append( token_ids[k] );
			            }
			        }
			        sql.append( " )" );
                    
			        sql.append( " AND " );
			        sql.append( sqlWordTable( depth, position ) );
			        sql.append( "_0.flags <= " );
			        sql.append( repository.strengthToWordFlag( word_strength ) );
			        sql.append( " " );			        
			        sql.append( " AND " );
			    }
			    else if ( combination == COMBINE_INTERCEPT )
			    {
			        int count = 0;
			        for ( int j = 0; j < all_token_ids.size(); j++ )
			        {
                        sql.append( "( " );
			            Integer[] token_ids = (Integer[])all_token_ids.get( j );
			            for ( int k=0; k<token_ids.length; k++ )
			            {
			                if ( k == 0 )
			                { // first token in each array should be closest match...
                              // use in AND statement
			                    sql.append( "(( " );
			                }
			                else
			                { // remaining token ids (if any) are for matches from wildcard
			                  // append with OR:
			                    sql.append( "OR " );
//			                    sql.append( "( " );
			                }
			                sql.append( sqlWordTable( depth, position ) );
			                sql.append( "_" );
			                sql.append( count );
			                sql.append( ".xml_token_id = " );
			                sql.append( token_ids[k] );
                            sql.append( " " );
                        }
                        sql.append( " )" );
			            sql.append( " AND " );
			            sql.append( sqlWordTable( depth, position ) );
			            sql.append( "_" );
			            sql.append( count );
			            sql.append( ".flags <= " );
			            sql.append( repository.strengthToWordFlag( word_strength ) );
			            sql.append( " ) " );
//			            }
			            sql.append( " ) " );
			            sql.append( " AND " );
			            count++;
			        }
			    }
			    else
			        throw new IllegalArgumentException( "Unknown combination operator." );
			}

			if ( text_criterion!=null )
				{
				throw new IllegalArgumentException( "Text criterion not implemented." );
				}
						

			// did containing query specify some joining SQL ?
			if ( sql_connection != null )
				sql.append( sql_connection );
			}
			
			
		if ( this.size()>1 &&
				relationship != RELATE_FAMILY && 
				relationship != RELATE_DESCENDENT && 
				relationship != RELATE_CHILD &&
				relationship != RELATE_ANCESTOR &&
				relationship != RELATE_PARENT
					)
				throw new IllegalArgumentException( "Unrecognized relationship option." );
				
		if ( hasCriteria() )
			{
			// parentheses needed if there are multiple siblings
			if ( this.size()>1 )
				sql.append( " ( " );
				
			for ( i=0; i<this.size(); i++ )
				{
				current = (XMLQuery)this.elementAt( i );

				// join to previous sibling
				if ( i>0 )
					{
					if ( combination == COMBINE_UNION )
						sql.append( " OR " );
					else if ( combination == COMBINE_INTERCEPT )
						sql.append( " AND " );
					else
						throw new IllegalArgumentException( "Unrecognized combination option." );
					}
					
				// start sub query
				sql.append( " EXISTS (" );
				
				// prepare a clause to join this query to sub query
				connect = new StringBuffer();
				// all type of relationship put the elements into the same object
				connect.append( sqlTable( depth, position ) );
				connect.append( ".xml_object_id = " );
				connect.append( sqlTable( depth+1, topQuery().last_position[depth+1]+1 ) );
				connect.append( ".xml_object_id AND " );
				// plain family relationships need no other specification
				// but descendent and child need another clause...
				if ( relationship != RELATE_FAMILY )
					{
    				if ( relationship == RELATE_DESCENDENT  )
			   		{
						connect.append( sqlTable( depth, position ) );
						connect.append( ".left_index " );
			   		connect.append( "BETWEEN " );
			   		connect.append( sqlTable( depth+1, topQuery().last_position[depth+1]+1 ) );
			   		connect.append( ".left_index AND " );
			   		connect.append( sqlTable( depth+1, topQuery().last_position[depth+1]+1 ) );
			   		connect.append( ".right_index " );
			   		}
    				else if ( relationship == RELATE_ANCESTOR  )
			   		{
						connect.append( sqlTable( depth+1, topQuery().last_position[depth+1]+1 ) );
						connect.append( ".left_index " );
			   		connect.append( "BETWEEN " );
			   		connect.append( sqlTable( depth, position ) );
			   		connect.append( ".left_index AND " );
			   		connect.append( sqlTable( depth, position ) );
			   		connect.append( ".right_index " );
			   		}
					else if ( relationship == RELATE_CHILD  )
			   		{
						connect.append( sqlTable( depth, position ) );
						connect.append( ".xml_parent_id = " );
			   		connect.append( sqlTable( depth+1, topQuery().last_position[depth+1]+1 ) );
			   		connect.append( ".xml_element_id " );
			   		}
			   	else
			   		{
						connect.append( sqlTable( depth, position ) );
			   		connect.append( ".xml_element_id = " );
			   		connect.append( sqlTable( depth+1, topQuery().last_position[depth+1]+1 ) );
						connect.append( ".xml_parent_id " );
			   		}
					connect.append( " AND " );
					}

				// insert sub query which may itself have subqueries
				current.sqlSelect( sql, depth+1, false, connect.toString() );
				
				// close sub query
				sql.append( " ) " );
				}
				
			// close parenthesis after multiple siblings
			if ( this.size()>1 )
				sql.append( " ) " );
			}
		else
			{
			for ( i=0; i<this.size(); i++ )
				{
				current = (XMLQuery)this.elementAt( i );

				// join to previous sibling
				if ( combination == COMBINE_UNION )
					{
					if ( i>0 )
						sql.append( " UNION " );
					if ( size() > 1 )
						sql.append( "(" );
					}
				else if ( combination == COMBINE_INTERCEPT )
					{
					if ( i>0 )
						sql.append( " AND EXISTS (" );
					}
				else
					throw new IllegalArgumentException( "Unrecognized combination option." );
					
				
				// prepare a clause to join this query to previous query
				if ( combination == COMBINE_UNION )
					connect=null;
				else
					{
					if ( i==0 )
						connect=null;
					else
						{
						connect = new StringBuffer();
						// all type of relationship put the elements into the same object
						if ( objects )
							{
							connect.append( sqlObjectTable( depth+1, topQuery().last_position[depth+1] ) );
							connect.append( ".xml_object_id = " );
							connect.append( sqlObjectTable( depth+1, topQuery().last_position[depth+1]+1 ) );
							connect.append( ".xml_object_id AND " );
							}
						else
							{
							connect.append( sqlTable( depth+1, topQuery().last_position[depth+1] ) );
							connect.append( ".xml_element_id = " );
							connect.append( sqlTable( depth+1, topQuery().last_position[depth+1]+1 ) );
							connect.append( ".xml_element_id AND " );
							}
						}
					}
					
				// insert sub query which may itself have subqueries
				current.sqlSelect( sql, depth+1, objects, connect!=null?connect.toString():null );
				
				// close sub query
				if ( combination == COMBINE_UNION )
					{
					if ( this.size() > 1 )
						sql.append( ")" );
					}
				}

            // anded subqueries are nested within each other
            // so they need closing
			if ( combination == COMBINE_INTERCEPT )
			    {
			    for ( i=1; i<this.size(); i++ )
    				sql.append( " ) " );
				}
			}
			
		// chop off trailing AND if there is one.
		if ( sql.substring( sql.length()-5, sql.length() ).equals( " AND " ) )
			sql.setLength( sql.length()-5 );

		}

	}
	
