Advertisement
Guest User

controller service impl

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.58 KB | None | 0 0
  1. package it.ipzs.acque.service.balneazione.impl;
  2.  
  3. import it.ipzs.acque.controller.utility.CriteriRicercaScadenzario;
  4. import it.ipzs.acque.domain.menu.Utente;
  5. import it.ipzs.acque.domain.menu.UtenteGeografia;
  6. import it.ipzs.acque.service.balneazione.CruscottiService;
  7.  
  8. import java.math.BigDecimal;
  9. import java.util.Iterator;
  10. import java.util.List;
  11.  
  12. import org.apache.commons.lang.StringUtils;
  13. import org.hibernate.Query;
  14. import org.hibernate.Session;
  15. import org.hibernate.SessionFactory;
  16. import org.springframework.orm.hibernate3.HibernateTemplate;
  17.  
  18. public class CruscottiServiceImpl implements CruscottiService {
  19. private HibernateTemplate hibernateTemplate;
  20.  
  21. public void setSessionFactory(SessionFactory sessionFactory) {
  22. this.hibernateTemplate = new HibernateTemplate(sessionFactory);
  23. }
  24.  
  25. //Cruscotto 1
  26. public List<Object[]> findScadenzarioByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
  27. Session session = hibernateTemplate.getSessionFactory().openSession();
  28. List<Object[]> listaCal = null;
  29. Query q = null;
  30. try {
  31. String codReg = null;
  32.  
  33. if(cr==null || StringUtils.isEmpty(cr.getRegione()))
  34. for (UtenteGeografia ug : utente.getGeografia())
  35. codReg = ug.getCodReg();
  36. else
  37. codReg = getCodReg(cr.getRegione());
  38.  
  39. String hql = getHqlQueryScadenzario(codReg, cr, false, firstR, offset);
  40. q = session.createQuery(hql);
  41. if(firstR!=null && offset!=null)
  42. q.setFirstResult(firstR).setMaxResults(offset);
  43. listaCal = q.list();
  44. }
  45. catch (Exception ecc) {
  46. ecc.printStackTrace();
  47. }
  48. finally {
  49. session.close();
  50. }
  51. return listaCal;
  52. }
  53.  
  54. public int findScadenzarioByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
  55. Session session = hibernateTemplate.getSessionFactory().openSession();
  56. Object countValue = null;
  57. try {
  58. String codReg = null;
  59.  
  60. if(StringUtils.isEmpty(cr.getRegione()))
  61. for (UtenteGeografia ug : utente.getGeografia())
  62. codReg = ug.getCodReg();
  63. else
  64. codReg = getCodReg(cr.getRegione());
  65.  
  66. String hql = getHqlQueryScadenzario(codReg, cr, true, null, null);
  67. Query query = session.createQuery(hql);
  68. countValue = query.uniqueResult();
  69.  
  70. } catch(Exception e) {
  71. throw e;
  72. } finally {
  73. session.close();
  74. }
  75.  
  76. return ((Long)countValue).intValue();
  77. }
  78.  
  79. private String getHqlQueryScadenzario(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
  80.  
  81. String hql = "select ";
  82. if(bCount)
  83. hql += " count (ab.idAreaBalneazione) ";
  84. else {
  85. hql += " ab.idAreaBalneazione, ab.regioni.nome, ab.province.nome, ab.comuni.nome, ab.nome, to_char(c.data, 'DD-MM-YYYY') ";
  86. if(firstR!=null && offset!=null) //solo campi visibili per excel
  87. hql += ", c.data, ab.puntiPrelievo.codice ";
  88. }
  89. hql += " from CalendarioAnalisi c ,AreeBalneazione ab where c.idAreaBalneazione = ab.idAreaBalneazione and ab.tipoAreaBalenazione.id != 3 and c.flagInserito = 0 ";
  90.  
  91. if(codReg != null && !codReg.equals("")) {
  92. if (!codReg.equals("099"))
  93. hql += " and ab.regioni.codice = '"+codReg+"'";
  94. }
  95. if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
  96. hql += " and upper(ab.regioni.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
  97. if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
  98. hql += " and ab.province.codice='" + cr.getProvincia() + "'";
  99. if (cr.getComune() != null && !cr.getComune().equals(""))
  100. hql += " and ab.comuni.codice='" + cr.getComune() + "'";
  101. }
  102. hql += (bCount)? "" : " order by c.data, ab.idAreaBalneazione asc";
  103.  
  104. return hql;
  105. }
  106.  
  107. //Cruscotto 2
  108. public List<Object[]> findAnalisiARByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
  109. Session session = hibernateTemplate.getSessionFactory().openSession();
  110. List<Object[]> result = null;
  111. Query q = null;
  112. try {
  113. String codReg = null;
  114.  
  115. if(cr==null || StringUtils.isEmpty(cr.getRegione()))
  116. for (UtenteGeografia ug : utente.getGeografia())
  117. codReg = ug.getCodReg();
  118. else
  119. codReg = getCodReg(cr.getRegione());
  120.  
  121. String hql = getHqlQueryAnalisiAR(codReg, cr, false, firstR, offset);
  122. q = session.createQuery(hql);
  123. if(firstR!=null && offset!=null)
  124. q.setFirstResult(firstR).setMaxResults(offset);
  125.  
  126. result = (List<Object[]>)q.list();
  127. } catch(Exception e) {
  128. throw e;
  129. } finally {
  130. session.close();
  131. }
  132. return result;
  133. }
  134.  
  135. public int findAnalisiARByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
  136. Session session = hibernateTemplate.getSessionFactory().openSession();
  137. Object countValue = null;
  138. try {
  139. String codReg = null;
  140.  
  141. if(StringUtils.isEmpty(cr.getRegione()))
  142. for (UtenteGeografia ug : utente.getGeografia())
  143. codReg = ug.getCodReg();
  144. else
  145. codReg = getCodReg(cr.getRegione());
  146.  
  147. String hql = getHqlQueryAnalisiAR(codReg, cr, true, null, null);
  148. Query query = session.createQuery(hql);
  149. countValue = query.uniqueResult();
  150.  
  151. } catch(Exception e) {
  152. throw e;
  153. } finally {
  154. session.close();
  155. }
  156.  
  157. return ((Long)countValue).intValue();
  158. }
  159.  
  160. private String getHqlQueryAnalisiAR(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
  161.  
  162. String hql = "select ";
  163. if(bCount)
  164. hql += " count (ab.idAreaBalneazione) ";
  165. else {
  166. hql += " ab.idAreaBalneazione, ab.regioni.nome, ab.province.nome, ab.comuni.nome, ab.nome, " +
  167. " to_char(ca.data, 'DD-MM-YYYY'), to_char(ap.data,'DD-MM-YYYY'), nvl(ap.note, mr.descrizione) ";
  168. if(firstR!=null && offset!=null) //solo campi visibili per excel
  169. hql += ", ta.shortName, ab.puntiPrelievo.codice ";
  170. }
  171. hql += " from AnalisiPtPrelievo ap, AreeBalneazione ab, TipoAnalisi ta, CalendarioAnalisi ca, MotivoRitardo mr " +
  172. " where ap.puntiPrelievo.codice = ab.puntiPrelievo.codice and " +
  173. " ap.tipoAnalisi.id = ta.id and " +
  174. " ap.calendario.id = ca.id and " +
  175. " ap.motivoRitardo.id = mr.id and " +
  176. " ta.shortName IN ('A','R') ";
  177.  
  178. if(codReg != null && !codReg.equals("")) {
  179. if (!codReg.equals("099"))
  180. hql += " and ab.regioni.codice = '"+codReg+"'";
  181. }
  182. if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
  183. hql += " and upper(ab.regioni.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
  184. if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
  185. hql += " and ab.province.codice='" + cr.getProvincia() + "'";
  186. if (cr.getComune() != null && !cr.getComune().equals(""))
  187. hql += " and ab.comuni.codice='" + cr.getComune() + "'";
  188. }
  189. hql += (bCount)? "" : " order by ab.idAreaBalneazione asc, to_char(ca.data, 'DD-MM-YYYY') asc, to_char(ap.data,'DD-MM-YYYY') asc";
  190.  
  191. return hql;
  192. }
  193.  
  194. //Cruscotto 3
  195. public List<Object[]> findAnalisiFuoriNormaByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
  196. Session session = hibernateTemplate.getSessionFactory().openSession();
  197. List<Object[]> result = null;
  198. Query q = null;
  199. try {
  200. String codReg = null;
  201.  
  202. if(cr==null || StringUtils.isEmpty(cr.getRegione()))
  203. for (UtenteGeografia ug : utente.getGeografia())
  204. codReg = ug.getCodReg();
  205. else
  206. codReg = getCodReg(cr.getRegione());
  207.  
  208. String sql = getSqlQueryAnalisiFuoriNorma(codReg, cr, false, firstR, offset);
  209. q = session.createSQLQuery(sql);
  210. if(firstR!=null && offset!=null)
  211. q.setFirstResult(firstR).setMaxResults(offset);
  212.  
  213. result = (List<Object[]>)q.list();
  214. } catch(Exception e) {
  215. throw e;
  216. } finally {
  217. session.close();
  218. }
  219. return result;
  220. }
  221.  
  222. public int findAnalisiFuoriNormaByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
  223. Session session = hibernateTemplate.getSessionFactory().openSession();
  224. Object countValue = null;
  225. try {
  226. String codReg = null;
  227.  
  228. if(StringUtils.isEmpty(cr.getRegione()))
  229. for (UtenteGeografia ug : utente.getGeografia())
  230. codReg = ug.getCodReg();
  231. else
  232. codReg = getCodReg(cr.getRegione());
  233.  
  234. String sql = getSqlQueryAnalisiFuoriNorma(codReg, cr, true, null, null);
  235. Query query = session.createSQLQuery(sql);
  236. countValue = query.uniqueResult();
  237.  
  238. } catch(Exception e) {
  239. throw e;
  240. } finally {
  241. session.close();
  242. }
  243.  
  244. return ((BigDecimal)countValue).intValue();
  245. }
  246.  
  247. private String getSqlQueryAnalisiFuoriNorma(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
  248.  
  249. String sql = "select ";
  250. if(bCount)
  251. sql += " count (ab.id_area_balneazione) ";
  252. else {
  253. 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 ";
  254. if(firstR!=null && offset!=null)
  255. sql += ", ap.cod_prelievo, p.codice as codProv, c.codice as codCom ";
  256. }
  257. sql += " from analisi_pp ap "+
  258. " inner join aree_balneazione ab on ap.cod_prelievo = ab.cod_prelievo "+
  259. " inner join regioni r on ab.cod_reg = r.codice "+
  260. " inner join province p on ab.cod_pro = p.codice "+
  261. " inner join comuni c on ab.cod_com = c.codice "+
  262. " inner join tipo_analisi ta on ap.tipo_analisi = ta.id "+
  263. " where ap.flag_oltre_limiti = 1 and ta.short_name = 'R' and "+
  264. " ap.id not in(select id_analisi_pp from AREE_INTERD_ANALISI_PP_ASS) ";
  265.  
  266. if(codReg != null && !codReg.equals("")) {
  267. if (!codReg.equals("099"))
  268. sql += " and r.codice = '"+codReg+"'";
  269. }
  270. if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
  271. sql += " and upper(r.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
  272. if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
  273. sql += " and p.codice='" + cr.getProvincia() + "'";
  274. if (cr.getComune() != null && !cr.getComune().equals(""))
  275. sql += " and c.codice='" + cr.getComune() + "'";
  276. }
  277. sql += (bCount)? "" : " order by idArea asc, dataAnalisi asc ";
  278.  
  279. return sql;
  280. }
  281.  
  282. //Cruscotto 4
  283. public List<Object[]> findOrdinanzeByUserAndPagination(Utente utente, Integer firstR, Integer offset, CriteriRicercaScadenzario cr) throws Exception {
  284. Session session = hibernateTemplate.getSessionFactory().openSession();
  285. List<Object[]> result = null;
  286. Query q = null;
  287. try {
  288. String codReg = null;
  289.  
  290. if(cr==null || StringUtils.isEmpty(cr.getRegione()))
  291. for (UtenteGeografia ug : utente.getGeografia())
  292. codReg = ug.getCodReg();
  293. else
  294. codReg = getCodReg(cr.getRegione());
  295.  
  296. String sql = getSqlQueryOrdinanze(codReg, cr, false, firstR, offset);
  297. q = session.createSQLQuery(sql);
  298. if(firstR!=null && offset!=null)
  299. q.setFirstResult(firstR).setMaxResults(offset);
  300.  
  301. result = (List<Object[]>)q.list();
  302. } catch(Exception e) {
  303. throw e;
  304. } finally {
  305. session.close();
  306. }
  307. return result;
  308. }
  309.  
  310. public int findOrdinanzeByUserAndCriteriaCount(CriteriRicercaScadenzario cr, Utente utente) throws Exception {
  311. Session session = hibernateTemplate.getSessionFactory().openSession();
  312. Object countValue = null;
  313. try {
  314. String codReg = null;
  315.  
  316. if(StringUtils.isEmpty(cr.getRegione()))
  317. for (UtenteGeografia ug : utente.getGeografia())
  318. codReg = ug.getCodReg();
  319. else
  320. codReg = getCodReg(cr.getRegione());
  321.  
  322. String sql = getSqlQueryOrdinanze(codReg, cr, true, null, null);
  323. Query query = session.createSQLQuery(sql);
  324. countValue = query.uniqueResult();
  325.  
  326. } catch(Exception e) {
  327. throw e;
  328. } finally {
  329. session.close();
  330. }
  331.  
  332. return ((BigDecimal)countValue).intValue();
  333. }
  334.  
  335. private String getSqlQueryOrdinanze(String codReg, CriteriRicercaScadenzario cr, boolean bCount, Integer firstR, Integer offset) throws Exception {
  336.  
  337. String sql = "SELECT" +
  338. (bCount ? " COUNT(*) " : " * ") + "FROM (" +
  339. "SELECT res.id_area_balneazione, r.nome regione, p.nome provincia, c.nome comune, res.nome nome_area_balneazione, res.note ordinanza, " +
  340. "TO_CHAR(res.datainterdizione, 'yyyy-mm-dd') data_inizio_interdizione, " +
  341. "TO_CHAR(res.datariapertura, 'yyyy-mm-dd') data_fine_interdizione, res.analisi analisi_associate" +
  342. (firstR != null && offset != null ? ", res.codice " : " ") +
  343. "FROM (" +
  344. "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, " +
  345. "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 " +
  346. "FROM aree_balneazione ab " +
  347. "INNER JOIN aree_interd_pt_prelievo_ass aippa ON ab.cod_prelievo = aippa.id_pt_prelievo " +
  348. "INNER JOIN aree_interdette ai ON aippa.id_aree_interdette = ai.codice AND ai.tipo = 'O' " +
  349. "INNER JOIN aree_interd_analisi_pp_ass aiapa ON ai.codice = aiapa.id_aree_interdette " +
  350. "INNER JOIN analisi_pp ap ON aiapa.id_analisi_pp = ap.id AND ab.cod_prelievo = ap.cod_prelievo " +
  351. "UNION ALL " +
  352. "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 " +
  353. "FROM aree_balneazione ab " +
  354. "INNER JOIN aree_interd_pt_prelievo_ass aippa ON ab.cod_prelievo = aippa.id_pt_prelievo " +
  355. "INNER JOIN aree_interdette ai ON aippa.id_aree_interdette = ai.codice AND ai.tipo = 'O' " +
  356. "LEFT JOIN aree_interd_analisi_pp_ass aiapa ON ai.codice = aiapa.id_aree_interdette " +
  357. "LEFT JOIN analisi_pp ap ON aiapa.id_analisi_pp = ap.id AND ab.cod_prelievo = ap.cod_prelievo " +
  358. "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 " +
  359. "HAVING COUNT(ap.id) = 0 " +
  360. ") res " +
  361. "INNER JOIN regioni r ON res.cod_reg = r.codice " +
  362. "INNER JOIN province p ON res.cod_pro = p.codice " +
  363. "INNER JOIN comuni c ON res.cod_com = c.codice";
  364.  
  365. if(codReg != null && !codReg.equals("")) {
  366. if (!codReg.equals("099"))
  367. sql += " where r.codice = '"+codReg+"'";
  368. }
  369. if (cr!=null && cr.getRegione() != null && !cr.getRegione().equals("") && !cr.getRegione().equals("-") && !cr.getRegione().equals("Tutte") ) {
  370. if (codReg!=null && codReg.equals("099"))
  371. sql += " where ";
  372. else
  373. sql += " and ";
  374. sql += " upper(r.nome)='" + cr.getRegione().toUpperCase().replace("'", "''") + "'";
  375. if (cr.getProvincia() != null && !cr.getProvincia().equals(""))
  376. sql += " and p.codice='" + cr.getProvincia() + "'";
  377. if (cr.getComune() != null && !cr.getComune().equals(""))
  378. sql += " and c.codice='" + cr.getComune() + "'";
  379. }
  380.  
  381. sql += ")" + (bCount ? "" : " ORDER BY data_inizio_interdizione asc, data_fine_interdizione asc, id_area_balneazione asc");
  382.  
  383. return sql;
  384. }
  385.  
  386. public String getCodReg(String Reg) throws Exception{
  387. try {
  388. String query = "select codice FROM balneazione.regioni where nome ='"+Reg+"'";
  389. Session sess = hibernateTemplate.getSessionFactory().openSession();
  390. Query q = sess.createSQLQuery(query);
  391. Iterator listaTmp = q.list().iterator();
  392. sess.close();
  393. String cod ="";
  394. while(listaTmp.hasNext())
  395. cod = (String)listaTmp.next();
  396. return cod;
  397. }
  398. catch(Exception exc){
  399. return "099";}
  400. }
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement