DBMappingUtils.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.mapping;

  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.CoreException;
  27. import org.openspcoop2.core.commons.DBUtils;
  28. import org.openspcoop2.core.commons.ISearch;
  29. import org.openspcoop2.core.commons.Liste;
  30. import org.openspcoop2.core.constants.CostantiDB;
  31. import org.openspcoop2.core.id.IDPortaApplicativa;
  32. import org.openspcoop2.core.id.IDPortaDelegata;
  33. import org.openspcoop2.core.id.IDServizio;
  34. import org.openspcoop2.core.id.IDSoggetto;
  35. import org.openspcoop2.core.id.IdentificativiErogazione;
  36. import org.openspcoop2.core.id.IdentificativiFruizione;
  37. import org.openspcoop2.utils.sql.ISQLQueryObject;
  38. import org.openspcoop2.utils.sql.SQLObjectFactory;

  39. /**
  40.  * Funzioni di utilita utilizzate dai driver
  41.  *
  42.  * @author Andrea Poli (apoli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class DBMappingUtils {

  47.     // -- MAPPING EROGAZIONE

  48.     public static int countMappingErogazione(Connection con, String tipoDB) throws CoreException{
  49.         return _countMappingErogazione(con, tipoDB, CostantiDB.SOGGETTI);
  50.     }
  51.     public static int countMappingErogazione(Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  52.         return _countMappingErogazione(con, tipoDB, tabellaSoggetti);
  53.     }
  54.     private static int _countMappingErogazione(Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  55.         PreparedStatement stmt = null;
  56.         ResultSet rs = null;
  57.         try {

  58.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  59.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  60.             sqlQueryObject.addSelectCountField("id","mapping_count");
  61.             String queryString = sqlQueryObject.createSQLQuery();
  62.             stmt = con.prepareStatement(queryString);
  63.             rs = stmt.executeQuery();
  64.             if (rs.next()) {
  65.                 return rs.getInt("mapping_count");
  66.             }
  67.             else{
  68.                 return 0;
  69.             }
  70.            
  71.         }catch(Exception e){
  72.             throw new CoreException("createMappingErogazione error",e);
  73.         } finally {

  74.             //Chiudo statement and resultset
  75.             try{
  76.                 if(rs!=null) rs.close();
  77.             }catch (Exception e) {
  78.                 //ignore
  79.             }
  80.             try{
  81.                 if(stmt!=null) stmt.close();
  82.             }catch (Exception e) {
  83.                 //ignore
  84.             }

  85.         }
  86.     }
  87.    
  88.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  89.             IDServizio idServizio,
  90.             ISearch ricerca,boolean ricerca_useOffsetLimit,
  91.             boolean orderByDescrizione) throws CoreException{
  92.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  93.                 idServizio, DBUtils.getIdServizio(idServizio.getNome(), idServizio.getTipo(), idServizio.getVersione(),
  94.                         idServizio.getSoggettoErogatore().getNome(), idServizio.getSoggettoErogatore().getTipo(),
  95.                         con, tipoDB),
  96.                 ricerca, ricerca_useOffsetLimit,CostantiDB.SOGGETTI,
  97.                 orderByDescrizione);
  98.     }
  99.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  100.             IDServizio idServizio,
  101.             ISearch ricerca,boolean ricerca_useOffsetLimit,String tabellaSoggetti,
  102.             boolean orderByDescrizione) throws CoreException{
  103.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  104.                 idServizio, DBUtils.getIdServizio(idServizio.getNome(), idServizio.getTipo(), idServizio.getVersione(),
  105.                         idServizio.getSoggettoErogatore().getNome(), idServizio.getSoggettoErogatore().getTipo(),
  106.                         con, tipoDB),
  107.                 ricerca, ricerca_useOffsetLimit,tabellaSoggetti,
  108.                 orderByDescrizione);
  109.     }
  110.    
  111.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  112.             IDServizio idServizio,
  113.             boolean orderByDescrizione) throws CoreException{
  114.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  115.                 idServizio, DBUtils.getIdServizio(idServizio.getNome(), idServizio.getTipo(), idServizio.getVersione(),
  116.                         idServizio.getSoggettoErogatore().getNome(), idServizio.getSoggettoErogatore().getTipo(),
  117.                         con, tipoDB),
  118.                 null,false,CostantiDB.SOGGETTI,
  119.                 orderByDescrizione);
  120.     }
  121.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  122.             IDServizio idServizio,
  123.             String tabellaSoggetti,
  124.             boolean orderByDescrizione) throws CoreException{
  125.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  126.                 idServizio, DBUtils.getIdServizio(idServizio.getNome(), idServizio.getTipo(), idServizio.getVersione(),
  127.                         idServizio.getSoggettoErogatore().getNome(), idServizio.getSoggettoErogatore().getTipo(),
  128.                         con, tipoDB),
  129.                 null,false,tabellaSoggetti,
  130.                 orderByDescrizione);
  131.     }
  132.    
  133.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  134.             IDServizio idServizio, long idServizioAsLong,
  135.             ISearch ricerca,boolean ricerca_useOffsetLimit,
  136.             boolean orderByDescrizione) throws CoreException{
  137.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  138.                 idServizio, idServizioAsLong,
  139.                 ricerca, ricerca_useOffsetLimit,CostantiDB.SOGGETTI,
  140.                 orderByDescrizione);
  141.     }
  142.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  143.             IDServizio idServizio, long idServizioAsLong,
  144.             ISearch ricerca,boolean ricerca_useOffsetLimit,String tabellaSoggetti,
  145.             boolean orderByDescrizione) throws CoreException{
  146.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  147.                 idServizio, idServizioAsLong,
  148.                 ricerca, ricerca_useOffsetLimit,tabellaSoggetti,
  149.                 orderByDescrizione);
  150.     }
  151.    
  152.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  153.             IDServizio idServizio, long idServizioAsLong,
  154.             boolean orderByDescrizione) throws CoreException{
  155.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  156.                 idServizio, idServizioAsLong,
  157.                 null,false,CostantiDB.SOGGETTI,
  158.                 orderByDescrizione);
  159.     }
  160.     public static List<MappingErogazionePortaApplicativa> mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  161.             IDServizio idServizio, long idServizioAsLong,
  162.             String tabellaSoggetti,
  163.             boolean orderByDescrizione) throws CoreException{
  164.         return _mappingErogazionePortaApplicativaList(con, tipoDB,
  165.                 idServizio, idServizioAsLong,
  166.                 null,false,tabellaSoggetti,
  167.                 orderByDescrizione);
  168.     }
  169.    
  170.     private static List<MappingErogazionePortaApplicativa> _mappingErogazionePortaApplicativaList(Connection con, String tipoDB,
  171.             IDServizio idServizio, long idServizioAsLong,
  172.             ISearch ricerca,boolean ricerca_useOffsetLimit,String tabellaSoggetti,
  173.             boolean orderByDescrizione) throws CoreException{
  174.         int idLista = Liste.CONFIGURAZIONE_EROGAZIONE;
  175.         int offset = 0;
  176.         int limit = 0;
  177.         String search = "";
  178.         String queryString;

  179.         if(ricerca != null) {
  180.             if(ricerca_useOffsetLimit) {
  181.                 limit = ricerca.getPageSize(idLista);
  182.                 offset = ricerca.getIndexIniziale(idLista);
  183.             }
  184.             search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  185.         }
  186.        
  187.         PreparedStatement stmt = null;
  188.         ResultSet risultato = null;
  189.        
  190.         List<MappingErogazionePortaApplicativa> lista = new ArrayList<MappingErogazionePortaApplicativa>();
  191.         try {
  192.             if(ricerca != null) {
  193.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  194.                 sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  195.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  196.                 sqlQueryObject.addSelectCountField("*", "cont");
  197.                 sqlQueryObject.addWhereCondition(CostantiDB.MAPPING_EROGAZIONE_PA+".id_erogazione = ?");
  198.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id="+CostantiDB.MAPPING_EROGAZIONE_PA+".id_porta");
  199.                 if (!search.equals("")) {
  200.                     sqlQueryObject.addWhereCondition(false,
  201.                             sqlQueryObject.getWhereLikeCondition(CostantiDB.MAPPING_EROGAZIONE_PA+".nome", search, true, true));
  202.                             //sqlQueryObject.getWhereLikeCondition(CostantiDB.PORTE_APPLICATIVE +".nome_porta", search, true, true));
  203.                 }
  204.                
  205.                 sqlQueryObject.setANDLogicOperator(true);
  206.                 queryString = sqlQueryObject.createSQLQuery();
  207.                 stmt = con.prepareStatement(queryString);
  208.                 int index = 1;
  209.                 stmt.setLong(index++, idServizioAsLong);
  210.                
  211.                 risultato = stmt.executeQuery();
  212.                 if (risultato.next())
  213.                     ricerca.setNumEntries(idLista,risultato.getInt(1));
  214.                 risultato.close();
  215.                 stmt.close();
  216.             }
  217.            
  218.             // ricavo le entries
  219.             if (limit == 0) // con limit
  220.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  221.            
  222.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  223.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  224.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  225.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_EROGAZIONE_PA+".id_erogazione");
  226.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_EROGAZIONE_PA+".id_porta");
  227.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_EROGAZIONE_PA+".is_default");
  228.             sqlQueryObject.addSelectAliasField(CostantiDB.MAPPING_EROGAZIONE_PA,"nome","nomeMapping");
  229.             sqlQueryObject.addSelectAliasField(CostantiDB.MAPPING_EROGAZIONE_PA,"descrizione","descrizioneMapping");
  230.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_EROGAZIONE_PA+".id");
  231.             sqlQueryObject.addSelectField(CostantiDB.PORTE_APPLICATIVE+".nome_porta");
  232.             sqlQueryObject.addWhereCondition(CostantiDB.MAPPING_EROGAZIONE_PA+".id_erogazione = ?");
  233.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id="+CostantiDB.MAPPING_EROGAZIONE_PA+".id_porta");
  234.            
  235.             if (ricerca!=null && !search.equals("")) {
  236.                 sqlQueryObject.addWhereCondition(false,
  237.                         sqlQueryObject.getWhereLikeCondition(CostantiDB.MAPPING_EROGAZIONE_PA+".nome", search, true, true));
  238.                         //sqlQueryObject.getWhereLikeCondition(CostantiDB.PORTE_APPLICATIVE +".nome_porta", search, true, true));
  239.             }
  240.             sqlQueryObject.setANDLogicOperator(true);
  241.             sqlQueryObject.addOrderBy(CostantiDB.MAPPING_EROGAZIONE_PA+".is_default",true);
  242.             if(orderByDescrizione) {
  243.                 sqlQueryObject.addOrderBy("descrizioneMapping");
  244.             }
  245.             else {
  246.                 sqlQueryObject.addOrderBy("nomeMapping");
  247.             }
  248.             sqlQueryObject.setSortType(true);
  249.             if(ricerca != null) {
  250.                 sqlQueryObject.setLimit(limit);
  251.                 sqlQueryObject.setOffset(offset);
  252.             }
  253.             queryString = sqlQueryObject.createSQLQuery();
  254.            
  255.             stmt = con.prepareStatement(queryString);
  256.            
  257.             int index = 1;
  258.             stmt.setLong(index++, idServizioAsLong);
  259.            
  260.             risultato = stmt.executeQuery();
  261.            
  262.             MappingErogazionePortaApplicativa mapping = null;

  263.             while (risultato.next()) {

  264.                 mapping = new MappingErogazionePortaApplicativa();
  265.                
  266.                 Long id = risultato.getLong("id");
  267. //              Long idPorta = risultato.getLong("id_porta");
  268. //              Long idErogazione = risultato.getLong("id_erogazione");
  269.                 String nome = risultato.getString("nomeMapping");
  270.                 String descrizione = risultato.getString("descrizioneMapping");
  271.                 Integer isDefaultInt = risultato.getInt("is_default");
  272.                 boolean isDefault = isDefaultInt > 0 ;
  273.                
  274.                 mapping.setNome(nome);
  275.                 mapping.setDescrizione(descrizione);
  276.                 mapping.setTableId(id);
  277.                 mapping.setDefault(isDefault);
  278.                 // evitiamo una join utilizzo l'id che mi sono passato come parametro
  279.                 mapping.setIdServizio(idServizio);
  280.                
  281.                 IDPortaApplicativa idPortaApplicativa = new IDPortaApplicativa();
  282.                 String nomePorta = risultato.getString("nome_porta");
  283.                 idPortaApplicativa.setNome(nomePorta);
  284.                
  285.                 IdentificativiErogazione identificativiErogazione = new IdentificativiErogazione();
  286.                 identificativiErogazione.setIdServizio(idServizio);
  287.                 idPortaApplicativa.setIdentificativiErogazione(identificativiErogazione);
  288.                
  289.                 mapping.setIdPortaApplicativa(idPortaApplicativa);

  290.                 lista.add(mapping);

  291.             }
  292.            
  293.         }catch(Exception e){
  294.             throw new CoreException("mappingErogazionePortaApplicativaList error",e);
  295.         } finally {

  296.             //Chiudo statement and resultset
  297.             try{
  298.                 if(risultato!=null) risultato.close();
  299.             }catch (Exception e) {
  300.                 //ignore
  301.             }
  302.             try{
  303.                 if(stmt!=null) stmt.close();
  304.             }catch (Exception e) {
  305.                 //ignore
  306.             }

  307.         }
  308.        
  309.        
  310.         return lista;
  311.     }
  312.    
  313.    
  314.     public static void createMappingErogazione(String nome, String descrizione, boolean isDefault, IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,  
  315.             Connection con, String tipoDB) throws CoreException{
  316.         createMappingErogazione(nome, descrizione, isDefault, idServizio, idPortaApplicativa, con, tipoDB, CostantiDB.SOGGETTI);
  317.     }
  318.     public static void createMappingErogazione(String nome, String descrizione, boolean isDefault, IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  319.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  320.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  321.         if(idServizioLong<=0){
  322.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  323.         }
  324.         _createMappingErogazione(nome, descrizione, isDefault, idServizioLong, idPortaApplicativa, con, tipoDB, tabellaSoggetti);
  325.     }
  326.    
  327.     private static void _createMappingErogazione(String nome, String descrizione, boolean isDefault, long idServizioLong, IDPortaApplicativa idPortaApplicativa,
  328.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  329.         PreparedStatement stmt = null;

  330.         try {

  331.             if(idServizioLong<=0){
  332.                 throw new Exception("IdServizio non fornito");
  333.             }
  334.             long idPA = DBUtils.getIdPortaApplicativa(idPortaApplicativa.getNome(), con, tipoDB);
  335.             if(idPA<=0){
  336.                 throw new Exception("PortaApplicativa ["+idPortaApplicativa+"] non esistente");
  337.             }
  338.            
  339.             int isDefaultInt = isDefault ? 1 : 0;
  340.            
  341.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  342.             sqlQueryObject.addInsertTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  343.             sqlQueryObject.addInsertField("id_erogazione", "?");
  344.             sqlQueryObject.addInsertField("id_porta", "?");
  345.             sqlQueryObject.addInsertField("nome", "?");
  346.             sqlQueryObject.addInsertField("descrizione", "?");
  347.             sqlQueryObject.addInsertField("is_default", "?");
  348.             String queryString = sqlQueryObject.createSQLInsert();
  349.             stmt = con.prepareStatement(queryString);
  350.             int index = 1;
  351.             stmt.setLong(index++, idServizioLong);
  352.             stmt.setLong(index++, idPA);
  353.             stmt.setString(index++, nome);
  354.             stmt.setString(index++, descrizione);
  355.             stmt.setInt(index++, isDefaultInt);
  356.             stmt.executeUpdate();
  357.             stmt.close();
  358.            
  359.         }catch(Exception e){
  360.             throw new CoreException("Creazione Mapping Erogazione fallita: "+e.getMessage(),e);
  361.         } finally {

  362.             //Chiudo statement and resultset
  363.             try{
  364.                 if(stmt!=null) stmt.close();
  365.             }catch (Exception e) {
  366.                 //ignore
  367.             }

  368.         }
  369.     }
  370.    
  371.    
  372.     public static void updateMappingErogazione(long tableId, String descrizione, Connection con, String tipoDB) throws CoreException{
  373.         PreparedStatement stmt = null;

  374.         try {

  375.             if(tableId<=0){
  376.                 throw new Exception("TableId non fornito");
  377.             }
  378.            
  379.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  380.             sqlQueryObject.addUpdateTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  381.             sqlQueryObject.addUpdateField("descrizione", "?");
  382.             sqlQueryObject.addWhereCondition("id=?");
  383.             String queryString = sqlQueryObject.createSQLUpdate();
  384.             stmt = con.prepareStatement(queryString);
  385.             int index = 1;
  386.             stmt.setString(index++, descrizione);
  387.             stmt.setLong(index++, tableId);
  388.             stmt.executeUpdate();
  389.             stmt.close();
  390.            
  391.         }catch(Exception e){
  392.             throw new CoreException("updateMappingErogazione error",e);
  393.         } finally {

  394.             //Chiudo statement and resultset
  395.             try{
  396.                 if(stmt!=null) stmt.close();
  397.             }catch (Exception e) {
  398.                 //ignore
  399.             }

  400.         }
  401.     }
  402.    
  403.        
  404.     public static void deleteMappingErogazione(IDServizio idServizio,
  405.             Connection con, String tipoDB) throws CoreException{
  406.         deleteMappingErogazione(idServizio, null, con, tipoDB, CostantiDB.SOGGETTI);
  407.     }
  408.     public static void deleteMappingErogazione(IDServizio idServizio,
  409.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  410.         deleteMappingErogazione(idServizio, null, con, tipoDB, tabellaSoggetti);
  411.     }
  412.     public static void deleteMappingErogazione(IDServizio idServizio, boolean deletePorte,
  413.             Connection con, String tipoDB) throws CoreException{
  414.         deleteMappingErogazione(idServizio, deletePorte, con, tipoDB, CostantiDB.SOGGETTI);
  415.     }
  416.     public static void deleteMappingErogazione(IDServizio idServizio, boolean deletePorte,
  417.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  418.         List<IDPortaApplicativa> list = null;
  419.         if(deletePorte) {
  420.             list = getIDPorteApplicativeAssociate(idServizio, con, tipoDB);
  421.         }
  422.         deleteMappingErogazione(idServizio, null, con, tipoDB, tabellaSoggetti);
  423.         if(list!=null && list.size()>0) {
  424.             for (IDPortaApplicativa idPortaApplicativa : list) {
  425.                 PreparedStatement stmt = null;
  426.                 try {
  427.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  428.                     sqlQueryObject.addDeleteTable(CostantiDB.PORTE_APPLICATIVE);
  429.                     sqlQueryObject.addWhereCondition("id=?");
  430.                     sqlQueryObject.setANDLogicOperator(true);
  431.                     String queryString = sqlQueryObject.createSQLDelete();
  432.                     stmt = con.prepareStatement(queryString);
  433.                     stmt.setLong(1, DBUtils.getIdPortaApplicativa(idPortaApplicativa.getNome(), con, tipoDB));
  434.                     stmt.executeUpdate();
  435.                     stmt.close();
  436.                 }catch(Exception e){
  437.                     throw new CoreException("deletePAMappingErogazione error",e);
  438.                 } finally {

  439.                     //Chiudo statement and resultset
  440.                     try{
  441.                         if(stmt!=null) stmt.close();
  442.                     }catch (Exception e) {
  443.                         //ignore
  444.                     }

  445.                 }
  446.             }
  447.         }
  448.     }
  449.     public static void deleteMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  450.             Connection con, String tipoDB) throws CoreException{
  451.         deleteMappingErogazione(idServizio, idPortaApplicativa, con, tipoDB, CostantiDB.SOGGETTI);
  452.     }
  453.     public static void deleteMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  454.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  455.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  456.         if(idServizioLong<=0){
  457.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  458.         }
  459.         _deleteMappingErogazione(idServizioLong, idPortaApplicativa, con, tipoDB, tabellaSoggetti);
  460.     }
  461.    
  462.     private static void _deleteMappingErogazione(long idServizioLong, IDPortaApplicativa idPortaApplicativa,
  463.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  464.         PreparedStatement stmt = null;

  465.         try {

  466.             if(idServizioLong<=0){
  467.                 throw new Exception("IdServizio non fornito");
  468.             }
  469.             long idPA = -1;
  470.             if(idPortaApplicativa!=null){
  471.                 idPA = DBUtils.getIdPortaApplicativa(idPortaApplicativa.getNome(), con, tipoDB);
  472.                 if(idPA<=0){
  473.                     throw new Exception("PortaApplicativa ["+idPortaApplicativa+"] non esistente");
  474.                 }
  475.             }
  476.            
  477.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  478.             sqlQueryObject.addDeleteTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  479.             sqlQueryObject.addWhereCondition("id_erogazione=?");
  480.             if(idPortaApplicativa!=null){
  481.                 sqlQueryObject.addWhereCondition("id_porta=?");
  482.             }
  483.             sqlQueryObject.setANDLogicOperator(true);
  484.             String queryString = sqlQueryObject.createSQLDelete();
  485.             stmt = con.prepareStatement(queryString);
  486.             stmt.setLong(1, idServizioLong);
  487.             if(idPortaApplicativa!=null){
  488.                 stmt.setLong(2, idPA);
  489.             }
  490.             stmt.executeUpdate();
  491.             stmt.close();
  492.            
  493.         }catch(Exception e){
  494.             throw new CoreException("deleteMappingErogazione error",e);
  495.         } finally {

  496.             //Chiudo statement and resultset
  497.             try{
  498.                 if(stmt!=null) stmt.close();
  499.             }catch (Exception e) {
  500.                 //ignore
  501.             }

  502.         }
  503.     }
  504.        

  505.     public static IDPortaApplicativa getIDPortaApplicativaAssociataDefault(IDServizio idServizio,
  506.             Connection con, String tipoDB) throws CoreException {
  507.         return getIDPortaApplicativaAssociataDefault(idServizio, con, tipoDB, CostantiDB.SOGGETTI);
  508.     }
  509.     public static IDPortaApplicativa getIDPortaApplicativaAssociataDefault(IDServizio idServizio,
  510.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  511.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  512.         if(idServizioLong<=0){
  513.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  514.         }
  515.         List<IDPortaApplicativa> l = _getIDPorteApplicativeAssociate(idServizioLong, true, null, con, tipoDB, tabellaSoggetti,
  516.                 idServizio);
  517.         if(l!=null && l.size()>0) {
  518.             if(l.size()>1) {
  519.                 throw new CoreException("Esiste più di un mapping di default per l'erogazione del servizio ["+idServizio.toString()+"]");
  520.             }
  521.             return l.get(0);
  522.         }
  523.         return null;
  524.     }
  525.    
  526.     public static IDPortaApplicativa getIDPortaApplicativaAssociataAzione(IDServizio idServizio,
  527.             Connection con, String tipoDB) throws CoreException {
  528.         return getIDPortaApplicativaAssociataAzione(idServizio, con, tipoDB, CostantiDB.SOGGETTI);
  529.     }
  530.     public static IDPortaApplicativa getIDPortaApplicativaAssociataAzione(IDServizio idServizio,
  531.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  532.         if(idServizio.getAzione()==null) {
  533.             throw new CoreException("Azione non indicata nel parametro 'idServizio'");
  534.         }
  535.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  536.         if(idServizioLong<=0){
  537.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  538.         }
  539.         List<IDPortaApplicativa> l = _getIDPorteApplicativeAssociate(idServizioLong, false, idServizio.getAzione(), con, tipoDB, tabellaSoggetti,
  540.                 idServizio);
  541.         if(l!=null && l.size()>0) {
  542.             if(l.size()>1) {
  543.                 throw new CoreException("Esiste più di un mapping per l'erogazione dell'azione '"+idServizio.getAzione()+"' del servizio ["+idServizio.toString()+"]");
  544.             }
  545.             return l.get(0);
  546.         }
  547.         return null;
  548.     }
  549.    
  550.     public static List<IDPortaApplicativa> getIDPorteApplicativeAssociate(IDServizio idServizio,
  551.             Connection con, String tipoDB) throws CoreException {
  552.         return getIDPorteApplicativeAssociate(idServizio, con, tipoDB, CostantiDB.SOGGETTI);
  553.     }
  554.     public static List<IDPortaApplicativa> getIDPorteApplicativeAssociate(IDServizio idServizio,
  555.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  556.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  557.         if(idServizioLong<=0){
  558.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  559.         }
  560.         return _getIDPorteApplicativeAssociate(idServizioLong, false, null, con, tipoDB, tabellaSoggetti,
  561.                 idServizio);
  562.     }
  563.    
  564.     private static List<IDPortaApplicativa> _getIDPorteApplicativeAssociate(long idServizioLong, boolean defaultMapping, String azioneMapping,
  565.             Connection con, String tipoDB,String tabellaSoggetti,
  566.             IDServizio idServizio) throws CoreException {
  567.         PreparedStatement stm = null;
  568.         ResultSet rs = null;

  569.         try {

  570.             if(idServizioLong<=0){
  571.                 throw new Exception("IdServizio non fornito");
  572.             }

  573.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  574.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  575.             sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  576.             sqlQueryObject.addSelectField(CostantiDB.PORTE_APPLICATIVE+".nome_porta");
  577.             sqlQueryObject.addWhereCondition("id_erogazione = ?");
  578.             if(defaultMapping) {
  579.                 sqlQueryObject.addWhereCondition("is_default = ?");
  580.             }
  581.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id="+CostantiDB.MAPPING_EROGAZIONE_PA+".id_porta");
  582.             if(azioneMapping!=null) {
  583.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_AZIONI);
  584.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id="+CostantiDB.PORTE_APPLICATIVE_AZIONI+".id_porta");
  585.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_AZIONI+".azione = ?");
  586.             }
  587.             sqlQueryObject.setANDLogicOperator(true);
  588.             String sqlQuery = sqlQueryObject.createSQLQuery();
  589.             stm = con.prepareStatement(sqlQuery);
  590.             int indexStmt = 1;
  591.             stm.setLong(indexStmt++,idServizioLong);
  592.             if(defaultMapping) {
  593.                 stm.setInt(indexStmt++,1);
  594.             }
  595.             if(azioneMapping!=null) {
  596.                 stm.setString(indexStmt++,azioneMapping);
  597.             }
  598.        
  599.             rs = stm.executeQuery();
  600.             List<IDPortaApplicativa> list = new ArrayList<IDPortaApplicativa>();
  601.             while (rs.next()) {
  602.                 IDPortaApplicativa idPA = new IDPortaApplicativa();
  603.                 String nome = rs.getString("nome_porta");
  604.                 idPA.setNome(nome);
  605.                
  606.                 IdentificativiErogazione identificativiErogazione = new IdentificativiErogazione();
  607.                 identificativiErogazione.setIdServizio(idServizio);
  608.                 idPA.setIdentificativiErogazione(identificativiErogazione);
  609.                
  610.                 list.add(idPA);
  611.             }
  612.             if(list.size()<=0){
  613.                 return null;
  614.             }
  615.             else {
  616.                 return list;
  617.             }
  618.         }catch(CoreException de){
  619.             throw de;
  620.         }
  621.         catch(Exception e){
  622.             throw new CoreException("getIDPortaApplicativaAssociata error",e);
  623.         } finally {

  624.             //Chiudo statement and resultset
  625.             try{
  626.                 if(rs!=null) rs.close();
  627.             }catch (Exception e) {
  628.                 //ignore
  629.             }
  630.             try{
  631.                 if(stm!=null) stm.close();
  632.             }catch (Exception e) {
  633.                 //ignore
  634.             }

  635.         }
  636.     }
  637.    
  638.     public static boolean existsIDPortaApplicativaAssociataDefault(IDServizio idServizio,
  639.             Connection con, String tipoDB) throws CoreException {
  640.         return existsIDPortaApplicativaAssociataDefault(idServizio, con, tipoDB, CostantiDB.SOGGETTI);
  641.     }
  642.     public static boolean existsIDPortaApplicativaAssociataDefault(IDServizio idServizio,
  643.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  644.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  645.         if(idServizioLong<=0){
  646.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  647.         }
  648.         int c = _countIDPorteApplicativeAssociate(idServizioLong, true, null, con, tipoDB, tabellaSoggetti);
  649.         if(c<=0) {
  650.             return false;
  651.         }
  652.         else {
  653.             if(c>1) {
  654.                 throw new CoreException("Esiste più di un mapping di default per l'erogazione del servizio ["+idServizio.toString()+"]");
  655.             }
  656.             return true;
  657.         }
  658.     }
  659.    
  660.     public static boolean existsIDPortaApplicativaAssociataAzione(IDServizio idServizio,
  661.             Connection con, String tipoDB) throws CoreException {
  662.         return existsIDPortaApplicativaAssociataAzione(idServizio, con, tipoDB, CostantiDB.SOGGETTI);
  663.     }
  664.     public static boolean existsIDPortaApplicativaAssociataAzione(IDServizio idServizio,
  665.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  666.         if(idServizio.getAzione()==null) {
  667.             throw new CoreException("Azione non indicata nel parametro 'idServizio'");
  668.         }
  669.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  670.         if(idServizioLong<=0){
  671.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  672.         }
  673.         int c = _countIDPorteApplicativeAssociate(idServizioLong, false, idServizio.getAzione(), con, tipoDB, tabellaSoggetti);
  674.         if(c<=0) {
  675.             return false;
  676.         }
  677.         else {
  678.             if(c>1) {
  679.                 throw new CoreException("Esiste più di un mapping per l'erogazione dell'azione '"+idServizio.getAzione()+"' del servizio ["+idServizio.toString()+"]");
  680.             }
  681.             return true;
  682.         }
  683.     }
  684.    
  685.     public static boolean existsIDPorteApplicativeAssociate(IDServizio idServizio,
  686.             Connection con, String tipoDB) throws CoreException {
  687.         return existsIDPorteApplicativeAssociate(idServizio, con, tipoDB, CostantiDB.SOGGETTI);
  688.     }
  689.     public static boolean existsIDPorteApplicativeAssociate(IDServizio idServizio,
  690.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  691.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  692.         if(idServizioLong<=0){
  693.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  694.         }
  695.         return _countIDPorteApplicativeAssociate(idServizioLong, false, null, con, tipoDB, tabellaSoggetti)>0;
  696.     }
  697.    
  698.     private static int _countIDPorteApplicativeAssociate(long idServizioLong, boolean defaultMapping, String azioneMapping,
  699.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  700.         PreparedStatement stm = null;
  701.         ResultSet rs = null;

  702.         try {

  703.             if(idServizioLong<=0){
  704.                 throw new Exception("IdServizio non fornito");
  705.             }

  706.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  707.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  708.             sqlQueryObject.addWhereCondition("id_erogazione = ?");
  709.             if(defaultMapping) {
  710.                 sqlQueryObject.addWhereCondition("is_default = ?");
  711.             }
  712.             if(azioneMapping!=null) {
  713.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE);
  714.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_APPLICATIVE_AZIONI);
  715.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id="+CostantiDB.MAPPING_EROGAZIONE_PA+".id_porta");
  716.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE+".id="+CostantiDB.PORTE_APPLICATIVE_AZIONI+".id_porta");
  717.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_APPLICATIVE_AZIONI+".azione = ?");
  718.             }
  719.             sqlQueryObject.setANDLogicOperator(true);
  720.             String sqlQuery = sqlQueryObject.createSQLQuery();
  721.             stm = con.prepareStatement(sqlQuery);
  722.             int indexStmt = 1;
  723.             stm.setLong(indexStmt++,idServizioLong);
  724.             if(defaultMapping) {
  725.                 stm.setInt(indexStmt++,1);
  726.             }
  727.             if(azioneMapping!=null) {
  728.                 stm.setString(indexStmt++,azioneMapping);
  729.             }
  730.        
  731.             rs = stm.executeQuery();
  732.             int c = 0;
  733.             while(rs.next()) {
  734.                 c++;
  735.             }
  736.             return c;
  737.         }catch(CoreException de){
  738.             throw de;
  739.         }
  740.         catch(Exception e){
  741.             throw new CoreException("existsIDPortaApplicativaAssociata error",e);
  742.         } finally {

  743.             //Chiudo statement and resultset
  744.             try{
  745.                 if(rs!=null) rs.close();
  746.             }catch (Exception e) {
  747.                 //ignore
  748.             }
  749.             try{
  750.                 if(stm!=null) stm.close();
  751.             }catch (Exception e) {
  752.                 //ignore
  753.             }

  754.         }
  755.     }
  756.    
  757.    
  758.    

  759.    
  760.     public static boolean existsMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  761.             Connection con, String tipoDB) throws CoreException {
  762.         return existsMappingErogazione(idServizio, idPortaApplicativa, con, tipoDB, CostantiDB.SOGGETTI);
  763.     }
  764.     public static boolean existsMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  765.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  766.         try {
  767.             long id = getTableIdMappingErogazione(idServizio, idPortaApplicativa, con, tipoDB);
  768.             return id>0;
  769.         }catch(Exception e) {
  770.             return false;
  771.         }
  772.     }
  773.    
  774.    
  775.    
  776.    
  777.     public static long getTableIdMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  778.             Connection con, String tipoDB) throws CoreException {
  779.         return getTableIdMappingErogazione(idServizio, idPortaApplicativa, con, tipoDB, CostantiDB.SOGGETTI);
  780.     }
  781.     public static long getTableIdMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  782.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  783.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  784.         if(idServizioLong<=0){
  785.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  786.         }
  787.         return _getTableIdMappingErogazione(idServizioLong, idPortaApplicativa, con, tipoDB, tabellaSoggetti);
  788.     }
  789.    
  790.     private static long _getTableIdMappingErogazione(long idServizioLong, IDPortaApplicativa idPortaApplicativa,
  791.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  792.         if(idServizioLong<=0){
  793.             throw new CoreException("IdServizio non fornito");
  794.         }
  795.         long idPA = DBUtils.getIdPortaApplicativa(idPortaApplicativa.getNome(), con, tipoDB);
  796.         if(idPA<=0){
  797.             throw new CoreException("PortaApplicativa ["+idPortaApplicativa+"] non esistente");
  798.         }

  799.         PreparedStatement stm = null;
  800.         ResultSet rs = null;

  801.         try {

  802.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  803.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  804.             sqlQueryObject.addWhereCondition("id_erogazione=?");
  805.             sqlQueryObject.addWhereCondition("id_porta=?");
  806.             sqlQueryObject.setANDLogicOperator(true);
  807.             String queryString = sqlQueryObject.createSQLQuery();
  808.             stm = con.prepareStatement(queryString);
  809.             stm.setLong(1, idServizioLong);
  810.             stm.setLong(2, idPA);
  811.             rs = stm.executeQuery();
  812.             if (rs.next()) {
  813.                 return rs.getLong("id");
  814.             }
  815.             else{
  816.                 throw new CoreException("Mapping tra PA (id:"+idPA+") e servizio (id:"+idServizioLong+") non esistente");
  817.             }
  818.         }catch(CoreException de){
  819.             throw de;
  820.         }
  821.         catch(Exception e){
  822.             throw new CoreException("getIDPortaApplicativaAssociata error",e);
  823.         } finally {

  824.             //Chiudo statement and resultset
  825.             try{
  826.                 if(rs!=null) rs.close();
  827.             }catch (Exception e) {
  828.                 //ignore
  829.             }
  830.             try{
  831.                 if(stm!=null) stm.close();
  832.             }catch (Exception e) {
  833.                 //ignore
  834.             }

  835.         }
  836.     }
  837.    
  838.     public static boolean checkMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa, boolean isDefault,
  839.             Connection con, String tipoDB) throws CoreException {
  840.         return checkMappingErogazione(idServizio, idPortaApplicativa, isDefault, con, tipoDB, CostantiDB.SOGGETTI);
  841.     }
  842.     public static boolean checkMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa, boolean isDefault,
  843.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  844.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  845.         if(idServizioLong<=0){
  846.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  847.         }
  848.         return _checkMappingErogazione(idServizioLong, idPortaApplicativa, isDefault, con, tipoDB, tabellaSoggetti);
  849.     }
  850.    
  851.     private static boolean _checkMappingErogazione(long idServizioLong, IDPortaApplicativa idPortaApplicativa, boolean isDefault,
  852.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  853.         PreparedStatement stm = null;
  854.         ResultSet rs = null;

  855.         try {

  856.             if(idServizioLong<=0){
  857.                 throw new Exception("IdServizio non fornito");
  858.             }
  859.             long idPA = DBUtils.getIdPortaApplicativa(idPortaApplicativa.getNome(), con, tipoDB);
  860.             if(idPA<=0){
  861.                 throw new Exception("PortaApplicativa ["+idPortaApplicativa+"] non esistente");
  862.             }
  863.            
  864.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  865.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  866.             sqlQueryObject.addSelectCountField("id","mapping_count");
  867.             sqlQueryObject.addWhereCondition("id_erogazione=?");
  868.             sqlQueryObject.addWhereCondition("id_porta=?");
  869.             sqlQueryObject.addWhereCondition("is_default = ?");
  870.             sqlQueryObject.setANDLogicOperator(true);
  871.             String queryString = sqlQueryObject.createSQLQuery();
  872.             stm = con.prepareStatement(queryString);
  873.             stm.setLong(1, idServizioLong);
  874.             stm.setLong(2, idPA);
  875.             stm.setLong(3, (isDefault ? 1 : 0));
  876.             rs = stm.executeQuery();
  877.             int found = 0;
  878.             if (rs.next()) {
  879.                 found = rs.getInt("mapping_count");
  880.             }
  881.            
  882.             return found > 0;
  883.         }catch(CoreException de){
  884.             throw de;
  885.         }
  886.         catch(Exception e){
  887.             throw new CoreException("_checkIsDefaultMappingErogazione error",e);
  888.         } finally {

  889.             //Chiudo statement and resultset
  890.             try{
  891.                 if(rs!=null) rs.close();
  892.             }catch (Exception e) {
  893.                 //ignore
  894.             }
  895.             try{
  896.                 if(stm!=null) stm.close();
  897.             }catch (Exception e) {
  898.                 //ignore
  899.             }
  900.         }
  901.     }
  902.    
  903.    
  904.     public static MappingErogazionePortaApplicativa getMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  905.             Connection con, String tipoDB) throws CoreException {
  906.         return getMappingErogazione(idServizio, idPortaApplicativa, con, tipoDB, CostantiDB.SOGGETTI);
  907.     }
  908.     public static MappingErogazionePortaApplicativa getMappingErogazione(IDServizio idServizio, IDPortaApplicativa idPortaApplicativa,
  909.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  910.         long idServizioLong = DBUtils.getIdAccordoServizioParteSpecifica(idServizio, con, tipoDB);
  911.         if(idServizioLong<=0){
  912.             throw new CoreException("Servizio ["+idServizio.toString()+"] non esistente");
  913.         }
  914.        
  915.         long idPA = DBUtils.getIdPortaApplicativa(idPortaApplicativa.getNome(), con, tipoDB);
  916.         if(idPA<=0){
  917.             throw new CoreException("PortaApplicativa ["+idPortaApplicativa+"] non esistente");
  918.         }
  919.        
  920.         MappingErogazionePortaApplicativa mapping = _getMappingErogazione(idServizioLong, idPA, con, tipoDB, tabellaSoggetti);
  921.         mapping.setIdPortaApplicativa(idPortaApplicativa);
  922.         mapping.setIdServizio(idServizio);
  923.         return mapping;
  924.     }
  925.     private static MappingErogazionePortaApplicativa _getMappingErogazione(long idServizioLong, long idPA,
  926.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  927.         PreparedStatement stm = null;
  928.         ResultSet rs = null;

  929.         try {

  930.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  931.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_EROGAZIONE_PA);
  932.             sqlQueryObject.addWhereCondition("id_erogazione=?");
  933.             sqlQueryObject.addWhereCondition("id_porta=?");
  934.             sqlQueryObject.setANDLogicOperator(true);
  935.             String queryString = sqlQueryObject.createSQLQuery();
  936.             stm = con.prepareStatement(queryString);
  937.             stm.setLong(1, idServizioLong);
  938.             stm.setLong(2, idPA);
  939.             rs = stm.executeQuery();
  940.             if (rs.next()) {
  941.                 MappingErogazionePortaApplicativa mapping = new MappingErogazionePortaApplicativa();
  942.                 int isDefault = rs.getInt("is_default");
  943.                 mapping.setDefault(isDefault == 1);
  944.                
  945.                 mapping.setTableId(rs.getLong("id"));
  946.                
  947.                 mapping.setNome(rs.getString("nome"));
  948.                 mapping.setDescrizione(rs.getString("descrizione"));
  949.                
  950.                 return mapping;
  951.             }
  952.             else{
  953.                 throw new CoreException("Mapping tra PA (id:"+idPA+") e servizio (id:"+idServizioLong+") non esistente");
  954.             }
  955.         }catch(CoreException de){
  956.             throw de;
  957.         }
  958.         catch(Exception e){
  959.             throw new CoreException("getMappingErogazione error",e);
  960.         } finally {

  961.             //Chiudo statement and resultset
  962.             try{
  963.                 if(rs!=null) rs.close();
  964.             }catch (Exception e) {
  965.                 //ignore
  966.             }
  967.             try{
  968.                 if(stm!=null) stm.close();
  969.             }catch (Exception e) {
  970.                 //ignore
  971.             }

  972.         }
  973.     }
  974.    
  975.    
  976.    
  977.    
  978.    
  979.     // -- MAPPING FRUIZIONE
  980.    
  981.     public static int countMappingFruizione(Connection con, String tipoDB) throws CoreException{
  982.         return _countMappingFruizione(con, tipoDB, CostantiDB.SOGGETTI);
  983.     }
  984.     public static int countMappingFruizione(Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  985.         return _countMappingFruizione(con, tipoDB, tabellaSoggetti);
  986.     }
  987.     private static int _countMappingFruizione(Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  988.         PreparedStatement stmt = null;
  989.         ResultSet rs = null;
  990.         try {

  991.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  992.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  993.             sqlQueryObject.addSelectCountField("id","mapping_count");
  994.             String queryString = sqlQueryObject.createSQLQuery();
  995.             stmt = con.prepareStatement(queryString);
  996.             rs = stmt.executeQuery();
  997.             if (rs.next()) {
  998.                 return rs.getInt("mapping_count");
  999.             }
  1000.             else{
  1001.                 return 0;
  1002.             }
  1003.            
  1004.         }catch(Exception e){
  1005.             throw new CoreException("createMappingFruizione error",e);
  1006.         } finally {

  1007.             //Chiudo statement and resultset
  1008.             try{
  1009.                 if(rs!=null) rs.close();
  1010.             }catch (Exception e) {
  1011.                 //ignore
  1012.             }
  1013.             try{
  1014.                 if(stmt!=null) stmt.close();
  1015.             }catch (Exception e) {
  1016.                 //ignore
  1017.             }

  1018.         }
  1019.     }
  1020.    
  1021.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1022.             long idFruizione,
  1023.             IDSoggetto idSoggettoFruitore,
  1024.             IDServizio idServizio,
  1025.             ISearch ricerca,boolean ricerca_useOffsetLimit,
  1026.             boolean orderByDescrizione) throws CoreException {
  1027.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1028.                 idFruizione,
  1029.                 idSoggettoFruitore,
  1030.                 idServizio,
  1031.                 ricerca, ricerca_useOffsetLimit, CostantiDB.SOGGETTI,
  1032.                 orderByDescrizione);
  1033.     }
  1034.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1035.             long idFruizione,
  1036.             IDSoggetto idSoggettoFruitore,
  1037.             IDServizio idServizio,
  1038.             ISearch ricerca,boolean ricerca_useOffsetLimit,String tabellaSoggetti,
  1039.             boolean orderByDescrizione) throws CoreException {
  1040.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1041.                 idFruizione,
  1042.                 idSoggettoFruitore,
  1043.                 idServizio,
  1044.                 ricerca, ricerca_useOffsetLimit, tabellaSoggetti,
  1045.                 orderByDescrizione);
  1046.     }
  1047.    
  1048.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1049.             long idFruizione,
  1050.             IDSoggetto idSoggettoFruitore,
  1051.             IDServizio idServizio,
  1052.             boolean orderByDescrizione) throws CoreException {
  1053.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1054.                 idFruizione,
  1055.                 idSoggettoFruitore,
  1056.                 idServizio,
  1057.                 null, false, CostantiDB.SOGGETTI,
  1058.                 orderByDescrizione);
  1059.     }
  1060.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1061.             long idFruizione,
  1062.             IDSoggetto idSoggettoFruitore,
  1063.             IDServizio idServizio,
  1064.             String tabellaSoggetti,
  1065.             boolean orderByDescrizione) throws CoreException {
  1066.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1067.                 idFruizione,
  1068.                 idSoggettoFruitore,
  1069.                 idServizio,
  1070.                 null, false, tabellaSoggetti,
  1071.                 orderByDescrizione);
  1072.     }
  1073.    
  1074.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1075.             IDSoggetto idSoggettoFruitore,
  1076.             IDServizio idServizio,
  1077.             ISearch ricerca,boolean ricerca_useOffsetLimit,
  1078.             boolean orderByDescrizione) throws CoreException {
  1079.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1080.                 DBUtils.getIdFruizioneServizio(idServizio, idSoggettoFruitore, con, tipoDB),
  1081.                 idSoggettoFruitore,
  1082.                 idServizio,
  1083.                 ricerca, ricerca_useOffsetLimit, CostantiDB.SOGGETTI,
  1084.                 orderByDescrizione);
  1085.     }
  1086.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1087.             IDSoggetto idSoggettoFruitore,
  1088.             IDServizio idServizio,
  1089.             ISearch ricerca,boolean ricerca_useOffsetLimit,String tabellaSoggetti,
  1090.             boolean orderByDescrizione) throws CoreException {
  1091.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1092.                 DBUtils.getIdFruizioneServizio(idServizio, idSoggettoFruitore, con, tipoDB),
  1093.                 idSoggettoFruitore,
  1094.                 idServizio,
  1095.                 ricerca, ricerca_useOffsetLimit, tabellaSoggetti,
  1096.                 orderByDescrizione);
  1097.     }
  1098.    
  1099.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1100.             IDSoggetto idSoggettoFruitore,
  1101.             IDServizio idServizio,
  1102.             boolean orderByDescrizione) throws CoreException {
  1103.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1104.                 DBUtils.getIdFruizioneServizio(idServizio, idSoggettoFruitore, con, tipoDB),
  1105.                 idSoggettoFruitore,
  1106.                 idServizio,
  1107.                 null, false, CostantiDB.SOGGETTI,
  1108.                 orderByDescrizione);
  1109.     }
  1110.     public static List<MappingFruizionePortaDelegata> mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1111.             IDSoggetto idSoggettoFruitore,
  1112.             IDServizio idServizio,
  1113.             String tabellaSoggetti,
  1114.             boolean orderByDescrizione) throws CoreException {
  1115.         return _mappingFruizionePortaDelegataList(con, tipoDB,
  1116.                 DBUtils.getIdFruizioneServizio(idServizio, idSoggettoFruitore, con, tipoDB),
  1117.                 idSoggettoFruitore,
  1118.                 idServizio,
  1119.                 null, false, tabellaSoggetti,
  1120.                 orderByDescrizione);
  1121.     }
  1122.    
  1123.     private static List<MappingFruizionePortaDelegata> _mappingFruizionePortaDelegataList(Connection con, String tipoDB,
  1124.             long idFruizione,
  1125.             IDSoggetto idSoggettoFruitore,
  1126.             IDServizio idServizio,
  1127.             ISearch ricerca,boolean ricerca_useOffsetLimit,String tabellaSoggetti,
  1128.             boolean orderByDescrizione) throws CoreException {
  1129.         int idLista = Liste.CONFIGURAZIONE_FRUIZIONE;
  1130.         int offset = 0;
  1131.         int limit = 0;
  1132.         String search = "";
  1133.         String queryString;

  1134.         if(ricerca != null) {
  1135.             if(ricerca_useOffsetLimit) {
  1136.                 limit = ricerca.getPageSize(idLista);
  1137.                 offset = ricerca.getIndexIniziale(idLista);
  1138.             }
  1139.             search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  1140.         }
  1141.        
  1142.         PreparedStatement stmt = null;
  1143.         ResultSet risultato = null;
  1144.        
  1145.         List<MappingFruizionePortaDelegata> lista = new ArrayList<MappingFruizionePortaDelegata>();
  1146.         try {
  1147.             if(ricerca != null) {
  1148.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1149.                 sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1150.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  1151.                 sqlQueryObject.addSelectCountField("*", "cont");
  1152.                 sqlQueryObject.addWhereCondition(CostantiDB.MAPPING_FRUIZIONE_PD+".id_fruizione = ?");
  1153.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id="+CostantiDB.MAPPING_FRUIZIONE_PD+".id_porta");
  1154.                 if (!search.equals("")) {
  1155.                     sqlQueryObject.addWhereCondition(false,
  1156.                             sqlQueryObject.getWhereLikeCondition(CostantiDB.MAPPING_FRUIZIONE_PD+".nome", search, true, true));
  1157.                             //sqlQueryObject.getWhereLikeCondition(CostantiDB.PORTE_DELEGATE +".nome_porta", search, true, true));
  1158.                 }
  1159.                
  1160.                 sqlQueryObject.setANDLogicOperator(true);
  1161.                 queryString = sqlQueryObject.createSQLQuery();
  1162.                 stmt = con.prepareStatement(queryString);
  1163.                 int index = 1;
  1164.                 stmt.setLong(index++, idFruizione);
  1165.                
  1166.                 risultato = stmt.executeQuery();
  1167.                 if (risultato.next())
  1168.                     ricerca.setNumEntries(idLista,risultato.getInt(1));
  1169.                 risultato.close();
  1170.                 stmt.close();
  1171.             }
  1172.            
  1173.             // ricavo le entries
  1174.             if (limit == 0) // con limit
  1175.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  1176.            
  1177.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1178.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1179.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  1180.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_FRUIZIONE_PD+".id_fruizione");
  1181.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_FRUIZIONE_PD+".id_porta");
  1182.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_FRUIZIONE_PD+".is_default");
  1183.             sqlQueryObject.addSelectAliasField(CostantiDB.MAPPING_FRUIZIONE_PD,"nome","nomeMapping");
  1184.             sqlQueryObject.addSelectAliasField(CostantiDB.MAPPING_FRUIZIONE_PD,"descrizione","descrizioneMapping");
  1185.             sqlQueryObject.addSelectField(CostantiDB.MAPPING_FRUIZIONE_PD+".id");
  1186.             sqlQueryObject.addSelectField(CostantiDB.PORTE_DELEGATE+".nome_porta");
  1187.             sqlQueryObject.addWhereCondition(CostantiDB.MAPPING_FRUIZIONE_PD+".id_fruizione = ?");
  1188.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id="+CostantiDB.MAPPING_FRUIZIONE_PD+".id_porta");
  1189.            
  1190.             if (ricerca != null && !search.equals("")) {
  1191.                 sqlQueryObject.addWhereCondition(false,
  1192.                         sqlQueryObject.getWhereLikeCondition(CostantiDB.MAPPING_FRUIZIONE_PD+".nome", search, true, true));
  1193.                         //sqlQueryObject.getWhereLikeCondition(CostantiDB.PORTE_DELEGATE +".nome_porta", search, true, true));
  1194.             }
  1195.             sqlQueryObject.setANDLogicOperator(true);
  1196.             sqlQueryObject.addOrderBy(CostantiDB.MAPPING_FRUIZIONE_PD+".is_default",true);
  1197.             if(orderByDescrizione) {
  1198.                 sqlQueryObject.addOrderBy("descrizioneMapping");
  1199.             }
  1200.             else {
  1201.                 sqlQueryObject.addOrderBy("nomeMapping");
  1202.             }
  1203.             sqlQueryObject.setSortType(true);
  1204.             if(ricerca != null) {
  1205.                 sqlQueryObject.setLimit(limit);
  1206.                 sqlQueryObject.setOffset(offset);
  1207.             }
  1208.             queryString = sqlQueryObject.createSQLQuery();
  1209.            
  1210.             stmt = con.prepareStatement(queryString);
  1211.            
  1212.             int index = 1;
  1213.             stmt.setLong(index++, idFruizione);
  1214.            
  1215.             risultato = stmt.executeQuery();
  1216.            
  1217.             MappingFruizionePortaDelegata mapping = null;

  1218.             while (risultato.next()) {

  1219.                 mapping = new MappingFruizionePortaDelegata();
  1220.                
  1221.                 Long id = risultato.getLong("id");
  1222. //              Long idPorta = risultato.getLong("id_porta");
  1223. //              Long idFruizione = risultato.getLong("id_fruizione");
  1224.                 String nome = risultato.getString("nomeMapping");
  1225.                 String descrizione = risultato.getString("descrizioneMapping");
  1226.                 Integer isDefaultInt = risultato.getInt("is_default");
  1227.                 boolean isDefault = isDefaultInt > 0 ;
  1228.                
  1229.                 mapping.setNome(nome);
  1230.                 mapping.setDescrizione(descrizione);
  1231.                 mapping.setTableId(id);
  1232.                 mapping.setDefault(isDefault);
  1233.                 // evitiamo una join utilizzo l'id che mi sono passato come parametro
  1234.                 mapping.setIdServizio(idServizio);
  1235.                 mapping.setIdFruitore(idSoggettoFruitore);
  1236.                
  1237.                 IDPortaDelegata idPortaDelegata = new IDPortaDelegata();
  1238.                 String nomePorta = risultato.getString("nome_porta");
  1239.                 idPortaDelegata.setNome(nomePorta);
  1240.                
  1241.                 IdentificativiFruizione identificativiFruizione = new IdentificativiFruizione();
  1242.                 identificativiFruizione.setSoggettoFruitore(idSoggettoFruitore);
  1243.                 identificativiFruizione.setIdServizio(idServizio);
  1244.                 idPortaDelegata.setIdentificativiFruizione(identificativiFruizione);
  1245.                
  1246.                 mapping.setIdPortaDelegata(idPortaDelegata);
  1247.                
  1248.                 lista.add(mapping);

  1249.             }
  1250.            
  1251.        
  1252.         }catch(Exception e){
  1253.             throw new CoreException("mappingFruizionePortaDelegataList error",e);
  1254.         } finally {
  1255.        
  1256.             //Chiudo statement and resultset
  1257.             try{
  1258.                 if(risultato!=null) risultato.close();
  1259.             }catch (Exception e) {
  1260.                 //ignore
  1261.             }
  1262.             try{
  1263.                 if(stmt!=null) stmt.close();
  1264.             }catch (Exception e) {
  1265.                 //ignore
  1266.             }
  1267.        
  1268.         }
  1269.        
  1270.         return lista;
  1271.     }

  1272.     public static void createMappingFruizione(String nome, String descrizione, boolean isDefault, IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1273.             Connection con, String tipoDB) throws CoreException{
  1274.         createMappingFruizione(nome, descrizione, isDefault, idServizio, idFruitore, idPortaDelegata, con, tipoDB, CostantiDB.SOGGETTI);
  1275.     }
  1276.     public static void createMappingFruizione(String nome, String descrizione, boolean isDefault, IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1277.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  1278.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1279.         if(idFruizione<=0){
  1280.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1281.         }
  1282.         _createMappingFruizione(nome, descrizione, isDefault, idFruizione, idPortaDelegata, con, tipoDB, tabellaSoggetti);
  1283.     }
  1284.    
  1285.     private static void _createMappingFruizione(String nome, String descrizione, boolean isDefault, long idFruizione, IDPortaDelegata idPortaDelegata,
  1286.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  1287.         PreparedStatement stmt = null;

  1288.         try {

  1289.             if(idFruizione<=0){
  1290.                 throw new Exception("IdFruizione non fornita");
  1291.             }
  1292.             long idPD = DBUtils.getIdPortaDelegata(idPortaDelegata.getNome(), con, tipoDB);
  1293.             if(idPD<=0){
  1294.                 throw new Exception("PortaDelegata ["+idPortaDelegata+"] non esistente");
  1295.             }
  1296.            
  1297.             int isDefaultInt = isDefault ? 1 : 0;
  1298.            
  1299.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1300.             sqlQueryObject.addInsertTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1301.             sqlQueryObject.addInsertField("id_fruizione", "?");
  1302.             sqlQueryObject.addInsertField("id_porta", "?");
  1303.             sqlQueryObject.addInsertField("nome", "?");
  1304.             sqlQueryObject.addInsertField("descrizione", "?");
  1305.             sqlQueryObject.addInsertField("is_default", "?");
  1306.             String queryString = sqlQueryObject.createSQLInsert();
  1307.             stmt = con.prepareStatement(queryString);
  1308.             int index = 1;
  1309.             stmt.setLong(index++, idFruizione);
  1310.             stmt.setLong(index++, idPD);
  1311.             stmt.setString(index++, nome);
  1312.             stmt.setString(index++, descrizione);
  1313.             stmt.setInt(index++, isDefaultInt);
  1314.             stmt.executeUpdate();
  1315.             stmt.close();
  1316.            
  1317.         }catch(Exception e){
  1318.             throw new CoreException("Creazione Mapping Fruizione fallita: "+e.getMessage(),e);
  1319.         } finally {

  1320.             //Chiudo statement and resultset
  1321.             try{
  1322.                 if(stmt!=null) stmt.close();
  1323.             }catch (Exception e) {
  1324.                 //ignore
  1325.             }

  1326.         }
  1327.     }
  1328.    
  1329.    
  1330.    
  1331.     public static void updateMappingFruizione(long tableId, String descrizione, Connection con, String tipoDB) throws CoreException{
  1332.         PreparedStatement stmt = null;

  1333.         try {

  1334.             if(tableId<=0){
  1335.                 throw new Exception("TableId non fornito");
  1336.             }
  1337.            
  1338.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1339.             sqlQueryObject.addUpdateTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1340.             sqlQueryObject.addUpdateField("descrizione", "?");
  1341.             sqlQueryObject.addWhereCondition("id=?");
  1342.             String queryString = sqlQueryObject.createSQLUpdate();
  1343.             stmt = con.prepareStatement(queryString);
  1344.             int index = 1;
  1345.             stmt.setString(index++, descrizione);
  1346.             stmt.setLong(index++, tableId);
  1347.             stmt.executeUpdate();
  1348.             stmt.close();
  1349.            
  1350.         }catch(Exception e){
  1351.             throw new CoreException("updateMappingFruizione error",e);
  1352.         } finally {

  1353.             //Chiudo statement and resultset
  1354.             try{
  1355.                 if(stmt!=null) stmt.close();
  1356.             }catch (Exception e) {
  1357.                 //ignore
  1358.             }

  1359.         }
  1360.     }
  1361.    
  1362.    
  1363.     public static void deleteMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore,
  1364.             Connection con, String tipoDB) throws CoreException{
  1365.         deleteMappingFruizione(idServizio, idFruitore, null, con, tipoDB, CostantiDB.SOGGETTI);
  1366.     }
  1367.     public static void deleteMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore,
  1368.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  1369.         deleteMappingFruizione(idServizio, idFruitore, null, con, tipoDB, tabellaSoggetti);
  1370.     }
  1371.     public static void deleteMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, boolean deletePorte,
  1372.             Connection con, String tipoDB) throws CoreException{
  1373.         deleteMappingFruizione(idServizio, idFruitore, deletePorte, con, tipoDB, CostantiDB.SOGGETTI);
  1374.     }
  1375.     public static void deleteMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, boolean deletePorte,
  1376.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  1377.         List<IDPortaDelegata> list = null;
  1378.         if(deletePorte) {
  1379.             list = getIDPorteDelegateAssociate(idServizio, idFruitore, con, tipoDB);
  1380.         }
  1381.         deleteMappingFruizione(idServizio, idFruitore, null, con, tipoDB, tabellaSoggetti);
  1382.         if(list!=null && list.size()>0) {
  1383.             for (IDPortaDelegata idPortaDelegata : list) {
  1384.                 PreparedStatement stmt = null;
  1385.                 try {
  1386.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1387.                     sqlQueryObject.addDeleteTable(CostantiDB.PORTE_DELEGATE);
  1388.                     sqlQueryObject.addWhereCondition("id=?");
  1389.                     sqlQueryObject.setANDLogicOperator(true);
  1390.                     String queryString = sqlQueryObject.createSQLDelete();
  1391.                     stmt = con.prepareStatement(queryString);
  1392.                     stmt.setLong(1, DBUtils.getIdPortaDelegata(idPortaDelegata.getNome(), con, tipoDB));
  1393.                     stmt.executeUpdate();
  1394.                     stmt.close();
  1395.                 }catch(Exception e){
  1396.                     throw new CoreException("deletePAMappingErogazione error",e);
  1397.                 } finally {

  1398.                     //Chiudo statement and resultset
  1399.                     try{
  1400.                         if(stmt!=null) stmt.close();
  1401.                     }catch (Exception e) {
  1402.                         //ignore
  1403.                     }

  1404.                 }
  1405.             }
  1406.         }
  1407.     }
  1408.     public static void deleteMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1409.             Connection con, String tipoDB) throws CoreException{
  1410.         deleteMappingFruizione(idServizio, idFruitore, idPortaDelegata, con, tipoDB, CostantiDB.SOGGETTI);
  1411.     }
  1412.     public static void deleteMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1413.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  1414.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1415.         if(idFruizione<=0){
  1416.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1417.         }
  1418.         _deleteMappingFruizione(idFruizione, idPortaDelegata, con, tipoDB, tabellaSoggetti);
  1419.     }
  1420.    
  1421.     private static void _deleteMappingFruizione(long idFruizione, IDPortaDelegata idPortaDelegata,
  1422.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException{
  1423.         PreparedStatement stmt = null;

  1424.         try {

  1425.             if(idFruizione<=0){
  1426.                 throw new Exception("IdFruizione non fornita");
  1427.             }
  1428.             long idPD = -1;
  1429.             if(idPortaDelegata!=null){
  1430.                 idPD = DBUtils.getIdPortaDelegata(idPortaDelegata.getNome(), con, tipoDB);
  1431.                 if(idPD<=0){
  1432.                     throw new Exception("PortaDelegata ["+idPortaDelegata+"] non esistente");
  1433.                 }
  1434.             }
  1435.            
  1436.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1437.             sqlQueryObject.addDeleteTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1438.             sqlQueryObject.addWhereCondition("id_fruizione=?");
  1439.             if(idPortaDelegata!=null){
  1440.                 sqlQueryObject.addWhereCondition("id_porta=?");
  1441.             }
  1442.             sqlQueryObject.setANDLogicOperator(true);
  1443.             String queryString = sqlQueryObject.createSQLDelete();
  1444.             stmt = con.prepareStatement(queryString);
  1445.             stmt.setLong(1, idFruizione);
  1446.             if(idPortaDelegata!=null){
  1447.                 stmt.setLong(2, idPD);
  1448.             }
  1449.             stmt.executeUpdate();
  1450.             stmt.close();
  1451.            
  1452.         }catch(Exception e){
  1453.             throw new CoreException("deleteMappingFruizione error",e);
  1454.         } finally {

  1455.             //Chiudo statement and resultset
  1456.             try{
  1457.                 if(stmt!=null) stmt.close();
  1458.             }catch (Exception e) {
  1459.                 //ignore
  1460.             }

  1461.         }
  1462.     }

  1463.     public static IDPortaDelegata getIDPortaDelegataAssociataDefault(IDServizio idServizio, IDSoggetto idFruitore,
  1464.             Connection con, String tipoDB) throws CoreException {
  1465.         return getIDPortaDelegataAssociataDefault(idServizio, idFruitore, con, tipoDB, CostantiDB.SOGGETTI);
  1466.     }
  1467.     public static IDPortaDelegata getIDPortaDelegataAssociataDefault(IDServizio idServizio, IDSoggetto idFruitore,
  1468.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1469.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1470.         if(idFruizione<=0){
  1471.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1472.         }
  1473.         List<IDPortaDelegata> l = _getIDPorteDelegateAssociate(idFruizione, true, null, con, tipoDB, tabellaSoggetti,
  1474.                 idFruitore, idServizio);
  1475.         if(l!=null && l.size()>0) {
  1476.             if(l.size()>1) {
  1477.                 throw new CoreException("Esiste più di un mapping di default per la fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"]");
  1478.             }
  1479.             return l.get(0);
  1480.         }
  1481.         return null;
  1482.     }
  1483.    
  1484.     public static IDPortaDelegata getIDPortaDelegataAssociataAzione(IDServizio idServizio, IDSoggetto idFruitore,
  1485.             Connection con, String tipoDB) throws CoreException {
  1486.         return getIDPortaDelegataAssociataAzione(idServizio, idFruitore, con, tipoDB, CostantiDB.SOGGETTI);
  1487.     }
  1488.     public static IDPortaDelegata getIDPortaDelegataAssociataAzione(IDServizio idServizio, IDSoggetto idFruitore,
  1489.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1490.         if(idServizio.getAzione()==null) {
  1491.             throw new CoreException("Azione non indicata nel parametro 'idServizio'");
  1492.         }
  1493.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1494.         if(idFruizione<=0){
  1495.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1496.         }
  1497.         List<IDPortaDelegata> l = _getIDPorteDelegateAssociate(idFruizione, false, idServizio.getAzione(), con, tipoDB, tabellaSoggetti,
  1498.                 idFruitore, idServizio);
  1499.         if(l!=null && l.size()>0) {
  1500.             if(l.size()>1) {
  1501.                 throw new CoreException("Esiste più di un mapping per l'azione '"+idServizio.getAzione()+"' per la fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"]");
  1502.             }
  1503.             return l.get(0);
  1504.         }
  1505.         return null;
  1506.     }
  1507.    
  1508.     public static List<IDPortaDelegata> getIDPorteDelegateAssociate(IDServizio idServizio, IDSoggetto idFruitore,
  1509.             Connection con, String tipoDB) throws CoreException {
  1510.         return getIDPorteDelegateAssociate(idServizio, idFruitore, con, tipoDB, CostantiDB.SOGGETTI);
  1511.     }
  1512.     public static List<IDPortaDelegata> getIDPorteDelegateAssociate(IDServizio idServizio, IDSoggetto idFruitore,
  1513.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1514.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1515.         if(idFruizione<=0){
  1516.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1517.         }
  1518.         return _getIDPorteDelegateAssociate(idFruizione, false, null, con, tipoDB, tabellaSoggetti,
  1519.                 idFruitore, idServizio);
  1520.     }
  1521.    
  1522.     private static List<IDPortaDelegata> _getIDPorteDelegateAssociate(long idFruizione, boolean defaultMapping, String azioneMapping,
  1523.             Connection con, String tipoDB,String tabellaSoggetti,
  1524.             IDSoggetto idFruitore, IDServizio idServizio) throws CoreException {
  1525.         PreparedStatement stm = null;
  1526.         ResultSet rs = null;

  1527.         try {

  1528.             if(idFruizione<=0){
  1529.                 throw new Exception("IdFruizione non fornita");
  1530.             }

  1531.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1532.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1533.             sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  1534.             sqlQueryObject.addSelectField(CostantiDB.PORTE_DELEGATE+".nome_porta");
  1535.             sqlQueryObject.addWhereCondition("id_fruizione = ?");
  1536.             if(defaultMapping) {
  1537.                 sqlQueryObject.addWhereCondition("is_default = ?");
  1538.             }
  1539.             sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id="+CostantiDB.MAPPING_FRUIZIONE_PD+".id_porta");
  1540.             if(azioneMapping!=null) {
  1541.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE_AZIONI);
  1542.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id="+CostantiDB.PORTE_DELEGATE_AZIONI+".id_porta");
  1543.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_AZIONI+".azione = ?");
  1544.             }
  1545.             sqlQueryObject.setANDLogicOperator(true);
  1546.             String sqlQuery = sqlQueryObject.createSQLQuery();
  1547.             stm = con.prepareStatement(sqlQuery);
  1548.             int indexStmt = 1;
  1549.             stm.setLong(indexStmt++,idFruizione);
  1550.             if(defaultMapping) {
  1551.                 stm.setInt(indexStmt++,1);
  1552.             }
  1553.             if(azioneMapping!=null) {
  1554.                 stm.setString(indexStmt++,azioneMapping);
  1555.             }
  1556.        
  1557.             rs = stm.executeQuery();
  1558.            
  1559.             List<IDPortaDelegata> list = new ArrayList<>();
  1560.            
  1561.             while (rs.next()) {
  1562.                 IDPortaDelegata idPD = new IDPortaDelegata();
  1563.                 String nome = rs.getString("nome_porta");
  1564.                 idPD.setNome(nome);
  1565.                
  1566.                 IdentificativiFruizione identificativiFruizione = new IdentificativiFruizione();
  1567.                 identificativiFruizione.setSoggettoFruitore(idFruitore);
  1568.                 identificativiFruizione.setIdServizio(idServizio);
  1569.                 idPD.setIdentificativiFruizione(identificativiFruizione);
  1570.                
  1571.                 list.add(idPD);
  1572.             }
  1573.             if(list.size()<=0){
  1574.                 //throw new CoreException("Mapping tra PD e Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1575.                 return null;
  1576.             }
  1577.             else {
  1578.                 return list;
  1579.             }
  1580.         }catch(CoreException de){
  1581.             throw de;
  1582.         }
  1583.         catch(Exception e){
  1584.             throw new CoreException("getIDPortaDelegataAssociata error",e);
  1585.         } finally {

  1586.             //Chiudo statement and resultset
  1587.             try{
  1588.                 if(rs!=null) rs.close();
  1589.             }catch (Exception e) {
  1590.                 //ignore
  1591.             }
  1592.             try{
  1593.                 if(stm!=null) stm.close();
  1594.             }catch (Exception e) {
  1595.                 //ignore
  1596.             }

  1597.         }
  1598.     }
  1599.    
  1600.    
  1601.    
  1602.     public static boolean existsIDPortaDelegataAssociataDefault(IDServizio idServizio, IDSoggetto idFruitore,
  1603.             Connection con, String tipoDB) throws CoreException {
  1604.         return existsIDPortaDelegataAssociataDefault(idServizio, idFruitore, con, tipoDB, CostantiDB.SOGGETTI);
  1605.     }
  1606.     public static boolean existsIDPortaDelegataAssociataDefault(IDServizio idServizio, IDSoggetto idFruitore,
  1607.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1608.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1609.         if(idFruizione<=0){
  1610.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1611.         }
  1612.         int c = _countIDPorteDelegateAssociate(idFruizione, true, null, con, tipoDB, tabellaSoggetti);
  1613.         if(c<=0) {
  1614.             return false;
  1615.         }
  1616.         else {
  1617.             if(c>1) {
  1618.                 throw new CoreException("Esiste più di un mapping di default per la fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"]");
  1619.             }
  1620.             return true;
  1621.         }
  1622.     }
  1623.    
  1624.     public static boolean existsIDPortaDelegataAssociataAzione(IDServizio idServizio, IDSoggetto idFruitore,
  1625.             Connection con, String tipoDB) throws CoreException {
  1626.         return existsIDPortaDelegataAssociataAzione(idServizio, idFruitore, con, tipoDB, CostantiDB.SOGGETTI);
  1627.     }
  1628.     public static boolean existsIDPortaDelegataAssociataAzione(IDServizio idServizio, IDSoggetto idFruitore,
  1629.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1630.         if(idServizio.getAzione()==null) {
  1631.             throw new CoreException("Azione non indicata nel parametro 'idServizio'");
  1632.         }
  1633.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1634.         if(idFruizione<=0){
  1635.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1636.         }
  1637.         int c = _countIDPorteDelegateAssociate(idFruizione, false, idServizio.getAzione(), con, tipoDB, tabellaSoggetti);
  1638.         if(c<=0) {
  1639.             return false;
  1640.         }
  1641.         else {
  1642.             if(c>1) {
  1643.                 throw new CoreException("Esiste più di un mapping per la fruizione dell'azione '"+idServizio.getAzione()+"' da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"]");
  1644.             }
  1645.             return true;
  1646.         }
  1647.     }

  1648.     public static boolean existsIDPorteDelegateAssociate(IDServizio idServizio, IDSoggetto idFruitore,
  1649.             Connection con, String tipoDB) throws CoreException {
  1650.         return existsIDPorteDelegateAssociate(idServizio, idFruitore, con, tipoDB, CostantiDB.SOGGETTI);
  1651.     }
  1652.     public static boolean existsIDPorteDelegateAssociate(IDServizio idServizio, IDSoggetto idFruitore,
  1653.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1654.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1655.         if(idFruizione<=0){
  1656.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1657.         }
  1658.         return _countIDPorteDelegateAssociate(idFruizione, false, null, con, tipoDB, tabellaSoggetti)>0;
  1659.     }
  1660.    
  1661.     private static int _countIDPorteDelegateAssociate(long idFruizione, boolean defaultMapping, String azioneMapping,
  1662.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1663.         PreparedStatement stm = null;
  1664.         ResultSet rs = null;

  1665.         try {

  1666.             if(idFruizione<=0){
  1667.                 throw new Exception("IdFruizione non fornita");
  1668.             }

  1669.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1670.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1671.             sqlQueryObject.addWhereCondition("id_fruizione = ?");
  1672.             if(defaultMapping) {
  1673.                 sqlQueryObject.addWhereCondition("is_default = ?");
  1674.             }
  1675.             if(azioneMapping!=null) {
  1676.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE);
  1677.                 sqlQueryObject.addFromTable(CostantiDB.PORTE_DELEGATE_AZIONI);
  1678.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id="+CostantiDB.MAPPING_FRUIZIONE_PD+".id_porta");
  1679.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE+".id="+CostantiDB.PORTE_DELEGATE_AZIONI+".id_porta");
  1680.                 sqlQueryObject.addWhereCondition(CostantiDB.PORTE_DELEGATE_AZIONI+".azione = ?");
  1681.             }
  1682.             sqlQueryObject.setANDLogicOperator(true);
  1683.             String sqlQuery = sqlQueryObject.createSQLQuery();
  1684.             stm = con.prepareStatement(sqlQuery);
  1685.             int indexStmt = 1;
  1686.             stm.setLong(indexStmt++,idFruizione);
  1687.             if(defaultMapping) {
  1688.                 stm.setInt(indexStmt++,1);
  1689.             }
  1690.             if(azioneMapping!=null) {
  1691.                 stm.setString(indexStmt++,azioneMapping);
  1692.             }
  1693.        
  1694.             rs = stm.executeQuery();
  1695.             int c = 0;
  1696.             while(rs.next()) {
  1697.                 c++;
  1698.             }
  1699.             return c;
  1700.         }catch(CoreException de){
  1701.             throw de;
  1702.         }
  1703.         catch(Exception e){
  1704.             throw new CoreException("existsIDPortaDelegataAssociata error",e);
  1705.         } finally {

  1706.             //Chiudo statement and resultset
  1707.             try{
  1708.                 if(rs!=null) rs.close();
  1709.             }catch (Exception e) {
  1710.                 //ignore
  1711.             }
  1712.             try{
  1713.                 if(stm!=null) stm.close();
  1714.             }catch (Exception e) {
  1715.                 //ignore
  1716.             }

  1717.         }
  1718.     }

  1719.    
  1720.    
  1721.    
  1722.    


  1723.    
  1724.    
  1725.     public static boolean existsMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1726.             Connection con, String tipoDB) throws CoreException {
  1727.         return existsMappingFruizione(idServizio, idFruitore, idPortaDelegata, con, tipoDB, CostantiDB.SOGGETTI);
  1728.     }
  1729.     public static boolean existsMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1730.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1731.         try {
  1732.             long id = getTableIdMappingFruizione(idServizio, idFruitore, idPortaDelegata, con, tipoDB);
  1733.             return id>0;
  1734.         }catch(Exception e) {
  1735.             return false;
  1736.         }
  1737.     }
  1738.    
  1739.    
  1740.    
  1741.    
  1742.    
  1743.    

  1744.     public static long getTableIdMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1745.             Connection con, String tipoDB) throws CoreException {
  1746.         return getTableIdMappingFruizione(idServizio, idFruitore, idPortaDelegata, con, tipoDB, CostantiDB.SOGGETTI);
  1747.     }
  1748.     public static long getTableIdMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1749.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1750.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1751.         if(idFruizione<=0){
  1752.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1753.         }
  1754.         return _getTableIdMappingFruizione(idFruizione, idPortaDelegata, con, tipoDB, tabellaSoggetti);
  1755.     }
  1756.    
  1757.     private static long _getTableIdMappingFruizione(long idFruizione, IDPortaDelegata idPortaDelegata,
  1758.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1759.         PreparedStatement stm = null;
  1760.         ResultSet rs = null;

  1761.         try {

  1762.             if(idFruizione<=0){
  1763.                 throw new Exception("IdFruizione non fornita");
  1764.             }
  1765.             long idPD = DBUtils.getIdPortaDelegata(idPortaDelegata.getNome(), con, tipoDB);
  1766.             if(idPD<=0){
  1767.                 throw new Exception("PortaDelegata ["+idPortaDelegata+"] non esistente");
  1768.             }
  1769.            
  1770.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1771.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1772.             sqlQueryObject.addWhereCondition("id_fruizione=?");
  1773.             sqlQueryObject.addWhereCondition("id_porta=?");
  1774.             sqlQueryObject.setANDLogicOperator(true);
  1775.             String queryString = sqlQueryObject.createSQLQuery();
  1776.             stm = con.prepareStatement(queryString);
  1777.             stm.setLong(1, idFruizione);
  1778.             stm.setLong(2, idPD);
  1779.             rs = stm.executeQuery();
  1780.             if (rs.next()) {
  1781.                 return rs.getLong("id");
  1782.             }
  1783.             else{
  1784.                 throw new CoreException("Mapping tra PD (id:"+idPD+") e Fruizione (id:"+idFruizione+") non esistente");
  1785.             }
  1786.         }catch(CoreException de){
  1787.             throw de;
  1788.         }
  1789.         catch(Exception e){
  1790.             throw new CoreException("_getTableIdMappingFruizione error",e);
  1791.         } finally {

  1792.             //Chiudo statement and resultset
  1793.             try{
  1794.                 if(rs!=null) rs.close();
  1795.             }catch (Exception e) {
  1796.                 //ignore
  1797.             }
  1798.             try{
  1799.                 if(stm!=null) stm.close();
  1800.             }catch (Exception e) {
  1801.                 //ignore
  1802.             }

  1803.         }
  1804.     }
  1805.    
  1806.     public static boolean checkMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,boolean isDefault,
  1807.             Connection con, String tipoDB) throws CoreException {
  1808.         return checkMappingFruizione(idServizio, idFruitore, idPortaDelegata, isDefault, con, tipoDB, CostantiDB.SOGGETTI);
  1809.     }
  1810.     public static boolean checkMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,boolean isDefault,
  1811.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1812.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1813.         if(idFruizione<=0){
  1814.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1815.         }
  1816.         return checkMappingFruizione(idFruizione, idPortaDelegata, isDefault, con, tipoDB, tabellaSoggetti);
  1817.     }
  1818.    
  1819.     private static boolean checkMappingFruizione(long idFruizione, IDPortaDelegata idPortaDelegata, boolean isDefault,
  1820.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1821.         PreparedStatement stm = null;
  1822.         ResultSet rs = null;

  1823.         try {

  1824.             if(idFruizione<=0){
  1825.                 throw new Exception("IdFruizione non fornita");
  1826.             }
  1827.             long idPD = DBUtils.getIdPortaDelegata(idPortaDelegata.getNome(), con, tipoDB);
  1828.             if(idPD<=0){
  1829.                 throw new Exception("PortaDelegata ["+idPortaDelegata+"] non esistente");
  1830.             }
  1831.            
  1832.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1833.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1834.             sqlQueryObject.addSelectCountField("id","mapping_count");
  1835.             sqlQueryObject.addWhereCondition("id_fruizione=?");
  1836.             sqlQueryObject.addWhereCondition("id_porta=?");
  1837.             sqlQueryObject.addWhereCondition("is_default = ?");
  1838.             sqlQueryObject.setANDLogicOperator(true);
  1839.             String queryString = sqlQueryObject.createSQLQuery();
  1840.             stm = con.prepareStatement(queryString);
  1841.             stm.setLong(1, idFruizione);
  1842.             stm.setLong(2, idPD);
  1843.             stm.setLong(3, (isDefault ? 1 : 0));
  1844.             rs = stm.executeQuery();
  1845.             int found = 0;
  1846.             if (rs.next()) {
  1847.                 found = rs.getInt("mapping_count");
  1848.             }
  1849.            
  1850.             return found > 0;
  1851.         }catch(CoreException de){
  1852.             throw de;
  1853.         }
  1854.         catch(Exception e){
  1855.             throw new CoreException("getIDPortaApplicativaAssociata error",e);
  1856.         } finally {

  1857.             //Chiudo statement and resultset
  1858.             try{
  1859.                 if(rs!=null) rs.close();
  1860.             }catch (Exception e) {
  1861.                 //ignore
  1862.             }
  1863.             try{
  1864.                 if(stm!=null) stm.close();
  1865.             }catch (Exception e) {
  1866.                 //ignore
  1867.             }

  1868.         }
  1869.     }
  1870.    
  1871.    
  1872.    
  1873.     public static MappingFruizionePortaDelegata getMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1874.             Connection con, String tipoDB) throws CoreException {
  1875.         return getMappingFruizione(idServizio, idFruitore, idPortaDelegata, con, tipoDB, CostantiDB.SOGGETTI);
  1876.     }
  1877.     public static MappingFruizionePortaDelegata getMappingFruizione(IDServizio idServizio, IDSoggetto idFruitore, IDPortaDelegata idPortaDelegata,
  1878.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1879.         long idFruizione = DBUtils.getIdFruizioneServizio(idServizio, idFruitore, con, tipoDB, tabellaSoggetti);
  1880.         if(idFruizione<=0){
  1881.             throw new CoreException("Fruizione da parte del soggetto ["+idFruitore.toString()+"] del servizio ["+idServizio.toString()+"] non esistente");
  1882.         }
  1883.        
  1884.         long idPD = DBUtils.getIdPortaDelegata(idPortaDelegata.getNome(), con, tipoDB);
  1885.         if(idPD<=0){
  1886.             throw new CoreException("PortaDelegata ["+idPortaDelegata+"] non esistente");
  1887.         }
  1888.        
  1889.         MappingFruizionePortaDelegata mapping = _getMappingFruizione(idFruizione, idPD, con, tipoDB, tabellaSoggetti);
  1890.         mapping.setIdPortaDelegata(idPortaDelegata);
  1891.         mapping.setIdServizio(idServizio);
  1892.         mapping.setIdFruitore(idFruitore);
  1893.         return mapping;
  1894.     }
  1895.     private static MappingFruizionePortaDelegata _getMappingFruizione(long idFruizione, long idPD,
  1896.             Connection con, String tipoDB,String tabellaSoggetti) throws CoreException {
  1897.         PreparedStatement stm = null;
  1898.         ResultSet rs = null;

  1899.         try {

  1900.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(tipoDB);
  1901.             sqlQueryObject.addFromTable(CostantiDB.MAPPING_FRUIZIONE_PD);
  1902.             sqlQueryObject.addWhereCondition("id_fruizione=?");
  1903.             sqlQueryObject.addWhereCondition("id_porta=?");
  1904.             sqlQueryObject.setANDLogicOperator(true);
  1905.             String queryString = sqlQueryObject.createSQLQuery();
  1906.             stm = con.prepareStatement(queryString);
  1907.             stm.setLong(1, idFruizione);
  1908.             stm.setLong(2, idPD);
  1909.             rs = stm.executeQuery();
  1910.             if (rs.next()) {
  1911.                 MappingFruizionePortaDelegata mapping = new MappingFruizionePortaDelegata();
  1912.                 int isDefault = rs.getInt("is_default");
  1913.                 mapping.setDefault(isDefault == 1);
  1914.                
  1915.                 mapping.setTableId(rs.getLong("id"));
  1916.                
  1917.                 mapping.setNome(rs.getString("nome"));          
  1918.                 mapping.setDescrizione(rs.getString("descrizione"));
  1919.                
  1920.                 return mapping;
  1921.             }
  1922.             else{
  1923.                 throw new CoreException("Mapping tra PD (id:"+idPD+") e Fruizione (id:"+idFruizione+") non esistente");
  1924.             }
  1925.         }catch(CoreException de){
  1926.             throw de;
  1927.         }
  1928.         catch(Exception e){
  1929.             throw new CoreException("getMappingFruizione error",e);
  1930.         } finally {

  1931.             //Chiudo statement and resultset
  1932.             try{
  1933.                 if(rs!=null) rs.close();
  1934.             }catch (Exception e) {
  1935.                 //ignore
  1936.             }
  1937.             try{
  1938.                 if(stm!=null) stm.close();
  1939.             }catch (Exception e) {
  1940.                 //ignore
  1941.             }

  1942.         }
  1943.     }
  1944. }