Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.ipzs.acque.service.balneazione.impl;
- import it.ipzs.acque.controller.utility.CriteriRicercaScadenzario;
- import it.ipzs.acque.domain.menu.Utente;
- import it.ipzs.acque.domain.menu.UtenteGeografia;
- import it.ipzs.acque.service.balneazione.CruscottiService;
- import java.math.BigDecimal;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.commons.lang.StringUtils;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- public class CruscottiServiceImpl implements CruscottiService {
- private HibernateTemplate hibernateTemplate;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.hibernateTemplate = new HibernateTemplate(sessionFactory);
- }
- //Cruscotto 1
- public List<Object[]> findScadenzarioByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- List<Object[]> listaCal = null;
- Query q = null;
- try {
- String codReg = null;
- if(cr==null || StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String hql = getHqlQueryScadenzario(codReg, cr, false, firstR, offset);
- q = session.createQuery(hql);
- if(firstR!=null && offset!=null)
- q.setFirstResult(firstR).setMaxResults(offset);
- listaCal = q.list();
- }
- catch (Exception ecc) {
- ecc.printStackTrace();
- }
- finally {
- session.close();
- }
- return listaCal;
- }
- public int findScadenzarioByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- Object countValue = null;
- try {
- String codReg = null;
- if(StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String hql = getHqlQueryScadenzario(codReg, cr, true, null, null);
- Query query = session.createQuery(hql);
- countValue = query.uniqueResult();
- } catch(Exception e) {
- throw e;
- } finally {
- session.close();
- }
- return ((Long)countValue).intValue();
- }
- private String getHqlQueryScadenzario(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
- String hql = "select ";
- if(bCount)
- hql += " count (ab.idAreaBalneazione) ";
- else {
- hql += " ab.idAreaBalneazione, ab.regioni.nome, ab.province.nome, ab.comuni.nome, ab.nome, to_char(c.data, 'DD-MM-YYYY') ";
- if(firstR!=null && offset!=null) //solo campi visibili per excel
- hql += ", c.data, ab.puntiPrelievo.codice ";
- }
- hql += " from CalendarioAnalisi c ,AreeBalneazione ab where c.idAreaBalneazione = ab.idAreaBalneazione and ab.tipoAreaBalenazione.id != 3 and c.flagInserito = 0 ";
- if(codReg != null && !codReg.equals("")) {
- if (!codReg.equals("099"))
- hql += " and ab.regioni.codice = '"+codReg+"'";
- }
- if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
- hql += " and upper(ab.regioni.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
- if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
- hql += " and ab.province.codice='" + cr.getProvincia() + "'";
- if (cr.getComune() != null && !cr.getComune().equals(""))
- hql += " and ab.comuni.codice='" + cr.getComune() + "'";
- }
- hql += (bCount)? "" : " order by c.data, ab.idAreaBalneazione asc";
- return hql;
- }
- //Cruscotto 2
- public List<Object[]> findAnalisiARByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- List<Object[]> result = null;
- Query q = null;
- try {
- String codReg = null;
- if(cr==null || StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String hql = getHqlQueryAnalisiAR(codReg, cr, false, firstR, offset);
- q = session.createQuery(hql);
- if(firstR!=null && offset!=null)
- q.setFirstResult(firstR).setMaxResults(offset);
- result = (List<Object[]>)q.list();
- } catch(Exception e) {
- throw e;
- } finally {
- session.close();
- }
- return result;
- }
- public int findAnalisiARByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- Object countValue = null;
- try {
- String codReg = null;
- if(StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String hql = getHqlQueryAnalisiAR(codReg, cr, true, null, null);
- Query query = session.createQuery(hql);
- countValue = query.uniqueResult();
- } catch(Exception e) {
- throw e;
- } finally {
- session.close();
- }
- return ((Long)countValue).intValue();
- }
- private String getHqlQueryAnalisiAR(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
- String hql = "select ";
- if(bCount)
- hql += " count (ab.idAreaBalneazione) ";
- else {
- hql += " ab.idAreaBalneazione, ab.regioni.nome, ab.province.nome, ab.comuni.nome, ab.nome, " +
- " to_char(ca.data, 'DD-MM-YYYY'), to_char(ap.data,'DD-MM-YYYY'), nvl(ap.note, mr.descrizione) ";
- if(firstR!=null && offset!=null) //solo campi visibili per excel
- hql += ", ta.shortName, ab.puntiPrelievo.codice ";
- }
- hql += " from AnalisiPtPrelievo ap, AreeBalneazione ab, TipoAnalisi ta, CalendarioAnalisi ca, MotivoRitardo mr " +
- " where ap.puntiPrelievo.codice = ab.puntiPrelievo.codice and " +
- " ap.tipoAnalisi.id = ta.id and " +
- " ap.calendario.id = ca.id and " +
- " ap.motivoRitardo.id = mr.id and " +
- " ta.shortName IN ('A','R') ";
- if(codReg != null && !codReg.equals("")) {
- if (!codReg.equals("099"))
- hql += " and ab.regioni.codice = '"+codReg+"'";
- }
- if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
- hql += " and upper(ab.regioni.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
- if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
- hql += " and ab.province.codice='" + cr.getProvincia() + "'";
- if (cr.getComune() != null && !cr.getComune().equals(""))
- hql += " and ab.comuni.codice='" + cr.getComune() + "'";
- }
- hql += (bCount)? "" : " order by ab.idAreaBalneazione asc, to_char(ca.data, 'DD-MM-YYYY') asc, to_char(ap.data,'DD-MM-YYYY') asc";
- return hql;
- }
- //Cruscotto 3
- public List<Object[]> findAnalisiFuoriNormaByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- List<Object[]> result = null;
- Query q = null;
- try {
- String codReg = null;
- if(cr==null || StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String sql = getSqlQueryAnalisiFuoriNorma(codReg, cr, false, firstR, offset);
- q = session.createSQLQuery(sql);
- if(firstR!=null && offset!=null)
- q.setFirstResult(firstR).setMaxResults(offset);
- result = (List<Object[]>)q.list();
- } catch(Exception e) {
- throw e;
- } finally {
- session.close();
- }
- return result;
- }
- public int findAnalisiFuoriNormaByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- Object countValue = null;
- try {
- String codReg = null;
- if(StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String sql = getSqlQueryAnalisiFuoriNorma(codReg, cr, true, null, null);
- Query query = session.createSQLQuery(sql);
- countValue = query.uniqueResult();
- } catch(Exception e) {
- throw e;
- } finally {
- session.close();
- }
- return ((BigDecimal)countValue).intValue();
- }
- private String getSqlQueryAnalisiFuoriNorma(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
- String sql = "select ";
- if(bCount)
- sql += " count (ab.id_area_balneazione) ";
- else {
- sql += " ab.id_area_balneazione as idArea, r.nome regione, p.nome provincia, c.nome comune, ab.nome nome_area_balneazione, to_char(ap.data,'DD-MM-YYYY') as dataAnalisi ";
- if(firstR!=null && offset!=null)
- sql += ", ap.cod_prelievo, p.codice as codProv, c.codice as codCom ";
- }
- sql += " from analisi_pp ap "+
- " inner join aree_balneazione ab on ap.cod_prelievo = ab.cod_prelievo "+
- " inner join regioni r on ab.cod_reg = r.codice "+
- " inner join province p on ab.cod_pro = p.codice "+
- " inner join comuni c on ab.cod_com = c.codice "+
- " inner join tipo_analisi ta on ap.tipo_analisi = ta.id "+
- " where ap.flag_oltre_limiti = 1 and ta.short_name = 'R' and "+
- " ap.id not in(select id_analisi_pp from AREE_INTERD_ANALISI_PP_ASS) ";
- if(codReg != null && !codReg.equals("")) {
- if (!codReg.equals("099"))
- sql += " and r.codice = '"+codReg+"'";
- }
- if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
- sql += " and upper(r.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
- if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
- sql += " and p.codice='" + cr.getProvincia() + "'";
- if (cr.getComune() != null && !cr.getComune().equals(""))
- sql += " and c.codice='" + cr.getComune() + "'";
- }
- sql += (bCount)? "" : " order by idArea asc, dataAnalisi asc ";
- return sql;
- }
- //Cruscotto 4
- public List<Object[]> findOrdinanzeByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- List<Object[]> result = null;
- Query q = null;
- try {
- String codReg = null;
- if(cr==null || StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String sql = getSqlQueryOrdinanze(codReg, cr, false, firstR, offset);
- q = session.createSQLQuery(sql);
- if(firstR!=null && offset!=null)
- q.setFirstResult(firstR).setMaxResults(offset);
- result = (List<Object[]>)q.list();
- } catch(Exception e) {
- throw e;
- } finally {
- session.close();
- }
- return result;
- }
- public int findOrdinanzeByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
- Session session = hibernateTemplate.getSessionFactory().openSession();
- Object countValue = null;
- try {
- String codReg = null;
- if(StringUtils.isEmpty(cr.getRegione()))
- for (UtenteGeografia ug : utente.getGeografia())
- codReg = ug.getCodReg();
- else
- codReg = getCodReg(cr.getRegione());
- String sql = getSqlQueryOrdinanze(codReg, cr, true, null, null);
- Query query = session.createSQLQuery(sql);
- countValue = query.uniqueResult();
- } catch(Exception e) {
- throw e;
- } finally {
- session.close();
- }
- return ((BigDecimal)countValue).intValue();
- }
- private String getSqlQueryOrdinanze(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
- String sql = "SELECT" +
- (bCount ? " COUNT(*) " : " * ") + "FROM (" +
- "SELECT res.id_area_balneazione, r.nome regione, p.nome provincia, c.nome comune, res.nome nome_area_balneazione, res.note ordinanza, " +
- "TO_CHAR(res.datainterdizione, 'yyyy-mm-dd') data_inizio_interdizione, " +
- "TO_CHAR(res.datariapertura, 'yyyy-mm-dd') data_fine_interdizione, res.analisi analisi_associate" +
- (firstR != null && offset != null ? ", res.codice " : " ") +
- "FROM (" +
- "SELECT DISTINCT ab.cod_reg, ab.cod_pro, ab.cod_com, ab.id_area_balneazione, ab.nome, ai.codice, ai.note, ai.datainterdizione, ai.datariapertura, " +
- "cast(REPLACE(listagg(ab.id_area_balneazione || ' - ' || TO_CHAR(ap.data, 'yyyy-mm-dd hh24:mi')) WITHIN GROUP (ORDER BY ai.codice, ab.id_area_balneazione) over (partition BY ai.codice, ab.id_area_balneazione), ',', chr(10)) as varchar2(4000)) analisi " +
- "FROM aree_balneazione ab " +
- "INNER JOIN aree_interd_pt_prelievo_ass aippa ON ab.cod_prelievo = aippa.id_pt_prelievo " +
- "INNER JOIN aree_interdette ai ON aippa.id_aree_interdette = ai.codice AND ai.tipo = 'O' " +
- "INNER JOIN aree_interd_analisi_pp_ass aiapa ON ai.codice = aiapa.id_aree_interdette " +
- "INNER JOIN analisi_pp ap ON aiapa.id_analisi_pp = ap.id AND ab.cod_prelievo = ap.cod_prelievo " +
- "UNION ALL " +
- "SELECT ab.cod_reg, ab.cod_pro, ab.cod_com, ab.id_area_balneazione, ab.nome, ai.codice, ai.note, ai.datainterdizione, ai.datariapertura, 'Nessuna' analisi " +
- "FROM aree_balneazione ab " +
- "INNER JOIN aree_interd_pt_prelievo_ass aippa ON ab.cod_prelievo = aippa.id_pt_prelievo " +
- "INNER JOIN aree_interdette ai ON aippa.id_aree_interdette = ai.codice AND ai.tipo = 'O' " +
- "LEFT JOIN aree_interd_analisi_pp_ass aiapa ON ai.codice = aiapa.id_aree_interdette " +
- "LEFT JOIN analisi_pp ap ON aiapa.id_analisi_pp = ap.id AND ab.cod_prelievo = ap.cod_prelievo " +
- "GROUP BY ab.cod_reg, ab.cod_pro, ab.cod_com, ab.id_area_balneazione, ab.nome, ai.codice, ai.note, ai.datainterdizione, ai.datariapertura " +
- "HAVING COUNT(ap.id) = 0 " +
- ") res " +
- "INNER JOIN regioni r ON res.cod_reg = r.codice " +
- "INNER JOIN province p ON res.cod_pro = p.codice " +
- "INNER JOIN comuni c ON res.cod_com = c.codice";
- if(codReg != null && !codReg.equals("")) {
- if (!codReg.equals("099"))
- sql += " where r.codice = '"+codReg+"'";
- }
- if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
- if (codReg!=null && codReg.equals("099"))
- sql += " where ";
- else
- sql += " and ";
- sql += " upper(r.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
- if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
- sql += " and p.codice='" + cr.getProvincia() + "'";
- if (cr.getComune() != null && !cr.getComune().equals(""))
- sql += " and c.codice='" + cr.getComune() + "'";
- }
- sql += ")" + (bCount ? "" : " ORDER BY data_inizio_interdizione asc, data_fine_interdizione asc, id_area_balneazione asc");
- return sql;
- }
- public String getCodReg(String Reg) throws Exception{
- try {
- String query = "select codice FROM balneazione.regioni where nome ='"+Reg+"'";
- Session sess = hibernateTemplate.getSessionFactory().openSession();
- Query q = sess.createSQLQuery(query);
- Iterator listaTmp = q.list().iterator();
- sess.close();
- String cod ="";
- while(listaTmp.hasNext())
- cod = (String)listaTmp.next();
- return cod;
- }
- catch(Exception exc){
- return "099";}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement