ApacheIdentifierGenerator.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.utils.id;

  21. import org.openspcoop2.utils.id.apache.IdentifierGenerator;
  22. import org.openspcoop2.utils.id.apache.serial.AlphanumericGenerator;
  23. import org.openspcoop2.utils.id.apache.serial.EnumTypeGenerator;
  24. import org.openspcoop2.utils.id.apache.serial.LongGenerator;
  25. import org.openspcoop2.utils.id.apache.serial.NumericGenerator;
  26. import org.openspcoop2.utils.id.apache.serial.PrefixedAlphanumericGenerator;
  27. import org.openspcoop2.utils.id.apache.serial.PrefixedLeftPaddedNumericGenerator;
  28. import org.openspcoop2.utils.id.apache.serial.PrefixedNumericGenerator;
  29. import org.openspcoop2.utils.id.apache.serial.TimeBasedAlphanumericIdentifierGenerator;


  30. /**
  31.  * Implementazione tramite java.util.UUID
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class ApacheIdentifierGenerator implements IUniqueIdentifierGenerator {

  38.     private IdentifierGenerator identifierGenerator;
  39.    
  40.     @Override
  41.     public IUniqueIdentifier newID() throws UniqueIdentifierException {
  42.         try{
  43.             return new BaseUniqueIdentifier(this.identifierGenerator.nextIdentifier());
  44.         }catch(Exception e){
  45.             throw new UniqueIdentifierException(e.getMessage(),e);
  46.         }
  47.     }

  48.     @Override
  49.     public IUniqueIdentifier convertFromString(String value)
  50.             throws UniqueIdentifierException {
  51.         BaseUniqueIdentifier id = new BaseUniqueIdentifier(value);
  52.         return id;
  53.     }

  54.     public void initialize(ApacheGeneratorConfiguration config) throws UniqueIdentifierException {
  55.         this.init(config);
  56.     }
  57.    
  58.     @Override
  59.     public void init(Object... o) throws UniqueIdentifierException {
  60.         if(o==null || o.length<1){
  61.             throw new UniqueIdentifierException("Devi indicare (tramite il primo parametro) la configurazione di ApacheGenerator tramite la classe: "+ApacheGeneratorConfiguration.class.getName());
  62.         }
  63.         if(!(o[0] instanceof ApacheGeneratorConfiguration)){
  64.             throw new UniqueIdentifierException("Devi indicare (tramite il primo parametro) la configurazione di ApacheGenerator tramite la classe: "+ApacheGeneratorConfiguration.class.getName());
  65.         }
  66.        
  67.         ApacheGeneratorConfiguration config = (ApacheGeneratorConfiguration) o[0];
  68.         if(config.getType()==null){
  69.             throw new UniqueIdentifierException("Il tipo di ApacheGenerator deve essere indicato nella configurazione");
  70.         }
  71.        
  72.         try{
  73.             switch (config.getType()) {
  74.             case ALPHANUMERIC:
  75.             case PREFIXED_ALPHANUMERIC:
  76.                
  77.                 if(EnumTypeGenerator.PREFIXED_ALPHANUMERIC.equals(config.getType())){
  78.                     if(config.getPrefix()==null){
  79.                         throw new UniqueIdentifierException("Il tipo di ApacheGenerator indicato nella configurazione richiede un prefisso");
  80.                     }
  81.                 }
  82.                
  83.                 if(config.getInitialStringValue()!=null){
  84.                     if(EnumTypeGenerator.ALPHANUMERIC.equals(config.getType())){
  85.                         this.identifierGenerator = new AlphanumericGenerator(config.isWrap(),config.getInitialStringValue());
  86.                     }
  87.                     else{
  88.                         this.identifierGenerator = new PrefixedAlphanumericGenerator(config.getPrefix(),config.isWrap(),config.getInitialStringValue());
  89.                     }
  90.                 }
  91.                 else if(config.getSize()!=null){
  92.                     if(EnumTypeGenerator.ALPHANUMERIC.equals(config.getType())){
  93.                         this.identifierGenerator = new AlphanumericGenerator(config.isWrap(),config.getSize());
  94.                     }
  95.                     else{
  96.                         this.identifierGenerator = new PrefixedAlphanumericGenerator(config.getPrefix(),config.isWrap(),config.getSize());
  97.                     }
  98.                 }
  99.                 else{
  100.                     if(EnumTypeGenerator.ALPHANUMERIC.equals(config.getType())){
  101.                         this.identifierGenerator = new AlphanumericGenerator(config.isWrap());
  102.                     }
  103.                     else{
  104.                         this.identifierGenerator = new PrefixedAlphanumericGenerator(config.getPrefix(),config.isWrap());
  105.                     }
  106.                 }
  107.                
  108.                 if(config.getEndDigit()!=null && config.getStartDigit()!=null){
  109.                     ((AlphanumericGenerator)this.identifierGenerator).setStartEndDigit(config.getStartDigit(), config.getEndDigit());
  110.                 }
  111.                 else if(config.getEndDigit()!=null){
  112.                     ((AlphanumericGenerator)this.identifierGenerator).setEndDigit(config.getEndDigit());
  113.                 }
  114.                 else if(config.getStartDigit()!=null){
  115.                     ((AlphanumericGenerator)this.identifierGenerator).setStartDigit(config.getStartDigit());
  116.                 }
  117.                
  118.                 if(config.getEndLetter()!=null && config.getStartLetter()!=null){
  119.                     ((AlphanumericGenerator)this.identifierGenerator).setStartEndChar(config.getStartLetter(), config.getEndLetter());
  120.                 }
  121.                 else if(config.getEndLetter()!=null){
  122.                     ((AlphanumericGenerator)this.identifierGenerator).setEndChar(config.getEndLetter());
  123.                 }
  124.                 else if(config.getStartLetter()!=null){
  125.                     ((AlphanumericGenerator)this.identifierGenerator).setStartChar(config.getStartLetter());
  126.                 }
  127.                        
  128.                 if(config.isEnableLowerCaseLetter()!=null){
  129.                     ((AlphanumericGenerator)this.identifierGenerator).setLowerChar(config.isEnableLowerCaseLetter());  
  130.                 }
  131.                 if(config.isEnableUpperCaseLetter()!=null){
  132.                     ((AlphanumericGenerator)this.identifierGenerator).setUpperChar(config.isEnableUpperCaseLetter());  
  133.                 }
  134.                
  135.                 break;
  136.                
  137.             case NUMERIC:
  138.             case PREFIXED_NUMERIC:
  139.                
  140.                 if(EnumTypeGenerator.PREFIXED_NUMERIC.equals(config.getType())){
  141.                     if(config.getPrefix()==null){
  142.                         throw new UniqueIdentifierException("Il tipo di ApacheGenerator indicato nella configurazione richiede un prefisso");
  143.                     }
  144.                 }
  145.                 if(config.getInitalLongValue()==null){
  146.                     throw new UniqueIdentifierException("Il tipo di ApacheGenerator indicato nella configurazione richiede il valore iniziale di tipo Long ('initialLongValue')");
  147.                 }
  148.                

  149.                 if(EnumTypeGenerator.NUMERIC.equals(config.getType())){
  150.                     this.identifierGenerator = new NumericGenerator(config.isWrap(),config.getInitalLongValue());
  151.                 }
  152.                 else{
  153.                     this.identifierGenerator = new PrefixedNumericGenerator(config.getPrefix(),config.isWrap(),config.getInitalLongValue());
  154.                 }
  155.                
  156.                 break;
  157.    
  158.             case PREFIXED_LEFT_PADDED_NUMERIC:
  159.                
  160.                 if(EnumTypeGenerator.PREFIXED_ALPHANUMERIC.equals(config.getType())){
  161.                     if(config.getPrefix()==null){
  162.                         throw new UniqueIdentifierException("Il tipo di ApacheGenerator indicato nella configurazione richiede un prefisso");
  163.                     }
  164.                 }
  165.                 if(config.getSize()==null){
  166.                     throw new UniqueIdentifierException("Il tipo di ApacheGenerator indicato nella configurazione richiede la dimensione ('size')");
  167.                 }
  168.                
  169.                 this.identifierGenerator = new PrefixedLeftPaddedNumericGenerator(config.getPrefix(),config.isWrap(),config.getSize());
  170.                
  171.                 break;
  172.                
  173.             case TIME_BASED_ALPHANUMERIC:
  174.                                
  175.                 this.identifierGenerator = new TimeBasedAlphanumericIdentifierGenerator();
  176.                
  177.                 break;
  178.                
  179.             case LONG:
  180.                
  181.                 if(config.getInitalLongValue()==null){
  182.                     throw new UniqueIdentifierException("Il tipo di ApacheGenerator indicato nella configurazione richiede il valore iniziale di tipo Long ('initialLongValue')");
  183.                 }
  184.                
  185.                 this.identifierGenerator = new LongGenerator(config.isWrap(),config.getInitalLongValue());
  186.                
  187.                 break;
  188.                
  189.             default:
  190.                 break;
  191.             }
  192.         }catch(Exception e){
  193.             throw new UniqueIdentifierException(e.getMessage(),e);
  194.         }
  195.     }

  196. }