/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package db.providers; import com.evolute.utils.error.ErrorLogger; import com.evolute.utils.strings.StringPlainer; import db.entidades.Area; import db.entidades.EstadoMedida; import db.entidades.Medida; import db.entidades.PlanoActuacao; import db.entidades.PostoTrabalho; import db.entidades.Risco; import db.entidades.Utilizador; import db.entidades.Valor; import global.Global; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Vector; import utils.Utils; /** * * @author lluis */ public class PlanosDataProvider extends GenericDataProvider{ // PLANOS public Integer createPlano(PlanoActuacao p) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("planos_actuacao"); String sql = "insert into planos_actuacao (id, estabelecimento_id, fase, data_visita, data_relatorio, nome_estabelecimento, empresa_id, nome_empresa, data_controlo, tecnico_hs_id, tecnico_hs_nome, concluido_por_desactivacao, data_desactivacao, tecnico_superior_hs_id, tecnico_superior_hs_nome) values ("; sql += newId + ", "; sql += p.getEstabelecimento_id() + ", "; sql += p.getFase() + ", '"; java.sql.Date sqlDate = new java.sql.Date(p.getData_visita().getTime()); sql += sqlDate + "', '"; sqlDate = new java.sql.Date(p.getData_relatorio().getTime()); sql += sqlDate + "', '"; sql += p.getNome_estabelecimento() + "', "; sql += p.getEmpresa_id() + ", '"; sql += p.getNome_empresa() + "', '"; Date today = new Date(); sqlDate = new java.sql.Date(today.getTime()); sql += sqlDate + "', "; sql += p.getTecnico_hs_id() + ", "; if(p.getTecnico_hs_nome() == null) { sql += null + ", "; } else { sql += "'" + p.getTecnico_hs_nome() + "', "; } //concluido_por_desactivacao if ( p.getConcluidoPorDesactivacao() == null ) { sql += Boolean.FALSE; sql += ", null"; } else { sql += p.getConcluidoPorDesactivacao(); if ( p.getConcluidoPorDesactivacao() ) { sql += ", now()"; } } sql += ", "; sql += p.getTecnico_superior_hs_id() + ", "; sql += p.getTecnico_superior_hs_nome() == null ? null : "'" + p.getTecnico_superior_hs_nome() + "'"; sql += ")"; System.out.println("SQL CREATE PLANO : " + sql); st.execute(sql); return newId; } public void updatePlano(PlanoActuacao p) throws Exception { if ( p.getConcluidoPorDesactivacao() == null ) { p.setConcluidoPorDesactivacao( false ); } if(p.getObs_correcao() == null) { p.setObs_correcao(""); } if(p.getObservacoes_dl() == null) { p.setObservacoes_dl(""); } if(p.getObservacoes_dns() == null) { p.setObservacoes_dns(""); } if(p.getUser_dir_loja() != null) { if(p.getUser_dir_loja().intValue() == 0) { p.setUser_dir_loja(null); } } if(p.getUser_dns() != null) { if(p.getUser_dns().intValue() == 0) { p.setUser_dns(null); } } if(p.getUser_hs() != null) { if(p.getUser_hs().intValue() == 0) { p.setUser_hs(null); } } Statement st = createStatement(); String sql = ""; //To Do sql string here : sql = "UPDATE planos_actuacao SET fase = " + p.getFase() + ", "; sql += "validacao_director_loja = " + p.getValidacao_director_loja() + ", "; sql += "validacao_dns = " + p.getValidacao_dns() + ", "; sql += "validacao_hs = " + p.getValidacao_hs() + ", "; sql += "observacoes_dl = '" + p.getObservacoes_dl() + "', "; sql += "observacoes_dns = '" + p.getObservacoes_dns() + "', "; sql += "obs_correcao = '" + p.getObs_correcao() + "', "; sql += "correcao = '" + p.getCorrecao() + "', "; sql += "fase_antes_correcao = " + p.getFase_antes_correcao() + ", "; sql += "concluido_por_desactivacao = " + p.getConcluidoPorDesactivacao() + ", "; sql += "data_desactivacao = " + new java.sql.Date( p.getDataDesactivacao().getTime() ) + ", "; if(p.getData_validacao_dir_loja() == null) { sql += "data_validacao_dir_loja = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(p.getData_validacao_dir_loja().getTime()); sql += "data_validacao_dir_loja = '" + sqlDate + "', "; } sql += "user_dir_loja = " + p.getUser_dir_loja() + ", "; if(p.getData_validacao_dns() == null) { sql += "data_validacao_dns = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(p.getData_validacao_dns().getTime()); sql += "data_validacao_dns = '" + sqlDate + "', "; } sql += "user_dns = " + p.getUser_dns() + ", "; if(p.getData_validacao_hs() == null) { sql += "data_validacao_hs = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(p.getData_validacao_hs().getTime()); sql += "data_validacao_hs = '" + sqlDate + "', "; } sql += "user_hs = " + p.getUser_hs() + ", "; if(p.getData_controlo() == null) { sql += "data_controlo = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(p.getData_controlo().getTime()); sql += "data_controlo = '" + sqlDate + "', "; } if(p.getData_email_controlo() == null) { sql += "data_email_controlo = " + null + " "; } else { java.sql.Date sqlDate = new java.sql.Date(p.getData_email_controlo().getTime()); sql += "data_email_controlo = '" + sqlDate + "' "; } sql += "WHERE id = " + p.getId(); System.out.println("SQL UPDATE PLANO : " + sql); ErrorLogger.log( "SAVE : PlanosDataProvider.updatePlano( p ) : " + sql ); st.execute(sql); } public void updateRisco(Risco r) throws Exception { if(r.getResponsavel_execucao() == null) { r.setResponsavel_execucao(""); } if(r.getPor() == null) { r.setPor(""); } if(r.getRecursos_necessarios() == null) { r.setRecursos_necessarios(""); } if(r.getParecer_dl() == null) { r.setParecer_dl(""); } if(r.getParecer_dns() == null) { r.setParecer_dns(""); } if(r.getVerificacao_siprp() == null) { r.setVerificacao_siprp(""); } Statement st = createStatement(); String sql = ""; //To Do sql string here : sql = "UPDATE plano_riscos SET responsavel_execucao = '" + r.getResponsavel_execucao() + "', "; sql += "por = '" + r.getPor() + "', "; sql += "recursos_necessarios = '" + r.getRecursos_necessarios() + "', "; if(r.getData_inicio() == null) { sql += "data_inicio = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(r.getData_inicio().getTime()); sql += "data_inicio = '" + sqlDate + "', "; } if(r.getData_fim() == null) { sql += "data_fim = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(r.getData_fim().getTime()); sql += "data_fim = '" + sqlDate + "', "; } sql += "parecer_dns = '" + r.getParecer_dns() + "', "; sql += "parecer_dl = '" + r.getParecer_dl() + "', "; sql += "verificacao_siprp = '" + r.getVerificacao_siprp() + "' "; sql += "WHERE id = " + r.getId(); System.out.println("SQL UPDATE RISCO : " + sql); ErrorLogger.log( "SAVE : PlanosDataProvider.updateRisco( r ) : " + sql ); st.execute(sql); } public void updateMedidas( Risco risco ) throws Exception { Statement st = createStatement(); List< Medida > medidas = risco.getMedidas(); for ( Medida m : medidas ) { Integer emID = null; if ( m.getValidarMedidaId() != null && m.getValidarMedidaId().intValue() != 0 ) { emID = m.getValidarMedidaId(); } String sql = "UPDATE plano_medidas SET estado_medidas_id = " + emID + " WHERE id = " + m.getId() + " AND risco_id = " + risco.getId(); System.out.println( "SQL UPDATE MEDIDAS : " + sql ); ErrorLogger.log( "SAVE : PlanosDataProvider.updateMedidas( r ) : " + sql ); st.execute( sql ); } } public void updateValor(Valor v) throws Exception { if(v.getResponsavel_execucao() == null) { v.setResponsavel_execucao(""); } if(v.getRecursos_necessarios() == null) { v.setRecursos_necessarios(""); } if(v.getParecer_dl() == null) { v.setParecer_dl(""); } if(v.getParecer_dns() == null) { v.setParecer_dns(""); } if(v.getVerificacao_siprp() == null) { v.setVerificacao_siprp(""); } Statement st = createStatement(); String sql = ""; //To Do sql string here : sql = "UPDATE plano_valores SET responsavel_execucao = '" + v.getResponsavel_execucao() + "', "; sql += "recursos_necessarios = '" + v.getRecursos_necessarios() + "', "; if(v.getData_inicio() == null) { sql += "data_inicio = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(v.getData_inicio().getTime()); sql += "data_inicio = '" + sqlDate + "', "; } if(v.getData_fim() == null) { sql += "data_fim = " + null + ", "; } else { java.sql.Date sqlDate = new java.sql.Date(v.getData_fim().getTime()); sql += "data_fim = '" + sqlDate + "', "; } sql += "parecer_dns = '" + v.getParecer_dns() + "', "; sql += "parecer_dl = '" + v.getParecer_dl() + "', "; sql += "verificacao_siprp = '" + v.getVerificacao_siprp() + "' "; sql += "WHERE id = " + v.getId(); System.out.println("SQL UPDATE VALOR : " + sql); ErrorLogger.log( "SAVE : PlanosDataProvider.updateValor( v ) : " + sql ); st.execute(sql); } public Integer createArea(Area a) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("plano_areas"); String sql = "insert into plano_areas (id, area_id, plano_id, descricao) values ("; sql += newId + ", "; sql += a.getArea_id() + ", "; sql += a.getPlano_id() + ", '"; sql += a.getDescricao() + "' "; sql += ")"; System.out.println("SQL CREATE AREA : " + sql); st.execute(sql); return newId; } public Integer createRisco(Risco r) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("plano_riscos"); String sql = "insert into plano_riscos (id, area_id, descricao, valor, risco_id) values ("; sql += newId + ", "; sql += r.getArea_id() + ", '"; sql += r.getDescricao() + "', "; sql += r.getValor() + ", "; sql += r.getId(); sql += ")"; System.out.println("SQL CREATE RISCO : " + sql); st.execute(sql); return newId; } public Integer createValor(Valor v) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("plano_valores"); String sql = "insert into plano_valores (id, risco_id, valor) values ("; sql += newId + ", "; sql += v.getRisco_id() + ", "; sql += v.getValor() + ""; sql += ")"; System.out.println("SQL CREATE VALOR : " + sql); st.execute(sql); return newId; } public Integer createMedida(Medida m) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("plano_medidas"); String sql = "insert into plano_medidas (id, medida_id, risco_id, descricao, estado_medidas_id) values ("; sql += newId + ", "; sql += m.getMedida_id() + ", "; sql += m.getRisco_id() + ", '"; sql += m.getDescricao() + "', "; sql += m.getValidarMedidaId(); sql += ")"; System.out.println("SQL CREATE MEDIDA : " + sql); st.execute(sql); return newId; } public void createPostoTrabalho(PostoTrabalho p) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("plano_postos_trabalho"); String sql = "insert into plano_postos_trabalho (id, posto_id, medida_id, descricao) values ("; sql += newId + ", "; sql += p.getPosto_id() + ", "; sql += p.getMedida_id() + ", '"; sql += p.getDescricao() + "' "; sql += ")"; System.out.println("SQL CREATE POSTO : " + sql); st.execute(sql); } // public List getPlanosOnline() throws Exception // { // GregorianCalendar xmas = new GregorianCalendar(1990, 11, 25); // Date dummyDate = xmas.getTime(); // ArrayList list = new ArrayList(); // list.add(dummyDate); // Statement st = createStatement(); // String sql = "select data_visita from planos_actuacao"; // ResultSet rs = st.executeQuery(sql); // // if(rs.isBeforeFirst()) // { // rs.first(); // do // { // java.sql.Date sqlDate = rs.getDate("data_visita"); // java.util.Date data_visita = new java.util.Date(sqlDate.getTime()); // System.out.println("DATA PLANO : " + data_visita.toString()); // list.add(data_visita); // }while(rs.next()); // } // return list; // } public String getExpression( String nome, String estabelecimento, String dataVisita ) { String tables = "planos_actuacao"; String whereExpression = "planos_actuacao.deleted_date IS NULL "; if ( nome != null ) { nome = StringPlainer.convertString( nome.trim() ); nome = nome.replaceAll( " ", "%" ); whereExpression += "AND ( plain_utf8( planos_actuacao.tecnico_hs_nome ) LIKE '%" + nome + "%' OR plain_utf8( planos_actuacao.tecnico_superior_hs_nome ) LIKE '%" + nome + "%' ) "; } if ( estabelecimento != null ) { estabelecimento = StringPlainer.convertString( estabelecimento.trim() ); estabelecimento = estabelecimento.replaceAll( " ", "%" ); whereExpression += "AND plain_utf8( planos_actuacao.nome_estabelecimento ) LIKE '%" + estabelecimento + "%' "; } if ( dataVisita != null ) { whereExpression += "AND planos_actuacao.data_visita = '" + dataVisita + "' "; } return "SELECT * FROM " + tables + " WHERE " + whereExpression; } private String getFaseActivos( Utilizador u ) { String sql = ""; int userType = u.getTipo().intValue(); switch ( userType ) { case Global.TECNICO_HS: sql += "(fase = 1 OR fase = 5)"; break; case Global.RESPONSAVEL_SEGURANCA: sql += "fase = 2"; break; case Global.DIRECTOR_LOJA: sql += "fase = 3"; break; case Global.DIRECTOR_NACIONAL_SEGURANCA: sql += "fase = 4"; break; } if ( userType != Global.DIRECTOR_SIPRP && userType != Global.TECNICO_HS && userType != Global.DIRECTOR_NACIONAL_SEGURANCA ) { sql += " AND estabelecimento_id = " + u.getEstabelecimento_id(); } return sql; } public List< PlanoActuacao > searchPlanosActivos( Utilizador u, String nome, String estabelecimento, String dataVisita ) throws Exception { Statement st = createStatement(); String sql = getExpression( nome, estabelecimento, dataVisita ); sql += " AND " + getFaseActivos( u ); System.out.println( "SEARCH ACTIVOS" ); System.out.println( "SQL: " + sql ); ResultSet rs = st.executeQuery(sql); List< PlanoActuacao > list = getFullPlanos( rs ); return list; } // public ArrayList getPlanosActivos(Utilizador u) throws Exception // { // Statement st = createStatement(); // int userType = u.getTipo().intValue(); // Statement st = createStatement(); // String sql = "select * from planos_actuacao WHERE "; // // switch(userType) // { // case Global.TECNICO_HS: // sql += "fase = 1 OR fase = 5"; // break; // // case Global.RESPONSAVEL_SEGURANCA: // sql += "fase = 2"; // break; // // case Global.DIRECTOR_LOJA: // sql += "fase = 3"; // break; // // case Global.DIRECTOR_NACIONAL_SEGURANCA: // sql += "fase = 4"; // break; // } // if ( userType != Global.DIRECTOR_SIPRP && userType != Global.TECNICO_HS && userType != Global.DIRECTOR_NACIONAL_SEGURANCA ) // { // sql += " AND estabelecimento_id = " + u.getEstabelecimento_id(); // } // sql += " AND deleted_date IS NULL"; // // ResultSet rs = st.executeQuery(sql); // ArrayList list = getFullPlanos(rs); // // return list; // } private String getFaseSeguimento( Utilizador u ) { int userType = u.getTipo(); String sql = ""; switch( userType ) { case Global.TECNICO_HS: sql = "(fase <> 1 AND fase <> 5 AND fase <> 6)"; break; case Global.RESPONSAVEL_SEGURANCA: sql = "(fase <> 2 AND fase <> 6)"; break; case Global.DIRECTOR_LOJA: sql = "(fase <> 3 AND fase <> 6)"; break; case Global.DIRECTOR_NACIONAL_SEGURANCA: sql = "(fase <> 4 AND fase <> 6)"; break; case Global.DIRECTOR_SIPRP: case Global.RH: case Global.DIRECTOR_GERAL_RH: case Global.GESTOR: sql = "(fase > 1 AND fase < 6)"; break; default: break; } if ( userType != Global.DIRECTOR_SIPRP && userType != Global.TECNICO_HS && userType != Global.DIRECTOR_NACIONAL_SEGURANCA && userType != Global.DIRECTOR_GERAL_RH && userType != Global.GESTOR ) { sql += " AND estabelecimento_id = " + u.getEstabelecimento_id(); } if ( userType == Global.GESTOR ) { if ( u.getEstabelecimento_gestor().intValue() > 0 ) { sql += " AND estabelecimento_id = " + u.getEstabelecimento_gestor(); } } return sql; } public List< PlanoActuacao > searchPlanosSeguimento( Utilizador u, String nome, String estabelecimento, String dataVisita ) throws Exception { Statement st = createStatement(); String sql = getExpression( nome, estabelecimento, dataVisita ); sql += " AND " + getFaseSeguimento( u ); System.out.println( "SEARCH SEGUIMENTO" ); System.out.println( "SQL: " + sql ); ResultSet rs = st.executeQuery(sql); ArrayList list = getFullPlanos(rs); return list; } // public ArrayList getPlanosSeguimento(Utilizador u) throws Exception // { // Statement st = createStatement(); // // String sql = "select * from planos_actuacao "; // switch(userType) // { // case Global.TECNICO_HS: // sql += "WHERE fase <> 1 AND fase <> 5 AND fase <> 6"; // break; // // case Global.RESPONSAVEL_SEGURANCA: // sql += "WHERE fase <> 2 AND fase <> 6"; // break; // // case Global.DIRECTOR_LOJA: // sql += "WHERE fase <> 3 AND fase <> 6"; // break; // // case Global.DIRECTOR_NACIONAL_SEGURANCA: // sql += "WHERE fase <> 4 AND fase <> 6"; // break; // // case Global.DIRECTOR_SIPRP: // sql += "WHERE fase > 1 AND fase < 6"; // break; // // case Global.RH: // sql += "WHERE fase > 1 AND fase < 6"; // break; // // case Global.DIRECTOR_GERAL_RH: // sql += "WHERE fase > 1 AND fase < 6"; // break; // // case Global.GESTOR: // sql += "WHERE fase > 1 AND fase < 6"; // break; // } // if(userType != Global.DIRECTOR_SIPRP && userType != Global.TECNICO_HS && userType != Global.DIRECTOR_NACIONAL_SEGURANCA && userType != Global.DIRECTOR_GERAL_RH && userType != Global.GESTOR) // { // sql += " and estabelecimento_id = " + u.getEstabelecimento_id(); // } // // //System.out.println("USER ESTABELECIMENTO GESTOR : " + u.getEstabelecimento_gestor().intValue()); // if(userType == Global.GESTOR) // { // if(u.getEstabelecimento_gestor().intValue() > 0) // { // sql += " and estabelecimento_id = " + u.getEstabelecimento_gestor(); // } // } // sql += " and deleted_date is null"; // // System.out.println("PLANOS SEGUIMENTO SQL : " + sql); // ResultSet rs = st.executeQuery(sql); // ArrayList list = getFullPlanos(rs); // return list; // } private String getFaseConcluidos( Utilizador u ) { int userType = u.getTipo().intValue(); String sql = "fase = 6"; if ( userType != Global.DIRECTOR_SIPRP && userType != Global.TECNICO_HS && userType != Global.DIRECTOR_NACIONAL_SEGURANCA && userType != Global.DIRECTOR_GERAL_RH ) { sql += " AND estabelecimento_id = " + u.getEstabelecimento_id(); } return sql; } public List< PlanoActuacao > searchPlanosConcluidos( Utilizador u, String nome, String estabelecimento, String dataVisita ) throws Exception { String sql = getExpression( nome, estabelecimento, dataVisita ); sql += " AND " + getFaseConcluidos( u ); System.out.println( "SEARCH CONCLUIDOS" ); System.out.println( "SQL: " + sql ); Statement st = createStatement(); ResultSet rs = st.executeQuery( sql ); ArrayList< PlanoActuacao > list = getFullPlanos( rs ); return list; } // public ArrayList getPlanosConcluidos(Utilizador u) throws Exception // { // int userType = u.getTipo().intValue(); // Statement st = createStatement(); // String sql = "select * from planos_actuacao where fase = 6"; // if(userType != Global.DIRECTOR_SIPRP && userType != Global.TECNICO_HS && userType != Global.DIRECTOR_NACIONAL_SEGURANCA && userType != Global.DIRECTOR_GERAL_RH) // { // sql += " and estabelecimento_id = " + u.getEstabelecimento_id(); // } // sql += " and deleted_date is null"; // System.out.println("PLANOS CONCLUIDOS SQL : " + sql); // ResultSet rs = st.executeQuery(sql); // ArrayList list = getFullPlanos(rs); // return list; // } private ArrayList getFullPlanos(ResultSet rs) throws Exception { ArrayList list = new ArrayList(); if(rs.isBeforeFirst()) { rs.first(); do { PlanoActuacao pa = new PlanoActuacao(); pa.setId( new Integer( rs.getInt("id") ) ); pa.setEstabelecimento_id(new Integer(rs.getInt("estabelecimento_id"))); pa.setNome_estabelecimento(rs.getString("nome_estabelecimento")); pa.setNome_empresa(rs.getString("nome_empresa")); pa.setFase( new Integer( rs.getInt("fase") ) ); java.sql.Date sqlDate = rs.getDate("data_visita"); pa.setData_visita(new java.util.Date(sqlDate.getTime())); pa.setValidacao_director_loja(new Boolean( rs.getBoolean("validacao_director_loja") )); pa.setValidacao_dns(new Boolean( rs.getBoolean("validacao_dns") )); pa.setValidacao_hs(new Boolean( rs.getBoolean("validacao_hs") )); pa.setObservacoes_dl(rs.getString("observacoes_dl")); pa.setObservacoes_dns(rs.getString("observacoes_dns")); pa.setObs_correcao(rs.getString("obs_correcao")); pa.setAreas(getAreasByPlano(pa)); pa.setTotal_riscos(getTotalRiscosByPlano(pa.getId())); pa.setCorrecao(rs.getString("correcao")); pa.setFase_antes_correcao(new Integer(rs.getInt("fase_antes_correcao"))); pa.setData_validacao_dir_loja(rs.getDate("data_validacao_dir_loja")); pa.setUser_dir_loja(new Integer(rs.getInt("user_dir_loja"))); pa.setData_validacao_dns(rs.getDate("data_validacao_dns")); pa.setUser_dns(new Integer(rs.getInt("user_dns"))); pa.setData_validacao_hs(rs.getDate("data_validacao_hs")); pa.setUser_hs(new Integer(rs.getInt("user_hs"))); pa.setTecnico_hs_id(new Integer(rs.getInt("tecnico_hs_id"))); pa.setTecnico_hs_nome(rs.getString("tecnico_hs_nome")); pa.setConcluidoPorDesactivacao( new Boolean( rs.getBoolean( "concluido_por_desactivacao" ) ) ); pa.setDataDesactivacao( rs.getDate( "data_desactivacao" ) ); pa.setTecnico_superior_hs_id( new Integer( rs.getInt( "tecnico_superior_hs_id" ) ) ); pa.setTecnico_superior_hs_nome( rs.getString( "tecnico_superior_hs_nome" ) ); list.add(pa); }while(rs.next()); } return list; } private List getAreasByPlano(PlanoActuacao p) throws Exception { Statement st = createStatement(); String sql = "SELECT * FROM plano_areas WHERE plano_id = " + p.getId() + " ORDER BY descricao"; ResultSet rs = st.executeQuery(sql); List areas = new ArrayList(); if(rs.isBeforeFirst()) { rs.first(); do { Area a = new Area(); a.setId( new Integer(rs.getInt("id")) ); a.setPlano_id(new Integer( rs.getInt("plano_id") )); a.setDescricao(Utils.unicodeToHTML(rs.getString("descricao"))); a.setRiscos(getRiscosByArea(a)); areas.add(a); }while(rs.next()); } return areas; } private List getRiscosByArea(Area a) throws Exception { List riscos = new ArrayList(); Statement st = createStatement(); // String sql = "SELECT * FROM plano_riscos WHERE area_id = " + a.getId() + " ORDER BY valor DESC"; String sql = "SELECT plano_riscos.*, coalesce(valor,-1) as ordem_valor FROM plano_riscos " + "WHERE area_id = " + a.getId() + " ORDER BY ordem_valor DESC, area_id, risco_id;"; ResultSet rs = st.executeQuery(sql); if(rs.isBeforeFirst()) { rs.first(); do { Risco r = new Risco(); r.setId( new Integer(rs.getInt("id")) ); r.setArea_id(new Integer( rs.getInt("area_id") )); r.setDescricao(Utils.unicodeToHTML(rs.getString("descricao"))); //// r.setResponsavel_execucao(rs.getString("responsavel_execucao")); // r.setRecursos_necessarios(rs.getString("recursos_necessarios")); // java.sql.Date sqlDataInicio = rs.getDate("data_inicio"); // if(sqlDataInicio != null) // { // r.setData_inicio(new java.util.Date(sqlDataInicio.getTime())); // } // // java.sql.Date sqlDataFim = rs.getDate("data_fim"); // if(sqlDataFim != null) // { // r.setData_fim(new java.util.Date(sqlDataFim.getTime())); // } //r.setValores(getValoresByRisco(r)); r.setResponsavel_execucao(rs.getString("responsavel_execucao")); r.setPor(rs.getString("por")); r.setRecursos_necessarios(rs.getString("recursos_necessarios")); java.sql.Date sqlDataInicio = rs.getDate("data_inicio"); if(sqlDataInicio != null) { r.setData_inicio(new java.util.Date(sqlDataInicio.getTime())); } java.sql.Date sqlDataFim = rs.getDate("data_fim"); if(sqlDataFim != null) { r.setData_fim(new java.util.Date(sqlDataFim.getTime())); } r.setParecer_dl(rs.getString("parecer_dl")); r.setParecer_dns(rs.getString("parecer_dns")); r.setVerificacao_siprp(rs.getString("verificacao_siprp")); r.setMedidas(getMedidasByRisco(r)); Object valorObject = rs.getObject("valor"); r.setValor( valorObject == null ? null : (Integer) valorObject ); riscos.add(r); }while(rs.next()); } return riscos; } // private List getValoresByRisco(Risco r) throws Exception // { // List valores = new ArrayList(); // // Statement st = createStatement(); // String sql = "SELECT * FROM plano_valores WHERE risco_id = " + r.getId() + " ORDER BY id"; // ResultSet rs = st.executeQuery(sql); // // if(rs.isBeforeFirst()) // { // rs.first(); // do // { // Valor v = new Valor(); // v.setId( new Integer(rs.getInt("id")) ); // v.setRisco_id(new Integer( rs.getInt("risco_id") )); // Object oValor = rs.getObject("valor"); // if(oValor == null) // { // v.setValor(null); // } // else // { // v.setValor(new Integer(rs.getInt("valor"))); // } // v.setMedidas(getMedidasByValor(v)); // // v.setResponsavel_execucao(rs.getString("responsavel_execucao")); // v.setRecursos_necessarios(rs.getString("recursos_necessarios")); // java.sql.Date sqlDataInicio = rs.getDate("data_inicio"); // if(sqlDataInicio != null) // { // v.setData_inicio(new java.util.Date(sqlDataInicio.getTime())); // } // // java.sql.Date sqlDataFim = rs.getDate("data_fim"); // if(sqlDataFim != null) // { // v.setData_fim(new java.util.Date(sqlDataFim.getTime())); // } // v.setParecer_dl(rs.getString("parecer_dl")); // v.setParecer_dns(rs.getString("parecer_dns")); // v.setVerificacao_siprp(rs.getString("verificacao_siprp")); // valores.add(v); // }while(rs.next()); // } // return valores; // } private List getMedidasByRisco(Risco r) throws Exception { List medidas = new ArrayList(); Statement st = createStatement(); String sql = "SELECT * FROM plano_medidas WHERE risco_id = " + r.getId() + " ORDER BY id"; ResultSet rs = st.executeQuery(sql); if(rs.isBeforeFirst()) { rs.first(); do { Medida m = new Medida(); m.setId( new Integer(rs.getInt("id")) ); //m.setValor_id(new Integer( rs.getInt("valor_id") )); m.setDescricao(Utils.unicodeToHTML(rs.getString("descricao"))); m.setPostos(getPostosByMedida(m)); m.setValidarMedidaId( rs.getInt( "estado_medidas_id" ) ); medidas.add(m); }while(rs.next()); } return medidas; } private List getPostosByMedida(Medida m) throws Exception { List postos = new ArrayList(); Statement st = createStatement(); String sql = "SELECT * FROM plano_postos_trabalho WHERE medida_id = " + m.getId() + " ORDER BY id"; ResultSet rs = st.executeQuery(sql); if(rs.isBeforeFirst()) { rs.first(); do { PostoTrabalho p = new PostoTrabalho(); p.setId( new Integer(rs.getInt("id")) ); p.setMedida_id(new Integer( rs.getInt("medida_id") )); p.setDescricao(Utils.unicodeToHTML(rs.getString("descricao"))); postos.add(p); }while(rs.next()); } return postos; } public int getTotalRiscosByPlano(Integer plano_id) { Statement st = createStatement(); String sql = "select count(plano_riscos.id) as total_riscos from plano_riscos "; sql += "inner join plano_areas on plano_riscos.area_id = plano_areas.id "; sql += "inner join planos_actuacao on plano_areas.plano_id = planos_actuacao.id "; sql += "where planos_actuacao.id = " + plano_id; sql += " and planos_actuacao.deleted_date is null"; int total_riscos = 0; try { ResultSet rs = st.executeQuery(sql); if(rs.isBeforeFirst()) { rs.first(); total_riscos = rs.getInt("total_riscos"); } } catch(Exception ex) { ErrorLogger.logException( ex ); } return total_riscos; } // POSTOS TRABALHO public void createPostoTrabalhoByRisco(Integer postoTrabalhoId, Integer riscoId, Integer planoId) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("postos_trabalho_risco"); String sql = "INSERT INTO postos_trabalho_risco (id, posto_trabalho_id, risco_id, plano_id) VALUES ("; sql += newId + ", "; sql += postoTrabalhoId + ", "; sql += riscoId + ", "; sql += planoId + ")"; System.out.println("SQL CREATE POSTO TRABALHO BY RISCO : " + sql); st.execute(sql); } public PostoTrabalho getPostoTrabalho(Integer postoTrabalhoId) throws Exception { PostoTrabalho pt = null; Statement st = createStatement(); String sql = "SELECT * FROM postos_trabalho WHERE id = " + postoTrabalhoId + " AND activo = 'y'"; ResultSet rs = st.executeQuery(sql); if(rs.isBeforeFirst()) { rs.first(); pt = new PostoTrabalho(); pt.setId( new Integer( rs.getInt("id") ) ); pt.setDescricao( rs.getString("descricao") ); pt.setActivo( rs.getString("activo") ); } return pt; } // public ArrayList getPostosTrabalhoByRisco(Integer planoId, Integer riscoId) throws Exception // { // ArrayList list = new ArrayList(); // Statement st = createStatement(); // String sql = "SELECT * FROM postos_trabalho_risco WHERE plano_id = " + planoId + " AND risco_id = " + riscoId; // // ResultSet rs = st.executeQuery(sql); // // if(rs.isBeforeFirst()) // { // rs.first(); // do // { // Integer postoTrabalhoId = new Integer( rs.getInt("posto_trabalho_id") ); // PostoTrabalho pt = getPostoTrabalho(postoTrabalhoId); // pt.setRisco_id(riscoId); // list.add(pt); // }while(rs.next()); // } // // return list; // } // RISCOS public void createRiscoByPlano(Integer riscoId, Integer planoId) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("riscos_plano_actuacao"); String sql = "INSERT INTO riscos_plano_actuacao (id, risco_id, plano_id) VALUES ("; sql += newId + ", "; sql += riscoId + ", "; sql += planoId + ")"; System.out.println("SQL CREATE RISCO BY PLANO : " + sql); st.execute(sql); } public Risco getRisco(Integer riscoId) throws Exception { Risco r = null; Statement st = createStatement(); String sql = "SELECT * FROM riscos WHERE id = " + riscoId + " AND activo = 'y'"; ResultSet rs = st.executeQuery(sql); if(rs.isBeforeFirst()) { rs.first(); r = new Risco(); r.setId( new Integer( rs.getInt("id") ) ); r.setDescricao( rs.getString("descricao") ); r.setResponsavel_execucao(rs.getString("responsavel_execucao")); r.setPor(rs.getString("por")); r.setRecursos_necessarios(rs.getString("recursos_necessarios")); java.sql.Date sqlDataInicio = rs.getDate("data_inicio"); if(sqlDataInicio != null) { r.setData_inicio(new java.util.Date(sqlDataInicio.getTime())); } java.sql.Date sqlDataFim = rs.getDate("data_fim"); if(sqlDataFim != null) { r.setData_fim(new java.util.Date(sqlDataFim.getTime())); } r.setParecer_dl(rs.getString("parecer_dl")); r.setParecer_dns(rs.getString("parecer_dns")); r.setVerificacao_siprp(rs.getString("verificacao_siprp")); r.setActivo( rs.getString("activo") ); r.setValor(new Integer(rs.getInt("valor"))); } return r; } // public ArrayList getRiscosByPlano(Integer planoId) throws Exception // { // ArrayList list = new ArrayList(); // Statement st = createStatement(); // String sql = "SELECT * FROM riscos_plano_actuacao WHERE plano_id = " + planoId; // // ResultSet rs = st.executeQuery(sql); // // if(rs.isBeforeFirst()) // { // rs.first(); // do // { // Integer riscoId = new Integer( rs.getInt("risco_id") ); // Risco r = getRisco(riscoId); // r.setPlano_id(planoId); // list.add(r); // }while(rs.next()); // } // // return list; // } // MEDIDAS public void createMedidaByRisco(Integer medidaId, Integer riscoId, Integer planoId) throws Exception { Statement st = createStatement(); Integer newId = getMaxTableId("medidas_plano_actuacao_risco"); String sql = "INSERT INTO postos_trabalho_risco (id, medida_id, risco_id, plano_id) VALUES ("; sql += newId + ", "; sql += medidaId + ", "; sql += riscoId + ", "; sql += planoId + ")"; System.out.println("SQL CREATE MEDIDA BY RISCO : " + sql); st.execute(sql); } public List< EstadoMedida > getEstadoMedidas() throws Exception { Statement st = createStatement(); String sql = "SELECT * FROM estado_medidas WHERE activo = 'y' ORDER BY ordem ASC"; ResultSet rs = st.executeQuery( sql ); List< EstadoMedida > list = new Vector< EstadoMedida >(); while( rs.next() ) { Integer ID = rs.getInt( "id" ); String desc = Utils.unicodeToHTML( rs.getString( "descricao" ) ); EstadoMedida estado = new EstadoMedida(); estado.setId( ID ); estado.setDescricao( desc ); list.add( estado ); } return list; } public Medida getMedida(Integer medidaId) throws Exception { Medida m= null; Statement st = createStatement(); String sql = "SELECT * FROM medidas_plano_actuacao WHERE id = " + medidaId + " AND activo = 'y'"; ResultSet rs = st.executeQuery(sql); if(rs.isBeforeFirst()) { rs.first(); m = new Medida(); m.setId( new Integer( rs.getInt("id") ) ); m.setDescricao( rs.getString("descricao") ); m.setActiva( rs.getString("activo") ); } return m; } // public List getPlanosDelayed() throws Exception // { // Date today = new Date(); // java.sql.Date sqlToday = new java.sql.Date(today.getTime()); // Statement st = createStatement(); // String sql = "select dias_atraso, dias_email_atraso from "; // sql += "(select id, EXTRACT(DAY FROM (now()- data_controlo)) as dias_atraso, EXTRACT(DAY FROM (now()- data_email_controlo)) as dias_email_atraso from planos_actuacao) subquery "; // sql += "where dias_atraso > 14 and (dias_email_atraso > 3 or dias_email_atraso isnull) and (fase = 2 or fase = 3) "; // ResultSet rs = st.executeQuery(sql); // // ArrayList list = new ArrayList(); // if(rs.isBeforeFirst()) // { // rs.first(); // do // { // PlanoActuacao pa = new PlanoActuacao(); // pa.setId( new Integer( rs.getInt("id") ) ); // pa.setEstabelecimento_id(new Integer(rs.getInt("estabelecimento_id"))); // pa.setNome_estabelecimento(rs.getString("nome_estabelecimento")); // pa.setFase( new Integer( rs.getInt("fase") ) ); // java.sql.Date sqlDate = rs.getDate("data_visita"); // pa.setData_visita(new java.util.Date(sqlDate.getTime())); // pa.setValidacao_director_loja(new Boolean( rs.getBoolean("validacao_director_loja") )); // pa.setValidacao_dns(new Boolean( rs.getBoolean("validacao_dns") )); // pa.setValidacao_hs(new Boolean( rs.getBoolean("validacao_hs") )); // pa.setObservacoes_dl(rs.getString("observacoes_dl")); // pa.setObservacoes_dns(rs.getString("observacoes_dns")); // pa.setObs_correcao(rs.getString("obs_correcao")); //// pa.setAreas(getAreasByPlano(pa)); //// pa.setTotal_riscos(getTotalRiscosByPlano(pa.getId())); // pa.setCorrecao(rs.getString("correcao")); // pa.setFase_antes_correcao(new Integer(rs.getInt("fase_antes_correcao"))); // pa.setData_validacao_dir_loja(rs.getDate("data_validacao_dir_loja")); // pa.setUser_dir_loja(new Integer(rs.getInt("user_dir_loja"))); // pa.setData_validacao_dns(rs.getDate("data_validacao_dns")); // pa.setUser_dns(new Integer(rs.getInt("user_dns"))); // pa.setData_validacao_hs(rs.getDate("data_validacao_hs")); // pa.setUser_hs(new Integer(rs.getInt("user_hs"))); // pa.setVerificacao_siprp(rs.getString("verificacao_siprp")); // pa.setData_controlo(rs.getDate("data_controlo")); // pa.setData_email_controlo(rs.getDate("data_email_controlo")); // list.add(pa); // // }while(rs.next()); // } // return list; // } // // public void updateControlo(PlanoActuacao p) throws Exception // { // Statement st = createStatement(); // String sql = ""; // // //To Do sql string here : // if(p.getData_controlo() == null) // { // sql = "UPDATE planos_actuacao SET data_controlo = " + null + ", "; // } // else // { // java.sql.Date sqlDate = new java.sql.Date(p.getData_controlo().getTime()); // sql = "UPDATE planos_actuacao SET data_controlo = '" + sqlDate + "', "; // } // // if(p.getData_email_controlo() == null) // { // sql += "data_email_controlo = " + null + " "; // } // else // { // java.sql.Date sqlDate = new java.sql.Date(p.getData_email_controlo().getTime()); // sql += "data_email_controlo = '" + sqlDate + "' "; // } // // sql += "WHERE id = " + p.getId(); // System.out.println("SQL UPDATE CONTROLO : " + sql); // st.execute(sql); // } }