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.
242 lines
11 KiB
242 lines
11 KiB
/*
|
|
* To change this template, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
package db.providers;
|
|
|
|
import db.Dblocal;
|
|
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.List;
|
|
|
|
/**
|
|
*
|
|
* @author lluis
|
|
*/
|
|
public class RelatoriosDataProvider extends GenericDataProvider {
|
|
Dblocal dblocal = new Dblocal();
|
|
|
|
public RelatoriosDataProvider() throws Exception
|
|
{
|
|
dblocal.connect();
|
|
}
|
|
|
|
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 = dblocal.createStatement();
|
|
|
|
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.setDescricao(rs.getString("descricao"));
|
|
try
|
|
{
|
|
a.setRiscos(getRiscosByArea(a));
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
a.setRiscos(null);
|
|
}
|
|
|
|
areas.add(a);
|
|
}while(rs.next());
|
|
plano.setAreas(areas);
|
|
}
|
|
return plano;
|
|
}
|
|
|
|
private List<Risco> getRiscosByArea(Area a) throws Exception
|
|
{
|
|
Statement st = dblocal.createStatement();
|
|
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") ));
|
|
r.setDescricao(rs.getString("descricao"));
|
|
r.setValores(getValoresByRisco(r, a));
|
|
riscos.add(r);
|
|
}while(rs.next());
|
|
}
|
|
return riscos;
|
|
|
|
}
|
|
|
|
private List<Valor> getValoresByRisco(Risco r, Area a) throws Exception
|
|
{
|
|
Statement st = dblocal.createStatement();
|
|
String sql = "select valor from ";
|
|
sql += "(select distinct hs_relatorio_posto_risco.risco_id, ";
|
|
sql += "case hs_relatorio_posto_risco.valor_qualitativo_id isnull when true 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();
|
|
if(rs.isBeforeFirst())
|
|
{
|
|
rs.first();
|
|
do
|
|
{
|
|
Valor v = new Valor();
|
|
Object oValor = rs.getObject("valor");
|
|
if(oValor == null)
|
|
{
|
|
v.setValor(null);
|
|
}
|
|
else
|
|
{
|
|
v.setValor((Integer) oValor);
|
|
}
|
|
//int valor = rs.getInt("valor");
|
|
//v.setValor(new Integer(valor));
|
|
v.setMedidas(getMedidasByValor(v, r, a));
|
|
valores.add(v);
|
|
}while(rs.next());
|
|
}
|
|
return valores;
|
|
}
|
|
|
|
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(rs.getString("descricao"));
|
|
m.setPostos(getPostosByMedidaAndValor(m, v, 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(rs.getString("descricao"));
|
|
postos.add(p);
|
|
}while(rs.next());
|
|
}
|
|
return postos;
|
|
}
|
|
|
|
public String getEmpresaNome(Integer empresa_id) throws Exception
|
|
{
|
|
Statement st = dblocal.createStatement();
|
|
String sql ="SELECT designacao_social FROM empresas WHERE id = " + empresa_id;
|
|
ResultSet rslocal = st.executeQuery(sql);
|
|
rslocal.first();
|
|
String nome = rslocal.getString("designacao_social");
|
|
return nome;
|
|
}
|
|
} |