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

package siprp.data;

import siprp.data.outer.EmpresasData;
import siprp.data.outer.EstabelecimentosData;
import siprp.data.outer.TrabalhadoresData;

import com.evolute.utils.Singleton;
import com.evolute.utils.arrays.Virtual2DArray;
import com.evolute.utils.db.DBManager;
import com.evolute.utils.db.Executer;
import com.evolute.utils.sql.Field;
import com.evolute.utils.sql.Select;
import com.evolute.utils.ui.search.SearchDialog;
import com.evolute.utils.ui.search.SearchExecuter;

/**
 *
 * @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( EmpresasData.class ) )
		{
			return getEmpresaSearchExecuter();
		}
		else if( objectClass == EstabelecimentosData.class )
		{
			return getEstabelecimentoSearchExecuter();
		}
		else if( objectClass == TrabalhadoresData.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[]{ EmpresasData.ID, EmpresasData.DESIGNACAO_SOCIAL, EmpresasData.DESIGNACAO_SOCIAL_PLAIN },
							new Field( EmpresasData.DESIGNACAO_SOCIAL_PLAIN ).isLike( "%" + com.evolute.utils.strings.StringPlainer.convertString( pattern, false, false ) + "%" ).and(
							new Field( EmpresasData.INACTIVO ).isDifferent( "y" ) ),
							new String[]{ EmpresasData.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[]{ EstabelecimentosData.ID, EstabelecimentosData.NOME, EstabelecimentosData.NOME_PLAIN },
							new Field( EstabelecimentosData.NOME_PLAIN ).isLike( "%" + com.evolute.utils.strings.StringPlainer.convertString( pattern, false, false ) + "%" ),
							new String[]{ EstabelecimentosData.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[]{ TrabalhadoresData.ID, TrabalhadoresData.NOME, TrabalhadoresData.NOME_PLAIN },
							new Field( TrabalhadoresData.NOME_PLAIN ).isLike( "%" + com.evolute.utils.strings.StringPlainer.convertString( pattern, false, false ) + "%" ),
							new String[]{ TrabalhadoresData.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;
	}
}
