forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
600 lines
26 KiB
600 lines
26 KiB
/*
|
|
* 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();
|
|
}
|
|
}
|