IDBuilder.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 java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.core.id.IDAccordo;
  24. import org.openspcoop2.core.id.IDAccordoCooperazione;
  25. import org.openspcoop2.core.registry.AccordoCooperazione;
  26. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  27. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  28. import org.openspcoop2.core.registry.Documento;
  29. import org.openspcoop2.core.registry.Fruitore;
  30. import org.openspcoop2.core.registry.Gruppo;
  31. import org.openspcoop2.core.registry.PortType;
  32. import org.openspcoop2.core.registry.PortaDominio;
  33. import org.openspcoop2.core.registry.Ruolo;
  34. import org.openspcoop2.core.registry.Scope;
  35. import org.openspcoop2.core.registry.Soggetto;
  36. import org.openspcoop2.utils.serialization.IOException;



  37. /**
  38.  * Classe utilizzata per generare gli identificatori degli oggetti presenti nel registro dei Servizi.
  39.  *
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */

  45. public class IDBuilder implements org.openspcoop2.utils.serialization.IDBuilder {

  46.     protected boolean prefix = false;

  47.     // Factory
  48.     private IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
  49.     private IDAccordoCooperazioneFactory idAccordoCooperazioneFactory = IDAccordoCooperazioneFactory.getInstance();
  50.     private IDServizioFactory idServizioFactory = IDServizioFactory.getInstance();
  51.    
  52.     public IDBuilder(boolean insertClassNamePrefix){
  53.         this.prefix = insertClassNamePrefix;
  54.     }
  55.     public IDBuilder(){
  56.         this.prefix = false;
  57.     }
  58.    
  59.     public static IDBuilder getIDBuilder(){
  60.         return new IDBuilder();
  61.     }
  62.    
  63.     @Override
  64.     public String toID(Object o) throws IOException {
  65.        
  66.         if(o==null)
  67.             throw new IOException("Oggetto is null");
  68.        
  69.         try{
  70.        
  71.             if(o instanceof AccordoCooperazione){
  72.                 AccordoCooperazione ac = (AccordoCooperazione) o;
  73.                 String id = this.idAccordoCooperazioneFactory.getUriFromAccordo(ac);
  74.                 if(this.prefix){
  75.                     return "[AccordoCooperazione] "+id;
  76.                 }else{
  77.                     return id;
  78.                 }
  79.             }
  80.             else if(o instanceof AccordoServizioParteComune){
  81.                 AccordoServizioParteComune as = (AccordoServizioParteComune) o;
  82.                 String id = this.idAccordoFactory.getUriFromAccordo(as);
  83.                 if(this.prefix){
  84.                     return "[AS] "+ id;
  85.                 }else{
  86.                     return id;
  87.                 }
  88.             }
  89.             else if(o instanceof PortType){
  90.                 PortType p = (PortType) o;
  91.                 String id = "IDAccordo["+p.getIdAccordo()+"]_"+  p.getNome();
  92.                 if(this.prefix){
  93.                     return "[PortType] "+ id;
  94.                 }else{
  95.                     return id;
  96.                 }
  97.             }
  98.             else if(o instanceof Documento){
  99.                 Documento d = (Documento) o;
  100.                 String id = "["+d.getRuolo()+"]["+d.getTipo()+"]"+" "+d.getFile();
  101.                 if(this.prefix){
  102.                     return "[Documento] "+ id;
  103.                 }else{
  104.                     return id;
  105.                 }
  106.             }
  107.             else if(o instanceof PortaDominio){
  108.                 PortaDominio p = (PortaDominio) o;
  109.                 String id = p.getNome();
  110.                 if(this.prefix){
  111.                     return "[PdD] "+ id;
  112.                 }else{
  113.                     return id;
  114.                 }
  115.             }
  116.             else if (o instanceof Gruppo) {
  117.                 Gruppo g = (Gruppo) o;
  118.                 String id = g.getNome();
  119.                 if(this.prefix){
  120.                     return "[Gruppo] "+ id;
  121.                 }else{
  122.                     return id;
  123.                 }
  124.             }
  125.             else if(o instanceof Ruolo){
  126.                 Ruolo r = (Ruolo) o;
  127.                 String id = r.getNome();
  128.                 if(this.prefix){
  129.                     return "[Ruolo] "+ id;
  130.                 }else{
  131.                     return id;
  132.                 }
  133.             }
  134.             else if(o instanceof Scope){
  135.                 Scope s = (Scope) o;
  136.                 String id = s.getNome();
  137.                 if(this.prefix){
  138.                     return "[Scope] "+ id;
  139.                 }else{
  140.                     return id;
  141.                 }
  142.             }
  143.             else if(o instanceof AccordoServizioParteSpecifica){
  144.                 AccordoServizioParteSpecifica s = (AccordoServizioParteSpecifica) o;
  145.                 String id = this.idServizioFactory.getUriFromAccordo(s);
  146.                 if(this.prefix){
  147.                     return "[Servizio] "+ id;
  148.                 }else{
  149.                     return id;
  150.                 }
  151.             }
  152.             else if(o instanceof Fruitore){
  153.                 Fruitore fr = (Fruitore) o;
  154.                 String id = fr.getTipo()+"/"+fr.getNome();
  155.                 if(fr.getIdServizio()!=null && fr.getIdServizio()>0)
  156.                     id=id+"#idServizio:"+fr.getIdServizio();
  157.                 if(this.prefix){
  158.                     return "[FruitoreServizio] "+ id;
  159.                 }else{
  160.                     return id;
  161.                 }
  162.             }
  163.             else if(o instanceof Soggetto){
  164.                 Soggetto s = (Soggetto) o;
  165.                 String id = s.getTipo()+"/"+s.getNome();
  166.                 if(this.prefix){
  167.                     return "[Soggetto] "+ id;
  168.                 }else{
  169.                     return id;
  170.                 }
  171.             }
  172.                        
  173.         }catch(Exception e){
  174.             throw new IOException("Trasformazione non riuscita: "+e.getMessage(),e);
  175.         }
  176.        
  177.         throw new IOException("Tipo di Oggetto non gestito ["+o.getClass().getName()+"]");
  178.     }

  179.     @Override
  180.     public String toID(Object o, String field) throws IOException {
  181.         if(o instanceof Documento){
  182.             return this.toID(o);
  183.         }else{
  184.             return this.toID(o) + "." + field;
  185.         }
  186.     }

  187.     /**
  188.      * Genera il vecchio identificatore che identificava l'oggetto passato come parametro prima di un update in corso
  189.      * L'oggetto in corso deve essere valorizzato negli elementi old_XXX
  190.      *
  191.      * @param o Oggetto su cui generare un identificatore univoco
  192.      * @return identificatore univoco
  193.      */
  194.     @Override
  195.     public String toOldID(Object o) throws IOException{
  196.        
  197.         if(o==null)
  198.             throw new IOException("Oggetto is null");
  199.        
  200.         try{
  201.        
  202.             if(o instanceof AccordoCooperazione){
  203.                 AccordoCooperazione ac = (AccordoCooperazione) o;
  204.                 IDAccordoCooperazione idOLD = ac.getOldIDAccordoForUpdate();
  205.                 if(idOLD==null){
  206.                     return null; // non lancio un errore
  207.                 }
  208.                 String id = this.idAccordoCooperazioneFactory.getUriFromIDAccordo(idOLD);
  209.                 if(this.prefix){
  210.                     return "[AccordoCooperazione] "+id;
  211.                 }else{
  212.                     return id;
  213.                 }
  214.             }
  215.             else if(o instanceof AccordoServizioParteComune){
  216.                 AccordoServizioParteComune as = (AccordoServizioParteComune) o;
  217.                 IDAccordo idOLD = as.getOldIDAccordoForUpdate();
  218.                 if(idOLD==null){
  219.                     return null; // non lancio un errore
  220.                 }
  221.                 String id = this.idAccordoFactory.getUriFromIDAccordo(idOLD);
  222.                 if(this.prefix){
  223.                     return "[AS] "+ id;
  224.                 }else{
  225.                     return id;
  226.                 }
  227.             }
  228.             else if(o instanceof PortType){
  229.                 return null; // oggetto non modificabile nei dati identificativi
  230.             }
  231.             else if(o instanceof Documento){
  232.                 return null; // oggetto non modificabile nei dati identificativi
  233.             }
  234.             else if(o instanceof PortaDominio){
  235.                 PortaDominio p = (PortaDominio) o;
  236.                 if(p.getOldNomeForUpdate()==null){
  237.                     return null; // non lancio un errore
  238.                 }
  239.                 String id = p.getOldNomeForUpdate();
  240.                 if(this.prefix){
  241.                     return "[PdD] "+ id;
  242.                 }else{
  243.                     return id;
  244.                 }
  245.             }
  246.             else if(o instanceof Gruppo){
  247.                 Gruppo g = (Gruppo) o;
  248.                 if(g.getOldIDGruppoForUpdate()==null){
  249.                     return null; // non lancio un errore
  250.                 }
  251.                 String id = g.getOldIDGruppoForUpdate().getNome();
  252.                 if(this.prefix){
  253.                     return "[Gruppo] "+ id;
  254.                 }else{
  255.                     return id;
  256.                 }
  257.             }
  258.             else if(o instanceof Ruolo){
  259.                 Ruolo r = (Ruolo) o;
  260.                 if(r.getOldIDRuoloForUpdate()==null){
  261.                     return null; // non lancio un errore
  262.                 }
  263.                 String id = r.getOldIDRuoloForUpdate().getNome();
  264.                 if(this.prefix){
  265.                     return "[Ruolo] "+ id;
  266.                 }else{
  267.                     return id;
  268.                 }
  269.             }
  270.             else if(o instanceof Scope){
  271.                 Scope s = (Scope) o;
  272.                 if(s.getOldIDScopeForUpdate()==null){
  273.                     return null; // non lancio un errore
  274.                 }
  275.                 String id = s.getOldIDScopeForUpdate().getNome();
  276.                 if(this.prefix){
  277.                     return "[Scope] "+ id;
  278.                 }else{
  279.                     return id;
  280.                 }
  281.             }
  282.             else if(o instanceof AccordoServizioParteSpecifica){
  283.                 AccordoServizioParteSpecifica as = (AccordoServizioParteSpecifica) o;
  284.                 if(as.getOldIDServizioForUpdate()==null){
  285.                     return null; // non lancio un errore
  286.                 }
  287.                
  288.                 String id = this.idServizioFactory.getUriFromIDServizio(as.getOldIDServizioForUpdate());
  289.                 if(this.prefix){
  290.                     return "[Servizio] "+ id;
  291.                 }else{
  292.                     return id;
  293.                 }
  294.             }
  295.             else if(o instanceof Soggetto){
  296.                 Soggetto s = (Soggetto) o;
  297.                 if(s.getOldIDSoggettoForUpdate()==null){
  298.                     return null; // non lancio un errore
  299.                 }
  300.                 String id = s.getOldIDSoggettoForUpdate().toString();
  301.                 if(this.prefix){
  302.                     return "[Soggetto] "+ id;
  303.                 }else{
  304.                     return id;
  305.                 }
  306.             }
  307.                        
  308.         }catch(Exception e){
  309.             throw new IOException("Trasformazione non riuscita: "+e.getMessage(),e);
  310.         }
  311.        
  312.         throw new IOException("Tipo di Oggetto non gestito ["+o.getClass().getName()+"]");
  313.        
  314.     }
  315.    
  316.    
  317.     /**
  318.      * Ritorna gli oggetti gestiti
  319.      *
  320.      * @return oggetti gestiti
  321.      * @throws DriverException
  322.      */
  323.     @Override
  324.     public String[] getManagedObjects(boolean simpleName) throws IOException{
  325.         List<String> oggetti = new ArrayList<>();
  326.        
  327.         if(simpleName){
  328.             oggetti.add(AccordoCooperazione.class.getSimpleName());
  329.             oggetti.add(AccordoServizioParteComune.class.getSimpleName());
  330.             oggetti.add(PortType.class.getSimpleName());
  331.             oggetti.add(Documento.class.getSimpleName());
  332.             oggetti.add(PortaDominio.class.getSimpleName());
  333.             oggetti.add(Gruppo.class.getSimpleName());
  334.             oggetti.add(Ruolo.class.getSimpleName());
  335.             oggetti.add(Scope.class.getSimpleName());
  336.             oggetti.add(AccordoServizioParteSpecifica.class.getSimpleName());
  337.             oggetti.add(Fruitore.class.getSimpleName());
  338.             oggetti.add(Soggetto.class.getSimpleName());
  339.         }
  340.         else{
  341.             oggetti.add(AccordoCooperazione.class.getName());
  342.             oggetti.add(AccordoServizioParteComune.class.getName());
  343.             oggetti.add(PortType.class.getName());
  344.             oggetti.add(Documento.class.getName());
  345.             oggetti.add(PortaDominio.class.getName());
  346.             oggetti.add(Gruppo.class.getName());
  347.             oggetti.add(Ruolo.class.getName());
  348.             oggetti.add(Scope.class.getName());
  349.             oggetti.add(AccordoServizioParteSpecifica.class.getName());
  350.             oggetti.add(Fruitore.class.getName());
  351.             oggetti.add(Soggetto.class.getName());
  352.         }
  353.        
  354.         String[]tmp = new String[1];
  355.         return oggetti.toArray(tmp);
  356.     }
  357.    
  358.    
  359.     /**
  360.      * Ritorna un nome descrittivo dell'oggetto.
  361.      *
  362.      * @param o
  363.      * @return nome descrittivo dell'oggetto.
  364.      * @throws DriverException
  365.      */
  366.     @Override
  367.     public String getSimpleName(Object o) throws IOException{
  368.         return o.getClass().getSimpleName();
  369.     }
  370. }