AllarmiDriverUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.core.allarmi.utils;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.ArrayList;
  25. import java.util.List;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.openspcoop2.core.allarmi.Allarme;
  28. import org.openspcoop2.core.allarmi.AllarmeHistory;
  29. import org.openspcoop2.core.allarmi.IdAllarme;
  30. import org.openspcoop2.core.allarmi.constants.RuoloPorta;
  31. import org.openspcoop2.core.allarmi.constants.StatoAllarme;
  32. import org.openspcoop2.core.allarmi.constants.TipoAllarme;
  33. import org.openspcoop2.core.allarmi.dao.IAllarmeHistoryService;
  34. import org.openspcoop2.core.allarmi.dao.IAllarmeHistoryServiceSearch;
  35. import org.openspcoop2.core.allarmi.dao.IAllarmeService;
  36. import org.openspcoop2.core.allarmi.dao.IAllarmeServiceSearch;
  37. import org.openspcoop2.core.allarmi.dao.jdbc.JDBCServiceManager;
  38. import org.openspcoop2.core.allarmi.dao.jdbc.converter.AllarmeFieldConverter;
  39. import org.openspcoop2.core.allarmi.dao.jdbc.converter.AllarmeHistoryFieldConverter;
  40. import org.openspcoop2.core.commons.Filtri;
  41. import org.openspcoop2.core.commons.ISearch;
  42. import org.openspcoop2.core.commons.Liste;
  43. import org.openspcoop2.core.commons.SearchUtils;
  44. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  45. import org.openspcoop2.core.constants.CostantiDB;
  46. import org.openspcoop2.core.id.IDServizio;
  47. import org.openspcoop2.core.id.IDServizioApplicativo;
  48. import org.openspcoop2.core.id.IDSoggetto;
  49. import org.openspcoop2.generic_project.beans.CustomField;
  50. import org.openspcoop2.generic_project.beans.Function;
  51. import org.openspcoop2.generic_project.beans.FunctionField;
  52. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  53. import org.openspcoop2.generic_project.exception.NotFoundException;
  54. import org.openspcoop2.generic_project.exception.ServiceException;
  55. import org.openspcoop2.generic_project.expression.IExpression;
  56. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  57. import org.openspcoop2.generic_project.expression.LikeMode;
  58. import org.openspcoop2.generic_project.expression.SortOrder;
  59. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  60. import org.openspcoop2.utils.sql.ISQLQueryObject;
  61. import org.openspcoop2.utils.sql.LikeConfig;
  62. import org.openspcoop2.utils.sql.SQLObjectFactory;
  63. import org.slf4j.Logger;

  64. /**
  65.  * AllarmiDriverUtils
  66.  *
  67.  * @author Poli Andrea (apoli@link.it)
  68.  * @author $Author$
  69.  * @version $Rev$, $Date$
  70.  */
  71. public class AllarmiDriverUtils {

  72.     public static List<Allarme> allarmiList(ISearch ricerca, RuoloPorta ruoloPorta, String nomePorta, Connection con, Logger log, String tipoDB) throws ServiceException {
  73.         String nomeMetodo = "allarmiList";
  74.         int idLista = Liste.CONFIGURAZIONE_ALLARMI;
  75.         int offset;
  76.         int limit;
  77.         String search;

  78.         limit = ricerca.getPageSize(idLista);
  79.         offset = ricerca.getIndexIniziale(idLista);
  80.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));

  81.         String filterStato = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_STATO);
  82.        
  83.         log.debug("search : " + search);
  84.         log.debug("filterStato : " + filterStato);
  85.                
  86.         try {
  87.             ServiceManagerProperties properties = new ServiceManagerProperties();
  88.             properties.setDatabaseType(tipoDB);
  89.             properties.setShowSql(true);
  90.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  91.            
  92.             IAllarmeServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeServiceSearch();
  93.            
  94.             IExpression expr = allarmiServiceSearch.newExpression();
  95.            
  96.             boolean addAnd = false;
  97.            
  98.             if(ruoloPorta!=null && nomePorta!=null) {
  99.                 expr.equals(Allarme.model().FILTRO.RUOLO_PORTA, ruoloPorta.getValue()).and().equals(Allarme.model().FILTRO.NOME_PORTA, nomePorta);
  100.                 addAnd = true;
  101.             }
  102.             else {
  103.                 expr.isNull(Allarme.model().FILTRO.NOME_PORTA);
  104.                 addAnd = true;
  105.             }
  106.            
  107.             if (!search.equals("")) {
  108.                 if(addAnd)
  109.                     expr.and();
  110.                
  111.                 expr.ilike(Allarme.model().ALIAS, search, LikeMode.ANYWHERE);
  112.                
  113.                 addAnd = true;
  114.             }
  115.            
  116.             if (filterStato!=null && !filterStato.equals("")) {
  117.                 if(addAnd)
  118.                     expr.and();
  119.                
  120.                 if(Filtri.FILTRO_STATO_VALORE_ABILITATO.equals(filterStato)) {
  121.                     expr.equals(Allarme.model().ENABLED, 1);
  122.                     addAnd = true;
  123.                 }
  124.                 else if(Filtri.FILTRO_STATO_VALORE_DISABILITATO.equals(filterStato)) {
  125.                     expr.equals(Allarme.model().ENABLED, 0);
  126.                     addAnd = true;
  127.                 }
  128.                 else if(Filtri.FILTRO_STATO_VALORE_OK.equals(filterStato)) {
  129.                     expr.equals(Allarme.model().ENABLED, 1);
  130.                     expr.and();
  131.                     expr.equals(Allarme.model().STATO, AllarmiConverterUtils.toIntegerValue(StatoAllarme.OK));
  132.                     addAnd = true;
  133.                 }
  134.                 else if(Filtri.FILTRO_STATO_VALORE_WARNING.equals(filterStato)) {
  135.                     expr.equals(Allarme.model().ENABLED, 1);
  136.                     expr.and();
  137.                     expr.equals(Allarme.model().STATO, AllarmiConverterUtils.toIntegerValue(StatoAllarme.WARNING));
  138.                     addAnd = true;
  139.                 }
  140.                 else if(Filtri.FILTRO_STATO_VALORE_ERROR.equals(filterStato)) {
  141.                     expr.equals(Allarme.model().ENABLED, 1);
  142.                     expr.and();
  143.                     expr.equals(Allarme.model().STATO, AllarmiConverterUtils.toIntegerValue(StatoAllarme.ERROR));
  144.                     addAnd = true;
  145.                 }
  146.                
  147.                 addAnd = true;
  148.             }
  149.            
  150.             NonNegativeNumber count = allarmiServiceSearch.count(expr);
  151.             ricerca.setNumEntries(idLista,(int) count.longValue());
  152.            
  153.             // ricavo le entries
  154.             if (limit == 0) // con limit
  155.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  156.            
  157.             IPaginatedExpression pagExpr = allarmiServiceSearch.toPaginatedExpression(expr);
  158.            
  159.             pagExpr.limit(limit).offset(offset);
  160.             pagExpr.addOrder(Allarme.model().ALIAS, SortOrder.ASC);

  161.             return allarmiServiceSearch.findAll(pagExpr);
  162.            
  163.         } catch (Exception qe) {
  164.             throw new ServiceException("[" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  165.         }
  166.     }
  167.    
  168.    
  169.     public static void createAllarme(Allarme allarme, Connection con, Logger log, String tipoDB) throws ServiceException {
  170.         String nomeMetodo = "createAllarme";

  171.         try {
  172.             ServiceManagerProperties properties = new ServiceManagerProperties();
  173.             properties.setDatabaseType(tipoDB);
  174.             properties.setShowSql(true);
  175.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  176.            
  177.             IAllarmeService allarmiService = jdbcServiceManager.getAllarmeService();
  178.            
  179.             allarmiService.create(allarme);
  180.            
  181.         } catch (Exception qe) {
  182.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  183.         }
  184.     }
  185.    
  186.     public static void updateAllarme(Allarme allarme, Connection con, Logger log, String tipoDB) throws ServiceException {
  187.         String nomeMetodo = "updateAllarme";

  188.         try {
  189.             ServiceManagerProperties properties = new ServiceManagerProperties();
  190.             properties.setDatabaseType(tipoDB);
  191.             properties.setShowSql(true);
  192.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  193.            
  194.             IAllarmeService allarmiService = jdbcServiceManager.getAllarmeService();
  195.            
  196.             IdAllarme idAll = new IdAllarme();
  197.             idAll.setNome(allarme.getNome());
  198.             allarmiService.update(idAll,allarme);
  199.            
  200.         } catch (Exception qe) {
  201.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  202.         }
  203.     }
  204.    
  205.     public static void deleteAllarme(Allarme allarme, Connection con, Logger log, String tipoDB) throws ServiceException {
  206.         String nomeMetodo = "deleteAllarme";

  207.         try {
  208.             ServiceManagerProperties properties = new ServiceManagerProperties();
  209.             properties.setDatabaseType(tipoDB);
  210.             properties.setShowSql(true);
  211.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  212.            
  213.             IAllarmeService allarmiService = jdbcServiceManager.getAllarmeService();
  214.            
  215.             allarmiService.delete(allarme);
  216.            
  217.         } catch (Exception qe) {
  218.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  219.         }
  220.     }

  221.     public static boolean existsAllarme(String nome, Connection con, Logger log, String tipoDB) throws ServiceException {
  222.         String nomeMetodo = "existsAllarme";
  223.        
  224.         try {
  225.             ServiceManagerProperties properties = new ServiceManagerProperties();
  226.             properties.setDatabaseType(tipoDB);
  227.             properties.setShowSql(true);
  228.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  229.            
  230.             IAllarmeServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeServiceSearch();
  231.            
  232.             IExpression expr = allarmiServiceSearch.newExpression();
  233.            
  234.             expr.equals(Allarme.model().NOME, nome);
  235.            
  236.             NonNegativeNumber count = allarmiServiceSearch.count(expr);
  237.            
  238.             return count.longValue() > 0;
  239.         } catch (Exception qe) {
  240.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  241.         }
  242.     }
  243.    
  244.     public static Allarme getAllarme(String nome, Connection con, Logger log, String tipoDB) throws ServiceException {
  245.         String nomeMetodo = "getAllarmeByNome";
  246.        
  247.         try {
  248.             ServiceManagerProperties properties = new ServiceManagerProperties();
  249.             properties.setDatabaseType(tipoDB);
  250.             properties.setShowSql(true);
  251.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  252.            
  253.             IAllarmeServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeServiceSearch();
  254.            
  255.             IdAllarme id = new IdAllarme();
  256.             id.setNome(nome);
  257.            
  258.             return allarmiServiceSearch.get(id);
  259.            
  260.         } catch (Exception qe) {
  261.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  262.         }
  263.     }
  264.    
  265.     public static Allarme getAllarme(Long id, Connection con, Logger log, String tipoDB) throws ServiceException {
  266.         String nomeMetodo = "getAllarme";
  267.        
  268.         try {
  269.             ServiceManagerProperties properties = new ServiceManagerProperties();
  270.             properties.setDatabaseType(tipoDB);
  271.             properties.setShowSql(true);
  272.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  273.            
  274.             IAllarmeServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeServiceSearch();
  275.            
  276.             IExpression expr = allarmiServiceSearch.newExpression();
  277.            
  278.             AllarmeFieldConverter converter = new AllarmeFieldConverter(tipoDB);
  279.             expr.equals(new CustomField("id", Long.class, "id", converter.toTable(Allarme.model())), id);
  280.            
  281.             return allarmiServiceSearch.find(expr);
  282.            
  283.         } catch (Exception qe) {
  284.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  285.         }
  286.     }
  287.    
  288.     public static Allarme getAllarmeByAlias(String alias,
  289.             RuoloPorta ruoloPorta, String nomePorta, Connection con, Logger log, String tipoDB) throws ServiceException,NotFoundException{
  290.         String nomeMetodo = "getAllarmeByAlias";
  291.        
  292.         try{
  293.             ServiceManagerProperties properties = new ServiceManagerProperties();
  294.             properties.setDatabaseType(tipoDB);
  295.             properties.setShowSql(true);
  296.             JDBCServiceManager serviceManager = new JDBCServiceManager(con, properties, log);
  297.             IExpression expression = serviceManager.getAllarmeServiceSearch().newExpression();
  298.            
  299.             expression.and();
  300.             expression.ilike(Allarme.model().ALIAS, alias, LikeMode.EXACT);
  301.            
  302.             if(ruoloPorta!=null && nomePorta!=null) {
  303.                 expression.equals(Allarme.model().FILTRO.ENABLED, true);
  304.                 expression.equals(Allarme.model().FILTRO.RUOLO_PORTA, ruoloPorta);
  305.                 expression.equals(Allarme.model().FILTRO.NOME_PORTA, nomePorta);
  306.             }
  307.             else {
  308.                 expression.isNull(Allarme.model().FILTRO.NOME_PORTA);
  309.             }
  310.        
  311.             return serviceManager.getAllarmeServiceSearch().find(expression);
  312.         }catch (NotFoundException e) {
  313.             throw new NotFoundException("[" + nomeMetodo + "] Allarme non presente.");
  314.         } catch (Exception qe) {
  315.             throw new ServiceException("[" + nomeMetodo +"] Errore : " + qe.getMessage(),qe);
  316.         }
  317.     }
  318.    
  319.     public static boolean existsAllarmi(TipoAllarme tipoAllarme, Connection con, Logger log, String tipoDB) throws ServiceException {
  320.         String nomeMetodo = "existsAllarmi";
  321.        
  322.         try {
  323.             ServiceManagerProperties properties = new ServiceManagerProperties();
  324.             properties.setDatabaseType(tipoDB);
  325.             properties.setShowSql(true);
  326.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  327.            
  328.             IAllarmeServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeServiceSearch();
  329.            
  330.             IExpression expr = allarmiServiceSearch.newExpression();
  331.            
  332.             if(tipoAllarme!=null) {
  333.                 expr.equals(Allarme.model().TIPO_ALLARME, tipoAllarme.getValue());
  334.             }
  335.            
  336.             NonNegativeNumber count = allarmiServiceSearch.count(expr);
  337.            
  338.             return count.longValue() > 0;
  339.         } catch (Exception qe) {
  340.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  341.         }
  342.     }
  343.    
  344.     public static long countAllarmi(TipoAllarme tipoAllarme, Connection con, Logger log, String tipoDB) throws ServiceException {
  345.         String nomeMetodo = "countAllarmi";
  346.        
  347.         try {
  348.             ServiceManagerProperties properties = new ServiceManagerProperties();
  349.             properties.setDatabaseType(tipoDB);
  350.             properties.setShowSql(true);
  351.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  352.            
  353.             IAllarmeServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeServiceSearch();
  354.            
  355.             IExpression expr = allarmiServiceSearch.newExpression();
  356.            
  357.             if(tipoAllarme!=null) {
  358.                 expr.equals(Allarme.model().TIPO_ALLARME, tipoAllarme.getValue());
  359.             }
  360.            
  361.             NonNegativeNumber count = allarmiServiceSearch.count(expr);
  362.            
  363.             return count.longValue();
  364.         } catch (Exception qe) {
  365.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  366.         }
  367.     }
  368.    
  369.     public static void createHistoryAllarme(AllarmeHistory allarme, Connection con, Logger log, String tipoDB) throws ServiceException {
  370.         String nomeMetodo = "createHistoryAllarme";

  371.         try {
  372.             ServiceManagerProperties properties = new ServiceManagerProperties();
  373.             properties.setDatabaseType(tipoDB);
  374.             properties.setShowSql(true);
  375.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  376.            
  377.             IAllarmeHistoryService allarmiService = jdbcServiceManager.getAllarmeHistoryService();
  378.            
  379.             allarmiService.create(allarme);
  380.            
  381.         } catch (Exception qe) {
  382.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  383.         }
  384.     }
  385.    
  386.     public static List<AllarmeHistory> allarmiHistoryList(ISearch ricerca, Long idAllarme, Connection con, Logger log, String tipoDB) throws ServiceException {
  387.         String nomeMetodo = "allarmiHistoryList";
  388.         int idLista = Liste.CONFIGURAZIONE_ALLARMI_HISTORY;
  389.         int offset;
  390.         int limit;
  391. //      String search;

  392.         limit = ricerca.getPageSize(idLista);
  393.         offset = ricerca.getIndexIniziale(idLista);
  394. //      search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));

  395. //      String filterTipoPlugin = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_TIPO_PLUGIN_CLASSI);
  396.        
  397. //      this.log.debug("search : " + search);
  398.        
  399.         try {
  400.             ServiceManagerProperties properties = new ServiceManagerProperties();
  401.             properties.setDatabaseType(tipoDB);
  402.             properties.setShowSql(true);
  403.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  404.            
  405.             IAllarmeHistoryServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeHistoryServiceSearch();
  406.            
  407.             IExpression expr = allarmiServiceSearch.newExpression();
  408.            
  409.             AllarmeHistoryFieldConverter converter = new AllarmeHistoryFieldConverter(tipoDB);
  410.             CustomField cf = new CustomField("id_allarme", Long.class, "id_allarme", converter.toTable(AllarmeHistory.model()));
  411.             expr.equals(cf, idAllarme);
  412.            
  413. //          boolean addAnd = false;
  414.            
  415. //          if (!search.equals("")) {
  416. //              expr.ilike(Allarme.model().NOME, search, LikeMode.ANYWHERE);
  417.                
  418. //              addAnd = true;
  419. //          }
  420.            
  421.             NonNegativeNumber count = allarmiServiceSearch.count(expr);
  422.             ricerca.setNumEntries(idLista,(int) count.longValue());
  423.            
  424.             // ricavo le entries
  425.             if (limit == 0) // con limit
  426.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  427.            
  428.             IPaginatedExpression pagExpr = allarmiServiceSearch.toPaginatedExpression(expr);
  429.            
  430.             pagExpr.limit(limit).offset(offset);
  431.             pagExpr.addOrder(AllarmeHistory.model().TIMESTAMP_UPDATE, SortOrder.DESC);

  432.             return allarmiServiceSearch.findAll(pagExpr);
  433.            
  434.         } catch (Exception qe) {
  435.             throw new ServiceException("[DriverConfigurazioneDB::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  436.         }
  437.     }
  438.    
  439.     private static String FREE_COUNTER_SEPARATOR_CHAR = "@";
  440.     private static int FREE_COUNTER_SEPARATOR_CHAR_PAD = 19; // colonna che memorizza l'info ha dimensione 275
  441.     public static String buildIdAlarm(String tipoPlugin, String serialId) {
  442.         String idAlarm = tipoPlugin+AllarmiDriverUtils.FREE_COUNTER_SEPARATOR_CHAR+serialId;
  443.         return idAlarm;
  444.     }
  445.    
  446.     private static String normalizeAlarmInstanceSerialId(int value) {
  447.         return StringUtils.leftPad(value+"", FREE_COUNTER_SEPARATOR_CHAR_PAD, "0");
  448.     }
  449.     public static String incrementAlarmInstanceSerialId(String value) {
  450.         int valueInt = 0;
  451.         if(value!=null && !"".equals(value)) {
  452.             StringBuilder sb = new StringBuilder();
  453.             for (int i = 0; i < value.length(); i++) {
  454.                 char c = value.charAt(i);
  455.                 if('0' == c && sb.length()<=0) {
  456.                     continue;
  457.                 }
  458.                 sb.append(c);
  459.             }
  460.             valueInt = Integer.valueOf(sb.toString());
  461.         }
  462.         valueInt++;
  463.         return StringUtils.leftPad(valueInt+"", FREE_COUNTER_SEPARATOR_CHAR_PAD, "0");
  464.     }
  465.    
  466.     //public static Integer getFreeCounterForAlarm(String tipoPlugin, Connection con, Logger log, String tipoDB) throws ServiceException{
  467.     public static String getNextAlarmInstanceSerialId(String tipoPlugin, Connection con, Logger log, String tipoDB) throws ServiceException{
  468.         String nomeMetodo = "getNextAlarmInstanceSerialId";
  469.        
  470.        
  471.         try{
  472.             ServiceManagerProperties properties = new ServiceManagerProperties();
  473.             properties.setDatabaseType(tipoDB);
  474.             properties.setShowSql(true);
  475.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  476.            
  477.             IExpression pagExpr = jdbcServiceManager.getAllarmeServiceSearch().newExpression();
  478.             pagExpr.and();
  479.             pagExpr.equals(Allarme.model().TIPO, tipoPlugin);
  480.            
  481.             // non funziona perchè :10 viene ordinato prima di :9
  482.             //pagExpr.addOrder(AttivazionePolicy.model().ID_ACTIVE_POLICY, SortOrder.DESC);
  483.             //pagExpr.limit(1);
  484.             // devo scorrerle tutte
  485.            
  486.             /*
  487.             try{
  488.                 // inefficiente
  489.                 List<Object> list = jdbcServiceManager.getAllarmeServiceSearch().select(pagExpr, Allarme.model().NOME);
  490.                 if(list!=null && !list.isEmpty()){
  491.                     int found = -1;
  492.                     for (Object r : list) {
  493.                         if(r instanceof String){
  494.                             String s = (String)r;
  495.                             if(s.contains(FREE_COUNTER_SEPARATOR_CHAR)){
  496.                                 int last = s.lastIndexOf(FREE_COUNTER_SEPARATOR_CHAR);
  497.                                 if(last<(s.length()-1)){
  498.                                     int value = Integer.parseInt(s.substring(s.lastIndexOf(FREE_COUNTER_SEPARATOR_CHAR)+1,s.length()));
  499.                                     if(value > found) {
  500.                                         found = value;
  501.                                     }
  502.                                 }
  503.                             }
  504.                         }  
  505.                     }
  506.                     if(found>0) {
  507.                         return found+1;
  508.                     }
  509.                 }
  510.             }catch(NotFoundException notF){
  511.                
  512.             }
  513.             return 1;*/
  514.            
  515.             FunctionField ff = new FunctionField(Allarme.model().NOME, Function.MAX, "maxAlarmId");
  516.             Object maxValue = null;
  517.             try {
  518.                 maxValue = jdbcServiceManager.getAllarmeServiceSearch().aggregate(pagExpr, ff);
  519.             }catch(NotFoundException notFound) {
  520.             }
  521.             if(maxValue!=null){
  522.                 if(maxValue instanceof String){
  523.                     String s = (String)maxValue;
  524.                     if(s.contains(FREE_COUNTER_SEPARATOR_CHAR)){
  525.                         int last = s.lastIndexOf(FREE_COUNTER_SEPARATOR_CHAR);
  526.                         if(last<(s.length()-1)){
  527.                             String actualMaxValue = s.substring(s.lastIndexOf(FREE_COUNTER_SEPARATOR_CHAR)+1,s.length());
  528.                             return incrementAlarmInstanceSerialId(actualMaxValue);
  529.                         }
  530.                     }
  531.                 }  
  532.             }
  533.             return normalizeAlarmInstanceSerialId(1);
  534.            
  535.         } catch (Exception qe) {
  536.             throw new ServiceException("[" + nomeMetodo +"] Errore : " + qe.getMessage(),qe);
  537.         }
  538.     }
  539.    
  540.     public static List<Allarme> configurazioneAllarmiList(ISearch ricerca, RuoloPorta ruoloPorta, String nomePorta,
  541.             Connection con, Logger log, String tipoDB,
  542.             String nomeMetodo,
  543.             IDSoggetto filtroSoggettoFruitore, IDServizioApplicativo filtroApplicativoFruitore,String filtroRuoloFruitore,
  544.             IDSoggetto filtroSoggettoErogatore, String filtroRuoloErogatore,
  545.             IDServizio filtroServizioAzione, String filtroRuolo) throws ServiceException{
  546.         // ritorna la configurazione allarmi
  547.         int idLista = Liste.CONFIGURAZIONE_ALLARMI;
  548.         String search = null;
  549.         int offset = 0;
  550.         int limit =0 ;

  551.         if(ricerca != null) {
  552.             search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  553.             limit = ricerca.getPageSize(idLista);
  554.             offset = ricerca.getIndexIniziale(idLista);
  555.         }
  556.         if (limit == 0) // con limit
  557.             limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  558.        
  559.         log.debug("search : " + search);
  560.        
  561.         List<Allarme> listaAllarmi = new ArrayList<Allarme>();
  562.        
  563.         long count = 0;
  564.         PreparedStatement pstmt = null;
  565.         ResultSet rs = null;
  566.         try {
  567.             if(ricerca!=null) {

  568.                 ISQLQueryObject sqlQueryObject = _prepareSqlQueryObjectAllarmi(tipoDB,ruoloPorta, nomePorta, search,
  569.                         filtroSoggettoFruitore, filtroApplicativoFruitore, filtroRuoloFruitore,
  570.                         filtroSoggettoErogatore, filtroRuoloErogatore,
  571.                         filtroServizioAzione, filtroRuolo);
  572.                 sqlQueryObject.addSelectCountField("nome", "allarmi");
  573.                 String query = sqlQueryObject.createSQLQuery();
  574.                 pstmt = con.prepareStatement(query);
  575.                 _prepareStatementAllarmi(tipoDB,pstmt, ruoloPorta, nomePorta, search,
  576.                         filtroSoggettoFruitore, filtroApplicativoFruitore, filtroRuoloFruitore,
  577.                         filtroSoggettoErogatore, filtroRuoloErogatore,
  578.                         filtroServizioAzione, filtroRuolo);
  579.                 rs = pstmt.executeQuery();
  580.                 if(rs.next()) {
  581.                     count = rs.getLong("allarmi");
  582.                 }
  583.                 rs.close();
  584.                 pstmt.close();
  585.                
  586.             }
  587.            
  588.             ServiceManagerProperties properties = new ServiceManagerProperties();
  589.             properties.setDatabaseType(tipoDB);
  590.             properties.setShowSql(true);
  591.             JDBCServiceManager serviceManager = new JDBCServiceManager(con, properties, log);
  592.            
  593.            
  594.             ISQLQueryObject sqlQueryObject = _prepareSqlQueryObjectAllarmi(tipoDB,ruoloPorta, nomePorta, search,
  595.                     filtroSoggettoFruitore, filtroApplicativoFruitore, filtroRuoloFruitore,
  596.                     filtroSoggettoErogatore, filtroRuoloErogatore,
  597.                     filtroServizioAzione, filtroRuolo);
  598.             sqlQueryObject.addSelectField(CostantiDB.ALLARMI, "nome");
  599.             sqlQueryObject.setOffset(offset);
  600.             sqlQueryObject.setLimit(limit);
  601.             //sqlQueryObject.addOrderBy("policy_alias", true);
  602.             //sqlQueryObject.addOrderBy("policy_id", true);
  603.             sqlQueryObject.addOrderBy(CostantiDB.ALLARMI+".nome", true);
  604.             String query = sqlQueryObject.createSQLQuery();
  605.             pstmt = con.prepareStatement(query);
  606.             _prepareStatementAllarmi(tipoDB,pstmt, ruoloPorta, nomePorta, search,
  607.                     filtroSoggettoFruitore, filtroApplicativoFruitore, filtroRuoloFruitore,
  608.                     filtroSoggettoErogatore, filtroRuoloErogatore,
  609.                     filtroServizioAzione, filtroRuolo);
  610.             rs = pstmt.executeQuery();
  611.             while(rs.next()) {
  612.                
  613.                 String nomeAllarme = rs.getString("nome");
  614.                
  615.                 IdAllarme idAllarmeAsObject = new IdAllarme();
  616.                 idAllarmeAsObject.setNome(nomeAllarme);
  617.                 Allarme allarme = serviceManager.getAllarmeServiceSearch().get(idAllarmeAsObject);
  618.                 listaAllarmi.add(allarme);
  619.                
  620.             }
  621.             rs.close();
  622.             pstmt.close();
  623.                        
  624.         } catch (Exception qe) {
  625.             throw new ServiceException("[" + nomeMetodo +"] Errore : " + qe.getMessage(),qe);
  626.         } finally {
  627.             try {
  628.                 if(rs!=null) {
  629.                     rs.close();
  630.                 }
  631.             } catch (Exception e) {
  632.                 // ignore exception
  633.             }
  634.             try {
  635.                 if(pstmt!=null) {
  636.                     pstmt.close();
  637.                 }
  638.             } catch (Exception e) {
  639.                 // ignore exception
  640.             }
  641.         }
  642.        
  643.         if(ricerca!=null) {
  644.             ricerca.setNumEntries(idLista,(int) count);
  645.         }
  646.        
  647.         return listaAllarmi;
  648.        
  649.     }
  650.     private static ISQLQueryObject _prepareSqlQueryObjectAllarmi(
  651.             String tipoDB,
  652.             RuoloPorta ruoloPorta, String nomePorta,
  653.             String search,
  654.             IDSoggetto filtroSoggettoFruitore, IDServizioApplicativo filtroApplicativoFruitore,String filtroRuoloFruitore,
  655.             IDSoggetto filtroSoggettoErogatore, String filtroRuoloErogatore,
  656.             IDServizio filtroServizioAzione, String filtroRuolo) throws Exception {
  657.         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  658.         sqlQueryObject.setANDLogicOperator(true);
  659.         sqlQueryObject.addFromTable(CostantiDB.ALLARMI);
  660.         if(ruoloPorta!=null && nomePorta!=null) {
  661.             sqlQueryObject.addWhereCondition("filtro_ruolo=?");
  662.             sqlQueryObject.addWhereCondition("filtro_porta=?");
  663.         }
  664.         else if(ruoloPorta!=null) {
  665.             sqlQueryObject.addWhereCondition("filtro_ruolo=?"); // se passo solo ruolo porta, voglio tutte le policy con quel ruolo. Altrimenti finisco nelle policy globali
  666.         }
  667.         else {
  668.             sqlQueryObject.addWhereIsNullCondition("filtro_porta");
  669.         }
  670.         if(search != null  && !"".equals(search)){
  671.             // fix: oramai l'alias e' obbligatorio
  672.             sqlQueryObject.addWhereCondition(false,
  673.                     //sqlQueryObject.getWhereLikeCondition("active_policy_id", search, false, true, true),
  674.                     sqlQueryObject.getWhereLikeCondition("alias", search, false, true, true));
  675.         }
  676.         if(filtroSoggettoFruitore!=null) {
  677.             if(filtroSoggettoFruitore.getTipo()!=null) {
  678.                 sqlQueryObject.addWhereCondition("filtro_tipo_fruitore=?");
  679.             }
  680.             if(filtroSoggettoFruitore.getNome()!=null) {
  681.                 sqlQueryObject.addWhereCondition("filtro_nome_fruitore=?");
  682.             }
  683.         }
  684.         if(filtroApplicativoFruitore!=null) {
  685.             if(filtroApplicativoFruitore.getIdSoggettoProprietario()!=null) {
  686.                 if(filtroApplicativoFruitore.getIdSoggettoProprietario().getTipo()!=null) {
  687.                     sqlQueryObject.addWhereCondition("filtro_tipo_fruitore=?");
  688.                 }
  689.                 if(filtroApplicativoFruitore.getIdSoggettoProprietario().getNome()!=null) {
  690.                     sqlQueryObject.addWhereCondition("filtro_nome_fruitore=?");
  691.                 }
  692.             }
  693.             if(filtroApplicativoFruitore.getNome()!=null) {
  694.                 sqlQueryObject.addWhereCondition("filtro_sa_fruitore=?");
  695.             }
  696.         }
  697.         if(filtroRuoloFruitore!=null) {
  698.             sqlQueryObject.addWhereCondition("filtro_ruolo_fruitore=?");
  699.         }
  700.         if(filtroSoggettoErogatore!=null) {
  701.             if(filtroSoggettoErogatore.getTipo()!=null) {
  702.                 sqlQueryObject.addWhereCondition("filtro_tipo_erogatore=?");
  703.             }
  704.             if(filtroSoggettoErogatore.getNome()!=null) {
  705.                 sqlQueryObject.addWhereCondition("filtro_nome_erogatore=?");
  706.             }
  707.         }
  708.         if(filtroRuoloErogatore!=null) {
  709.             sqlQueryObject.addWhereCondition("filtro_ruolo_erogatore=?");
  710.         }
  711.         if(filtroServizioAzione!=null) {
  712.             if(filtroServizioAzione.getSoggettoErogatore()!=null) {
  713.                 if(filtroServizioAzione.getSoggettoErogatore().getTipo()!=null) {
  714.                     sqlQueryObject.addWhereCondition("filtro_tipo_erogatore=?");
  715.                 }
  716.                 if(filtroServizioAzione.getSoggettoErogatore().getNome()!=null) {
  717.                     sqlQueryObject.addWhereCondition("filtro_nome_erogatore=?");
  718.                 }
  719.             }
  720.             if(filtroServizioAzione.getTipo()!=null) {
  721.                 sqlQueryObject.addWhereCondition("filtro_tipo_servizio=?");
  722.             }
  723.             if(filtroServizioAzione.getNome()!=null) {
  724.                 sqlQueryObject.addWhereCondition("filtro_nome_servizio=?");
  725.             }
  726.             if(filtroServizioAzione.getVersione()!=null) {
  727.                 sqlQueryObject.addWhereCondition("filtro_versione_servizio=?");
  728.             }
  729.             if(filtroServizioAzione.getAzione()!=null) {
  730.                 // condizione di controllo
  731.                 ISQLQueryObject sqlQueryObjectOr = SQLObjectFactory.createSQLQueryObject(tipoDB);
  732.                 sqlQueryObjectOr.setANDLogicOperator(false);
  733.                 // (filtro_azione == 'NOME') OR (filtro_azione like 'NOME,%') OR (filtro_azione like '%,NOME') OR (filtro_azione like '%,applicabilita_azioni,%')
  734.                 // CLOB sqlQueryObjectOr.addWhereCondition(CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY+".filtro_azione = ?");
  735.                 sqlQueryObjectOr.addWhereLikeCondition(CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY+".filtro_azione", filtroServizioAzione.getAzione(), false , false);
  736.                 sqlQueryObjectOr.addWhereLikeCondition(CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY+".filtro_azione", filtroServizioAzione.getAzione()+",", LikeConfig.startsWith(false));
  737.                 sqlQueryObjectOr.addWhereLikeCondition(CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY+".filtro_azione", ","+filtroServizioAzione.getAzione(), LikeConfig.endsWith(false));
  738.                 sqlQueryObjectOr.addWhereLikeCondition(CostantiDB.CONTROLLO_TRAFFICO_ACTIVE_POLICY+".filtro_azione", ","+filtroServizioAzione.getAzione()+",", true , false);
  739.                 sqlQueryObject.addWhereCondition(sqlQueryObjectOr.createSQLConditions());
  740.             }
  741.         }
  742.         if(filtroRuolo!=null) {
  743.             sqlQueryObject.addWhereCondition(false, "filtro_ruolo_fruitore=?", "filtro_ruolo_erogatore=?");
  744.         }
  745.         return sqlQueryObject;
  746.     }
  747.     private static void _prepareStatementAllarmi(
  748.             String tipoDB,
  749.             PreparedStatement pstmt,
  750.             RuoloPorta ruoloPorta, String nomePorta,
  751.             String search,
  752.             IDSoggetto filtroSoggettoFruitore, IDServizioApplicativo filtroApplicativoFruitore,String filtroRuoloFruitore,
  753.             IDSoggetto filtroSoggettoErogatore, String filtroRuoloErogatore,
  754.             IDServizio filtroServizioAzione, String filtroRuolo) throws Exception {
  755.         int index = 1;
  756.         if(ruoloPorta!=null && nomePorta!=null) {
  757.             pstmt.setString(index++, ruoloPorta.getValue());
  758.             pstmt.setString(index++, nomePorta);
  759.         }
  760.         else if(ruoloPorta!=null) {
  761.             pstmt.setString(index++, ruoloPorta.getValue());
  762.         }
  763.         if(filtroSoggettoFruitore!=null) {
  764.             if(filtroSoggettoFruitore.getTipo()!=null) {
  765.                 pstmt.setString(index++, filtroSoggettoFruitore.getTipo());
  766.             }
  767.             if(filtroSoggettoFruitore.getNome()!=null) {
  768.                 pstmt.setString(index++, filtroSoggettoFruitore.getNome());
  769.             }
  770.         }
  771.         if(filtroApplicativoFruitore!=null) {
  772.             if(filtroApplicativoFruitore.getIdSoggettoProprietario()!=null) {
  773.                 if(filtroApplicativoFruitore.getIdSoggettoProprietario().getTipo()!=null) {
  774.                     pstmt.setString(index++, filtroApplicativoFruitore.getIdSoggettoProprietario().getTipo());
  775.                 }
  776.                 if(filtroApplicativoFruitore.getIdSoggettoProprietario().getNome()!=null) {
  777.                     pstmt.setString(index++, filtroApplicativoFruitore.getIdSoggettoProprietario().getNome());
  778.                 }
  779.             }
  780.             if(filtroApplicativoFruitore.getNome()!=null) {
  781.                 pstmt.setString(index++, filtroApplicativoFruitore.getNome());
  782.             }
  783.         }
  784.         if(filtroRuoloFruitore!=null) {
  785.             pstmt.setString(index++, filtroRuoloFruitore);
  786.         }
  787.         if(filtroSoggettoErogatore!=null) {
  788.             if(filtroSoggettoErogatore.getTipo()!=null) {
  789.                 pstmt.setString(index++, filtroSoggettoErogatore.getTipo());
  790.             }
  791.             if(filtroSoggettoErogatore.getNome()!=null) {
  792.                 pstmt.setString(index++, filtroSoggettoErogatore.getNome());
  793.             }
  794.         }
  795.         if(filtroRuoloErogatore!=null) {
  796.             pstmt.setString(index++, filtroRuoloErogatore);
  797.         }
  798.         if(filtroServizioAzione!=null) {
  799.             if(filtroServizioAzione.getSoggettoErogatore()!=null) {
  800.                 if(filtroServizioAzione.getSoggettoErogatore().getTipo()!=null) {
  801.                     pstmt.setString(index++, filtroServizioAzione.getSoggettoErogatore().getTipo());
  802.                 }
  803.                 if(filtroServizioAzione.getSoggettoErogatore().getNome()!=null) {
  804.                     pstmt.setString(index++, filtroServizioAzione.getSoggettoErogatore().getNome());
  805.                 }
  806.             }
  807.             if(filtroServizioAzione.getTipo()!=null) {
  808.                 pstmt.setString(index++, filtroServizioAzione.getTipo());
  809.             }
  810.             if(filtroServizioAzione.getNome()!=null) {
  811.                 pstmt.setString(index++, filtroServizioAzione.getNome());
  812.             }
  813.             if(filtroServizioAzione.getVersione()!=null) {
  814.                 pstmt.setInt(index++, filtroServizioAzione.getVersione());
  815.             }
  816.             if(filtroServizioAzione.getAzione()!=null) {
  817.                 // inserito direttamente nella preparazione
  818.             }
  819.         }
  820.         if(filtroRuolo!=null) {
  821.             pstmt.setString(index++, filtroRuolo);
  822.             pstmt.setString(index++, filtroRuolo);
  823.         }
  824.     }
  825.    
  826.    
  827.     public static List<Allarme> allarmiForPolicyRateLimiting(String activeIdPolicy, RuoloPorta ruoloPorta, String nomePorta, Connection con, Logger log, String tipoDB) throws ServiceException {
  828.         String nomeMetodo = "allarmiForPolicyRateLimiting";
  829.                
  830.         try {
  831.             ServiceManagerProperties properties = new ServiceManagerProperties();
  832.             properties.setDatabaseType(tipoDB);
  833.             properties.setShowSql(true);
  834.             JDBCServiceManager jdbcServiceManager = new JDBCServiceManager(con, properties, log);
  835.            
  836.             IAllarmeServiceSearch allarmiServiceSearch = jdbcServiceManager.getAllarmeServiceSearch();
  837.            
  838.             IExpression expr = allarmiServiceSearch.newExpression();
  839.            
  840.             expr.and();
  841.            
  842.             if(ruoloPorta!=null && nomePorta!=null) {
  843.                 expr.equals(Allarme.model().FILTRO.RUOLO_PORTA, ruoloPorta.getValue()).and().equals(Allarme.model().FILTRO.NOME_PORTA, nomePorta);
  844.             }
  845.             else {
  846.                 expr.isNull(Allarme.model().FILTRO.NOME_PORTA);
  847.             }
  848.            
  849.             expr.equals(Allarme.model().ALLARME_PARAMETRO.ID_PARAMETRO, CostantiConfigurazione.PARAM_POLICY_ID);
  850.             if(activeIdPolicy!=null) {
  851.                 expr.like(Allarme.model().ALLARME_PARAMETRO.VALORE, activeIdPolicy,LikeMode.EXACT);
  852.             }
  853.             else {
  854.                 expr.isNull(Allarme.model().ALLARME_PARAMETRO.VALORE);
  855.             }

  856.             IPaginatedExpression pagExpr = allarmiServiceSearch.toPaginatedExpression(expr);
  857.            
  858.             pagExpr.addOrder(Allarme.model().ALIAS, SortOrder.ASC);

  859.             return allarmiServiceSearch.findAll(pagExpr);
  860.            
  861.         } catch (Exception qe) {
  862.             throw new ServiceException("[" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  863.         }
  864.     }
  865.    
  866. }