HandlersDriverUtils.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.plugins.utils.handlers;

  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.openspcoop2.core.commons.ISearch;
  27. import org.openspcoop2.core.config.ConfigurazioneHandler;
  28. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  29. import org.openspcoop2.core.constants.CostantiDB;
  30. import org.openspcoop2.core.constants.TipoPdD;
  31. import org.openspcoop2.core.plugins.Plugin;
  32. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  33. import org.openspcoop2.core.plugins.dao.jdbc.converter.PluginFieldConverter;
  34. import org.openspcoop2.core.plugins.utils.PluginsDriverUtils;
  35. import org.openspcoop2.generic_project.exception.ServiceException;
  36. import org.openspcoop2.utils.sql.ISQLQueryObject;
  37. import org.openspcoop2.utils.sql.SQLObjectFactory;
  38. import org.slf4j.Logger;

  39. /**
  40.  * HandlersDriverUtils
  41.  *
  42.  * @author Poli Andrea (apoli@link.it)
  43.  * @author Pintori Giuliano (pintori@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class HandlersDriverUtils {

  48.     public static int numeroHandlerList(String tipologia, TipoPdD ruoloPorta, Long idPorta, TipoPlugin tipoPlugin, String nomeMetodo, Connection con, Logger log, String tipoDB) throws ServiceException {
  49.         String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  50.         if(ruoloPorta !=null) {
  51.             if(ruoloPorta.equals(TipoPdD.DELEGATA)) {
  52.                 tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  53.             }
  54.             else if(ruoloPorta.equals(TipoPdD.APPLICATIVA)) {
  55.                 tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  56.             }
  57.         }      
  58.        
  59.         String queryString;

  60.         PreparedStatement stmt=null;
  61.         ResultSet risultato=null;

  62.         try {
  63.             PluginFieldConverter converter = new PluginFieldConverter(tipoDB);
  64.             String tabellaPlugin = converter.toTable(Plugin.model());
  65.            
  66.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  67.             sqlQueryObject.addFromTable(tabella);
  68.             sqlQueryObject.addFromTable(tabellaPlugin);
  69.             sqlQueryObject.addSelectCountField(tabella + ".id", "cont");
  70.            
  71.             sqlQueryObject.addWhereCondition(tabellaPlugin +".tipo=" + tabella + ".tipo");
  72.             if(idPorta !=null) {
  73.                 sqlQueryObject.addWhereCondition(tabella + ".id_porta=?");
  74.             }
  75.             sqlQueryObject.addWhereCondition(tabella + ".tipologia=?");
  76.             sqlQueryObject.addWhereCondition(tabellaPlugin + ".tipo_plugin=?");
  77.             sqlQueryObject.setANDLogicOperator(true);
  78.             queryString = sqlQueryObject.createSQLQuery();
  79.             stmt = con.prepareStatement(queryString);
  80.             int index = 1;
  81.             if(idPorta!=null) {
  82.                 stmt.setLong(index++, idPorta);
  83.             }
  84.             stmt.setString(index++, tipologia);
  85.             stmt.setString(index++, tipoPlugin.getValue());
  86.            
  87.             risultato = stmt.executeQuery();
  88.             int res = 0;
  89.             if (risultato.next()) {
  90.                 res = risultato.getInt(1);
  91.             }
  92.             risultato.close();
  93.             stmt.close();

  94.             return res;
  95.         } catch (Exception se) {
  96.             throw new ServiceException("[" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  97.         } finally {

  98.             //Chiudo statement and resultset
  99.             try{
  100.                 if(risultato!=null)
  101.                     risultato.close();
  102.             }catch (Exception e) {
  103.                 //ignore
  104.             }
  105.             try{
  106.                 if(stmt!=null)
  107.                     stmt.close();
  108.             }catch (Exception e) {
  109.                 //ignore
  110.             }
  111.         }
  112.     }
  113.    
  114.     public static List<ConfigurazioneHandlerBean> handlerList(ISearch ricerca, String tipologia, TipoPdD ruoloPorta,
  115.             Long idPorta, String nomeMetodo, int idLista, TipoPlugin tipoPlugin, Connection con, Logger log, String tipoDB) throws ServiceException {
  116.         String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  117.         if(ruoloPorta !=null) {
  118.             if(ruoloPorta.equals(TipoPdD.DELEGATA)) {
  119.                 tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  120.             }
  121.             else if(ruoloPorta.equals(TipoPdD.APPLICATIVA)) {
  122.                 tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  123.             }
  124.         }      
  125.        
  126.         int offset;
  127.         int limit;
  128.         String queryString;
  129.         String search;

  130.         limit = ricerca.getPageSize(idLista);
  131.         offset = ricerca.getIndexIniziale(idLista);
  132.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));

  133.         PreparedStatement stmt=null;
  134.         ResultSet risultato=null;
  135.         ArrayList<ConfigurazioneHandlerBean> lista = new ArrayList<ConfigurazioneHandlerBean>();

  136.         try {
  137.             PluginFieldConverter converter = new PluginFieldConverter(tipoDB);
  138.             String tabellaPlugin = converter.toTable(Plugin.model());
  139.            
  140.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  141.             sqlQueryObject.addFromTable(tabella);
  142.             sqlQueryObject.addFromTable(tabellaPlugin);
  143.            
  144.             sqlQueryObject.addSelectCountField(tabella + ".id", "cont");
  145.             sqlQueryObject.addWhereCondition(tabellaPlugin +".tipo=" + tabella + ".tipo");
  146.            
  147.             if (!search.equals("")) {
  148.                 sqlQueryObject.addWhereLikeCondition(tabellaPlugin +".label", search, true, true);
  149.             }
  150.             if(idPorta !=null) {
  151.                 sqlQueryObject.addWhereCondition(tabella + ".id_porta=?");
  152.             }
  153.             sqlQueryObject.addWhereCondition(tabella + ".tipologia=?");
  154.             sqlQueryObject.addWhereCondition(tabellaPlugin + ".tipo_plugin=?");
  155.             sqlQueryObject.setANDLogicOperator(true);
  156.            
  157.             queryString = sqlQueryObject.createSQLQuery();
  158.             stmt = con.prepareStatement(queryString);
  159.             int index = 1;
  160.             if(idPorta!=null) {
  161.                 stmt.setLong(index++, idPorta);
  162.             }
  163.             stmt.setString(index++, tipologia);
  164.             stmt.setString(index++, tipoPlugin.getValue());
  165.            
  166.             risultato = stmt.executeQuery();
  167.             if (risultato.next())
  168.                 ricerca.setNumEntries(idLista,risultato.getInt(1));
  169.             risultato.close();
  170.             stmt.close();

  171.             // ricavo le entries
  172.             if (limit == 0) // con limit
  173.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;

  174.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  175.             sqlQueryObject.addFromTable(tabella);
  176.             sqlQueryObject.addFromTable(tabellaPlugin);
  177.             sqlQueryObject.addSelectAliasField(tabella, "id","IdHandler");
  178.             sqlQueryObject.addSelectField(tabella,"tipologia");
  179.             String aliasTIPO = "tipoTabConf";
  180.             sqlQueryObject.addSelectAliasField(tabella,"tipo",aliasTIPO);        
  181.             sqlQueryObject.addSelectField(tabella,"posizione");      
  182.             sqlQueryObject.addSelectField(tabella,"stato");
  183.             if(idPorta !=null) {
  184.                 sqlQueryObject.addSelectField(tabella,"id_porta");
  185.             }
  186.            
  187.             sqlQueryObject.addWhereCondition(tabellaPlugin +".tipo=" + tabella + ".tipo");
  188.            
  189.             if (!search.equals("")) {
  190.                 sqlQueryObject.addWhereLikeCondition(tabellaPlugin +".label", search, true, true);
  191.             }
  192.             if(idPorta !=null) {
  193.                 sqlQueryObject.addWhereCondition(tabella + ".id_porta=?");
  194.             }
  195.             sqlQueryObject.addWhereCondition(tabella + ".tipologia=?");
  196.             sqlQueryObject.addWhereCondition(tabellaPlugin + ".tipo_plugin=?");
  197.             sqlQueryObject.setANDLogicOperator(true);
  198.             sqlQueryObject.addOrderBy(tabella + ".posizione");
  199.             /**sqlQueryObject.addOrderBy(tabella + ".tipo");*/
  200.             sqlQueryObject.addOrderBy(aliasTIPO);
  201.             sqlQueryObject.setSortType(true);
  202.             sqlQueryObject.setLimit(limit);
  203.             sqlQueryObject.setOffset(offset);
  204.             queryString = sqlQueryObject.createSQLQuery();
  205.             stmt = con.prepareStatement(queryString);
  206.             index = 1;
  207.             if(idPorta!=null) {
  208.                 stmt.setLong(index++, idPorta);
  209.             }
  210.             stmt.setString(index++, tipologia);
  211.             stmt.setString(index++, tipoPlugin.getValue());
  212.             risultato = stmt.executeQuery();

  213.            
  214.             while (risultato.next()) {
  215.                 ConfigurazioneHandler handler = new ConfigurazioneHandler();
  216.                 handler.setId(risultato.getLong("IdHandler"));
  217.                 handler.setPosizione(risultato.getInt("posizione"));
  218.                 handler.setStato(getEnumStatoFunzionalita(risultato.getString("stato")));
  219.                 /**handler.setTipo(risultato.getString("tipo"));*/
  220.                 handler.setTipo(risultato.getString(aliasTIPO));
  221.                
  222.                 Plugin plugin = PluginsDriverUtils.getPlugin(tipoPlugin.getValue(), handler.getTipo(), true, con, log, tipoDB);
  223.                
  224.                 ConfigurazioneHandlerBean bean = new ConfigurazioneHandlerBean(handler, plugin);
  225.                
  226.                 lista.add(bean);
  227.            
  228.             }
  229.             risultato.close();
  230.             stmt.close();
  231.            
  232.             return lista;

  233.         } catch (Exception se) {
  234.             throw new ServiceException("[" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  235.         } finally {

  236.             //Chiudo statement and resultset
  237.             try{
  238.                 if(risultato!=null)
  239.                     risultato.close();
  240.             }catch (Exception e) {
  241.                 //ignore
  242.             }
  243.             try{
  244.                 if(stmt!=null)
  245.                     stmt.close();
  246.             }catch (Exception e) {
  247.                 //ignore
  248.             }
  249.         }
  250.     }
  251.    
  252.     public static int getMaxPosizioneHandlers(String tipologia, TipoPdD ruoloPorta, Long idPorta, String nomeMetodo, TipoPlugin tipoPlugin, Connection con, Logger log, String tipoDB) throws ServiceException {
  253.         String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  254.         if(ruoloPorta !=null) {
  255.             if(ruoloPorta.equals(TipoPdD.DELEGATA)) {
  256.                 tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  257.             }
  258.             else if(ruoloPorta.equals(TipoPdD.APPLICATIVA)) {
  259.                 tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  260.             }
  261.         }      
  262.        
  263.         String queryString;

  264.         PreparedStatement stmt=null;
  265.         ResultSet risultato=null;
  266.         int posizione = 0;

  267.         try {
  268.             PluginFieldConverter converter = new PluginFieldConverter(tipoDB);
  269.             String tabellaPlugin = converter.toTable(Plugin.model());
  270.            
  271.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  272.             sqlQueryObject.addFromTable(tabella);
  273.             sqlQueryObject.addFromTable(tabellaPlugin);
  274.             sqlQueryObject.addWhereCondition(tabellaPlugin +".tipo=" + tabella + ".tipo");
  275.            
  276.             sqlQueryObject.addSelectMaxField(tabella + ".posizione", "posizione");      

  277.             if(idPorta !=null) {
  278.                 sqlQueryObject.addWhereCondition(tabella + ".id_porta=?");
  279.             }
  280.             sqlQueryObject.addWhereCondition(tabella + ".tipologia=?");
  281.             sqlQueryObject.addWhereCondition(tabellaPlugin + ".tipo_plugin=?");
  282.             sqlQueryObject.setANDLogicOperator(true);
  283.             queryString = sqlQueryObject.createSQLQuery();
  284.             stmt = con.prepareStatement(queryString);
  285.             int index = 1;
  286.             if(idPorta!=null) {
  287.                 stmt.setLong(index++, idPorta);
  288.             }
  289.             stmt.setString(index++, tipologia);
  290.             stmt.setString(index++, tipoPlugin.getValue());
  291.             risultato = stmt.executeQuery();

  292.             if(risultato.next()) {
  293.                 posizione = risultato.getInt("posizione");
  294.             }
  295.            
  296.             risultato.close();
  297.             stmt.close();
  298.            
  299.             return posizione;

  300.         } catch (Exception se) {
  301.             throw new ServiceException("[" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  302.         } finally {

  303.             //Chiudo statement and resultset
  304.             try{
  305.                 if(risultato!=null)
  306.                     risultato.close();
  307.             }catch (Exception e) {
  308.                 //ignore
  309.             }
  310.             try{
  311.                 if(stmt!=null)
  312.                     stmt.close();
  313.             }catch (Exception e) {
  314.                 //ignore
  315.             }
  316.         }
  317.     }
  318.    
  319.     public static boolean existsHandler(String tipologia, TipoPdD ruoloPorta, Long idPorta, String nomeMetodo, TipoPlugin tipoPlugin, String tipo, Connection con, Logger log, String tipoDB) throws ServiceException {
  320.         String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  321.         if(ruoloPorta !=null) {
  322.             if(ruoloPorta.equals(TipoPdD.DELEGATA)) {
  323.                 tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  324.             }
  325.             else if(ruoloPorta.equals(TipoPdD.APPLICATIVA)) {
  326.                 tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  327.             }
  328.         }      
  329.        
  330.         String queryString;

  331.         PreparedStatement stmt=null;
  332.         ResultSet risultato=null;
  333.         boolean exists = false;

  334.         try {
  335.             PluginFieldConverter converter = new PluginFieldConverter(tipoDB);
  336.             String tabellaPlugin = converter.toTable(Plugin.model());
  337.            
  338.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  339.             sqlQueryObject.addFromTable(tabella);
  340.             sqlQueryObject.addFromTable(tabellaPlugin);
  341.             sqlQueryObject.addWhereCondition(tabellaPlugin +".tipo=" + tabella + ".tipo");
  342.            
  343.             sqlQueryObject.addSelectCountField(tabella + ".id", "cont");  

  344.             if(idPorta !=null) {
  345.                 sqlQueryObject.addWhereCondition(tabella + ".id_porta=?");
  346.             }
  347.             sqlQueryObject.addWhereCondition(tabella + ".tipologia=?");
  348.             sqlQueryObject.addWhereCondition(tabella + ".tipo=?");
  349.             sqlQueryObject.addWhereCondition(tabellaPlugin + ".tipo_plugin=?");
  350.             sqlQueryObject.setANDLogicOperator(true);
  351.             queryString = sqlQueryObject.createSQLQuery();
  352.             stmt = con.prepareStatement(queryString);
  353.             int index = 1;
  354.             if(idPorta!=null) {
  355.                 stmt.setLong(index++, idPorta);
  356.             }
  357.             stmt.setString(index++, tipologia);
  358.             stmt.setString(index++, tipo);
  359.             stmt.setString(index++, tipoPlugin.getValue());
  360.             risultato = stmt.executeQuery();

  361.             if(risultato.next()) {
  362.                 exists = risultato.getInt(1) > 0;
  363.             }
  364.            
  365.             risultato.close();
  366.             stmt.close();
  367.            
  368.             return exists;

  369.         } catch (Exception se) {
  370.             throw new ServiceException("[" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  371.         } finally {

  372.             //Chiudo statement and resultset
  373.             try{
  374.                 if(risultato!=null)
  375.                     risultato.close();
  376.             }catch (Exception e) {
  377.                 //ignore
  378.             }
  379.             try{
  380.                 if(stmt!=null)
  381.                     stmt.close();
  382.             }catch (Exception e) {
  383.                 //ignore
  384.             }
  385.         }
  386.     }
  387.    
  388.     public static ConfigurazioneHandlerBean getHandler(String tipologia, TipoPdD ruoloPorta,
  389.             Long idPorta, Long idHandler, String nomeMetodo, TipoPlugin tipoPlugin, Connection con, Logger log, String tipoDB) throws ServiceException {
  390.        
  391.         String tabella = CostantiDB.CONFIGURAZIONE_HANDLERS;
  392.         if(ruoloPorta !=null) {
  393.             if(ruoloPorta.equals(TipoPdD.DELEGATA)) {
  394.                 tabella = CostantiDB.PORTE_DELEGATE_HANDLERS;
  395.             }
  396.             else if(ruoloPorta.equals(TipoPdD.APPLICATIVA)) {
  397.                 tabella = CostantiDB.PORTE_APPLICATIVE_HANDLERS;
  398.             }
  399.         }      
  400.        
  401.         String queryString;

  402.         PreparedStatement stmt=null;
  403.         ResultSet risultato=null;
  404.         ConfigurazioneHandlerBean bean = null;

  405.         try {
  406.             PluginFieldConverter converter = new PluginFieldConverter(tipoDB);
  407.             String tabellaPlugin = converter.toTable(Plugin.model());
  408.            
  409.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  410.             sqlQueryObject.addFromTable(tabella);
  411.             sqlQueryObject.addFromTable(tabellaPlugin);
  412.             sqlQueryObject.addSelectField(tabella + ".id");
  413.             sqlQueryObject.addSelectField(tabella + ".tipologia");
  414.             sqlQueryObject.addSelectField(tabella + ".tipo");        
  415.             sqlQueryObject.addSelectField(tabella + ".posizione");      
  416.             sqlQueryObject.addSelectField(tabella + ".stato");
  417.             if(idPorta !=null) {
  418.                 sqlQueryObject.addSelectField(tabella + ".id_porta");
  419.             }
  420.            
  421.             sqlQueryObject.addWhereCondition(tabellaPlugin +".tipo=" + tabella + ".tipo");
  422.            
  423.             sqlQueryObject.addWhereCondition(tabella + ".id=?");
  424.             if(idPorta !=null) {
  425.                 sqlQueryObject.addWhereCondition(tabella + ".id_porta=?");
  426.             }
  427.             sqlQueryObject.addWhereCondition(tabella + ".tipologia=?");
  428.             sqlQueryObject.addWhereCondition(tabellaPlugin + ".tipo_plugin=?");
  429.             sqlQueryObject.setANDLogicOperator(true);
  430.             queryString = sqlQueryObject.createSQLQuery();
  431.             stmt = con.prepareStatement(queryString);
  432.             int index = 1;
  433.            
  434.             stmt.setLong(index++, idHandler);
  435.             if(idPorta!=null) {
  436.                 stmt.setLong(index++, idPorta);
  437.             }
  438.             stmt.setString(index++, tipologia);
  439.             stmt.setString(index++, tipoPlugin.getValue());
  440.             risultato = stmt.executeQuery();

  441.            
  442.             while (risultato.next()) {
  443.                 ConfigurazioneHandler handler = new ConfigurazioneHandler();
  444.                 handler.setId(risultato.getLong("id"));
  445.                 handler.setPosizione(risultato.getInt("posizione"));
  446.                 handler.setStato(getEnumStatoFunzionalita(risultato.getString("stato")));
  447.                 handler.setTipo(risultato.getString("tipo"));
  448.                
  449.                 Plugin plugin = PluginsDriverUtils.getPlugin(tipoPlugin.getValue(), handler.getTipo(), true, con, log, tipoDB);
  450.                
  451.                 bean = new ConfigurazioneHandlerBean(handler, plugin);
  452.            
  453.             }
  454.             risultato.close();
  455.             stmt.close();
  456.            
  457.             return bean;

  458.         } catch (Exception se) {
  459.             throw new ServiceException("[" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  460.         } finally {

  461.             //Chiudo statement and resultset
  462.             try{
  463.                 if(risultato!=null)
  464.                     risultato.close();
  465.             }catch (Exception e) {
  466.                 //ignore
  467.             }
  468.             try{
  469.                 if(stmt!=null)
  470.                     stmt.close();
  471.             }catch (Exception e) {
  472.                 //ignore
  473.             }
  474.         }
  475.     }
  476.    
  477.     private static StatoFunzionalita getEnumStatoFunzionalita(String value){
  478.         if(value==null){
  479.             return null;
  480.         }
  481.         else{
  482.             return StatoFunzionalita.toEnumConstant(value);
  483.         }
  484.     }
  485. }