/*
 * PrestadoresDataProvider.java
 *
 * Created on February 2, 2007, 9:53 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.medicina.prestadores;

import com.evolute.utils.Singleton;
import com.evolute.utils.arrays.Object2DArray;
import com.evolute.utils.arrays.Virtual2DArray;
import com.evolute.utils.data.IDObject;
import com.evolute.utils.data.MappableObject;
import com.evolute.utils.db.DBManager;
import com.evolute.utils.db.Executer;
import com.evolute.utils.jdo.JDOProvider;
import com.evolute.utils.sql.Assignment;
import com.evolute.utils.sql.Begin;
import com.evolute.utils.sql.Commit;
import com.evolute.utils.sql.Delete;
import com.evolute.utils.sql.Field;
import com.evolute.utils.sql.Insert;
import com.evolute.utils.sql.Rollback;
import com.evolute.utils.sql.Select;
import com.evolute.utils.ui.search.SearchDialog;
import com.evolute.utils.ui.search.SearchExecuter;
import siprp.data.ContactoData;
import siprp.medicina.prestadores.data.PrestadoresData;

/**
 *
 * @author fpalma
 */
public class PrestadoresDataProvider
		implements SearchExecuter
{
	private static final Object LOCK = new Object();
	private static PrestadoresDataProvider instance = null;
	
	private static final String SEARCH_TITLE = "Procurar prestadores";
	private static final String SEARCH_COLUMNS[] = 
			new String[]{ "Designa\u00e7\u00e3o", "Servi\u00e7os" };
	
	private Executer EXECUTER;
	
	private JDOProvider JDO;
	
	/** Creates a new instance of PrestadoresDataProvider */
	public PrestadoresDataProvider()
		throws Exception
	{
		DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER );
		EXECUTER = dbm.getSharedExecuter( this );
		JDO = ( JDOProvider ) Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER );
	}
	
	public static PrestadoresDataProvider getProvider()
		throws Exception
	{
		synchronized( LOCK )
		{
			if( instance == null )
			{
				instance = new PrestadoresDataProvider();
			}
		}
		return instance;
	}

	public boolean hasDetails()
	{
		return false;
	}

	public String getSearchTitle()
	{
		return SEARCH_TITLE;
	}

	public String[] getColumnNames()
	{
		return SEARCH_COLUMNS;
	}

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

	public Virtual2DArray search(String pattern) throws Exception
	{
		pattern = pattern.trim().toLowerCase();
		pattern = pattern.replace( ' ', '%' );
		Select select = 
				new Select( new String[]{ "prestadores" },
							new String[]{ "id", "nome", "faz_consultas", "faz_ecds", "nome_plain" },
							new Field( "nome_plain" ).isLike( "%" + pattern + "%" ),
							new String[]{ "nome_plain" },
							null );
		Virtual2DArray array = EXECUTER.executeQuery( select );
		Object data[][] = new Object[ array.columnLength() ][ 3 ];
		for( int n = 0; n < data.length; n++ )
		{
			data[ n ][ 0 ] = array.get( n, 0 );
			data[ n ][ 1 ] = array.get( n, 1 );
			String str = "";
			if( "y".equals( array.get( n, 2 ) ) )
			{
				str += "consultas";
			}
			if( "y".equals( array.get( n, 3 ) ) )
			{
				str += ( str.length() > 0 ? ", " : "" ) + "ecds";
			}
			data[ n ][ 2 ] = str;
		}
		return new Object2DArray( data );
	}
	
	public void setGruposProtocoloForPrestador( Integer prestadorID, Integer gruposProtocoloID[] )
		throws Exception
	{
		EXECUTER.executeQuery( Begin.BEGIN );
		try
		{
			Delete delete = new Delete( "prestadores_grupos_protocolo",
										new Field( "prestador_id" ).isEqual( prestadorID ) );
			EXECUTER.executeQuery( delete );
			Insert insert = 
					new Insert( "prestadores_grupos_protocolo",
								new Assignment[]{ 
									new Assignment( new Field( "prestador_id" ), prestadorID ),
									new Assignment( new Field( "grupo_protocolo_id" ), gruposProtocoloID ) } );
			EXECUTER.executeQuery( insert );
		}
		catch( Exception ex )
		{
			EXECUTER.executeQuery( Rollback.ROLLBACK );
		}
		EXECUTER.executeQuery( Commit.COMMIT );
	}
	
	public Integer []getGruposProtocoloIDByPrestadorID( Integer prestadorID )
		throws Exception
	{
		Select select = 
				new Select( new String[]{ "prestadores_grupos_protocolo" },
							new String[]{ "grupo_protocolo_id" },
							new Field( "prestador_id" ).isEqual( prestadorID ) );
		Virtual2DArray array = EXECUTER.executeQuery( select );
		Integer ids[] = new Integer[ array.columnLength() ];
		for( int n = 0; n < ids.length; n++ )
		{
			ids[ n ] = ( Integer ) array.get( n, 0 );
		}
		return ids;
	}
	
	public PrestadoresData getPrestadorByID( Integer id )
		throws Exception
	{
		return ( PrestadoresData ) JDO.load( PrestadoresData.class, id );
	}
	
	public void savePrestador( PrestadoresData prestador )
		throws Exception
	{
		if( prestador.get( PrestadoresData.CONTACTO_ID ) instanceof ContactoData )
		{
			ContactoData contacto = ( ContactoData )prestador.get( PrestadoresData.CONTACTO_ID );
			contacto.save();
			prestador.set( PrestadoresData.CONTACTO_ID, contacto.get( ContactoData.ID ) );
		}
		prestador.save();
	}
	
	public IDObject[] getPrestadoresActivos()
		throws Exception
	{
		Select select = 
				new Select( new String[]{ "prestadores" },
							new String[]{ "id", "nome", "nome_plain" },
							new Field( "activo" ).isEqual( "y" ),
							new String[]{ "nome_plain" }, null );
		Virtual2DArray array = EXECUTER.executeQuery( select );
		IDObject prestadores[] = new IDObject[ array.columnLength() ];
		for( int n = 0; n < prestadores.length; n++ )
		{
			Integer id = ( Integer ) array.get( n, 0 );
			String nome = ( String ) array.get( n, 1 );
			prestadores[ n ] = new MappableObject( id, nome );
		}
		return prestadores;
	}
	
	public IDObject[] getPrestadoresConsultasActivos()
		throws Exception
	{
		Select select = 
				new Select( new String[]{ "prestadores" },
							new String[]{ "id", "nome", "nome_plain" },
							new Field( "activo" ).isEqual( "y" ).and( 
								new Field( "faz_consultas" ).isEqual( "y" ) ),
							new String[]{ "nome_plain" }, null );
		Virtual2DArray array = EXECUTER.executeQuery( select );
		IDObject prestadores[] = new IDObject[ array.columnLength() ];
		for( int n = 0; n < prestadores.length; n++ )
		{
			Integer id = ( Integer ) array.get( n, 0 );
			String nome = ( String ) array.get( n, 1 );
			prestadores[ n ] = new MappableObject( id, nome );
		}
		return prestadores;
	}
	
	public IDObject[] getPrestadoresECDsActivos()
		throws Exception
	{
		Select select = 
				new Select( new String[]{ "prestadores" },
							new String[]{ "id", "nome", "nome_plain" },
							new Field( "activo" ).isEqual( "y" ).and( 
								new Field( "faz_ecds" ).isEqual( "y" ) ),
							new String[]{ "nome_plain" }, null );
		Virtual2DArray array = EXECUTER.executeQuery( select );
		IDObject prestadores[] = new IDObject[ array.columnLength() ];
		for( int n = 0; n < prestadores.length; n++ )
		{
			Integer id = ( Integer ) array.get( n, 0 );
			String nome = ( String ) array.get( n, 1 );
			prestadores[ n ] = new MappableObject( id, nome );
		}
		return prestadores;
	}
}
