/* * UtilizadoresDataProvider.java * * Created on September 20, 2007, 10:03 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package db; import beans.Medico; import beans.Permissao; import beans.TecnicoSaude; import beans.Utilizador; import com.evolute.utils.error.ErrorLogger; import com.sun.rave.web.ui.model.Option; import global.Global; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.ListIterator; import java.util.StringTokenizer; import utils.Utils; /** * * @author lluis */ public class UtilizadoresDataProvider { /** Creates a new instance of UtilizadoresDataProvider */ public UtilizadoresDataProvider() { } public Integer getMaxUserId() { Statement st = createStatement(); String sql = "SELECT max(utilizadores.id)+1 AS MAXUSERID FROM utilizadores"; try { ResultSet rs = st.executeQuery(sql); rs.first(); Integer newId = new Integer(rs.getInt("MAXUSERID")); if(newId.intValue() == 0) { newId = new Integer(1); } return newId; } catch(Exception ex) { ErrorLogger.logException( ex ); return new Integer(1); } } public Integer createNewUtilizador(Utilizador u) throws Exception { Statement st = createStatement(); Integer newUserId = getMaxUserId(); String sql = "INSERT INTO utilizadores (id, login, password, data_password, email, empresa_id, estabelecimento_id, administrador, tipo, numero_cedula, cap, nome, medico_id, funcionario_hst_id, activo, responsavel_loja, gestor_geral, numero_mecanografico) VALUES ("; sql += newUserId + ", '"; sql += u.getLogin() + "', '"; sql += u.getPassword() + "', "; if(u.getData_password() == null) { sql += null + ", '"; } else { sql += "'" + u.getData_password() + "', '"; } sql += u.getEmail() + "', "; sql += u.getEmpresa_id() + ", "; sql += u.getEstabelecimento_id() + ", '"; sql += u.getAdministrador() + "', "; sql += u.getTipo() + ", '"; sql += u.getNumero_cedula() + "', '"; sql += u.getCap() + "', '"; sql += u.getNome() + "', "; sql += u.getMedico_id() + ", "; sql += u.getFuncionario_hst_id() + ", '"; sql += u.getActivo() + "', '"; sql += u.getResponsavel_loja() + "', '"; sql += u.getGestor_geral() + "', '"; sql += u.getNumero_mecanografico() + "')"; System.out.println("USER SQL : " + sql); st.execute(sql); return newUserId; } public void updateUtilizador(Utilizador u) throws Exception { Statement st = createStatement(); String sql = ""; if(u.getData_password() == null) { sql = "UPDATE utilizadores SET login = '" + u.getLogin() + "', password = '" + u.getPassword() + "', data_password = " + null + ", email = '" + u.getEmail() + "', empresa_id = " + u.getEmpresa_id() + ", estabelecimento_id = " + u.getEstabelecimento_id() + ", administrador = '" + u.getAdministrador() + "', tipo = " + u.getTipo() + ", numero_cedula = '" + u.getNumero_cedula() + "', cap = '" + u.getCap() + "', nome = '" + u.getNome() + "', medico_id = " + u.getMedico_id() + ", funcionario_hst_id = " + u.getFuncionario_hst_id() + ", activo = '" + u.getActivo() + "', responsavel_loja = '" + u.getResponsavel_loja() + "', gestor_geral = '" + u.getGestor_geral() + "', primeiro_login = '" + u.getPrimeiro_login() + "' WHERE id = " + u.getId(); } else { sql = "UPDATE utilizadores SET login = '" + u.getLogin() + "', password = '" + u.getPassword() + "', data_password = '" + u.getData_password() + "', email = '" + u.getEmail() + "', empresa_id = " + u.getEmpresa_id() + ", estabelecimento_id = " + u.getEstabelecimento_id() + ", administrador = '" + u.getAdministrador() + "', tipo = " + u.getTipo() + ", numero_cedula = '" + u.getNumero_cedula() + "', cap = '" + u.getCap() + "', nome = '" + u.getNome() + "', medico_id = " + u.getMedico_id() + ", funcionario_hst_id = " + u.getFuncionario_hst_id() + ", activo = '" + u.getActivo() + "', responsavel_loja = '" + u.getResponsavel_loja() + "', gestor_geral = '" + u.getGestor_geral() + "', primeiro_login = '" + u.getPrimeiro_login() + "' WHERE id = " + u.getId(); } System.out.println("SQL UPDATE USER : " + sql); st.execute(sql); } public void deleteUser(Integer userId) throws Exception { Statement st = createStatement(); String sql = "UPDATE utilizadores SET apagado = 'y', activo = 'n' WHERE id = " + userId; st.execute(sql); } public Utilizador getUtilizador(String login) throws Exception { Utilizador u = new Utilizador(); Statement st = createStatement(); String sql = "SELECT * FROM utilizadores WHERE LOWER(login) = '" + login.toLowerCase() + "' AND apagado = 'n'"; ResultSet rs = st.executeQuery(sql); if ( rs.first() ) { u.setId(new Integer(rs.getInt("id"))); u.setLogin(rs.getString("login")); u.setPassword(rs.getString("password")); u.setData_password(rs.getDate("data_password")); u.setEmail(rs.getString("email")); u.setEmpresa_id(new Integer(rs.getInt("empresa_id"))); u.setEstabelecimento_id(new Integer(rs.getInt("estabelecimento_id"))); u.setAdministrador(rs.getString("administrador")); u.setTipo(new Integer(rs.getInt("tipo"))); u.setNumero_cedula(rs.getString("numero_cedula")); u.setCap(rs.getString("cap")); u.setNome(rs.getString("nome")); u.setMedico_id(new Integer(rs.getInt("medico_id"))); u.setFuncionario_hst_id(new Integer(rs.getInt("funcionario_hst_id"))); u.setActivo(rs.getString("activo")); u.setResponsavel_loja(rs.getString("responsavel_loja")); u.setGestor_geral(rs.getString("gestor_geral")); u.setPrimeiro_login(rs.getString("primeiro_login")); u.setApagado(rs.getString("apagado")); } return u; } public Utilizador getUtilizador(Integer id) throws Exception { Utilizador u = new Utilizador(); Statement st = createStatement(); String sql = "SELECT * FROM utilizadores WHERE id = " + id; ResultSet rs = st.executeQuery(sql); if ( rs.first() ) { u.setId(new Integer(rs.getInt("id"))); u.setLogin(rs.getString("login")); u.setPassword(rs.getString("password")); u.setData_password(rs.getDate("data_password")); u.setEmail(rs.getString("email")); u.setEmpresa_id(new Integer(rs.getInt("empresa_id"))); u.setEstabelecimento_id(new Integer(rs.getInt("estabelecimento_id"))); u.setAdministrador(rs.getString("administrador")); u.setTipo(new Integer(rs.getInt("tipo"))); u.setNumero_cedula(rs.getString("numero_cedula")); u.setCap(rs.getString("cap")); u.setNome(rs.getString("nome")); u.setMedico_id(new Integer(rs.getInt("medico_id"))); u.setFuncionario_hst_id(new Integer(rs.getInt("funcionario_hst_id"))); u.setActivo(rs.getString("activo")); u.setResponsavel_loja(rs.getString("responsavel_loja")); u.setGestor_geral(rs.getString("gestor_geral")); u.setApagado(rs.getString("apagado")); } return u; } public ArrayList getUtilizadoresListByTipo(Integer tipo, String responsavel_loja, Integer estabelecimento_id) throws Exception { ArrayList list = new ArrayList(); Statement st = createStatement(); String sql = ""; String estabelecimento_constraint = ""; if(tipo.intValue() == Global.TIPO_UTILIZADOR_RH || tipo.intValue() == Global.TIPO_UTILIZADOR_SEGURANCA) { estabelecimento_constraint = " AND estabelecimento_id = " + estabelecimento_id; } if(responsavel_loja.matches("y")) { sql = "SELECT * FROM utilizadores WHERE activo = 'y' AND apagado = 'n' AND responsavel_loja = 'y' AND tipo = " + tipo + estabelecimento_constraint; } else { sql = "SELECT * FROM utilizadores WHERE activo = 'y' AND apagado = 'n' AND tipo = " + tipo + estabelecimento_constraint; } ResultSet rs = st.executeQuery(sql); rs.first(); do { Utilizador u = new Utilizador(); u.setId(new Integer(rs.getInt("id"))); u.setLogin(rs.getString("login")); u.setPassword(rs.getString("password")); u.setData_password(rs.getDate("data_password")); u.setEmail(rs.getString("email")); u.setEmpresa_id(new Integer(rs.getInt("empresa_id"))); u.setEstabelecimento_id(new Integer(rs.getInt("estabelecimento_id"))); u.setAdministrador(rs.getString("administrador")); u.setTipo(new Integer(rs.getInt("tipo"))); u.setNumero_cedula(rs.getString("numero_cedula")); u.setCap(rs.getString("cap")); u.setNome(rs.getString("nome")); u.setMedico_id(new Integer(rs.getInt("medico_id"))); u.setFuncionario_hst_id(new Integer(rs.getInt("funcionario_hst_id"))); u.setActivo(rs.getString("activo")); u.setResponsavel_loja(rs.getString("responsavel_loja")); list.add(u); }while(rs.next()); return list; } public ArrayList getUtilizadoresList(String por, String nome, Integer estabelecimento_id, String activo, String booDirRh) throws Exception { StringTokenizer stk = null; AnalisesDataProvider adp = new AnalisesDataProvider(); ArrayList list = new ArrayList(); Statement st = createStatement(); String sql = ""; String sql1 = ""; String sql2 = ""; String sql3 = ""; if(por != null && nome != null && estabelecimento_id != null) { sql1 = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(numero_mecanografico) LIKE '%" + por + "%' AND ("; sql3 = ") AND estabelecimento_id = " + estabelecimento_id + " AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; stk = new StringTokenizer (nome); int n = 0; while (stk.hasMoreTokens ()) { if(n > 0) { sql2 += "AND "; } sql2 += "LOWER(nome) LIKE '%" + stk.nextToken () + "%' "; n++; } sql = sql1 + sql2 + sql3; //sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(numero_mecanografico) LIKE '%" + por + "%' AND LOWER(nome) LIKE '%" + nome + "%' AND estabelecimento_id = " + estabelecimento_id + " AND utilizadores.activo ='" + activo + "' ORDER BY numero_mecanografico"; } else if(por != null && nome != null) { sql1 = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(numero_mecanografico) LIKE '%" + por + "%' AND ("; sql3 = ") AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; stk = new StringTokenizer (nome); int n = 0; while (stk.hasMoreTokens ()) { if(n > 0) { sql2 += "AND "; } sql2 += "LOWER(nome) LIKE '%" + stk.nextToken () + "%' "; n++; } sql = sql1 + sql2 + sql3; //sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(numero_mecanografico) LIKE '%" + por + "%' AND LOWER(nome) LIKE '%" + nome + "%' AND utilizadores.activo ='" + activo + "' ORDER BY numero_mecanografico"; } else if(por != null && estabelecimento_id != null) { sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(numero_mecanografico) LIKE '%" + por + "%' AND estabelecimento_id = " + estabelecimento_id + " AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; } else if(nome != null && estabelecimento_id != null) { sql1 = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND ("; sql3 = ") AND estabelecimento_id = " + estabelecimento_id + " AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; stk = new StringTokenizer (nome); int n = 0; while (stk.hasMoreTokens ()) { if(n > 0) { sql2 += "AND "; } sql2 += "LOWER(nome) LIKE '%" + stk.nextToken () + "%' "; n++; } sql = sql1 + sql2 + sql3; //sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(nome) LIKE '%" + nome + "%' AND estabelecimento_id = " + estabelecimento_id + " AND utilizadores.activo ='" + activo + "' ORDER BY numero_mecanografico"; } else if(por != null) { sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(numero_mecanografico) LIKE '%" + por + "%' AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; } else if(nome != null) { sql1 = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND ("; sql3 = ") AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; stk = new StringTokenizer (nome); int n = 0; while (stk.hasMoreTokens ()) { if(n > 0) { sql2 += "AND "; } sql2 += "LOWER(nome) LIKE '%" + stk.nextToken () + "%' "; n++; } sql = sql1 + sql2 + sql3; //sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND LOWER(nome) LIKE '%" + nome + "%' AND utilizadores.activo ='" + activo + "' ORDER BY numero_mecanografico"; } else if(estabelecimento_id != null) { sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND estabelecimento_id = " + estabelecimento_id + " AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; } else // all null { sql = "SELECT * FROM utilizadores, tipos_utilizadores WHERE utilizadores.tipo = tipos_utilizadores.tipo AND utilizadores.activo ='" + activo + "' AND apagado = 'n' AND utilizadores.tipo <> " + Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP + " ORDER BY numero_mecanografico"; } System.out.println("UTILIZADORES SQL : " + sql); ResultSet rs = st.executeQuery(sql); rs.first(); do { Utilizador u = new Utilizador(); u.setId(new Integer(rs.getInt(1))); u.setLogin(rs.getString("login")); u.setPassword(rs.getString("password")); u.setData_password(rs.getDate("data_password")); u.setEmail(rs.getString("email")); u.setEmpresa_id(new Integer(rs.getInt("empresa_id"))); u.setEstabelecimento_id(new Integer(rs.getInt("estabelecimento_id"))); u.setAdministrador(rs.getString("administrador")); u.setTipo(new Integer(rs.getInt("tipo"))); u.setNumero_cedula(rs.getString("numero_cedula")); u.setCap(rs.getString("cap")); u.setNome(Utils.unicodeToHTML(rs.getString("nome")) ); u.setMedico_id(new Integer(rs.getInt("medico_id"))); u.setFuncionario_hst_id(new Integer(rs.getInt("funcionario_hst_id"))); u.setActivo(rs.getString(14)); u.setResponsavel_loja(rs.getString("responsavel_loja")); u.setGestor_geral(rs.getString("gestor_geral")); u.setNumero_mecanografico(rs.getString("numero_mecanografico")); u.setCategoria(rs.getString("descricao")); u.setApagado(rs.getString("apagado")); try { u.setNome_estabelecimento( adp.getEstabelecimentoNome(u.getEstabelecimento_id()) ); } catch(Exception e) { ErrorLogger.logException( e ); } if(booDirRh.matches("y")) { int uType = u.getTipo().intValue(); if ( uType == Global.TIPO_UTILIZADOR_SEGURANCA || uType == Global.TIPO_UTILIZADOR_RH || uType == Global.TIPO_UTILIZADOR_GESTOR || uType == Global.TIPO_UTILIZADOR_DIRECTOR_LOJA ) { list.add( u ); } } else { list.add(u); } }while(rs.next()); return list; } public ArrayList getResponsavelRhList(Integer estabelecimento_id) { ArrayList list = new ArrayList(); Utilizador u = null; Statement st = createStatement(); String sql ="SELECT * FROM utilizadores WHERE activo = 'y' AND tipo = " + Global.TIPO_UTILIZADOR_RH + " AND apagado = 'n' AND responsavel_loja = 'y' AND estabelecimento_id = " + estabelecimento_id; try { ResultSet rs = st.executeQuery(sql); rs.first(); do { u = new Utilizador(); u.setId(new Integer(rs.getInt("id"))); u.setNome(rs.getString("nome")); u.setLogin(rs.getString("login")); u.setEmail(rs.getString("email")); list.add(u); }while(rs.next()); return list; } catch(Exception ex) { ErrorLogger.logException( ex ); return null; } } public Medico getMedico(Integer estabelecimento_id) { Medico m = null; Statement st = createStatement(); String sql ="SELECT * FROM utilizadores WHERE activo = 'y' AND tipo = " + Global.TIPO_UTILIZADOR_MEDICO + " AND apagado = 'n' AND estabelecimento_id = " + estabelecimento_id; try { ResultSet rs = st.executeQuery(sql); rs.first(); m = new Medico(); m.setId(new Integer(rs.getInt("id"))); m.setNome(rs.getString("nome")); m.setNumero_cedula(rs.getString("numero_cedula")); return m; } catch(Exception ex) { ErrorLogger.logException( ex ); return null; } } public com.sun.rave.web.ui.model.Option[] getMedicosList() throws Exception { // Dblocal dblocal = new Dblocal(); // dblocal.connect(); // Statement stlocal = dblocal.createStatement(); Statement st = createStatement(); ArrayList list = new ArrayList(); // Medico m = new Medico(); // m.setId(new Integer(0)); // m.setNome("-Seleccionar-"); // list.add(m); String sql ="SELECT * FROM utilizadores WHERE activo = 'y' AND tipo = 5 AND apagado = 'n' "; ResultSet rs = st.executeQuery(sql); rs.first(); do { Medico m = new Medico(); m.setId(new Integer(rs.getInt("id"))); m.setNome(Utils.unicodeToHTML(rs.getString("nome"))); list.add(m); }while(rs.next()); com.sun.rave.web.ui.model.Option[] listOptions = new com.sun.rave.web.ui.model.Option[list.size()]; ListIterator iter = list.listIterator(); int i = 0; while(iter.hasNext()) { Medico m = (Medico) iter.next(); listOptions[i] = new Option(m.getId(), Utils.unicodeToHTML(m.getNome())); i++; } // dblocal.close(); return listOptions; } public com.sun.rave.web.ui.model.Option[] getTecnicosSaudeList() throws Exception { // Dblocal dblocal = new Dblocal(); // dblocal.connect(); // Statement stlocal = dblocal.createStatement(); Statement st = createStatement(); ArrayList list = new ArrayList(); TecnicoSaude t = new TecnicoSaude(); t.setId(new Integer(0)); t.setNome(""); list.add(t); String sql ="SELECT * FROM utilizadores WHERE activo = 'y' AND tipo = " + Global.TIPO_UTILIZADOR_HS; ResultSet rs = st.executeQuery(sql); rs.first(); do { t = new TecnicoSaude(); t.setId(new Integer(rs.getInt("id"))); t.setNome(Utils.unicodeToHTML(rs.getString("nome"))); list.add(t); }while(rs.next()); com.sun.rave.web.ui.model.Option[] listOptions = new com.sun.rave.web.ui.model.Option[list.size()]; ListIterator iter = list.listIterator(); int i = 0; while(iter.hasNext()) { t = (TecnicoSaude) iter.next(); listOptions[i] = new Option(t.getId(), Utils.unicodeToHTML(t.getNome())); i++; } // dblocal.close(); return listOptions; } public Integer getMaxPermissaoId() { Statement st = createStatement(); String sql = "SELECT max(permissoes.id)+1 AS MAXPERMISSAOID FROM permissoes"; try { ResultSet rs = st.executeQuery(sql); rs.first(); Integer newId = new Integer(rs.getInt("MAXPERMISSAOID")); if(newId.intValue() == 0) { newId = new Integer(1); } return newId; } catch(Exception ex) { ErrorLogger.logException( ex ); return new Integer(1); } } public Integer createPermissao(Permissao p) throws Exception { Statement st = createStatement(); Integer newId = getMaxPermissaoId(); String sql = "INSERT INTO permissoes (id, user_id, codigo_permissao) VALUES (" + newId + ", " + p.getUser_id() + ", " + p.getCodigo_permissao() + ")"; st.execute(sql); return newId; } public Integer getPermissionCode(Integer userId) throws Exception { Statement st = createStatement(); String sql = "SELECT * FROM permissoes WHERE user_id = " + userId; ResultSet rs = st.executeQuery(sql); rs.first(); Integer permission = new Integer(rs.getInt("codigo_permissao")); return permission; } private Statement createStatement() { Db db = new Db(); return db.createStatement(); } }