forked from Coded/SIPRP
				
			04/11/2008
	
		
	
				
					
				
			git-svn-id: https://svn.coded.pt/svn/SIPRP@797 bb69d46d-e84e-40c8-a05a-06db0d6337410'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
							parent
							
								
									868fc4da14
								
							
						
					
					
						commit
						ee8745dfd6
					
				| @ -0,0 +1,65 @@ | |||||||
|  | /* | ||||||
|  |  * Dblocal.java | ||||||
|  |  * | ||||||
|  |  * Created on September 20, 2007, 2:07 PM | ||||||
|  |  * | ||||||
|  |  * To change this template, choose Tools | Template Manager | ||||||
|  |  * 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 javax.servlet.http.HttpSession; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  * @author lluis | ||||||
|  |  */ | ||||||
|  | public class Dblocal { | ||||||
|  |     String connectionURL = "jdbc:postgresql://storage/siprp_local"; //testes
 | ||||||
|  |     //String connectionURL = "jdbc:postgresql://localhost:5436/siprp_local_3";    
 | ||||||
|  |     String User = "postgres"; | ||||||
|  |     String Pass = null; | ||||||
|  |     Connection connection = null;     | ||||||
|  |      | ||||||
|  |     /** Creates a new instance of Dblocal */ | ||||||
|  |     public Dblocal() { | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     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,51 @@ | |||||||
|  | /* | ||||||
|  |  * To change this template, choose Tools | Templates | ||||||
|  |  * and open the template in the editor. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | package db.entidades; | ||||||
|  | 
 | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  * @author lluis | ||||||
|  |  */ | ||||||
|  | public class Area { | ||||||
|  |     private Integer id; | ||||||
|  |     private Integer plano_id; | ||||||
|  |     private String descricao; | ||||||
|  |     private List<Risco> riscos; | ||||||
|  | 
 | ||||||
|  |     public Integer getId() { | ||||||
|  |         return id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setId(Integer id) { | ||||||
|  |         this.id = id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getDescricao() { | ||||||
|  |         return descricao; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setDescricao(String descricao) { | ||||||
|  |         this.descricao = descricao; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public List<Risco> getRiscos() { | ||||||
|  |         return riscos; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setRiscos(List<Risco> riscos) { | ||||||
|  |         this.riscos = riscos; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Integer getPlano_id() { | ||||||
|  |         return plano_id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setPlano_id(Integer plano_id) { | ||||||
|  |         this.plano_id = plano_id; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,51 @@ | |||||||
|  | /* | ||||||
|  |  * To change this template, choose Tools | Templates | ||||||
|  |  * and open the template in the editor. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | package db.entidades; | ||||||
|  | 
 | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  * @author lluis | ||||||
|  |  */ | ||||||
|  | public class Valor { | ||||||
|  |     private Integer id; | ||||||
|  |     private Integer risco_id; | ||||||
|  |     private Integer valor; | ||||||
|  |     private List<Medida> medidas; | ||||||
|  | 
 | ||||||
|  |     public Integer getValor() { | ||||||
|  |         return valor; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setValor(Integer valor) { | ||||||
|  |         this.valor = valor; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public List<Medida> getMedidas() { | ||||||
|  |         return medidas; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setMedidas(List<Medida> medidas) { | ||||||
|  |         this.medidas = medidas; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Integer getId() { | ||||||
|  |         return id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setId(Integer id) { | ||||||
|  |         this.id = id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Integer getRisco_id() { | ||||||
|  |         return risco_id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setRisco_id(Integer risco_id) { | ||||||
|  |         this.risco_id = risco_id; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,220 @@ | |||||||
|  | /* | ||||||
|  |  * 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(); | ||||||
|  |                 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 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 += "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 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_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;                         | ||||||
|  |     } | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue