DriverConfigurazioneDBSoggettiSearch.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.config.driver.db;

  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.DBUtils;
  27. import org.openspcoop2.core.commons.Filtri;
  28. import org.openspcoop2.core.commons.ISearch;
  29. import org.openspcoop2.core.commons.Liste;
  30. import org.openspcoop2.core.commons.SearchUtils;
  31. import org.openspcoop2.core.config.Soggetto;
  32. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  33. import org.openspcoop2.core.constants.CostantiDB;
  34. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  35. import org.openspcoop2.utils.sql.ISQLQueryObject;
  36. import org.openspcoop2.utils.sql.SQLObjectFactory;

  37. /**
  38.  * DriverConfigurazioneDB_soggettiSearchDriver
  39.  *
  40.  *
  41.  * @author Sandra Giangrandi (sandra@link.it)
  42.  * @author Stefano Corallo (corallo@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class DriverConfigurazioneDBSoggettiSearch {

  47.     private DriverConfigurazioneDB driver = null;
  48.    
  49.     protected DriverConfigurazioneDBSoggettiSearch(DriverConfigurazioneDB driver) {
  50.         this.driver = driver;
  51.     }
  52.    
  53.     private String getPrefixError(String nomeMetodo) {
  54.         return "[DriverConfigurazioneDB::" + nomeMetodo + "] ";
  55.     }
  56.    
  57.     protected List<Soggetto> soggettiList(String superuser, ISearch ricerca) throws DriverConfigurazioneException {
  58.         String nomeMetodo = "soggettiList";
  59.         int idLista = Liste.SOGGETTI;
  60.         int offset;
  61.         int limit;
  62.         String search;
  63.         String queryString;

  64.         limit = ricerca.getPageSize(idLista);
  65.         offset = ricerca.getIndexIniziale(idLista);
  66.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));

  67.         String filterProtocollo = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_PROTOCOLLO);
  68.         String filterProtocolli = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_PROTOCOLLI);
  69.         List<String> tipoSoggettiProtocollo = null;
  70.         try {
  71.             tipoSoggettiProtocollo = Filtri.convertToTipiSoggetti(filterProtocollo, filterProtocolli);
  72.         }catch(Exception e) {
  73.             throw new DriverConfigurazioneException(e.getMessage(),e);
  74.         }
  75.        
  76.         String filtroProprietaNome = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_PROPRIETA_NOME);
  77.         String filtroProprietaValore = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_PROPRIETA_VALORE);
  78.         if((filtroProprietaNome!=null && "".equals(filtroProprietaNome))) {
  79.             filtroProprietaNome=null;
  80.         }
  81.         if((filtroProprietaValore!=null && "".equals(filtroProprietaValore))) {
  82.             filtroProprietaValore=null;
  83.         }
  84.         boolean filtroProprieta = filtroProprietaNome!=null || filtroProprietaValore!=null;

  85.         this.driver.logDebug("search : " + search);
  86.         this.driver.logDebug("filterProtocollo : " + filterProtocollo);
  87.         this.driver.logDebug("filterProtocolli : " + filterProtocolli);
  88.         this.driver.logDebug("filtroProprietaNome : " + filtroProprietaNome);
  89.         this.driver.logDebug("filtroProprietaValore : " + filtroProprietaValore);
  90.        
  91.         Connection con = null;
  92.         PreparedStatement stmt=null;
  93.         ResultSet risultato=null;
  94.         ArrayList<Soggetto> lista = new ArrayList<>();

  95.         if (this.driver.atomica) {
  96.             try {
  97.                 con = this.driver.getConnectionFromDatasource("soggettiList");
  98.             } catch (Exception e) {
  99.                 throw new DriverConfigurazioneException(getPrefixError(nomeMetodo)+"Exception accedendo al datasource :" + e.getMessage(),e);

  100.             }

  101.         } else
  102.             con = this.driver.globalConnection;

  103.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  104.         try {

  105.             if (!search.equals("")) {
  106.                 //query con search
  107.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  108.                 sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  109.                 sqlQueryObject.addSelectCountField("*", "cont");
  110.                 if(tipoSoggettiProtocollo!=null && !tipoSoggettiProtocollo.isEmpty()) {
  111.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO, true, tipoSoggettiProtocollo.toArray(new String[1]));
  112.                 }
  113.                 if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  114.                     sqlQueryObject.addWhereCondition(CostantiDB.SUPERUSER_COLUMN+"= ?");
  115.                 sqlQueryObject.addWhereLikeCondition(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO, search, true, true);
  116.                 if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  117.                     sqlQueryObject.setANDLogicOperator(true);
  118.                 if(filtroProprieta) {
  119.                     DBUtils.setFiltriProprietaSoggetto(sqlQueryObject, this.driver.tipoDB,
  120.                             filtroProprietaNome, filtroProprietaValore);
  121.                 }
  122.                 queryString = sqlQueryObject.createSQLQuery();
  123.             } else {
  124.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  125.                 sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  126.                 sqlQueryObject.addSelectCountField("*", "cont");
  127.                 if(tipoSoggettiProtocollo!=null && !tipoSoggettiProtocollo.isEmpty()) {
  128.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO, true, tipoSoggettiProtocollo.toArray(new String[1]));
  129.                 }
  130.                 if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  131.                     sqlQueryObject.addWhereCondition(CostantiDB.SUPERUSER_COLUMN+"= ?");
  132.                 if(filtroProprieta) {
  133.                     DBUtils.setFiltriProprietaSoggetto(sqlQueryObject, this.driver.tipoDB,
  134.                             filtroProprietaNome, filtroProprietaValore);
  135.                 }
  136.                 queryString = sqlQueryObject.createSQLQuery();
  137.             }
  138.             stmt = con.prepareStatement(queryString);
  139.             if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  140.                 stmt.setString(1, superuser);
  141.             risultato = stmt.executeQuery();
  142.             if (risultato.next())
  143.                 ricerca.setNumEntries(idLista,risultato.getInt(1));
  144.             risultato.close();
  145.             stmt.close();

  146.             // ricavo le entries
  147.             if (limit == 0) // con limit
  148.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  149.             if (!search.equals("")) { // con search
  150.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  151.                 sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  152.                 sqlQueryObject.addSelectField("id");
  153.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO);
  154.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO);
  155.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_DESCRIZIONE);
  156.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_IDENTIFICATIVO_PORTA);
  157.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_ROUTER);
  158.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_DEFAULT);
  159.                 if(tipoSoggettiProtocollo!=null && !tipoSoggettiProtocollo.isEmpty()) {
  160.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO, true, tipoSoggettiProtocollo.toArray(new String[1]));
  161.                 }
  162.                 if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  163.                     sqlQueryObject.addWhereCondition(CostantiDB.SUPERUSER_COLUMN+"= ?");
  164.                 if(filtroProprieta) {
  165.                     DBUtils.setFiltriProprietaSoggetto(sqlQueryObject, this.driver.tipoDB,
  166.                             filtroProprietaNome, filtroProprietaValore);
  167.                 }
  168.                 sqlQueryObject.addWhereLikeCondition(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO, search, true, true);
  169.                 if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  170.                     sqlQueryObject.setANDLogicOperator(true);
  171.                 sqlQueryObject.addOrderBy(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO);
  172.                 sqlQueryObject.addOrderBy(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO);
  173.                 sqlQueryObject.setSortType(true);
  174.                 sqlQueryObject.setLimit(limit);
  175.                 sqlQueryObject.setOffset(offset);
  176.                 queryString = sqlQueryObject.createSQLQuery();
  177.             } else {
  178.                 // senza search
  179.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  180.                 sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  181.                 sqlQueryObject.addSelectField("id");
  182.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO);
  183.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO);
  184.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_DESCRIZIONE);
  185.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_IDENTIFICATIVO_PORTA);
  186.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_ROUTER);
  187.                 sqlQueryObject.addSelectField(CostantiDB.SOGGETTI_COLUMN_DEFAULT);
  188.                 if(tipoSoggettiProtocollo!=null && !tipoSoggettiProtocollo.isEmpty()) {
  189.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO, true, tipoSoggettiProtocollo.toArray(new String[1]));
  190.                 }
  191.                 if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  192.                     sqlQueryObject.addWhereCondition(CostantiDB.SUPERUSER_COLUMN+"= ?");
  193.                 if(filtroProprieta) {
  194.                     DBUtils.setFiltriProprietaSoggetto(sqlQueryObject, this.driver.tipoDB,
  195.                             filtroProprietaNome, filtroProprietaValore);
  196.                 }
  197.                 sqlQueryObject.addOrderBy(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO);
  198.                 sqlQueryObject.addOrderBy(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO);
  199.                 sqlQueryObject.setSortType(true);
  200.                 sqlQueryObject.setLimit(limit);
  201.                 sqlQueryObject.setOffset(offset);
  202.                 queryString = sqlQueryObject.createSQLQuery();
  203.             }
  204.             stmt = con.prepareStatement(queryString);
  205.             if(this.driver.useSuperUser && superuser!=null && !superuser.equals(""))
  206.                 stmt.setString(1, superuser);
  207.             risultato = stmt.executeQuery();

  208.             Soggetto sog;
  209.             while (risultato.next()) {

  210.                 sog = new Soggetto();
  211.                 sog.setId(risultato.getLong("id"));
  212.                 sog.setNome(risultato.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO));
  213.                 sog.setTipo(risultato.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO));
  214.                 sog.setDescrizione(risultato.getString(CostantiDB.SOGGETTI_COLUMN_DESCRIZIONE));
  215.                 sog.setIdentificativoPorta(risultato.getString(CostantiDB.SOGGETTI_COLUMN_IDENTIFICATIVO_PORTA));
  216.                 sog.setRouter(risultato.getInt(CostantiDB.SOGGETTI_COLUMN_ROUTER) == CostantiDB.TRUE);
  217.                 sog.setDominioDefault(risultato.getInt(CostantiDB.SOGGETTI_COLUMN_DEFAULT) == CostantiDB.TRUE);
  218.                 lista.add(sog);
  219.             }

  220.             return lista;

  221.         } catch (Exception qe) {
  222.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo)+"Errore : " + qe.getMessage(),qe);
  223.         } finally {
  224.             //Chiudo statement and resultset
  225.             JDBCUtilities.closeResources(risultato, stmt);
  226.             this.driver.closeConnection(con);
  227.         }
  228.     }

  229.     protected List<Soggetto> soggettiWithServiziList(ISearch ricerca) throws DriverConfigurazioneException {
  230.         return soggettiWithServiziList(null,ricerca);
  231.     }
  232.     protected List<Soggetto> soggettiWithServiziList(String superuser,ISearch ricerca) throws DriverConfigurazioneException {
  233.         String nomeMetodo = "soggettiWithServiziList";
  234.         int idLista = Liste.SOGGETTI;
  235.         int offset;
  236.         int limit;
  237.         String queryString;

  238.         limit = ricerca.getPageSize(idLista);
  239.         offset = ricerca.getIndexIniziale(idLista);
  240.        
  241.         Connection con = null;
  242.         PreparedStatement stmt=null;
  243.         ResultSet risultato=null;
  244.         ArrayList<Soggetto> lista = new ArrayList<>();

  245.         if (this.driver.atomica) {
  246.             try {
  247.                 con = this.driver.getConnectionFromDatasource("soggettiWithServiziList");
  248.             } catch (Exception e) {
  249.                 throw new DriverConfigurazioneException(getPrefixError(nomeMetodo)+"Exception accedendo al datasource :" + e.getMessage(),e);

  250.             }

  251.         } else
  252.             con = this.driver.globalConnection;

  253.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  254.         try {

  255.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  256.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  257.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  258.             sqlQueryObject.addSelectCountField(this.driver.tabellaSoggetti+".id", "cont", true);
  259.             sqlQueryObject.addWhereCondition(this.driver.tabellaSoggetti+".id = "+CostantiDB.SERVIZI+".id_soggetto");
  260.             if(this.driver.useSuperUser && superuser!=null && (!"".equals(superuser))){
  261.                 sqlQueryObject.addWhereCondition(this.driver.tabellaSoggetti+".superuser=?");
  262.             }
  263.             sqlQueryObject.setANDLogicOperator(true);
  264.             queryString = sqlQueryObject.createSQLQuery();
  265.             stmt = con.prepareStatement(queryString);
  266.             if(this.driver.useSuperUser && superuser!=null && (!"".equals(superuser))){
  267.                 stmt.setString(1, superuser);
  268.             }
  269.             risultato = stmt.executeQuery();
  270.             if (risultato.next())
  271.                 ricerca.setNumEntries(idLista,risultato.getInt(1));
  272.             risultato.close();
  273.             stmt.close();

  274.             // ricavo le entries
  275.             if (limit == 0) // con limit
  276.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  277.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  278.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);
  279.             sqlQueryObject.addFromTable(CostantiDB.SERVIZI);
  280.             sqlQueryObject.setSelectDistinct(true);
  281.             sqlQueryObject.addSelectField(this.driver.tabellaSoggetti+".id");
  282.             sqlQueryObject.addSelectField(this.driver.tabellaSoggetti+".tipo_soggetto");
  283.             sqlQueryObject.addSelectField(this.driver.tabellaSoggetti+".nome_soggetto");
  284.             sqlQueryObject.addWhereCondition(this.driver.tabellaSoggetti+".id = "+CostantiDB.SERVIZI+".id_soggetto");
  285.             if(this.driver.useSuperUser && superuser!=null && (!"".equals(superuser))){
  286.                 sqlQueryObject.addWhereCondition(this.driver.tabellaSoggetti+".superuser=?");
  287.             }
  288.             sqlQueryObject.setANDLogicOperator(true);
  289.             sqlQueryObject.addOrderBy(this.driver.tabellaSoggetti+".tipo_soggetto");
  290.             sqlQueryObject.addOrderBy(this.driver.tabellaSoggetti+".nome_soggetto");
  291.             sqlQueryObject.setSortType(true);
  292.             sqlQueryObject.setLimit(limit);
  293.             sqlQueryObject.setOffset(offset);
  294.             queryString = sqlQueryObject.createSQLQuery();
  295.             stmt = con.prepareStatement(queryString);
  296.             if(this.driver.useSuperUser && superuser!=null && (!"".equals(superuser))){
  297.                 stmt.setString(1, superuser);
  298.             }
  299.             risultato = stmt.executeQuery();

  300.             Soggetto sog;
  301.             while (risultato.next()) {

  302.                 sog = new Soggetto();
  303.                 sog.setId(risultato.getLong("id"));
  304.                 sog.setNome(risultato.getString(CostantiDB.SOGGETTI_COLUMN_NOME_SOGGETTO));
  305.                 sog.setTipo(risultato.getString(CostantiDB.SOGGETTI_COLUMN_TIPO_SOGGETTO));
  306.                 lista.add(sog);
  307.             }

  308.             return lista;

  309.         } catch (Exception qe) {
  310.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo)+"Errore : " + qe.getMessage(),qe);
  311.         } finally {
  312.             //Chiudo statement and resultset
  313.             JDBCUtilities.closeResources(risultato, stmt);
  314.             this.driver.closeConnection(con);
  315.         }
  316.     }
  317.    
  318.    
  319.    
  320. }