IDSoggetto.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.id;

  21. /**
  22.  * Classe utilizzata per rappresentare un Soggetto nel registro dei Servizi.
  23.  *
  24.  * @author Poli Andrea (apoli@link.it)
  25.  * @author Nardi Lorenzo (nardi@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */

  29. public class IDSoggetto implements java.io.Serializable, Cloneable {

  30.      /**
  31.      * serialVersionUID
  32.      */
  33.     private static final long serialVersionUID = 1L;

  34.     protected String tipo;

  35.     protected String nome;

  36.     protected String codicePorta;
  37.    
  38.    
  39.     /* ********  C O S T R U T T O R E  ******** */

  40.    
  41.     /**
  42.      * Costruttore.
  43.      *
  44.      * @param tipo Tipo del Soggetto
  45.      * @param nome Nome del Soggetto
  46.      *
  47.      */
  48.     public IDSoggetto(String tipo, String nome){
  49.         this.tipo = tipo;
  50.         this.nome = nome;
  51.     }
  52.     /**
  53.      * Costruttore.
  54.      *
  55.      * @param tipo Tipo del Soggetto
  56.      * @param nome Nome del Soggetto
  57.      * @param codicePorta identificativo del dominio
  58.      *
  59.      */
  60.     public IDSoggetto(String tipo, String nome , String codicePorta){
  61.         this.tipo = tipo;
  62.         this.nome = nome;
  63.         this.codicePorta = codicePorta;
  64.     }
  65.     /**
  66.      * Costruttore.
  67.      *
  68.      *
  69.      */
  70.     public IDSoggetto(){}




  71.     /* ********  S E T T E R   ******** */

  72.     /**
  73.      * Imposta il nome del Soggetto.
  74.      *
  75.      * @param nome Nome del Soggetto.
  76.      *
  77.      */
  78.     public void setNome(String nome){
  79.         this.nome = nome;
  80.     }
  81.     /**
  82.      * Imposta il tipo del Soggetto
  83.      *
  84.      * @param tipo Tipo del Soggetto
  85.      *
  86.      */
  87.     public void setTipo(String tipo){
  88.         this.tipo = tipo;
  89.     }
  90.     /**
  91.      * Imposta il codice porta del Soggetto
  92.      *
  93.      * @param codicePorta Codice porta del Soggetto
  94.      *
  95.      */
  96.     public void setCodicePorta(String codicePorta){
  97.         this.codicePorta = codicePorta;
  98.     }

  99.    

  100.     /* ********  G E T T E R   ******** */

  101.     /**
  102.      * Ritorna il nome del Soggetto.
  103.      *
  104.      * @return Nome del Soggetto
  105.      *
  106.      */
  107.     public String getNome(){
  108.         return this.nome;
  109.     }
  110.     /**
  111.      * Ritorna il tipo del Soggetto
  112.      *
  113.      * @return Tipo del Soggetto
  114.      *
  115.      */
  116.     public String getTipo(){
  117.         return this.tipo;
  118.     }

  119.     /**
  120.      * Ritorna il codice porta del Soggetto
  121.      *
  122.      * @return Codice porta del Soggetto
  123.      *
  124.      */
  125.     public String getCodicePorta(){
  126.         return this.codicePorta;
  127.     }

  128.    
  129.    
  130.     @Override
  131.     public String toString(){
  132.         return this.tipo + "/" + this.nome;
  133.     }
  134.    
  135.     @Override
  136.     public boolean equals(Object soggetto){
  137.         return equalsEngine(soggetto, true);
  138.     }
  139.     protected boolean equalsEngine(Object soggetto, boolean verifyClass){
  140.         if(soggetto == null)
  141.             return false;
  142.         if(verifyClass) {
  143.             String objectClassName = soggetto.getClass().getName() + "";
  144.             if(!objectClassName.equals(this.getClass().getName())) {
  145.                 return false;
  146.             }
  147.         }
  148.         IDSoggetto id = (IDSoggetto) soggetto;
  149.         return equasEngine(id);
  150.     }
  151.     private boolean equasEngine(IDSoggetto id) {
  152.         // TIPO
  153.         if(this.getTipo()==null){
  154.             if(id.getTipo()!=null)
  155.                 return false;
  156.         }else{
  157.             if(!this.getTipo().equals(id.getTipo()))
  158.                 return false;
  159.         }
  160.         // NOME
  161.         if(this.getNome()==null){
  162.             if(id.getNome()!=null)
  163.                 return false;
  164.         }else{
  165.             if(!this.getNome().equals(id.getNome()))
  166.                 return false;
  167.         }
  168.        
  169.         return true;
  170.     }
  171.    
  172.     // Utile per usare l'oggetto in hashtable come chiave
  173.     @Override
  174.     public int hashCode(){
  175.         return this.toString().hashCode();
  176.     }
  177.    
  178.     @Override
  179.     public IDSoggetto clone(){
  180.         IDSoggetto clone = null;
  181.         try {
  182.             clone = (IDSoggetto) super.clone();
  183.         }catch(Exception t) {
  184.             clone = new IDSoggetto();
  185.         }
  186.        
  187.         clone.setCodicePorta(this.codicePorta!=null ? this.codicePorta+"" : null);
  188.         clone.setTipo(this.tipo!=null ? this.tipo+"" : null);
  189.         clone.setNome(this.nome!=null ? this.nome+"" : null);
  190.        
  191.         return clone;
  192.     }
  193.    
  194.    
  195.     public String toFormatString(){
  196.         StringBuilder sb = new StringBuilder();
  197.         sb.append(this.tipo);
  198.         sb.append("/");
  199.         sb.append(this.nome);
  200.         return sb.toString();
  201.     }
  202.    
  203.     public static IDSoggetto toIDSoggetto(String formatString) throws IDException {
  204.         String [] tmp = formatString.split("/");
  205.         if(tmp.length!=2) {
  206.             throw new IDException("Formato non supportato, attesi 2 valori, trovati "+tmp.length);
  207.         }
  208.         String tipo = tmp[0];
  209.         String nome = tmp[1];
  210.         IDSoggetto idSoggetto = new IDSoggetto();
  211.         idSoggetto.tipo=tipo;
  212.         idSoggetto.nome=nome;
  213.         return idSoggetto;
  214.     }
  215. }