forked from Coded/SIPRP
14/01/2009
git-svn-id: https://svn.coded.pt/svn/SIPRP@910 bb69d46d-e84e-40c8-a05a-06db0d6337410'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
parent
8c6e627e8e
commit
20c0107877
@ -1,8 +1,8 @@
|
||||
build.xml.data.CRC32=78944a2b
|
||||
build.xml.data.CRC32=d13032c5
|
||||
build.xml.script.CRC32=50b8b881
|
||||
build.xml.stylesheet.CRC32=c0ebde35
|
||||
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
|
||||
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
|
||||
nbproject/build-impl.xml.data.CRC32=78944a2b
|
||||
nbproject/build-impl.xml.script.CRC32=20e88eee
|
||||
nbproject/build-impl.xml.data.CRC32=d13032c5
|
||||
nbproject/build-impl.xml.script.CRC32=e228659e
|
||||
nbproject/build-impl.xml.stylesheet.CRC32=174458fc
|
||||
|
||||
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import javax.faces.context.FacesContext;
|
||||
import planosactuacao.ApplicationBean1;
|
||||
import utils.JSFUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author lluis
|
||||
*/
|
||||
public class DbRelatorios {
|
||||
//String connectionURL = "jdbc:postgresql://storage:5432/siprp"; //testes
|
||||
String connectionURL = "jdbc:postgresql://localhost:5436/siprp"; //real
|
||||
|
||||
|
||||
String User = "postgres";
|
||||
String Pass = null;
|
||||
Connection connection = null;
|
||||
|
||||
public Connection connect() throws Exception
|
||||
{
|
||||
Class.forName("org.postgresql.Driver").newInstance();
|
||||
//FacesContext fc = FacesContext.getCurrentInstance();
|
||||
//ApplicationBean1 application = JSFUtils.getApplicationBean(fc);
|
||||
//if(application.getConnection() == null)
|
||||
//{
|
||||
connection = DriverManager.getConnection(connectionURL, User, Pass);
|
||||
//application.setConnection(connection);
|
||||
//}
|
||||
|
||||
// connection = DriverManager.getConnection(connectionURL, User, Pass);
|
||||
// HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
|
||||
// session.setAttribute("connection", connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public Connection getConnection()
|
||||
{
|
||||
////FacesContext fc = FacesContext.getCurrentInstance();
|
||||
////ApplicationBean1 application = JSFUtils.getApplicationBean(fc);
|
||||
////connection = application.getConnection();
|
||||
//HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
|
||||
//connection = (Connection) session.getAttribute("connection");
|
||||
return connection;
|
||||
}
|
||||
|
||||
public Statement createStatement()
|
||||
{
|
||||
//HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
|
||||
//connection = (Connection) session.getAttribute("connection");
|
||||
////FacesContext fc = FacesContext.getCurrentInstance();
|
||||
////ApplicationBean1 application = JSFUtils.getApplicationBean(fc);
|
||||
////connection = application.getConnection();
|
||||
Statement st;
|
||||
try
|
||||
{
|
||||
st = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
return st;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import javax.faces.context.FacesContext;
|
||||
import planosactuacao.ApplicationBean1;
|
||||
import utils.JSFUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author lluis
|
||||
*/
|
||||
public class DblocalRelatorios {
|
||||
//String connectionURL = "jdbc:postgresql://storage/siprp_local"; //testes
|
||||
|
||||
//String connectionURL = "jdbc:postgresql://storage/siprp_local_3"; //testes local_3
|
||||
String connectionURL = "jdbc:postgresql://localhost:5436/siprp_local_3";
|
||||
|
||||
|
||||
|
||||
String User = "postgres";
|
||||
String Pass = null;
|
||||
Connection connection = null;
|
||||
|
||||
public Connection connect() throws Exception
|
||||
{
|
||||
Class.forName("org.postgresql.Driver").newInstance();
|
||||
connection = DriverManager.getConnection(connectionURL, User, Pass);
|
||||
|
||||
|
||||
|
||||
//HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
|
||||
//session.setAttribute("connection_local", connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public Statement createStatement()
|
||||
{
|
||||
//HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
|
||||
//connection = (Connection) session.getAttribute("connection_local");
|
||||
|
||||
Statement st;
|
||||
try
|
||||
{
|
||||
st = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
return st;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws Exception
|
||||
{
|
||||
connection.close();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,599 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package db.providers;
|
||||
|
||||
import db.DbRelatorios;
|
||||
import db.DblocalRelatorios;
|
||||
import db.entidades.Area;
|
||||
import db.entidades.Medida;
|
||||
import db.entidades.PlanoActuacao;
|
||||
import db.entidades.PostoTrabalho;
|
||||
import db.entidades.Risco;
|
||||
import db.entidades.Valor;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import utils.Utils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author lluis
|
||||
*/
|
||||
public class CreatePlanosDataProvider {
|
||||
DbRelatorios db;
|
||||
DblocalRelatorios dblocal;
|
||||
|
||||
public CreatePlanosDataProvider()
|
||||
{
|
||||
try
|
||||
{
|
||||
db = new DbRelatorios();
|
||||
db.connect();
|
||||
dblocal = new DblocalRelatorios();
|
||||
dblocal.connect();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected Integer getMaxTableId(String table)
|
||||
{
|
||||
Statement st = createStatement();
|
||||
String id = table + ".id";
|
||||
String sql = "SELECT max(" + id + ")+1 AS MAXID FROM " + table;
|
||||
try
|
||||
{
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
rs.first();
|
||||
Integer newId = new Integer(rs.getInt("MAXID"));
|
||||
if(newId.intValue() == 0)
|
||||
{
|
||||
newId = new Integer(1);
|
||||
}
|
||||
return newId;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
return new Integer(1);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Date> 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 PlanoActuacao getFullPlano(PlanoActuacao plano, Integer relatorioId)
|
||||
{
|
||||
try
|
||||
{
|
||||
plano = getAreasByPlano(plano, relatorioId);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return plano;
|
||||
}
|
||||
|
||||
private PlanoActuacao getAreasByPlano(PlanoActuacao plano, Integer relatorioId) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
|
||||
String sql = "select distinct area_id, hs_relatorio_area.description as descricao from hs_relatorio_posto ";
|
||||
sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id ";
|
||||
sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id ";
|
||||
sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id ";
|
||||
sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id ";
|
||||
sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
sql += "where hs_relatorio.id = " + relatorioId + " ";
|
||||
sql += "order by area_id ";
|
||||
System.out.println("AREAS BY PLANO SQL : " + sql);
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
|
||||
List<Area> areas = new ArrayList();
|
||||
if(rs.isBeforeFirst())
|
||||
{
|
||||
rs.first();
|
||||
do
|
||||
{
|
||||
Area a = new Area();
|
||||
a.setId(new Integer( rs.getInt("area_id") ));
|
||||
a.setArea_id(a.getId());
|
||||
if(existemMedidasByArea(a.getId()))
|
||||
{
|
||||
a.setDescricao(Utils.unicodeToHTML(rs.getString("descricao")));
|
||||
try
|
||||
{
|
||||
a.setRiscos(getRiscosByArea(a));
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
System.out.println("ERRO RISCOS BY AREA !!!!");
|
||||
ex.printStackTrace();
|
||||
a.setRiscos(null);
|
||||
}
|
||||
|
||||
areas.add(a);
|
||||
}
|
||||
|
||||
}while(rs.next());
|
||||
plano.setAreas(areas);
|
||||
}
|
||||
return plano;
|
||||
}
|
||||
|
||||
private List<Risco> getRiscosByArea(Area a) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
String sql = "select distinct hs_relatorio_risco.id, hs_relatorio_risco.description as descricao from hs_relatorio_posto ";
|
||||
sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id ";
|
||||
sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id ";
|
||||
sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id ";
|
||||
sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id ";
|
||||
sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
sql += "where area_id = " + a.getId() + " ";
|
||||
sql += "order by hs_relatorio_risco.id ";
|
||||
System.out.println("RISCOS BY AREA SQL : " + sql);
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
List<Risco> riscos = new ArrayList();
|
||||
if(rs.isBeforeFirst())
|
||||
{
|
||||
|
||||
rs.first();
|
||||
do
|
||||
{
|
||||
Risco r = new Risco();
|
||||
r.setId(new Integer( rs.getInt("id") ));
|
||||
if(existemMedidasByRisco(r.getId()))
|
||||
{
|
||||
r.setDescricao(Utils.unicodeToHTML(rs.getString("descricao")));
|
||||
r.setValores(getValoresByRisco(r, a));
|
||||
//Integer valor = getValorByRisco(r, a);
|
||||
//r.setValor(valor);
|
||||
r.setRisco_id(new Integer( rs.getInt("id") ));
|
||||
r.setMedidas(getMedidasByRisco( r, a));
|
||||
riscos.add(r);
|
||||
}
|
||||
|
||||
}while(rs.next());
|
||||
}
|
||||
return riscos;
|
||||
|
||||
}
|
||||
|
||||
private List<Valor> getValoresByRisco(Risco r, Area a) throws Exception
|
||||
//private Integer getValorByRisco(Risco r, Area a) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
String sql = "select valor from ";
|
||||
sql += "(select distinct hs_relatorio_posto_risco.risco_id, ";
|
||||
sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null ";
|
||||
sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade ";
|
||||
sql += "else hs_relatorio_posto_risco.valor_qualitativo_id ";
|
||||
sql += "end as valor ";
|
||||
sql += "from hs_relatorio_posto ";
|
||||
sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id ";
|
||||
sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id ";
|
||||
sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id ";
|
||||
sql += "inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) ";
|
||||
sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id ";
|
||||
sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
sql += "where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + ") subquery ";
|
||||
sql += "order by subquery.valor";
|
||||
System.out.println("VALORES BY RISCO : " + sql);
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
List<Valor> valores = new ArrayList();
|
||||
Integer valor = null;
|
||||
if(rs.isBeforeFirst())
|
||||
{
|
||||
rs.first();
|
||||
do
|
||||
{
|
||||
Valor v = new Valor();
|
||||
Object oValor = rs.getObject("valor");
|
||||
|
||||
if(oValor == null)
|
||||
{
|
||||
v.setValor(null);
|
||||
//r.setValor(null);
|
||||
valor = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
v.setValor((Integer) oValor);
|
||||
//r.setValor((Integer) oValor);
|
||||
valor = (Integer) oValor;
|
||||
}
|
||||
// System.out.println("RISCO : " + r.getId().toString() + "VALOR : " + oValor.toString());
|
||||
//int valor = rs.getInt("valor");
|
||||
//v.setValor(new Integer(valor));
|
||||
//v.setMedidas(getMedidasByValor(v, r, a));
|
||||
valores.add(v);
|
||||
}while(rs.next());
|
||||
}
|
||||
return valores;
|
||||
//return valor;
|
||||
}
|
||||
|
||||
// private List<Medida> getMedidasByValor(Valor v, Risco r, Area a) throws Exception
|
||||
// {
|
||||
// Statement st = dblocal.createStatement();
|
||||
// String sql = "select distinct medida_id, valor, descricao from ";
|
||||
// sql += "( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, ";
|
||||
// sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null ";
|
||||
// sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade ";
|
||||
// sql += "else hs_relatorio_posto_risco.valor_qualitativo_id ";
|
||||
// sql += "end as valor ";
|
||||
// sql += "from hs_relatorio_posto ";
|
||||
// sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id ";
|
||||
// sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id ";
|
||||
// sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id ";
|
||||
// sql += "inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) ";
|
||||
// sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id ";
|
||||
// sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
// sql += "where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + " and hs_relatorio_posto_medida.is_plano_actuacao = true" + ") subquery ";
|
||||
// //sql += "where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + ") subquery ";
|
||||
// //sql += "where valor = " + v.getValor() + " ";
|
||||
// sql += "order by subquery.medida_id, valor";
|
||||
// System.out.println("MEDIDAS BY VALOR SQL : " + sql);
|
||||
// ResultSet rs = st.executeQuery(sql);
|
||||
// List<Medida> medidas = new ArrayList();
|
||||
// if(rs.isBeforeFirst())
|
||||
// {
|
||||
// rs.first();
|
||||
// do
|
||||
// {
|
||||
// Medida m = new Medida();
|
||||
// //int valor = rs.getInt("valor");
|
||||
// //m.setValor(new Integer(valor));
|
||||
// m.setId(new Integer( rs.getInt("medida_id") ));
|
||||
// m.setDescricao(Utils.unicodeToHTML(rs.getString("descricao")));
|
||||
// m.setPostos(getPostosByMedidaAndValor(m, v, a));
|
||||
// medidas.add(m);
|
||||
// }while(rs.next());
|
||||
// }
|
||||
// return medidas;
|
||||
// }
|
||||
|
||||
private List<Medida> getMedidasByRisco(Risco r, Area a) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
String sql = "select distinct medida_id, valor, descricao from ";
|
||||
sql += "( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, ";
|
||||
sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null ";
|
||||
sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade ";
|
||||
sql += "else hs_relatorio_posto_risco.valor_qualitativo_id ";
|
||||
sql += "end as valor ";
|
||||
sql += "from hs_relatorio_posto ";
|
||||
sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id ";
|
||||
sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id ";
|
||||
sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id ";
|
||||
sql += "inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) ";
|
||||
sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id ";
|
||||
sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
sql += "where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + " and hs_relatorio_posto_medida.is_plano_actuacao = true" + ") subquery ";
|
||||
//sql += "where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + ") subquery ";
|
||||
//sql += "where valor = " + v.getValor() + " ";
|
||||
sql += "order by subquery.medida_id, valor";
|
||||
System.out.println("MEDIDAS BY RISCO SQL : " + sql);
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
List<Medida> medidas = new ArrayList();
|
||||
if(rs.isBeforeFirst())
|
||||
{
|
||||
rs.first();
|
||||
do
|
||||
{
|
||||
Medida m = new Medida();
|
||||
//int valor = rs.getInt("valor");
|
||||
//m.setValor(new Integer(valor));
|
||||
m.setId(new Integer( rs.getInt("medida_id") ));
|
||||
m.setMedida_id(m.getId());
|
||||
m.setDescricao(Utils.unicodeToHTML(rs.getString("descricao")));
|
||||
//m.setPostos(getPostosByMedidaAndValor(m, v, a));
|
||||
m.setPostos(getPostosByMedida(r, m, a));
|
||||
medidas.add(m);
|
||||
}while(rs.next());
|
||||
}
|
||||
return medidas;
|
||||
}
|
||||
|
||||
// private List<PostoTrabalho> getPostosByMedidaAndValor(Medida m, Valor v, Area a) throws Exception
|
||||
// {
|
||||
// Statement st = dblocal.createStatement();
|
||||
// String sql = "select subquery.posto_id, valor, descricao from ";
|
||||
// sql += "(select hs_relatorio_posto_medida.posto_id, hs_relatorio_posto.description as descricao, ";
|
||||
// sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null ";
|
||||
// sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade ";
|
||||
// sql += "else hs_relatorio_posto_risco.valor_qualitativo_id ";
|
||||
// sql += "end as valor ";
|
||||
// sql += "from hs_relatorio_posto_medida ";
|
||||
// sql += "inner join hs_relatorio_posto_risco on hs_relatorio_posto_risco.posto_id = hs_relatorio_posto_medida.posto_id ";
|
||||
// sql += "inner join hs_relatorio_posto on hs_relatorio_posto.id = hs_relatorio_posto_medida.posto_id ";
|
||||
// sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
// sql += "where medida_id = " + m.getId() + " and area_id = " + a.getId() + ") subquery ";
|
||||
// //sql += "where valor = " + v.getValor();
|
||||
// System.out.println("POSTOS BY MEDIDA SQL : " + sql);
|
||||
// ResultSet rs = st.executeQuery(sql);
|
||||
// List<PostoTrabalho> postos = new ArrayList();
|
||||
// if(rs.isBeforeFirst())
|
||||
// {
|
||||
// rs.first();
|
||||
// do
|
||||
// {
|
||||
// PostoTrabalho p = new PostoTrabalho();
|
||||
// p.setId(new Integer( rs.getInt("posto_id") ));
|
||||
// p.setDescricao(Utils.unicodeToHTML(rs.getString("descricao")));
|
||||
// postos.add(p);
|
||||
// }while(rs.next());
|
||||
// }
|
||||
// return postos;
|
||||
// }
|
||||
|
||||
private List<PostoTrabalho> getPostosByMedida(Risco r, Medida m, Area a) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
String sql = "select subquery.posto_id, valor, descricao from ";
|
||||
sql += "(select hs_relatorio_posto_medida.posto_id, hs_relatorio_posto.description as descricao, ";
|
||||
sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null ";
|
||||
sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade ";
|
||||
sql += "else hs_relatorio_posto_risco.valor_qualitativo_id ";
|
||||
sql += "end as valor ";
|
||||
sql += "from hs_relatorio_posto_medida ";
|
||||
sql += "inner join hs_relatorio_posto_risco on hs_relatorio_posto_risco.posto_id = hs_relatorio_posto_medida.posto_id ";
|
||||
sql += "inner join hs_relatorio_posto on hs_relatorio_posto.id = hs_relatorio_posto_medida.posto_id ";
|
||||
sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
sql += "where medida_id = " + m.getId() + " and area_id = " + a.getId() + " and risco_id = " + r.getId() + ") subquery ";
|
||||
//sql += "where valor = " + v.getValor();
|
||||
System.out.println("POSTOS BY MEDIDA SQL : " + sql);
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
List<PostoTrabalho> postos = new ArrayList();
|
||||
if(rs.isBeforeFirst())
|
||||
{
|
||||
rs.first();
|
||||
do
|
||||
{
|
||||
PostoTrabalho p = new PostoTrabalho();
|
||||
p.setId(new Integer( rs.getInt("posto_id") ));
|
||||
p.setPosto_id(p.getId());
|
||||
p.setDescricao(Utils.unicodeToHTML(rs.getString("descricao")));
|
||||
postos.add(p);
|
||||
}while(rs.next());
|
||||
}
|
||||
return postos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private boolean existemMedidasByArea(Integer area_id) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
String sql = "select distinct medida_id, valor, descricao ";
|
||||
sql += "from ( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, ";
|
||||
sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null ";
|
||||
sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade else hs_relatorio_posto_risco.valor_qualitativo_id end as valor from hs_relatorio_posto ";
|
||||
sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id ";
|
||||
sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id ";
|
||||
sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id ";
|
||||
sql += "inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) ";
|
||||
sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id ";
|
||||
sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
sql += "where area_id = " + area_id + " and hs_relatorio_posto_medida.is_plano_actuacao = true) subquery order by subquery.medida_id, valor";
|
||||
System.out.println("EXISTEM MEDIDAS BY AREA SQL : " + sql);
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
if(rs.isBeforeFirst())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean existemMedidasByRisco(Integer risco_id) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
String sql = "select distinct medida_id ";
|
||||
sql += "from ( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, ";
|
||||
sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null ";
|
||||
sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade else hs_relatorio_posto_risco.valor_qualitativo_id end as valor from hs_relatorio_posto ";
|
||||
sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id ";
|
||||
sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id ";
|
||||
sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id ";
|
||||
sql += "inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) ";
|
||||
sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id ";
|
||||
sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id ";
|
||||
sql += "where hs_relatorio_posto_risco.risco_id = " + risco_id + " and hs_relatorio_posto_medida.is_plano_actuacao = true) subquery order by medida_id";
|
||||
System.out.println("EXISTEM MEDIDAS BY RISCO SQL : " + sql);
|
||||
ResultSet rs = st.executeQuery(sql);
|
||||
if(rs.isBeforeFirst())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
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) 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() + "'";
|
||||
}
|
||||
|
||||
sql += ")";
|
||||
|
||||
System.out.println("SQL CREATE PLANO : " + sql);
|
||||
st.execute(sql);
|
||||
|
||||
return newId;
|
||||
}
|
||||
|
||||
|
||||
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) values (";
|
||||
sql += newId + ", ";
|
||||
sql += m.getMedida_id() + ", ";
|
||||
sql += m.getRisco_id() + ", '";
|
||||
sql += m.getDescricao() + "' ";
|
||||
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 String getEmpresaNome(Integer empresa_id) throws Exception
|
||||
{
|
||||
Statement st = createLocalStatement();
|
||||
String sql ="SELECT designacao_social FROM empresas WHERE id = " + empresa_id;
|
||||
ResultSet rslocal = st.executeQuery(sql);
|
||||
rslocal.first();
|
||||
String nome = Utils.unicodeToHTML(rslocal.getString("designacao_social"));
|
||||
return nome;
|
||||
}
|
||||
|
||||
|
||||
protected Statement createStatement()
|
||||
{
|
||||
|
||||
return db.createStatement();
|
||||
}
|
||||
|
||||
protected Statement createLocalStatement()
|
||||
{
|
||||
|
||||
return dblocal.createStatement();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue