/* * LembretesDataProvider.java * * Created on February 5, 2007, 6:09 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package siprp.lembretes; 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.Assignment; import com.evolute.utils.sql.Delete; import com.evolute.utils.sql.Field; import com.evolute.utils.sql.Insert; import com.evolute.utils.sql.Select; import java.util.Date; import java.util.HashMap; /** * * @author lflores */ public class LembretesDataProvider { private static final Object LOCK = new Object(); private static LembretesDataProvider instance = null; private final HashMap TIPOS_LEMBRETES_BY_CODIGO = new HashMap(); private final HashMap TIPOS_LEMBRETES_BY_ID = new HashMap(); private TipoLembrete tiposLembrete[]; private Executer EXECUTER; /** Creates a new instance of LembretesDataProvider */ public LembretesDataProvider() throws Exception { DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER ); EXECUTER = dbm.getSharedExecuter( this ); } public static LembretesDataProvider getProvider() throws Exception { synchronized( LOCK ) { if( instance == null ) { instance = new LembretesDataProvider(); } } return instance; } public TipoLembrete[] getTiposLembrete() throws Exception { if( tiposLembrete == null ) { Select select = new Select( new String[]{ "lembretes_tipos" }, new String[]{ "id", "codigo", "descricao", "ordem" }, new Field( "activo" ).isEqual( "y" ), new String[]{ "ordem" }, null ); Virtual2DArray array = EXECUTER.executeQuery( select ); tiposLembrete = new TipoLembrete[ array.columnLength() ]; for( int n = 0; n < tiposLembrete.length; n++ ) { Integer id = ( Integer ) array.get( n, 0 ); String codigo = ( String ) array.get( n, 1 ); String descricao = ( String ) array.get( n, 2 ); Integer ordem = ( Integer ) array.get( n, 3 ); tiposLembrete[ n ] = new TipoLembrete( id, codigo, descricao, ordem ); TIPOS_LEMBRETES_BY_CODIGO.put( codigo, tiposLembrete[ n ] ); TIPOS_LEMBRETES_BY_ID.put( id, tiposLembrete[ n ] ); } } return tiposLembrete; } public TipoLembrete getTipoLembreteByCodigo( String codigo ) throws Exception { getTiposLembrete(); return TIPOS_LEMBRETES_BY_CODIGO.get( codigo ); } public TipoLembrete getTipoLembreteByID( Integer id ) throws Exception { getTiposLembrete(); return TIPOS_LEMBRETES_BY_ID.get( id ); } public void criarLembrete( Integer tipoID, Date data, String descricao, String texto, Integer empresaID, Integer estabelecimentoID, Integer trabalhadorID, Integer marcacaoEstabelecimentoID, Integer marcacaoTrabalhadorID, boolean enviarEmail, Integer periodicidadeDias, Integer periodicidadeMeses ) throws Exception { Insert insert = new Insert( "lembretes", new Assignment[]{ new Assignment( new Field( "tipo_id" ), tipoID ), new Assignment( new Field( "data" ), data ), new Assignment( new Field( "descricao" ), descricao ), new Assignment( new Field( "texto" ), texto ), new Assignment( new Field( "empresa_id" ), empresaID ), new Assignment( new Field( "estabelecimento_id" ), estabelecimentoID ), new Assignment( new Field( "trabalhador_id" ), trabalhadorID ), new Assignment( new Field( "marcacao_estabelecimento_id" ), marcacaoEstabelecimentoID ), new Assignment( new Field( "marcacao_trabalhador_id" ), marcacaoTrabalhadorID ), new Assignment( new Field( "enviar_email" ), enviarEmail ? "y" : "n" ), new Assignment( new Field( "periodicidade_dias" ), periodicidadeDias ), new Assignment( new Field( "periodicidade_meses" ), periodicidadeMeses ) } ); EXECUTER.executeQuery( insert ); } public void apagarLembreteByID( Integer id ) throws Exception { Delete delete = new Delete( "lembretes", new Field( "id" ).isEqual( id ) ); EXECUTER.executeQuery( delete ); } public void apagarLembreteByMarcacaoTrabalhadorID( Integer marcacaoID ) throws Exception { Delete delete = new Delete( "lembretes", new Field( "marcacao_trabalhador_id" ).isEqual( marcacaoID ) ); EXECUTER.executeQuery( delete ); } public int countLembretesByTipo( Integer tipoID ) throws Exception { Select select = new Select( new String[]{ "lembretes" }, new String[]{ "COUNT(*)" }, new Field( "tipo_id" ).isEqual( tipoID ).and( new Field( "data" ).isLessOrEqual( new Date() ) ) ); Virtual2DArray array = EXECUTER.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } else { return ( ( Number ) array.get( 0, 0 ) ).intValue(); } } public Lembrete[] getLembretesByTipo( Integer tipoID ) throws Exception { Select select = new Select( new String[]{ "lembretes" }, new String[]{ "id", "tipo_id", "data", "descricao", "texto", "empresa_id", "estabelecimento_id", "trabalhador_id", "marcacao_estabelecimento_id", "marcacao_trabalhador_id", "enviar_email", "periodicidade_dias", "periodicidade_meses" }, new Field( "tipo_id" ).isEqual( tipoID ).and( new Field( "data" ).isLessOrEqual( new Date() ) ) ); Virtual2DArray array = EXECUTER.executeQuery( select ); Lembrete lembretes[] = new Lembrete[ array.columnLength() ]; for( int n = 0; n < lembretes.length; n++ ) { Integer id = ( Integer ) array.get( n, 0 ); Date data = ( Date ) array.get( n, 2 ); String descricao = ( String ) array.get( n, 3 ); String texto = ( String ) array.get( n, 4 ); Integer empresaID = ( Integer ) array.get( n, 5 ); Integer estabelecimentoID = ( Integer ) array.get( n, 6 ); Integer trabalhadorID = ( Integer ) array.get( n, 7 ); Integer marcacaoEstabelecimentoID = ( Integer ) array.get( n, 8 ); Integer marcacaoTrabalhadorID = ( Integer ) array.get( n, 9 ); boolean enviarEmail = "y".equals( array.get( n, 10 ) ); Integer periodicidadeDias = ( Integer ) array.get( n, 11 ); Integer periodicidadeMeses = ( Integer ) array.get( n, 12 ); lembretes[ n ] = new Lembrete( id, tipoID, data, descricao, texto, empresaID, estabelecimentoID, trabalhadorID, marcacaoEstabelecimentoID, marcacaoTrabalhadorID, enviarEmail, periodicidadeDias, periodicidadeMeses ); } return lembretes; } public Lembrete getLembreteByID( Integer id ) throws Exception { Select select = new Select( new String[]{ "lembretes" }, new String[]{ "id", "tipo_id", "data", "descricao", "texto", "empresa_id", "estabelecimento_id", "trabalhador_id", "marcacao_estabelecimento_id", "marcacao_trabalhador_id", "enviar_email", "periodicidade_dias", "periodicidade_meses" }, new Field( "id" ).isEqual( id ) ); Virtual2DArray array = EXECUTER.executeQuery( select ); if( array.columnLength() == 0 ) { return null; } else { Integer tipoID = ( Integer ) array.get( 0, 1 ); Date data = ( Date ) array.get( 0, 2 ); String descricao = ( String ) array.get( 0, 3 ); String texto = ( String ) array.get( 0, 4 ); Integer empresaID = ( Integer ) array.get( 0, 5 ); Integer estabelecimentoID = ( Integer ) array.get( 0, 6 ); Integer trabalhadorID = ( Integer ) array.get( 0, 7 ); Integer marcacaoEstabelecimentoID = ( Integer ) array.get( 0, 8 ); Integer marcacaoTrabalhadorID = ( Integer ) array.get( 0, 9 ); boolean enviarEmail = "y".equals( array.get( 0, 10 ) ); Integer periodicidadeDias = ( Integer ) array.get( 0, 11 ); Integer periodicidadeMeses = ( Integer ) array.get( 0, 12 ); return new Lembrete( id, tipoID, data, descricao, texto, empresaID, estabelecimentoID, trabalhadorID, marcacaoEstabelecimentoID, marcacaoTrabalhadorID, enviarEmail, periodicidadeDias, periodicidadeMeses ); } } }