DriverRegistroServiziDB_accordiRestLIB.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.registry.driver.db;

  21. import static org.openspcoop2.core.constants.CostantiDB.CREATE;
  22. import static org.openspcoop2.core.constants.CostantiDB.DELETE;
  23. import static org.openspcoop2.core.constants.CostantiDB.UPDATE;

  24. import java.sql.Connection;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import java.util.ArrayList;
  29. import java.util.List;

  30. import org.openspcoop2.core.byok.IDriverBYOK;
  31. import org.openspcoop2.core.commons.DBUtils;
  32. import org.openspcoop2.core.constants.CostantiDB;
  33. import org.openspcoop2.core.constants.ProprietariProtocolProperty;
  34. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  35. import org.openspcoop2.core.registry.Resource;
  36. import org.openspcoop2.core.registry.ResourceParameter;
  37. import org.openspcoop2.core.registry.ResourceRepresentation;
  38. import org.openspcoop2.core.registry.ResourceRequest;
  39. import org.openspcoop2.core.registry.ResourceResponse;
  40. import org.openspcoop2.core.registry.constants.CostantiRegistroServizi;
  41. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  42. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  43. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  44. import org.openspcoop2.utils.sql.ISQLQueryObject;
  45. import org.openspcoop2.utils.sql.SQLObjectFactory;

  46. /**
  47.  * Classe utilizzata per effettuare query ad un registro dei servizi openspcoop
  48.  * formato db.
  49.  *
  50.  *
  51.  * @author Sandra Giangrandi (sandra@link.it)
  52.  * @author Stefano Corallo (corallo@link.it)
  53.  * @author $Author$
  54.  * @version $Rev$, $Date$
  55.  */
  56. public class DriverRegistroServiziDB_accordiRestLIB {
  57.    
  58.    
  59.     public static int CRUDResource(int type, AccordoServizioParteComune as,Resource resource, Connection con, long idAccordo, IDriverBYOK driverBYOK) throws DriverRegistroServiziException {
  60.         PreparedStatement updateStmt = null;
  61.         String updateQuery;
  62.         PreparedStatement selectStmt = null;
  63.         ResultSet selectRS = null;
  64.         int n = 0;
  65.         if (idAccordo <= 0)
  66.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDResource] ID Accordo non valido.");

  67.         try {
  68.             switch (type) {
  69.             case CREATE:
  70.                 // create
  71.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  72.                 sqlQueryObject.addInsertTable(CostantiDB.API_RESOURCES);
  73.                 sqlQueryObject.addInsertField("id_accordo", "?");
  74.                 sqlQueryObject.addInsertField("nome", "?");
  75.                 sqlQueryObject.addInsertField("descrizione", "?");
  76.                 sqlQueryObject.addInsertField("http_method", "?");
  77.                 sqlQueryObject.addInsertField("path", "?");
  78.                 sqlQueryObject.addInsertField("message_type", "?");
  79.                 sqlQueryObject.addInsertField("message_type_request", "?");
  80.                 sqlQueryObject.addInsertField("message_type_response", "?");
  81.                 sqlQueryObject.addInsertField("profilo_azione", "?");
  82.                 sqlQueryObject.addInsertField("filtro_duplicati", "?");
  83.                 sqlQueryObject.addInsertField("conferma_ricezione", "?");
  84.                 sqlQueryObject.addInsertField("identificativo_collaborazione", "?");
  85.                 sqlQueryObject.addInsertField("id_riferimento_richiesta", "?");
  86.                 sqlQueryObject.addInsertField("consegna_in_ordine", "?");
  87.                 sqlQueryObject.addInsertField("scadenza", "?");
  88.                 updateQuery = sqlQueryObject.createSQLInsert();
  89.                 updateStmt = con.prepareStatement(updateQuery);
  90.                 int index = 1;
  91.                 updateStmt.setLong(index++, idAccordo);
  92.                 updateStmt.setString(index++, resource.getNome());
  93.                 updateStmt.setString(index++, resource.getDescrizione());
  94.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getMethod()));
  95.                 if(resource.getPath()==null) {
  96.                     updateStmt.setString(index++, CostantiDB.API_RESOURCE_PATH_ALL_VALUE);
  97.                 }
  98.                 else {
  99.                     updateStmt.setString(index++, resource.getPath());
  100.                 }
  101.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getMessageType()));
  102.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getRequestMessageType()));
  103.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getResponseMessageType()));
  104.                
  105.                 DriverRegistroServiziDB_LIB.log.debug("Aggiungo resource ["+resource.getNome()+"] con profilo ["+resource.getProfAzione()+"]");

  106.                 updateStmt.setString(index++, resource.getProfAzione());
  107.                
  108.                 if(CostantiRegistroServizi.PROFILO_AZIONE_RIDEFINITO.equals(resource.getProfAzione())){
  109.                     DriverRegistroServiziDB_LIB.log.debug("ridefinizione...");
  110.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getFiltroDuplicati()));
  111.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getConfermaRicezione()));
  112.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getIdCollaborazione()));
  113.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getIdRiferimentoRichiesta()));
  114.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getConsegnaInOrdine()));
  115.                     updateStmt.setString(index++, resource.getScadenza());
  116.                 }else{
  117.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getFiltroDuplicati()));
  118.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getConfermaRicezione()));
  119.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getIdCollaborazione()));
  120.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getIdRiferimentoRichiesta()));
  121.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getConsegnaInOrdine()));
  122.                     updateStmt.setString(index++, as.getScadenza());
  123.                 }

  124.                 // log.debug("CRUDAzione CREATE :
  125.                 // \n"+formatSQLString(updateQuery,idAccordo,idSoggettoFruitore,idConnettore,wsdlImplementativoErogatore,wsdlImplementativoFruitore));
  126.                 n = updateStmt.executeUpdate();

  127.                 DriverRegistroServiziDB_LIB.log.debug("CRUDResource type = " + type + " row affected =" + n);

  128.                 break;

  129.             case UPDATE:
  130.                 // update
  131.                 //
  132.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  133.                 sqlQueryObject.addUpdateTable(CostantiDB.PORT_TYPE);
  134.                 sqlQueryObject.addUpdateField("descrizione", "?");
  135.                 sqlQueryObject.addUpdateField("http_method", "?");
  136.                 sqlQueryObject.addUpdateField("path", "?");
  137.                 sqlQueryObject.addUpdateField("message_type", "?");
  138.                 sqlQueryObject.addUpdateField("message_type_request", "?");
  139.                 sqlQueryObject.addUpdateField("message_type_response", "?");
  140.                 sqlQueryObject.addUpdateField("profilo_azione", "?");
  141.                 sqlQueryObject.addUpdateField("filtro_duplicati", "?");
  142.                 sqlQueryObject.addUpdateField("conferma_ricezione", "?");
  143.                 sqlQueryObject.addUpdateField("identificativo_collaborazione", "?");
  144.                 sqlQueryObject.addUpdateField("id_riferimento_richiesta", "?");
  145.                 sqlQueryObject.addUpdateField("consegna_in_ordine", "?");
  146.                 sqlQueryObject.addUpdateField("scadenza", "?");
  147.                 sqlQueryObject.addWhereCondition("id_accordo=?");
  148.                 sqlQueryObject.addWhereCondition("nome=?");
  149.                 sqlQueryObject.setANDLogicOperator(true);
  150.                 updateQuery = sqlQueryObject.createSQLUpdate();
  151.                 updateStmt = con.prepareStatement(updateQuery);
  152.                 index = 1;
  153.                 updateStmt.setString(index++, resource.getDescrizione());
  154.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getMethod()));
  155.                 if(resource.getPath()==null) {
  156.                     updateStmt.setString(index++, CostantiDB.API_RESOURCE_PATH_ALL_VALUE);
  157.                 }
  158.                 else {
  159.                     updateStmt.setString(index++, resource.getPath());
  160.                 }
  161.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getMessageType()));
  162.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getRequestMessageType()));
  163.                 updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getResponseMessageType()));
  164.                
  165.                 updateStmt.setString(index++, resource.getProfAzione());
  166.                
  167.                 if(CostantiRegistroServizi.PROFILO_AZIONE_RIDEFINITO.equals(resource.getProfAzione())){
  168.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getFiltroDuplicati()));
  169.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getConfermaRicezione()));
  170.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getIdCollaborazione()));
  171.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getIdRiferimentoRichiesta()));
  172.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(resource.getConsegnaInOrdine()));
  173.                     updateStmt.setString(index++, resource.getScadenza());
  174.                 }else{
  175.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getFiltroDuplicati()));
  176.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getConfermaRicezione()));
  177.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getIdCollaborazione()));
  178.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getIdRiferimentoRichiesta()));
  179.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(as.getConsegnaInOrdine()));
  180.                     updateStmt.setString(index++, as.getScadenza());
  181.                 }
  182.                
  183.                 updateStmt.setLong(index++, idAccordo);
  184.                 updateStmt.setString(index++, resource.getNome());
  185.                 n = updateStmt.executeUpdate();

  186.                 DriverRegistroServiziDB_LIB.log.debug("CRUDResource type = " + type + " row affected =" + n);
  187.                 // log.debug("CRUDAzione UPDATE :
  188.                 // \n"+formatSQLString(updateQuery,wsdlImplementativoErogatore,wsdlImplementativoFruitore,
  189.                 // idServizio,idSoggettoFruitore,idConnettore));

  190.                 break;

  191.             case DELETE:
  192.                 // delete

  193.                 Long idResource = 0l;
  194.                 if(resource.getId()==null || resource.getId()<=0){
  195.                     idResource = DBUtils.getIdResource(idAccordo, resource.getNome(), con);
  196.                     if(idResource==null || idResource<=0)
  197.                         throw new Exception("ID della risorsa ["+resource.getNome()+"] idAccordo["+idAccordo+"] non trovato");
  198.                 }
  199.                 else {
  200.                     idResource = resource.getId();
  201.                 }
  202.                
  203.                 // gestione request
  204.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  205.                 sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_MEDIA);
  206.                 sqlQueryObject.addWhereCondition("id_resource_media=?");
  207.                 sqlQueryObject.setANDLogicOperator(true);
  208.                 updateQuery = sqlQueryObject.createSQLDelete();
  209.                 updateStmt=con.prepareStatement(updateQuery);
  210.                 updateStmt.setLong(1, idResource );
  211.                 updateStmt.executeUpdate();
  212.                 updateStmt.close();
  213.                
  214.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  215.                 sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_PARAMETER);
  216.                 sqlQueryObject.addWhereCondition("id_resource_parameter=?");
  217.                 sqlQueryObject.setANDLogicOperator(true);
  218.                 updateQuery = sqlQueryObject.createSQLDelete();
  219.                 updateStmt=con.prepareStatement(updateQuery);
  220.                 updateStmt.setLong(1, idResource);
  221.                 updateStmt.executeUpdate();
  222.                 updateStmt.close();
  223.                
  224.                 // gestione response
  225.                 List<Long> idResourceResponse = new ArrayList<Long>();
  226.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  227.                 sqlQueryObject.addFromTable(CostantiDB.API_RESOURCES_RESPONSE);
  228.                 sqlQueryObject.addSelectField("id");
  229.                 sqlQueryObject.addWhereCondition("id_resource=?");
  230.                 updateQuery = sqlQueryObject.createSQLQuery();
  231.                 updateStmt=con.prepareStatement(updateQuery);
  232.                 updateStmt.setLong(1, idResource);
  233.                 selectRS=updateStmt.executeQuery();
  234.                 while(selectRS.next()){
  235.                     idResourceResponse.add(selectRS.getLong("id"));
  236.                 }
  237.                 selectRS.close();
  238.                 updateStmt.close();
  239.    
  240.                 while(idResourceResponse.size()>0){
  241.                    
  242.                     long idRR = idResourceResponse.remove(0);
  243.                    
  244.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  245.                     sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_MEDIA);
  246.                     sqlQueryObject.addWhereCondition("id_resource_response_media=?");
  247.                     sqlQueryObject.setANDLogicOperator(true);
  248.                     updateQuery = sqlQueryObject.createSQLDelete();
  249.                     updateStmt=con.prepareStatement(updateQuery);
  250.                     updateStmt.setLong(1, idRR );
  251.                     updateStmt.executeUpdate();
  252.                     updateStmt.close();
  253.                    
  254.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  255.                     sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_PARAMETER);
  256.                     sqlQueryObject.addWhereCondition("id_resource_response_par=?");
  257.                     sqlQueryObject.setANDLogicOperator(true);
  258.                     updateQuery = sqlQueryObject.createSQLDelete();
  259.                     updateStmt=con.prepareStatement(updateQuery);
  260.                     updateStmt.setLong(1, idRR);
  261.                     updateStmt.executeUpdate();
  262.                     updateStmt.close();
  263.                 }
  264.                
  265.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  266.                 sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_RESPONSE);
  267.                 sqlQueryObject.addWhereCondition("id_resource=?");
  268.                 sqlQueryObject.setANDLogicOperator(true);
  269.                 updateQuery = sqlQueryObject.createSQLDelete();
  270.                 updateStmt=con.prepareStatement(updateQuery);
  271.                 updateStmt.setLong(1, idResource);
  272.                 updateStmt.executeUpdate();
  273.                 updateStmt.close();
  274.                
  275.                 // elimino risorsa
  276.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  277.                 sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES);
  278.                 sqlQueryObject.addWhereCondition("id=?");
  279.                 sqlQueryObject.setANDLogicOperator(true);
  280.                 updateQuery = sqlQueryObject.createSQLDelete();
  281.                 updateStmt=con.prepareStatement(updateQuery);
  282.                 updateStmt.setLong(1, idResource);
  283.                 n = updateStmt.executeUpdate();
  284.                 updateStmt.close();

  285.                 DriverRegistroServiziDB_LIB.log.debug("CRUDResource type = " + type + " row affected =" + n);
  286.                 // log.debug("CRUDAzione DELETE :
  287.                 // \n"+formatSQLString(updateQuery,idServizio,idSoggettoFruitore,idConnettore));

  288.                 break;
  289.             }



  290.             if ( (CostantiDB.CREATE == type) || (CostantiDB.UPDATE == type)) {
  291.                 Long idResource = DBUtils.getIdResource(idAccordo, resource.getNome(), con);
  292.                 if(idResource==null || idResource<=0)
  293.                     throw new Exception("ID della risorsa ["+resource.getNome()+"] idAccordo["+idAccordo+"] non trovato");

  294.                 DriverRegistroServiziDB_LIB.log.debug("ID risorsa: "+idResource);

  295.                 if ( CostantiDB.UPDATE == type ){
  296.                    
  297.                     if(resource.getRequest()!=null) {
  298.                         DriverRegistroServiziDB_accordiRestLIB.CRUDResourceRequest(CostantiDB.DELETE, as, resource, resource.getRequest(), con, idResource);
  299.                         DriverRegistroServiziDB_LIB.log.info("Cancellato dettagli di richiesta della risorsa ["+idResource+"] associata all'accordo "+idAccordo);
  300.                     }
  301.                    
  302.                     n = 0;
  303.                     for(int i=0;i<resource.sizeResponseList();i++){
  304.                         DriverRegistroServiziDB_accordiRestLIB.CRUDResourceResponse(CostantiDB.DELETE, as, resource, resource.getResponse(i), con, idResource);
  305.                     }
  306.                     DriverRegistroServiziDB_LIB.log.info("Cancellate "+n+" dettagli di risposta della risorsa ["+idResource+"] associata all'accordo "+idAccordo);
  307.                 }

  308.                 if(resource.getRequest()!=null) {
  309.                     DriverRegistroServiziDB_accordiRestLIB.CRUDResourceRequest(CostantiDB.CREATE, as, resource, resource.getRequest(), con, idResource);
  310.                 }
  311.                
  312.                 for(int i=0;i<resource.sizeResponseList();i++){
  313.                     DriverRegistroServiziDB_accordiRestLIB.CRUDResourceResponse(CostantiDB.CREATE, as, resource, resource.getResponse(i), con, idResource);
  314.                 }                  
  315.                 DriverRegistroServiziDB_LIB.log.debug("inserite " + resource.sizeResponseList() + " dettagli di risposta relative alla risorsa ["+resource.getNome()+"] id-risorsa["+resource.getId()+"] dell'accordo :" + IDAccordoFactory.getInstance().getUriFromAccordo(as) + " id-accordo :" + idAccordo);
  316.                
  317.                
  318.                 // ProtocolProperties
  319.                 DriverRegistroServiziDB_LIB.CRUDProtocolProperty(type, resource.getProtocolPropertyList(),
  320.                         idResource, ProprietariProtocolProperty.RESOURCE, con, DriverRegistroServiziDB_LIB.tipoDB, driverBYOK);
  321.             }


  322.             return n;

  323.         } catch (SQLException se) {
  324.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDResource] SQLException : " + se.getMessage(),se);
  325.         } catch (Exception se) {
  326.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDResource] Exception : " + se.getMessage(),se);
  327.         } finally {
  328.             JDBCUtilities.closeResources(selectRS, selectStmt);
  329.             JDBCUtilities.closeResources(updateStmt);
  330.         }
  331.     }
  332.    
  333.     public static void CRUDResourceRequest(int type, AccordoServizioParteComune as,Resource resource,ResourceRequest resourceRequest, Connection con, long idResource) throws DriverRegistroServiziException {
  334.         _CRUDResourceRequestResponse(type, as, resource, resourceRequest, null, con, idResource);
  335.     }
  336.     public static void CRUDResourceResponse(int type, AccordoServizioParteComune as,Resource resource,ResourceResponse resourceResponse, Connection con, long idResource) throws DriverRegistroServiziException {
  337.         _CRUDResourceRequestResponse(type, as, resource, null, resourceResponse, con, idResource);
  338.     }
  339.    
  340.     private static void _CRUDResourceRequestResponse(int type, AccordoServizioParteComune as,Resource resource,
  341.             ResourceRequest resourceRequest,ResourceResponse resourceResponse, Connection con, long idResource) throws DriverRegistroServiziException {

  342.         PreparedStatement updateStmt = null;
  343.         String updateQuery;
  344.         PreparedStatement selectStmt = null;
  345.         String selectQuery = "";
  346.         ResultSet selectRS = null;
  347.         long n = 0;
  348.         if (idResource <= 0)
  349.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::_CRUDResourceRequestResponse] ID Risorda non valido.");

  350.         try {
  351.             switch (type) {
  352.             case CREATE:
  353.                
  354.                 long idFK = -1;
  355.                 if(resourceRequest!=null) {
  356.                     idFK = idResource;
  357.                 }
  358.                 else {
  359.                    
  360.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  361.                     sqlQueryObject.addInsertTable(CostantiDB.API_RESOURCES_RESPONSE);
  362.                     sqlQueryObject.addInsertField("id_resource", "?");
  363.                     sqlQueryObject.addInsertField("descrizione", "?");
  364.                     sqlQueryObject.addInsertField("status", "?");
  365.                     updateQuery = sqlQueryObject.createSQLInsert();
  366.                     updateStmt = con.prepareStatement(updateQuery);
  367.                     int index = 1;
  368.                     updateStmt.setLong(index++, idResource);
  369.                     updateStmt.setString(index++, resourceResponse.getDescrizione());
  370.                     updateStmt.setInt(index++, resourceResponse.getStatus());
  371.                     DriverRegistroServiziDB_LIB.log.debug("_CRUDResourceRequestResponse (RESPONSE) CREATE :\n"+updateQuery);
  372.                     n = updateStmt.executeUpdate();
  373.                     updateStmt.close();
  374.                     DriverRegistroServiziDB_LIB.log.debug("_CRUDResourceRequestResponse (RESPONSE) type = " + type + " row affected =" + n);
  375.                    
  376.                     index = 1;
  377.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  378.                     sqlQueryObject.addFromTable(CostantiDB.API_RESOURCES_RESPONSE);
  379.                     sqlQueryObject.addSelectField("id");
  380.                     sqlQueryObject.addWhereCondition("id_resource = ?");
  381.                     sqlQueryObject.addWhereCondition("status = ?");
  382.                     sqlQueryObject.setANDLogicOperator(true);
  383.                     selectQuery = sqlQueryObject.createSQLQuery();
  384.                     selectStmt = con.prepareStatement(selectQuery);
  385.                     selectStmt.setLong(index++, idResource);
  386.                     selectStmt.setInt(index++, resourceResponse.getStatus());
  387.                     selectRS = selectStmt.executeQuery();
  388.                     if (selectRS.next()) {
  389.                         idFK = selectRS.getLong("id");
  390.                     }
  391.                     else {
  392.                         throw new Exception("Recupero dell'id della tabella '"+CostantiDB.API_RESOURCES_RESPONSE+"' con id_resource='"+idResource+"' e status='"+resourceResponse.getStatus()+"' non riuscito");
  393.                     }
  394.                 }
  395.                 if(idFK<=0) {
  396.                     throw new Exception("Recupero dell'id della tabella padre non riuscito");
  397.                 }
  398.                 if(resourceRequest!=null) {
  399.                     resourceRequest.setIdResource(idFK);
  400.                 }
  401.                 else {
  402.                     resourceResponse.setIdResource(idFK);
  403.                 }
  404.                
  405.                 List<ResourceRepresentation> lRR = null;
  406.                 if(resourceRequest!=null) {
  407.                     lRR = resourceRequest.getRepresentationList();
  408.                 }
  409.                 else {
  410.                     lRR = resourceResponse.getRepresentationList();
  411.                 }
  412.                 for (ResourceRepresentation rr : lRR) {
  413.                    
  414.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  415.                     sqlQueryObject.addInsertTable(CostantiDB.API_RESOURCES_MEDIA);
  416.                     if(resourceRequest!=null) {
  417.                         sqlQueryObject.addInsertField("id_resource_media", "?");
  418.                     }
  419.                     else {
  420.                         sqlQueryObject.addInsertField("id_resource_response_media", "?");
  421.                     }
  422.                     sqlQueryObject.addInsertField("media_type", "?");
  423.                     sqlQueryObject.addInsertField("message_type", "?");
  424.                     sqlQueryObject.addInsertField("nome", "?");
  425.                     sqlQueryObject.addInsertField("descrizione", "?");
  426.                     sqlQueryObject.addInsertField("tipo", "?");
  427.                     sqlQueryObject.addInsertField("xml_tipo", "?");
  428.                     sqlQueryObject.addInsertField("xml_name", "?");
  429.                     sqlQueryObject.addInsertField("xml_namespace", "?");
  430.                     sqlQueryObject.addInsertField("json_type", "?");
  431.                     updateQuery = sqlQueryObject.createSQLInsert();
  432.                     updateStmt = con.prepareStatement(updateQuery);
  433.                     int index = 1;
  434.                     updateStmt.setLong(index++, idFK);
  435.                     updateStmt.setString(index++, rr.getMediaType());
  436.                     updateStmt.setString(index++,  DriverRegistroServiziDB_LIB.getValue(rr.getMessageType()));
  437.                     updateStmt.setString(index++, rr.getNome());
  438.                     updateStmt.setString(index++, rr.getDescrizione());
  439.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(rr.getRepresentationType()));
  440.                     updateStmt.setString(index++, rr.getXml()!=null ? DriverRegistroServiziDB_LIB.getValue(rr.getXml().getXmlType()) : null);
  441.                     updateStmt.setString(index++, rr.getXml()!=null ? rr.getXml().getNome() : null);
  442.                     updateStmt.setString(index++, rr.getXml()!=null ? rr.getXml().getNamespace() : null);
  443.                     updateStmt.setString(index++, rr.getJson()!=null ? rr.getJson().getTipo() : null);                  
  444.                     DriverRegistroServiziDB_LIB.log.debug("_CRUDResourceRequestResponse (MEDIA) CREATE :\n"+updateQuery);
  445.                     n = updateStmt.executeUpdate();
  446.                     DriverRegistroServiziDB_LIB.log.debug("_CRUDResourceRequestResponse (MEDIA) type = " + type + " row affected =" + n);
  447.                     updateStmt.close();
  448.                    
  449.                 }
  450.                
  451.                 List<ResourceParameter> lRP = null;
  452.                 if(resourceRequest!=null) {
  453.                     lRP = resourceRequest.getParameterList();
  454.                 }
  455.                 else {
  456.                     lRP = resourceResponse.getParameterList();
  457.                 }
  458.                 for (ResourceParameter rp : lRP) {
  459.                    
  460.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  461.                     sqlQueryObject.addInsertTable(CostantiDB.API_RESOURCES_PARAMETER);
  462.                     if(resourceRequest!=null) {
  463.                         sqlQueryObject.addInsertField("id_resource_parameter", "?");
  464.                     }
  465.                     else {
  466.                         sqlQueryObject.addInsertField("id_resource_response_par", "?");
  467.                     }
  468.                     sqlQueryObject.addInsertField("nome", "?");
  469.                     sqlQueryObject.addInsertField("descrizione", "?");
  470.                     sqlQueryObject.addInsertField("tipo_parametro", "?");
  471.                     sqlQueryObject.addInsertField("required", "?");
  472.                     sqlQueryObject.addInsertField("tipo", "?");
  473.                     sqlQueryObject.addInsertField("restrizioni", "?");
  474.                     updateQuery = sqlQueryObject.createSQLInsert();
  475.                     updateStmt = con.prepareStatement(updateQuery);
  476.                     int index = 1;
  477.                     updateStmt.setLong(index++, idFK);
  478.                     updateStmt.setString(index++, rp.getNome());
  479.                     updateStmt.setString(index++, rp.getDescrizione());
  480.                     updateStmt.setString(index++, DriverRegistroServiziDB_LIB.getValue(rp.getParameterType()));
  481.                     updateStmt.setBoolean(index++, rp.isRequired());
  482.                     updateStmt.setString(index++, rp.getTipo());            
  483.                     updateStmt.setString(index++, rp.getRestrizioni());
  484.                     DriverRegistroServiziDB_LIB.log.debug("_CRUDResourceRequestResponse (PARAMETER) CREATE :\n"+updateQuery);
  485.                     n = updateStmt.executeUpdate();
  486.                     DriverRegistroServiziDB_LIB.log.debug("_CRUDResourceRequestResponse (PARAMETER) type = " + type + " row affected =" + n);
  487.                     updateStmt.close();
  488.                    
  489.                 }
  490.                
  491.                 break;

  492.             case UPDATE:
  493.                 throw new Exception("Not Implemented");

  494.                 //break;

  495.             case DELETE:
  496.                 // delete

  497.                
  498.                 // gestione request
  499.                 if(resourceRequest!=null) {
  500.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  501.                     sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_MEDIA);
  502.                     sqlQueryObject.addWhereCondition("id_resource_media=?");
  503.                     sqlQueryObject.setANDLogicOperator(true);
  504.                     updateQuery = sqlQueryObject.createSQLDelete();
  505.                     updateStmt=con.prepareStatement(updateQuery);
  506.                     updateStmt.setLong(1, idResource );
  507.                     updateStmt.executeUpdate();
  508.                     updateStmt.close();
  509.                    
  510.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  511.                     sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_PARAMETER);
  512.                     sqlQueryObject.addWhereCondition("id_resource_parameter=?");
  513.                     sqlQueryObject.setANDLogicOperator(true);
  514.                     updateQuery = sqlQueryObject.createSQLDelete();
  515.                     updateStmt=con.prepareStatement(updateQuery);
  516.                     updateStmt.setLong(1, idResource);
  517.                     updateStmt.executeUpdate();
  518.                     updateStmt.close();
  519.                 }
  520.                 else {
  521.                
  522.                     // gestione response
  523.                     List<Long> idResourceResponse = new ArrayList<Long>();
  524.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  525.                     sqlQueryObject.addFromTable(CostantiDB.API_RESOURCES_RESPONSE);
  526.                     sqlQueryObject.addSelectField("id");
  527.                     sqlQueryObject.addWhereCondition("id_resource=?");
  528.                     updateQuery = sqlQueryObject.createSQLQuery();
  529.                     updateStmt=con.prepareStatement(updateQuery);
  530.                     updateStmt.setLong(1, idResource);
  531.                     selectRS=updateStmt.executeQuery();
  532.                     while(selectRS.next()){
  533.                         idResourceResponse.add(selectRS.getLong("id"));
  534.                     }
  535.                     selectRS.close();
  536.                     updateStmt.close();
  537.        
  538.                     while(idResourceResponse.size()>0){
  539.                        
  540.                         long idRR = idResourceResponse.remove(0);
  541.                        
  542.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  543.                         sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_MEDIA);
  544.                         sqlQueryObject.addWhereCondition("id_resource_response_media=?");
  545.                         sqlQueryObject.setANDLogicOperator(true);
  546.                         updateQuery = sqlQueryObject.createSQLDelete();
  547.                         updateStmt=con.prepareStatement(updateQuery);
  548.                         updateStmt.setLong(1, idRR );
  549.                         updateStmt.executeUpdate();
  550.                         updateStmt.close();
  551.                        
  552.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  553.                         sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_PARAMETER);
  554.                         sqlQueryObject.addWhereCondition("id_resource_response_par=?");
  555.                         sqlQueryObject.setANDLogicOperator(true);
  556.                         updateQuery = sqlQueryObject.createSQLDelete();
  557.                         updateStmt=con.prepareStatement(updateQuery);
  558.                         updateStmt.setLong(1, idRR);
  559.                         updateStmt.executeUpdate();
  560.                         updateStmt.close();
  561.                     }
  562.                    
  563.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  564.                     sqlQueryObject.addDeleteTable(CostantiDB.API_RESOURCES_RESPONSE);
  565.                     sqlQueryObject.addWhereCondition("id_resource=?");
  566.                     sqlQueryObject.setANDLogicOperator(true);
  567.                     updateQuery = sqlQueryObject.createSQLDelete();
  568.                     updateStmt=con.prepareStatement(updateQuery);
  569.                     updateStmt.setLong(1, idResource);
  570.                     updateStmt.executeUpdate();
  571.                     updateStmt.close();
  572.                
  573.                 }
  574.                
  575.                 break;
  576.             }


  577.         } catch (SQLException se) {
  578.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::_CRUDResourceRequestResponse] SQLException : " + se.getMessage(),se);
  579.         } catch (Exception se) {
  580.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::_CRUDResourceRequestResponse] Exception : " + se.getMessage(),se);
  581.         } finally {
  582.             JDBCUtilities.closeResources(selectRS, selectStmt);
  583.             JDBCUtilities.closeResources(updateStmt);
  584.         }
  585.     }
  586.    
  587. }