AccordoServizioParteComuneMappingCore.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.web.ctrlstat.servlet.apc;

  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.openspcoop2.core.id.IDAccordo;
  25. import org.openspcoop2.core.id.IDPortType;
  26. import org.openspcoop2.core.id.IDPortTypeAzione;
  27. import org.openspcoop2.core.id.IDResource;
  28. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  29. import org.openspcoop2.core.registry.Documento;
  30. import org.openspcoop2.core.registry.Operation;
  31. import org.openspcoop2.core.registry.PortType;
  32. import org.openspcoop2.core.registry.Resource;
  33. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;
  34. import org.openspcoop2.core.registry.constants.CostantiRegistroServizi;
  35. import org.openspcoop2.core.registry.constants.FormatoSpecifica;
  36. import org.openspcoop2.core.registry.constants.RuoliDocumento;
  37. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  38. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  39. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  40. import org.openspcoop2.message.xml.MessageXMLUtils;
  41. import org.openspcoop2.message.xml.XMLDiff;
  42. import org.openspcoop2.protocol.basic.Costanti;
  43. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  44. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  45. import org.openspcoop2.utils.LoggerWrapperFactory;
  46. import org.openspcoop2.utils.rest.ApiFactory;
  47. import org.openspcoop2.utils.rest.ApiFormats;
  48. import org.openspcoop2.utils.rest.ApiReaderConfig;
  49. import org.openspcoop2.utils.rest.IApiReader;
  50. import org.openspcoop2.utils.rest.api.Api;
  51. import org.openspcoop2.utils.wsdl.WSDLUtilities;
  52. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  53. import org.openspcoop2.utils.xml.XSDUtils;
  54. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  55. import org.openspcoop2.web.ctrlstat.driver.DriverControlStationException;
  56. import org.openspcoop2.web.ctrlstat.servlet.aps.AccordiServizioParteSpecificaAdd;
  57. import org.openspcoop2.web.ctrlstat.servlet.archivi.ArchiviCore;
  58. import org.openspcoop2.web.lib.mvc.BinaryParameter;
  59. import org.openspcoop2.web.lib.mvc.ServletUtils;
  60. import org.w3c.dom.Document;
  61. import org.w3c.dom.Element;
  62. import org.w3c.dom.Node;

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

  71.     protected AccordoServizioParteComuneMappingCore(ControlStationCore core) throws DriverControlStationException {
  72.         super(core);
  73.     }
  74.    
  75.     public void mappingAutomatico(String protocollo , AccordoServizioParteComune as, boolean validazioneDocumenti) throws DriverRegistroServiziException {
  76.         String nomeMetodo = "mappingAutomatico";
  77.         try {
  78.             IProtocolFactory<?> protocol = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocollo);
  79.             protocol.createArchive().setProtocolInfo(as);
  80.         }catch (Exception e) {
  81.             if(validazioneDocumenti) {
  82.                 ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  83.                 throw new DriverRegistroServiziException(getPrefixError(nomeMetodo,  e), e);
  84.             }
  85.         }
  86.     }

  87.    
  88.     public void popolaResourceDaUnAltroASPC(AccordoServizioParteComune aspcDestinazione, AccordoServizioParteComune aspcSorgente,
  89.             boolean aggiornaRisorseEsistenti, boolean eliminaRisorseNonPresentiNuovaInterfaccia, List<IDResource> risorseEliminate) throws DriverRegistroServiziException{
  90.         String nomeMetodo = "popolaResourceDaUnAltroASPC";
  91.         try {
  92.             if(aspcSorgente.sizeResourceList() > 0){
  93.                 for (Resource nuovoResource : aspcSorgente.getResourceList()) {
  94.                    
  95.                     Resource vecchioResourceByMethodPath = find(nuovoResource, aspcDestinazione.getResourceList());
  96.                    
  97.                     // non ho trovato l'elemento corrente nel aspc destinazione
  98.                     if(vecchioResourceByMethodPath == null){
  99.                        
  100.                         // prima di aggiungerlo, avendolo cercato per method/path verifico che lo stesso nome non sia stato utilizzato per unl'altra risorsa
  101.                         boolean foundName = true;
  102.                         int index = 2;
  103.                         while(foundName) {
  104.                             foundName = false;
  105.                             for (Resource vecchioResourceTMP : aspcDestinazione.getResourceList()) {
  106.                                 if(vecchioResourceTMP.getNome().equals(nuovoResource.getNome())){
  107.                                     foundName = true;
  108.                                     break;
  109.                                 }
  110.                             }
  111.                            
  112.                             if(foundName) {
  113.                                 nuovoResource.setNome(nuovoResource.getNome()+"_"+index);
  114.                                 index++;
  115.                             }
  116.                         }
  117.                        
  118.                         aspcDestinazione.addResource(nuovoResource);
  119.                     } else {
  120.                         if(aggiornaRisorseEsistenti) {
  121.                             // ho trovato l'elemento, aggiorno i valori  rimpiazzando la risorsa
  122.                             Resource vecchiaResource = null;
  123.                             for (int i = 0; i < aspcDestinazione.sizeResourceList(); i++) {
  124.                                 if(aspcDestinazione.getResource(i).getNome().equals(vecchioResourceByMethodPath.getNome())) {
  125.                                     vecchiaResource = aspcDestinazione.removeResource(i);
  126.                                     break;
  127.                                 }
  128.                             }
  129.                             aspcDestinazione.addResource(nuovoResource);
  130.                            
  131.                             if(vecchiaResource!=null) {

  132.                                 // riporto eventuali properties
  133.                                 if(vecchiaResource.sizeProtocolPropertyList()>0) {
  134.                                     for (int i = 0; i < vecchiaResource.sizeProtocolPropertyList(); i++) {
  135.                                         nuovoResource.addProtocolProperty(vecchiaResource.getProtocolProperty(i));      
  136.                                     }
  137.                                 }
  138.                                
  139.                                 if(!CostantiRegistroServizi.PROFILO_AZIONE_RIDEFINITO.equals(vecchiaResource.getProfAzione())) {
  140.                                     continue;
  141.                                 }

  142.                                 boolean ridefinisci = false;
  143.                                 if(vecchiaResource.getConfermaRicezione()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaResource.getConfermaRicezione())) {
  144.                                     nuovoResource.setConfermaRicezione(vecchiaResource.getConfermaRicezione());
  145.                                     ridefinisci = true;
  146.                                 }
  147.                                 if(vecchiaResource.getConsegnaInOrdine()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaResource.getConsegnaInOrdine())) {
  148.                                     nuovoResource.setConsegnaInOrdine(vecchiaResource.getConsegnaInOrdine());
  149.                                     ridefinisci = true;
  150.                                 }
  151.                                 if(vecchiaResource.getIdCollaborazione()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaResource.getIdCollaborazione())) {
  152.                                     nuovoResource.setIdCollaborazione(vecchiaResource.getIdCollaborazione());
  153.                                     ridefinisci = true;
  154.                                 }
  155.                                 if(vecchiaResource.getIdRiferimentoRichiesta()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaResource.getIdRiferimentoRichiesta())) {
  156.                                     nuovoResource.setIdRiferimentoRichiesta(vecchiaResource.getIdRiferimentoRichiesta());
  157.                                     ridefinisci = true;
  158.                                 }
  159.                                 if(vecchiaResource.getScadenza()!=null && !"".equals(vecchiaResource.getScadenza())) {
  160.                                     nuovoResource.setScadenza(vecchiaResource.getScadenza());
  161.                                     ridefinisci = true;
  162.                                 }
  163.                                 if(vecchiaResource.getDescrizione()!=null && !"".equals(vecchiaResource.getDescrizione())) {
  164.                                     nuovoResource.setDescrizione(vecchiaResource.getDescrizione());
  165.                                     ridefinisci = true;
  166.                                 }
  167.                                 // filtro duplicati gestito nel BasicArchive.setProtocolInfo
  168.                                 if(vecchiaResource.getFiltroDuplicati()!=null &&
  169.                                     !vecchiaResource.getFiltroDuplicati().equals(nuovoResource.getFiltroDuplicati())) {
  170.                                     nuovoResource.setFiltroDuplicati(vecchiaResource.getFiltroDuplicati());
  171.                                     ridefinisci = true;
  172.                                 }
  173.                                 if(ridefinisci) {
  174.                                     nuovoResource.setProfAzione(CostantiRegistroServizi.PROFILO_AZIONE_RIDEFINITO);
  175.                                 }
  176.                                
  177.                             }
  178.                         }
  179.                     }
  180.                 }
  181.             }
  182.            
  183.             if(eliminaRisorseNonPresentiNuovaInterfaccia) {
  184.                
  185.                 IDAccordo idAccordo = IDAccordoFactory.getInstance().getIDAccordoFromAccordo(aspcDestinazione);
  186.                
  187.                 if(aspcDestinazione.sizeResourceList() > 0){
  188.                     List<Resource> risorseDaEliminare = new ArrayList<>();
  189.                     for (Resource oldResource : aspcDestinazione.getResourceList()) {
  190.                        
  191.                         Resource find = find(oldResource, aspcSorgente.getResourceList());
  192.                         if(find==null) {
  193.                             risorseDaEliminare.add(oldResource);
  194.                         }
  195.                        
  196.                     }
  197.                     while(!risorseDaEliminare.isEmpty()) {
  198.                         Resource risorsaDaEliminare = risorseDaEliminare.remove(0);
  199.                         for (int i = 0; i < aspcDestinazione.sizeResourceList(); i++) {
  200.                             if(aspcDestinazione.getResource(i).getNome().equals(risorsaDaEliminare.getNome())) {
  201.                                 aspcDestinazione.removeResource(i);
  202.                                 IDResource idResource = new IDResource();
  203.                                 idResource.setIdAccordo(idAccordo);
  204.                                 idResource.setNome(risorsaDaEliminare.getNome());
  205.                                
  206.                                 risorseEliminate.add(idResource);
  207.                                
  208.                                 break;
  209.                             }
  210.                         }
  211.                     }
  212.                 }
  213.             }
  214.         }
  215.         catch (Exception e) {
  216.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  217.             throw e;
  218.         }
  219.     }
  220.    
  221.     private Resource find(Resource resourceSearched, List<Resource> resources) {
  222.         if(resources==null || resources.isEmpty()) {
  223.             return null;
  224.         }
  225.         Resource resourceByMethodPath = null;
  226.         for (Resource resourceTMP : resources) {
  227.            
  228.             if(resourceTMP.getMethod()==null) {
  229.                 if(resourceSearched.getMethod()!=null) {
  230.                     continue;
  231.                 }
  232.             }else {
  233.                 if(!resourceTMP.getMethod().equals(resourceSearched.getMethod())) {
  234.                     continue;
  235.                 }
  236.             }
  237.            
  238.             if(resourceTMP.getPath()==null) {
  239.                 if(resourceSearched.getPath()!=null) {
  240.                     continue;
  241.                 }
  242.             }else {
  243.                 if(!resourceTMP.getPath().equals(resourceSearched.getPath())) {
  244.                     continue;
  245.                 }
  246.             }
  247.            
  248.             resourceByMethodPath = resourceTMP;
  249.             break;
  250.         }  
  251.         return resourceByMethodPath;
  252.     }
  253.    
  254.     public void popolaPorttypeOperationDaUnAltroASPC(AccordoServizioParteComune aspcDestinazione, AccordoServizioParteComune aspcSorgente,
  255.             boolean aggiornaServiziAzioniEsistenti, boolean eliminaServiziAzioniNonPresentiNuovaInterfaccia,
  256.             List<IDPortType> portTypeEliminati, List<IDPortTypeAzione> operationEliminate) throws DriverRegistroServiziException{
  257.         String nomeMetodo = "popolaPorttypeOperationDaUnAltroASPC";
  258.         try {
  259.             IDAccordo idAccordo = null;
  260.             if(eliminaServiziAzioniNonPresentiNuovaInterfaccia) {
  261.                 idAccordo = IDAccordoFactory.getInstance().getIDAccordoFromAccordo(aspcDestinazione);
  262.             }
  263.            
  264.             if(aspcSorgente.sizePortTypeList() > 0){
  265.                 for (PortType nuovoPortType : aspcSorgente.getPortTypeList()) {
  266.                     PortType vecchioPortType = null;
  267.                     for (PortType vecchioPortTypeTMP : aspcDestinazione.getPortTypeList()) {
  268.                         if(vecchioPortTypeTMP.getNome().equals(nuovoPortType.getNome())){
  269.                             vecchioPortType = vecchioPortTypeTMP;
  270.                             break;
  271.                         }
  272.                     }
  273.                    
  274.                     // non ho trovato l'elemento corrente nel aspc destinazione
  275.                     if(vecchioPortType == null){
  276.                         aspcDestinazione.addPortType(nuovoPortType);
  277.                     } else {
  278.                         if(aggiornaServiziAzioniEsistenti) {
  279.                             // ho trovato l'elemento, aggiorno i valori  rimpiazzando la risorsa
  280.                             PortType oldPT = null;
  281.                             for (int i = 0; i < aspcDestinazione.sizePortTypeList(); i++) {
  282.                                 if(aspcDestinazione.getPortType(i).getNome().equals(vecchioPortType.getNome())) {
  283.                                     oldPT = aspcDestinazione.removePortType(i);
  284.                                     break;
  285.                                 }
  286.                             }
  287.                             aspcDestinazione.addPortType(nuovoPortType);
  288.                            
  289.                             if(oldPT!=null) {
  290.                                
  291.                                 // riporto eventuali properties
  292.                                 if(oldPT.sizeProtocolPropertyList()>0) {
  293.                                     for (int i = 0; i < oldPT.sizeProtocolPropertyList(); i++) {
  294.                                         nuovoPortType.addProtocolProperty(oldPT.getProtocolProperty(i));        
  295.                                     }
  296.                                 }
  297.                                
  298.                                 // riporto funzionalità non gestite nel BasicArchive.setProtocolInfo
  299.                                 for (Operation vecchiaAzione : oldPT.getAzioneList()) {
  300.                                    
  301.                                     if(!CostantiRegistroServizi.PROFILO_AZIONE_RIDEFINITO.equals(vecchiaAzione.getProfAzione()) && vecchiaAzione.sizeProtocolPropertyList()<=0 ) {
  302.                                         continue;
  303.                                     }
  304.                                                                            
  305.                                     Operation nuovaAzione = null;
  306.                                     for (Operation azCheck : nuovoPortType.getAzioneList()) {
  307.                                         if(azCheck.getNome().equals(vecchiaAzione.getNome())) {
  308.                                             nuovaAzione = azCheck;
  309.                                             break;
  310.                                         }
  311.                                     }
  312.                                    
  313.                                     if(nuovaAzione!=null && vecchiaAzione.sizeProtocolPropertyList()>0) {
  314.                                        
  315.                                         // riporto eventuali properties
  316.                                         for (int i = 0; i < vecchiaAzione.sizeProtocolPropertyList(); i++) {
  317.                                             nuovaAzione.addProtocolProperty(vecchiaAzione.getProtocolProperty(i));      
  318.                                         }
  319.                                                                                
  320.                                     }
  321.                                    
  322.                                     if(nuovaAzione!=null && CostantiRegistroServizi.PROFILO_AZIONE_RIDEFINITO.equals(vecchiaAzione.getProfAzione())) {
  323.                                        
  324.                                         boolean ridefinisci = false;
  325.                                         if(vecchiaAzione.getConfermaRicezione()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaAzione.getConfermaRicezione())) {
  326.                                             nuovaAzione.setConfermaRicezione(vecchiaAzione.getConfermaRicezione());
  327.                                             ridefinisci = true;
  328.                                         }
  329.                                         if(vecchiaAzione.getConsegnaInOrdine()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaAzione.getConsegnaInOrdine())) {
  330.                                             nuovaAzione.setConsegnaInOrdine(vecchiaAzione.getConsegnaInOrdine());
  331.                                             ridefinisci = true;
  332.                                         }
  333.                                         if(vecchiaAzione.getIdCollaborazione()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaAzione.getIdCollaborazione())) {
  334.                                             nuovaAzione.setIdCollaborazione(vecchiaAzione.getIdCollaborazione());
  335.                                             ridefinisci = true;
  336.                                         }
  337.                                         if(vecchiaAzione.getIdRiferimentoRichiesta()!=null && org.openspcoop2.core.registry.constants.StatoFunzionalita.ABILITATO.equals(vecchiaAzione.getIdRiferimentoRichiesta())) {
  338.                                             nuovaAzione.setIdRiferimentoRichiesta(vecchiaAzione.getIdRiferimentoRichiesta());
  339.                                             ridefinisci = true;
  340.                                         }
  341.                                         if(vecchiaAzione.getScadenza()!=null && !"".equals(vecchiaAzione.getScadenza())) {
  342.                                             nuovaAzione.setScadenza(vecchiaAzione.getScadenza());
  343.                                             ridefinisci = true;
  344.                                         }
  345.                                         // filtro duplicati gestito nel BasicArchive.setProtocolInfo
  346.                                         if(vecchiaAzione.getFiltroDuplicati()!=null &&
  347.                                             !vecchiaAzione.getFiltroDuplicati().equals(nuovaAzione.getFiltroDuplicati())) {
  348.                                             nuovaAzione.setFiltroDuplicati(vecchiaAzione.getFiltroDuplicati());
  349.                                             ridefinisci = true;
  350.                                         }
  351.                                         if(ridefinisci) {
  352.                                             nuovaAzione.setProfAzione(CostantiRegistroServizi.PROFILO_AZIONE_RIDEFINITO);
  353.                                             if(nuovaAzione.getProfiloCollaborazione()!=null) {
  354.                                                 nuovaAzione.setProfiloCollaborazione(nuovaAzione.getProfiloCollaborazione());
  355.                                             }
  356.                                             else if(nuovoPortType.getProfiloCollaborazione()!=null) {
  357.                                                 nuovaAzione.setProfiloCollaborazione(nuovoPortType.getProfiloCollaborazione());
  358.                                             }
  359.                                             else {
  360.                                                 nuovaAzione.setProfiloCollaborazione(aspcDestinazione.getProfiloCollaborazione());
  361.                                             }
  362.                                         }
  363.                                     }
  364.                                 }
  365.                             }
  366.                        
  367.                             // riporto le azioni presenti nel vecchio e non nel nuovo.
  368.                             if(nuovoPortType.sizeAzioneList() > 0){
  369.                                 for (Operation vecchiaAzione : vecchioPortType.getAzioneList()) {
  370.                                     boolean find = false;
  371.                                     for (Operation nuovaAzione : nuovoPortType.getAzioneList()) {
  372.                                         if(nuovaAzione.getNome().equals(vecchiaAzione.getNome())) {
  373.                                             find = true;
  374.                                             break;
  375.                                         }
  376.                                     }
  377.                                     if(!find) {
  378.                                         if(eliminaServiziAzioniNonPresentiNuovaInterfaccia) {
  379.                                             IDPortType idPortType = new IDPortType();
  380.                                             idPortType.setIdAccordo(idAccordo);
  381.                                             idPortType.setNome(vecchioPortType.getNome());
  382.                                            
  383.                                             IDPortTypeAzione idPortTypeAzione = new IDPortTypeAzione();
  384.                                             idPortTypeAzione.setIdPortType(idPortType);
  385.                                             idPortTypeAzione.setNome(vecchiaAzione.getNome());
  386.                                             operationEliminate.add(idPortTypeAzione);
  387.                                         }
  388.                                         else {
  389.                                             nuovoPortType.addAzione(vecchiaAzione);
  390.                                         }
  391.                                     }
  392.                                 }
  393.                             }
  394.                             else {
  395.                                 if(vecchioPortType.sizeAzioneList()>0) {
  396.                                     for (Operation op : vecchioPortType.getAzioneList()) {
  397.                                         if(eliminaServiziAzioniNonPresentiNuovaInterfaccia) {
  398.                                             IDPortType idPortType = new IDPortType();
  399.                                             idPortType.setIdAccordo(idAccordo);
  400.                                             idPortType.setNome(vecchioPortType.getNome());
  401.                                            
  402.                                             IDPortTypeAzione idPortTypeAzione = new IDPortTypeAzione();
  403.                                             idPortTypeAzione.setIdPortType(idPortType);
  404.                                             idPortTypeAzione.setNome(op.getNome());
  405.                                             operationEliminate.add(idPortTypeAzione);
  406.                                         }
  407.                                         else {
  408.                                             nuovoPortType.addAzione(op);
  409.                                         }
  410.                                     }
  411.                                 }
  412.                             }
  413.                         }
  414.                         else {
  415.                             // Aggiungo solo le azioni nuove
  416.                             if(nuovoPortType.sizeAzioneList() > 0){
  417.                                 for (Operation nuovaAzione : nuovoPortType.getAzioneList()) {
  418.                                     boolean find = false;
  419.                                     for (Operation vecchiaAzione : vecchioPortType.getAzioneList()) {
  420.                                         if(nuovaAzione.getNome().equals(vecchiaAzione.getNome())) {
  421.                                             find = true;
  422.                                             break;
  423.                                         }
  424.                                     }
  425.                                     if(!find) {
  426.                                         vecchioPortType.addAzione(nuovaAzione);
  427.                                     }
  428.                                 }
  429.                             }
  430.                         }
  431.                     }
  432.                 }
  433.             }
  434.             if(eliminaServiziAzioniNonPresentiNuovaInterfaccia) {
  435.                
  436.                 // elimino port type non più presenti
  437.                 if(aspcDestinazione.sizePortTypeList() > 0){
  438.                     List<PortType> portTypeDaEliminare = new ArrayList<>();
  439.                     for (PortType oldPortType : aspcDestinazione.getPortTypeList()) {
  440.                        
  441.                         if(aspcSorgente.sizePortTypeList() > 0){
  442.                             boolean find = false;
  443.                             for (PortType nuovoPortType : aspcSorgente.getPortTypeList()) {
  444.                                 if(nuovoPortType.getNome().equals(oldPortType.getNome())){
  445.                                     find = true;
  446.                                     break;
  447.                                 }
  448.                             }
  449.                             if(!find) {
  450.                                 portTypeDaEliminare.add(oldPortType);
  451.                             }
  452.                         }
  453.                        
  454.                     }
  455.                     while(!portTypeDaEliminare.isEmpty()) {
  456.                         PortType ptDaEliminare = portTypeDaEliminare.remove(0);
  457.                         for (int i = 0; i < aspcDestinazione.sizePortTypeList(); i++) {
  458.                             if(aspcDestinazione.getPortType(i).getNome().equals(ptDaEliminare.getNome())) {
  459.                                 aspcDestinazione.removePortType(i);
  460.                                
  461.                                 IDPortType idPortType = new IDPortType();
  462.                                 idPortType.setIdAccordo(idAccordo);
  463.                                 idPortType.setNome(ptDaEliminare.getNome());
  464.                                 portTypeEliminati.add(idPortType);
  465.                                
  466.                                 break;
  467.                             }
  468.                         }
  469.                     }
  470.                 }
  471.                
  472.                 // elimino azioni non più presenti
  473.                 if(aspcDestinazione.sizePortTypeList() > 0){
  474.                     for (PortType oldPortType : aspcDestinazione.getPortTypeList()) {
  475.                        
  476.                         List<Operation> operationDaEliminare = new ArrayList<Operation>();
  477.                         if(oldPortType.sizeAzioneList() > 0){
  478.                             for (Operation oldOperation : oldPortType.getAzioneList()) {
  479.                                
  480.                                 // find operation in new
  481.                                 PortType newPortType = null;
  482.                                 for (PortType nuovoPortType : aspcSorgente.getPortTypeList()) {
  483.                                     if(nuovoPortType.getNome().equals(oldPortType.getNome())){
  484.                                         newPortType = nuovoPortType;
  485.                                         break;
  486.                                     }
  487.                                 }
  488.                                 boolean find = false;
  489.                                 if(newPortType!=null && newPortType.sizeAzioneList()>0) {
  490.                                     for (Operation nuovaAzione : newPortType.getAzioneList()) {
  491.                                         if(nuovaAzione.getNome().equals(oldOperation.getNome())) {
  492.                                             find = true;
  493.                                             break;
  494.                                         }
  495.                                     }
  496.                                 }
  497.                                 if(!find) {
  498.                                     operationDaEliminare.add(oldOperation);
  499.                                 }
  500.                             }
  501.                         }
  502.                         while(!operationDaEliminare.isEmpty()) {
  503.                             Operation opDaEliminare = operationDaEliminare.remove(0);
  504.                             for (int i = 0; i < oldPortType.sizeAzioneList(); i++) {
  505.                                 if(oldPortType.getAzione(i).getNome().equals(opDaEliminare.getNome())) {
  506.                                     oldPortType.removeAzione(i);
  507.                                    
  508.                                     IDPortType idPortType = new IDPortType();
  509.                                     idPortType.setIdAccordo(idAccordo);
  510.                                     idPortType.setNome(oldPortType.getNome());
  511.                                    
  512.                                     IDPortTypeAzione idPortTypeAzione = new IDPortTypeAzione();
  513.                                     idPortTypeAzione.setIdPortType(idPortType);
  514.                                     idPortTypeAzione.setNome(opDaEliminare.getNome());
  515.                                     operationEliminate.add(idPortTypeAzione);
  516.                                    
  517.                                     break;
  518.                                 }
  519.                             }
  520.                         }
  521.                     }
  522.                 }
  523.             }
  524.         }
  525.         catch (Exception e) {
  526.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  527.             throw e;
  528.         }
  529.     }
  530.    
  531.     private static final String WSDL_PREFIX = "WsdlTypes_";
  532.     private static final String COMPARE_ERROR = "Compare external failed: ";
  533.    
  534.     public void estraiSchemiFromWSDLTypesAsAllegati(AccordoServizioParteComune as, byte[] wsdl, String tipoWSDL, Map<String, byte[]> schemiAggiuntiInQuestaOperazione) throws Exception{
  535.                
  536.         String nomeMetodo = "addSchemiFromWSDLTypesAsAllegati";
  537.         try {
  538.            
  539.             // Allegati
  540.             List<byte[]> schemiPresentiInternamenteTypes = new ArrayList<>();
  541.             List<String> nomiSchemiPresentiInternamenteTypes = new ArrayList<>();
  542.            
  543.             try{
  544.                
  545.                 AbstractXMLUtils xmlUtils = MessageXMLUtils.DEFAULT;
  546.                 XSDUtils xsdUtils = new XSDUtils(xmlUtils);
  547.                 WSDLUtilities wsdlUtilities = new WSDLUtilities(xmlUtils);
  548.                
  549.                 ArchiviCore archiviCore = null;
  550.                
  551.                 Document dConAllegati = xmlUtils.newDocument(wsdl);
  552.                
  553.                 Map<String, String> declarationNamespacesWSDL = xmlUtils.getNamespaceDeclaration(dConAllegati.getDocumentElement());
  554.                
  555.                 List<Node> schemi = wsdlUtilities.getSchemiXSD(dConAllegati);
  556.                 if(!schemi.isEmpty()){
  557.                     wsdlUtilities.removeSchemiIntoTypes(dConAllegati);
  558.                    
  559.                     for (int i = 0; i < schemi.size(); i++) {
  560.                        
  561.                         Node schema = schemi.get(i);
  562.                        
  563.                         // NOTA: Volendo si potrebbe utilizzare la solita gestione anche per schemi che hanno solo gli include
  564.                         //        Al momento dell'implementazione non avevo chiaro però se era utile avere tra gli XSD Collections (utilizzati per la validazione)
  565.                         //        Lo schema originale che contiene gli include (per cambi di namespace)
  566.                         //        Quindi ho preferito tenere l'originale schema con gli include tra gli schemi e qua importarlo.
  567.                         boolean schemaWithOnlyImport = xsdUtils.isSchemaWithOnlyImports(schema);
  568.                         if(schemaWithOnlyImport){
  569.                             // riaggiungo l'import
  570.                             wsdlUtilities.addSchemaIntoTypes(dConAllegati, schema);
  571.                             continue;
  572.                         }
  573.                        
  574.                         if(archiviCore==null)
  575.                             archiviCore = new ArchiviCore(this);
  576.                        
  577.                         String targetNamespace = xsdUtils.getTargetNamespace(schema);
  578.                         if(targetNamespace!=null){
  579.                                            
  580.                             if(declarationNamespacesWSDL!=null && declarationNamespacesWSDL.size()>0){
  581.                                 xmlUtils.addNamespaceDeclaration(declarationNamespacesWSDL, (Element) schema);
  582.                             }  
  583.                            
  584.                             String nomeSchema = null;
  585.                             if(schemiAggiuntiInQuestaOperazione!=null && schemiAggiuntiInQuestaOperazione.size()>0){
  586.                                 for (String nomeFile : schemiAggiuntiInQuestaOperazione.keySet()) {
  587.                                     byte[] content = schemiAggiuntiInQuestaOperazione.get(nomeFile);
  588.                                     // check se si tratta di questo documento
  589.                                     try{
  590.                                         String tmp = checkXsdAlreadyExists(xmlUtils, content, nomeFile, schema);
  591.                                         if(tmp!=null){
  592.                                             nomeSchema = tmp;
  593.                                             break;
  594.                                         }
  595.                                     }catch(Exception t){
  596.                                         logError(COMPARE_ERROR+t.getMessage(),t);
  597.                                     }
  598.                                 }
  599.                             }
  600.                             if(nomeSchema==null &&
  601.                                 as.getByteWsdlDefinitorio()!=null){
  602.                                 // check se si tratta di questo documento
  603.                                 try{
  604.                                     String tmp = checkXsdAlreadyExists(xmlUtils, as.getByteWsdlDefinitorio(),
  605.                                             Costanti.OPENSPCOOP2_ARCHIVE_ACCORDI_FILE_WSDL_INTERFACCIA_DEFINITORIA, schema);
  606.                                     if(tmp!=null){
  607.                                         nomeSchema = tmp;
  608.                                     }
  609.                                 }catch(Exception t){
  610.                                     logError(COMPARE_ERROR+t.getMessage(),t);
  611.                                 }
  612.                             }
  613.                             if(nomeSchema==null &&
  614.                                 as.sizeAllegatoList()>0){
  615.                                 for (Documento docTMP : as.getAllegatoList()) {
  616.                                     String nomeDocumento = docTMP.getFile();
  617.                                     try{
  618.                                         Documento doc = archiviCore.getDocumento(docTMP.getId(),true);
  619.                                         if(xsdUtils.isXSDSchema(doc.getByteContenuto())){
  620.                                             // check se si tratta di questo documento
  621.                                             String tmp = checkXsdAlreadyExists(xmlUtils, doc.getByteContenuto(), nomeDocumento, schema);
  622.                                             if(tmp!=null){
  623.                                                 nomeSchema = tmp;
  624.                                                 break;
  625.                                             }
  626.                                         }
  627.                                     }catch(Exception t){
  628.                                         logError(COMPARE_ERROR+t.getMessage(),t);
  629.                                     }
  630.                                 }
  631.                             }
  632.                             if(nomeSchema==null &&
  633.                                 as.sizeSpecificaSemiformaleList()>0){
  634.                                 for (Documento docTMP : as.getSpecificaSemiformaleList()) {
  635.                                     String nomeDocumento = docTMP.getFile();
  636.                                     try{
  637.                                         Documento doc = archiviCore.getDocumento(docTMP.getId(),true);
  638.                                         if(xsdUtils.isXSDSchema(doc.getByteContenuto())){
  639.                                             // check se si tratta di questo documento
  640.                                             String tmp = checkXsdAlreadyExists(xmlUtils, doc.getByteContenuto(), nomeDocumento, schema);
  641.                                             if(tmp!=null){
  642.                                                 nomeSchema = tmp;
  643.                                                 break;
  644.                                             }
  645.                                         }
  646.                                     }catch(Exception t){
  647.                                         logError(COMPARE_ERROR+t.getMessage(),t);
  648.                                     }
  649.                                 }
  650.                             }
  651.                            
  652.                             boolean alreadyExistsSchema = (nomeSchema!=null);
  653.                            
  654.                             if(nomeSchema==null){
  655.                                 // build new nome
  656.                                 int index = 1;
  657.                                 nomeSchema = WSDL_PREFIX+(index)+".xsd";
  658.                                 while(index<10000){ // 10000 allegati??
  659.                                     boolean found = false;
  660.                                     if(nomiSchemiPresentiInternamenteTypes.contains(nomeSchema)){
  661.                                         found = true;
  662.                                     }
  663.                                     else{
  664.                                         for (Documento vecchioAllegatoTMP : as.getAllegatoList()) {
  665.                                             if(vecchioAllegatoTMP.getFile().startsWith(WSDL_PREFIX) && vecchioAllegatoTMP.getFile().equals(nomeSchema)){
  666.                                                 found = true;
  667.                                                 break;
  668.                                             }
  669.                                         }
  670.                                     }
  671.                                     if(!found){
  672.                                         break;
  673.                                     }
  674.                                     index++;
  675.                                     nomeSchema = WSDL_PREFIX+(index)+".xsd";
  676.                                 }
  677.                             }
  678.                            
  679.                             wsdlUtilities.addImportSchemaIntoTypes(dConAllegati, targetNamespace, nomeSchema);
  680.                            
  681.                             if(!alreadyExistsSchema){
  682.                                
  683.                                 nomiSchemiPresentiInternamenteTypes.add(nomeSchema);
  684.                                 schemiPresentiInternamenteTypes.add(xmlUtils.toByteArray(schema));

  685.                             }
  686.                                                        
  687.                         }
  688.                         else{
  689.                             try{
  690.                                 logError("Presente schema senza targetNamespace? (Viene usato l'originale) ["+xmlUtils.toString(schema)+"]");
  691.                             }catch(Exception t){
  692.                                 logError("Presente schema senza targetNamespace? (Viene usato l'originale)");
  693.                             }
  694.                             wsdlUtilities.addSchemaIntoTypes(dConAllegati, schema);
  695.                         }
  696.                        
  697.                     }
  698.                    
  699.                     // Aggiorno WSDL
  700.                    
  701.                     byte [] wsdlPulito = xmlUtils.toByteArray(dConAllegati);
  702.                    
  703.                     if(AccordiServizioParteComuneCostanti.TIPO_WSDL_CONCETTUALE.equals(tipoWSDL)){
  704.                         as.setByteWsdlConcettuale(wsdlPulito);
  705.                     }
  706.                     else if(AccordiServizioParteComuneCostanti.TIPO_WSDL_EROGATORE.equals(tipoWSDL)){
  707.                         as.setByteWsdlLogicoErogatore(wsdlPulito);
  708.                     }
  709.                     else if(AccordiServizioParteComuneCostanti.TIPO_WSDL_FRUITORE.equals(tipoWSDL)){
  710.                         as.setByteWsdlLogicoFruitore(wsdlPulito);
  711.                     }
  712.                 }
  713.                
  714.             }catch(Exception t){
  715.                 logError("Errore durante la lettura degli schemi presenti all'interno del wsdl: "+t.getMessage(),t);
  716.             }
  717.            
  718.             if(!nomiSchemiPresentiInternamenteTypes.isEmpty()){
  719.                 int i = 0;
  720.                 for (String nomeNuovoAllegato : nomiSchemiPresentiInternamenteTypes) {
  721.                    
  722.                     try{
  723.                    
  724.                         if(schemiAggiuntiInQuestaOperazione!=null){
  725.                             schemiAggiuntiInQuestaOperazione.put(nomeNuovoAllegato, schemiPresentiInternamenteTypes.get(i));
  726.                         }
  727.                        
  728.                         Documento vecchioAllegato = null;
  729.                         for (Documento vecchioAllegatoTMP : as.getAllegatoList()) {
  730.                             if(vecchioAllegatoTMP.getFile().equals(nomeNuovoAllegato)){
  731.                                 vecchioAllegato = vecchioAllegatoTMP;
  732.                                 break;
  733.                             }
  734.                         }
  735.                        
  736.                         // non ho trovato l'elemento corrente nel aspc destinazione
  737.                         if(vecchioAllegato == null){
  738.                             Documento allegato = new Documento();
  739.                             allegato.setRuolo(RuoliDocumento.allegato.toString());
  740.                             allegato.setByteContenuto(schemiPresentiInternamenteTypes.get(i));
  741.                             allegato.setFile(nomeNuovoAllegato);
  742.                             allegato.setTipo("xsd");
  743.                             allegato.setIdProprietarioDocumento(as.getId());
  744.                             as.addAllegato(allegato);
  745.                         } else {
  746.                            
  747.                             // CASO CHE NON DOVREBBE OCCORRERE MAI VISTO LA LOGICA CON XMLDIFF
  748.                            
  749.                             // ho trovato l'elemento, aggiorno i valori
  750.                             vecchioAllegato.setByteContenuto(schemiPresentiInternamenteTypes.get(i));
  751.                         }
  752.                        
  753.                         i++;
  754.                        
  755.                     }catch(Exception t){
  756.                         logError("Errore durante l'aggiornamento dello schema ["+nomeNuovoAllegato+"] estratto dal wsdl: "+t.getMessage(),t);
  757.                     }
  758.                 }
  759.             }
  760.            
  761.         }
  762.         catch (Exception e) {
  763.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  764.             throw e;
  765.         }
  766.     }
  767.    
  768.     private String checkXsdAlreadyExists(AbstractXMLUtils xmlUtils, byte[] xsdEsistenteContenuto, String xsdEsistenteNome,  Node schema){
  769.         // check se si tratta di questo documento
  770.         try{
  771.             Node n = xmlUtils.newElement(xsdEsistenteContenuto);
  772.             XMLDiff xmlDiff = new XMLDiff(OpenSPCoop2MessageFactory.getDefaultMessageFactory());
  773.             /**System.out.println("["+vecchioAllegatoTMP.getFile()+"] N:"+xmlUtils.toString(n));
  774.             System.out.println("["+vecchioAllegatoTMP.getFile()+"] Schema:"+xmlUtils.toString(schema));*/
  775.             // NOTA: la ricostruzione in N2 è necessaria, poichè schema si porta dietro il definition wsdl (non ho capito perchè)
  776.             Node n2 = xmlUtils.newElement(xmlUtils.toString(schema).getBytes());
  777.             /**System.out.println("["+vecchioAllegatoTMP.getFile()+"] N2:"+xmlUtils.toString(n2));*/
  778.             if(xmlDiff.diff(n, n2)){
  779.                 return xsdEsistenteNome;
  780.                 /**System.out.println("["+vecchioAllegatoTMP.getFile()+"] TROVATO UGUALE ["+nomeSchema+"]");*/
  781.             }
  782.             /**else{
  783.                 System.out.println("["+vecchioAllegatoTMP.getFile()+"] TROVATO NON UGUALE: \n"+xmlDiff.getDifferenceDetails());
  784.             }*/
  785.             return null;
  786.         }catch(Exception t){
  787.             logError("Compare failed: "+t.getMessage(),t);
  788.             return null;
  789.         }
  790.     }
  791.    
  792.     public String readEndpoint(AccordoServizioParteComuneSintetico as, String portTypeParam, String servcorr,
  793.             BinaryParameter wsdlimpler, BinaryParameter wsdlimplfru) {
  794.         if(as==null) {
  795.             return null;
  796.         }
  797.         String portType = portTypeParam;
  798.         if("".equals(portTypeParam)) {
  799.             portType = null;
  800.         }
  801.         try {
  802.             FormatoSpecifica formato = as.getFormatoSpecifica();
  803.             String urlSuggerita = null;
  804.             switch (formato) {
  805.             case OPEN_API_3:
  806.             case SWAGGER_2:
  807.             case WADL:
  808.                 if(as.getByteWsdlConcettuale()!=null) {
  809.                     IApiReader apiReader = null;
  810.                     if(FormatoSpecifica.OPEN_API_3.equals(formato)) {
  811.                         apiReader = ApiFactory.newApiReader(ApiFormats.OPEN_API_3);
  812.                     }
  813.                     else if(FormatoSpecifica.SWAGGER_2.equals(formato)) {
  814.                         apiReader = ApiFactory.newApiReader(ApiFormats.SWAGGER_2);
  815.                     }
  816.                     else {
  817.                         apiReader = ApiFactory.newApiReader(ApiFormats.WADL);
  818.                     }
  819.                     ApiReaderConfig config = new ApiReaderConfig();
  820.                     config.setProcessInclude(false);
  821.                     config.setProcessInlineSchema(false);
  822.                     apiReader.init(LoggerWrapperFactory.getLogger(AccordiServizioParteSpecificaAdd.class), as.getByteWsdlConcettuale(), config);
  823.                     Api api = apiReader.read();
  824.                     if(api.getBaseURL()!=null) {
  825.                         urlSuggerita = api.getBaseURL().toString();
  826.                     }
  827.                 }
  828.                 break;
  829.             case WSDL_11:
  830.                 byte [] wsdl = null;
  831.                 if(ServletUtils.isCheckBoxEnabled(servcorr)) {
  832.                     if(wsdlimplfru!=null && wsdlimplfru.getValue()!=null) {
  833.                         wsdl = wsdlimplfru.getValue();
  834.                     }
  835.                     else {
  836.                         wsdl = as.getByteWsdlLogicoFruitore();
  837.                     }
  838.                 }
  839.                 else{
  840.                     if(wsdlimpler!=null && wsdlimpler.getValue()!=null) {
  841.                         wsdl = wsdlimpler.getValue();
  842.                     }
  843.                     else {
  844.                         wsdl = as.getByteWsdlLogicoErogatore();
  845.                     }
  846.                 }
  847.                 if(wsdl==null) {
  848.                     wsdl = as.getByteWsdlConcettuale();
  849.                 }
  850.                 if(wsdl!=null) {
  851.                     WSDLUtilities utilities = new WSDLUtilities(org.openspcoop2.utils.xml.XMLUtils.getInstance());
  852.                     urlSuggerita = utilities.getServiceEndpoint(wsdl, portType);
  853.                 }
  854.                 break;
  855.             }
  856.             return urlSuggerita;
  857.         }catch(Throwable t){
  858.             if( portType!=null || !FormatoSpecifica.WSDL_11.equals(as.getFormatoSpecifica()) ) {
  859.                 log.error("Read endpoint from interface failed: "+t.getMessage(),t);
  860.             }
  861.             else {
  862.                 log.debug("Read endpoint from interface failed: "+t.getMessage(),t);
  863.             }
  864.             return null;
  865.         }
  866.     }
  867. }