/* * HigieneDataProvider.java * * Created on February 1, 2006, 10:49 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package siprp.impressaofichas; import java.text.*; import java.util.*; import com.evolute.utils.*; import com.evolute.utils.arrays.*; import com.evolute.utils.data.*; import com.evolute.utils.db.*; import com.evolute.utils.metadb.*; import com.evolute.utils.sql.*; import com.evolute.utils.strings.*; import com.evolute.utils.tables.*; import siprp.*; import siprp.data.*; /** * * @author fpalma */ public class ImpressaoFichasDataProvider extends MetaProvider { private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance( DateFormat.SHORT, new Locale( "PT", "pt" ) ); private static final Object LOCK = new Object(); private static ImpressaoFichasDataProvider instance = null; private final Executer executer; /** Creates a new instance of HigieneDataProvider */ public ImpressaoFichasDataProvider() throws Exception { DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER /*SingletonConstants.DBMANAGER*/ ); executer = dbm.getSharedExecuter( this ); } public static MetaProvider getProvider() throws Exception { synchronized( LOCK ) { if( instance == null ) { instance = new ImpressaoFichasDataProvider(); } } return instance; } public ColumnizedMappable[] getAllEmpresas() throws Exception { Select select = new Select( new String[]{ "empresas" }, new String[]{ "id", "designacao_social", "designacao_social_plain" }, new Field( "inactivo" ).isDifferent( "y" ), new String[]{ "designacao_social_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); ColumnizedMappable empresas[] = new ColumnizedMappable[ array.columnLength() ]; for( int n = 0; n < empresas.length; n++ ) { Integer id = ( Integer ) array.get( n, 0 ); String designacao = ( String ) array.get( n, 1 ); empresas[ n ] = new ColumnizedMappable( id, designacao ); } return empresas; } public ColumnizedMappable[] getAllEstabelecimentosForEmpresa( Integer empresaID ) throws Exception { Select select = new Select( new String[]{ "estabelecimentos" }, new String[]{ "id", "nome", "nome_plain" }, new Field( "empresa_id" ).isEqual( empresaID ).and( new Field( "inactivo" ).isDifferent( "y" ) ), new String[]{ "nome_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); ColumnizedMappable estabelecimentos[] = new ColumnizedMappable[ array.columnLength() ]; for( int n = 0; n < estabelecimentos.length; n++ ) { Integer id = ( Integer ) array.get( n, 0 ); String nome = ( String ) array.get( n, 1 ); estabelecimentos[ n ] = new ColumnizedMappable( id, nome ); } return estabelecimentos; } public Object [][]getAllFichasForEstabelecimentoAndDatas( Integer estabelecimentoID, Date dataInicio, Date dataFim ) throws Exception { Vector fichas = new Vector(); Select select = new Select( new String[]{ "trabalhadores" }, new String[]{ "id", "nome", "nome_plain" }, new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ), new String[]{ "nome_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Integer id = ( Integer ) array.get( n, 0 ); String nome = ( String ) array.get( n, 1 ); String nomePlain = ( String ) array.get( n, 2 ); MappableObject fichasTrabalhador[] = getAllFichasForTrabalhadorAndDatas( id, dataInicio, dataFim ); for( int f = 0; f < fichasTrabalhador.length; f++ ) { fichas.add( new Object[]{ fichasTrabalhador[ f ].getID(), nome, fichasTrabalhador[ f ].getValue(), nomePlain } ); } } Object fichasArray[][] = ( Object[][] )fichas.toArray( new Object[ fichas.size() ][] ); Arrays.sort( fichasArray, new Comparator(){ public int compare( Object o1, Object o2 ) { Object arr1[] = ( Object[] )o1; Object arr2[] = ( Object[] )o2; int res = 0; if( arr1[ 2 ] == null && arr2[ 2 ] == null ) { res = 0; } else if( arr1[ 2 ] == null ) { res = -1; } else if( arr2[ 2 ] == null ) { res = 1; } else { res = ( (Date)arr1[ 2 ]).compareTo( (Date)arr2[ 2 ] ); } if( res == 0 ) { res = ( (String)arr1[ 3 ]).compareTo( (String)arr2[ 3 ] ); } return res; } } ); for( int n = 0; n < fichasArray.length; n++ ) { fichasArray[ n ][ 2 ] = fichasArray[ n ][ 2 ] != null? DATE_FORMAT.format( (Date)fichasArray[ n ][ 2 ] ): ""; } return fichasArray; } public MappableObject []getAllFichasForTrabalhadorAndDatas( Integer trabalhadorID, Date dataInicio, Date dataFim ) throws Exception { Select select = new Select( new String[]{ "exames" }, new String[]{ "MAX(id)", "data" }, new Field( "trabalhador_id" ).isEqual( trabalhadorID ).and( new Field( "inactivo" ).isEqual( "n" ) ).and( new Field( "data" ).between( new Object[]{ dataInicio, dataFim } ) ), new String[]{ "data DESC" }, new String[]{ "data" } ); Virtual2DArray array = executer.executeQuery( select ); MappableObject exames[] = new MappableObject[ array.columnLength() ]; for( int n = 0; n < exames.length; n++ ) { Date date = (Date)array.get( n, 1 ); Integer id = new Integer( ((Number)array.get( n, 0 )).intValue() ); exames[ n ] = new MappableObject( id, date ); } return exames; } public byte[] getPDF( Integer exameID ) throws Exception { Select select = new Select( new String[]{ "exames" }, new String[]{ "pdf" }, new Field( "id" ).isEqual( exameID ) ); Virtual2DArray array = executer.executeQuery( select ); byte pdf[] = ( byte[] ) array.get( 0, 0 ); return pdf; } public String getNomeTrabalhadorForExameID( Integer exameID ) throws Exception { Select select = new Select( new String[]{ "exames", "trabalhadores" }, new String[]{ "trabalhadores.nome_plain" }, new Field( "exames.id" ).isEqual( exameID ).and( new Field( "exames.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); return ( String ) array.get( 0, 0 ); } public Object[] getDadosForExameID( Integer exameID ) throws Exception { Select select = new Select( new String[]{ "exames", "trabalhadores" }, new String[]{ "trabalhadores.nome", "exames.data", "exames.proximo_exame" }, new Field( "exames.id" ).isEqual( exameID ).and( new Field( "exames.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); return array.getObjects()[ 0 ]; } }