IDAccordoFactory.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;

  21. import org.openspcoop2.core.id.IDAccordo;
  22. import org.openspcoop2.core.id.IDSoggetto;
  23. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  24. import org.openspcoop2.core.registry.beans.AccordoServizioParteComuneSintetico;

  25. /**
  26.  * IDAccordoFactory
  27.  *
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class IDAccordoFactory {

  34.     private static final String ACCORDO_NON_FORNITO = "Accordo non fornito";
  35.    
  36.     private static IDAccordoFactory factory = null;
  37.     private static synchronized void init(){
  38.         if(IDAccordoFactory.factory==null){
  39.             IDAccordoFactory.factory = new IDAccordoFactory();
  40.         }
  41.     }
  42.     public static IDAccordoFactory getInstance(){
  43.         if(IDAccordoFactory.factory==null){
  44.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  45.             synchronized (IDAccordoFactory.class) {
  46.                 IDAccordoFactory.init();
  47.             }
  48.         }
  49.         return IDAccordoFactory.factory;
  50.     }
  51.    
  52.     private IDAccordoFactory() {}
  53.    
  54.     @SuppressWarnings("deprecation")
  55.     private IDAccordo build(String nome,IDSoggetto soggettoReferente,Integer versione){
  56.         IDAccordo idAccordo = new IDAccordo();
  57.         idAccordo.setNome(nome);
  58.         idAccordo.setSoggettoReferente(soggettoReferente);
  59.         idAccordo.setVersione(versione);
  60.         return idAccordo;
  61.     }
  62.    
  63.     public String normalizeUri(String uri) throws DriverRegistroServiziException{
  64.         // La uri può non contenere la versione, che invece nella 3.0 è obbligatoria.
  65.         // Facendo la doppia conversione, viene aggiunta la versione di default
  66.         IDAccordo idAccordo = this.getIDAccordoFromUri(uri);
  67.         return this.getUriFromIDAccordo(idAccordo);
  68.     }
  69.    
  70.     public String getUriFromIDAccordo(IDAccordo idAccordo) throws DriverRegistroServiziException{
  71.         if(idAccordo==null){
  72.             throw new DriverRegistroServiziException("IDAccordo non fornito");
  73.         }
  74.         if(idAccordo.getNome()==null){
  75.             throw new DriverRegistroServiziException("Nome accordo non fornito");
  76.         }
  77.         IDSoggetto soggettoReferente = idAccordo.getSoggettoReferente();
  78.         if(soggettoReferente!=null){
  79.             if(soggettoReferente.getTipo()==null){
  80.                 throw new DriverRegistroServiziException("Tipo soggetto referente non fornito?");
  81.             }
  82.             if(soggettoReferente.getNome()==null){
  83.                 throw new DriverRegistroServiziException("Nome soggetto referente non fornito?");
  84.             }
  85.         }
  86.         if(soggettoReferente!=null){
  87.             return soggettoReferente.toString() + ":" + idAccordo.getNome() + ":" + idAccordo.getVersione();
  88.         }else{
  89.             return idAccordo.getNome() + ":" + idAccordo.getVersione();
  90.         }
  91.     }
  92.    
  93.     public String getUriFromAccordo(AccordoServizioParteComune accordo)  throws DriverRegistroServiziException{
  94.         if(accordo==null){
  95.             throw new DriverRegistroServiziException(ACCORDO_NON_FORNITO);
  96.         }
  97.         IDAccordo idAccordo = this.build(accordo.getNome(),BeanUtilities.getSoggettoReferenteID(accordo.getSoggettoReferente()),accordo.getVersione());
  98.         return this.getUriFromIDAccordo(idAccordo);
  99.     }
  100.     public String getUriFromAccordo(AccordoServizioParteComuneSintetico accordo)  throws DriverRegistroServiziException{
  101.         if(accordo==null){
  102.             throw new DriverRegistroServiziException(ACCORDO_NON_FORNITO);
  103.         }
  104.         IDAccordo idAccordo = this.build(accordo.getNome(),BeanUtilities.getSoggettoReferenteID(accordo.getSoggettoReferente()),accordo.getVersione());
  105.         return this.getUriFromIDAccordo(idAccordo);
  106.     }
  107.    
  108.     public String getUriFromValues(String nomeAS,String tipoSoggettoReferente,String nomeSoggettoReferente,Integer ver)  throws DriverRegistroServiziException{
  109.         if(nomeAS==null){
  110.             throw new DriverRegistroServiziException(ACCORDO_NON_FORNITO);
  111.         }
  112.         IDSoggetto soggettoReferente = null;
  113.         if(tipoSoggettoReferente!=null && nomeSoggettoReferente!=null)
  114.             soggettoReferente = new IDSoggetto(tipoSoggettoReferente,nomeSoggettoReferente);
  115.         IDAccordo idAccordo = this.build(nomeAS,soggettoReferente,ver);
  116.         return this.getUriFromIDAccordo(idAccordo);
  117.     }
  118.    
  119.     public String getUriFromValues(String nomeAS,IDSoggetto soggettoReferente,Integer ver)  throws DriverRegistroServiziException{
  120.         if(nomeAS==null){
  121.             throw new DriverRegistroServiziException(ACCORDO_NON_FORNITO);
  122.         }
  123.         if(soggettoReferente==null){
  124.             return this.getUriFromValues(nomeAS,null,null,ver);
  125.         }else{
  126.             return this.getUriFromValues(nomeAS,soggettoReferente.getTipo(),soggettoReferente.getNome(),ver);
  127.         }
  128.     }
  129.    
  130.     public IDAccordo getIDAccordoFromUri(String uriAccordo) throws DriverRegistroServiziException{
  131.         try{
  132.        
  133.             // possibili casi:
  134.             // nomeAccordo
  135.             // nomeAccordo:versione
  136.             // tipoSoggettoReferente/nomeSoggettoReferente:nomeAccordo:versione
  137.            
  138.             if(uriAccordo==null){
  139.                 throw new DriverRegistroServiziException("Uri accordo non fornita");
  140.             }
  141.             int primoMarcatore = uriAccordo.indexOf(":");
  142.             int secondoMarcatore = -1;
  143.             if(primoMarcatore>=0){
  144.                 secondoMarcatore = uriAccordo.indexOf(":",primoMarcatore+1);
  145.             }
  146.             int terzoMarcatore = -1;
  147.             if(secondoMarcatore>0){
  148.                 terzoMarcatore = uriAccordo.indexOf(":",secondoMarcatore+1);
  149.                 if(terzoMarcatore>0){
  150.                     throw new DriverRegistroServiziException("sintassi non corretta, possibili usi: nomeAccordo  nomeAccordo:versione  tipoSoggettoReferente/nomeSoggettoReferente:nomeAccordo:versione");
  151.                 }
  152.             }
  153.            
  154.             if(primoMarcatore<0){
  155.                 return this.build(uriAccordo,null,1);
  156.             }
  157.            
  158.             String tmp1 = null;
  159.             String tmp2 = null;
  160.             String tmp3 = null;
  161.             if(primoMarcatore>=0 && secondoMarcatore>0){
  162.                 tmp1 = uriAccordo.substring(0, primoMarcatore); //soggetto referente
  163.                 tmp2 = uriAccordo.substring((primoMarcatore+1), secondoMarcatore); // nome accordo
  164.                 tmp3 = uriAccordo.substring((secondoMarcatore+1),uriAccordo.length()); // versione
  165.                
  166.                 int versione = 1;
  167.                 try{
  168.                     versione = Integer.parseInt(tmp3);
  169.                 }catch(Exception e){
  170.                     throw new DriverRegistroServiziException("sintassi della versione non corretta: "+e.getMessage(),e);
  171.                 }
  172.                
  173.                 int divisorioSoggettoReferente = tmp1.indexOf("/");
  174.                 if(divisorioSoggettoReferente<=0){
  175.                     throw new DriverRegistroServiziException("sintassi del soggetto referente non corretta, l'uri deve essere definita con la seguente forma: tipoSoggettoReferente/nomeSoggettoReferente:nomeAccordo:versione");
  176.                 }
  177.                 String tipoSoggettoReferente = tmp1.substring(0,divisorioSoggettoReferente);
  178.                 String nomeSoggettoReferente = tmp1.substring((divisorioSoggettoReferente+1),tmp1.length());
  179.                 return this.build(tmp2,new IDSoggetto(tipoSoggettoReferente,nomeSoggettoReferente),versione);
  180.             }else {
  181.                 tmp1 = uriAccordo.substring(0, primoMarcatore);
  182.                 tmp2 = uriAccordo.substring((primoMarcatore+1),uriAccordo.length());
  183.                
  184.                 int divisorioSoggettoReferente = tmp1.indexOf("/");
  185.                 if(divisorioSoggettoReferente<0){
  186.                     // nomeAccordo:versione
  187.                    
  188.                     int versione = 1;
  189.                     try{
  190.                         versione = Integer.parseInt(tmp2);
  191.                     }catch(Exception e){
  192.                         throw new DriverRegistroServiziException("sintassi della versione non corretta: "+e.getMessage(),e);
  193.                     }
  194.                    
  195.                     return this.build(tmp1,null,versione);
  196.                 }else if(divisorioSoggettoReferente==0){
  197.                     throw new DriverRegistroServiziException("sintassi non corretta, possibili usi: nomeAccordo  tipoSoggettoReferente/nomeSoggettoReferente:nomeAccordo  nomeAccordo:versione  tipoSoggettoReferente/nomeSoggettoReferente:nomeAccordo:versione");
  198.                 }else{
  199.                     // tipoSoggettoReferente/nomeSoggettoReferente:nomeAccordo
  200.                     String tipoSoggettoReferente = tmp1.substring(0,divisorioSoggettoReferente);
  201.                     String nomeSoggettoReferente = tmp1.substring((divisorioSoggettoReferente+1),tmp1.length());
  202.                     return this.build(tmp2,new IDSoggetto(tipoSoggettoReferente,nomeSoggettoReferente),1);
  203.                 }
  204.             }
  205.        
  206.         }catch(Exception e){
  207.             throw new DriverRegistroServiziException("Parsing uriAccordo["+uriAccordo+"] non riusciuto: "+e.getMessage());
  208.         }
  209.     }
  210.    
  211.     public IDAccordo getIDAccordoFromAccordo(AccordoServizioParteComune accordo) throws DriverRegistroServiziException{
  212.         if(accordo==null){
  213.             throw new DriverRegistroServiziException(ACCORDO_NON_FORNITO);
  214.         }
  215.         return this.build(accordo.getNome(),BeanUtilities.getSoggettoReferenteID(accordo.getSoggettoReferente()),accordo.getVersione());
  216.     }
  217.    
  218.     public IDAccordo getIDAccordoFromAccordo(AccordoServizioParteComuneSintetico accordo) throws DriverRegistroServiziException{
  219.         if(accordo==null){
  220.             throw new DriverRegistroServiziException(ACCORDO_NON_FORNITO);
  221.         }
  222.         return this.build(accordo.getNome(),BeanUtilities.getSoggettoReferenteID(accordo.getSoggettoReferente()),accordo.getVersione());
  223.     }
  224.    
  225.     public IDAccordo getIDAccordoFromValues(String nomeAS,String tipoSoggettoReferente,String nomeSoggettoReferente,Integer ver) throws DriverRegistroServiziException{
  226.         if(nomeAS==null){
  227.             throw new DriverRegistroServiziException(ACCORDO_NON_FORNITO);
  228.         }
  229.         IDSoggetto soggettoReferente = null;
  230.         if(tipoSoggettoReferente!=null && nomeSoggettoReferente!=null)
  231.             soggettoReferente = new IDSoggetto(tipoSoggettoReferente,nomeSoggettoReferente);
  232.         return this.build(nomeAS,soggettoReferente,ver);
  233.     }
  234.     public IDAccordo getIDAccordoFromValuesWithoutCheck(String nomeAS,String tipoSoggettoReferente,String nomeSoggettoReferente,Integer ver) {
  235.         IDSoggetto soggettoReferente = new IDSoggetto(tipoSoggettoReferente,nomeSoggettoReferente);
  236.         return this.build(nomeAS,soggettoReferente,ver);
  237.     }
  238.     public IDAccordo getIDAccordoFromValues(String nomeAS,IDSoggetto soggettoReferente,Integer ver) throws DriverRegistroServiziException{
  239.         if(soggettoReferente==null){
  240.             return this.getIDAccordoFromValues(nomeAS,null,null,ver);
  241.         }else{
  242.             return this.getIDAccordoFromValues(nomeAS,soggettoReferente.getTipo(),soggettoReferente.getNome(),ver);
  243.         }
  244.     }
  245.    
  246. }