/*
 * SearchExecuterFactory.java
 *
 * Created on 24 de Maio de 2004, 19:30
 */

package siprp.data;

import com.evolute.utils.*;
import com.evolute.utils.arrays.*;
import com.evolute.utils.db.*;
import com.evolute.utils.sql.*;
import com.evolute.utils.ui.search.*;

/**
 *
 * @author  fpalma
 */
public class SearchExecuterFactory
{
	private static SearchExecuter empresaExecuter;
	private static SearchExecuter estabelecimentoExecuter;
	private static SearchExecuter trabalhadorExecuter;
	
	
	
	/** Creates a new instance of SearchExecuterFactory */
	private SearchExecuterFactory()
	{
	}
	
	public static SearchExecuter getSearchExecuter( Class objectClass )
	{
		if( objectClass.equals( EmpresaData.class ) )
		{
System.out.println( "Empresa" );
			return getEmpresaSearchExecuter();
		}
		else if( objectClass == EstabelecimentoData.class )
		{
			return getEstabelecimentoSearchExecuter();
		}
		else if( objectClass == TrabalhadorData.class )
		{
			return getTrabalhadorSearchExecuter();
		}
		return null;
	}
	
	private static SearchExecuter getEmpresaSearchExecuter()
	{
		if( empresaExecuter == null )
		{
			empresaExecuter = new SearchExecuter(){
		
				private final DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER /*SingletonConstants.DBMANAGER*/ );
				
				public Virtual2DArray search( String pattern )
					throws Exception
				{

					Executer executer = dbm.getSharedExecuter( this );
					Select select = new Select( new String[]{ "empresas" },
							new String[]{ EmpresaData.ID, EmpresaData.DESIGNACAO_SOCIAL, EmpresaData.DESIGNACAO_SOCIAL_PLAIN },
							new Field( EmpresaData.DESIGNACAO_SOCIAL_PLAIN ).isLike( "%" + com.evolute.utils.strings.StringPlainer.convertString( pattern, false, false ) + "%" ).and(
							new Field( EmpresaData.INACTIVO ).isDifferent( "y" ) ),
							new String[]{ EmpresaData.DESIGNACAO_SOCIAL_PLAIN }, null );
System.out.println( "SELECT : " + select );
					return executer.executeQuery( select );
				}

				public boolean hasDetails()
				{
					return false;
				}

				public void showDetails( SearchDialog dialog, Object o )
					throws Exception
				{
				}

				public String getSearchTitle()
				{
					return "Procurar Empresa";
				}

				public String[] getColumnNames()
				{
					return new String[]{ "Designa\u00e7\u00e3o Social" };
				}
			};
		}
		return empresaExecuter;
	}
	
	private static SearchExecuter getEstabelecimentoSearchExecuter()
	{
		if( estabelecimentoExecuter == null )
		{
			estabelecimentoExecuter = new SearchExecuter(){
				private final DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER /*SingletonConstants.DBMANAGER*/ );
				
				public Virtual2DArray search( String pattern )
					throws Exception
				{
					Executer executer = dbm.getSharedExecuter( this );
					Select select = new Select( new String[]{ "estabelecimentos" },
							new String[]{ EstabelecimentoData.ID, EstabelecimentoData.NOME, EstabelecimentoData.NOME_PLAIN },
							new Field( EstabelecimentoData.NOME_PLAIN ).isLike( "%" + com.evolute.utils.strings.StringPlainer.convertString( pattern, false, false ) + "%" ),
							new String[]{ EstabelecimentoData.NOME_PLAIN }, null );
					return executer.executeQuery( select );
				}

				public boolean hasDetails()
				{
					return false;
				}

				public void showDetails( SearchDialog dialog, Object o )
					throws Exception
				{
				}

				public String getSearchTitle()
				{
					return "Procurar Estabelecimento";
				}

				public String[] getColumnNames()
				{
					return new String[]{ "Designa\u00e7\u00e3o" };
				}
			};
		}
		return estabelecimentoExecuter;
	}
	
	
	private static SearchExecuter getTrabalhadorSearchExecuter()
	{
		if( trabalhadorExecuter == null )
		{
			trabalhadorExecuter = new SearchExecuter(){
				private final DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER /*SingletonConstants.DBMANAGER*/ );				
				
				public Virtual2DArray search( String pattern )
					throws Exception
				{
					Executer executer = dbm.getSharedExecuter( this );
					Select select = new Select( new String[]{ "trabalhadores" },
							new String[]{ TrabalhadorData.ID, TrabalhadorData.NOME, TrabalhadorData.NOME_PLAIN },
							new Field( TrabalhadorData.NOME_PLAIN ).isLike( "%" + com.evolute.utils.strings.StringPlainer.convertString( pattern, false, false ) + "%" ),
							new String[]{ TrabalhadorData.NOME_PLAIN }, null );
					return executer.executeQuery( select );
				}

				public boolean hasDetails()
				{
					return false;
				}

				public void showDetails( SearchDialog dialog, Object o )
					throws Exception
				{
				}

				public String getSearchTitle()
				{
					return "Procurar Trablhador";
				}

				public String[] getColumnNames()
				{
					return new String[]{ "Nome" };
				}
			};
		}
		return trabalhadorExecuter;
	}
}
