MappingModeTypesExtensions.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.protocol.sdk.archive;

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

  25. import org.openspcoop2.protocol.sdk.ProtocolException;
  26. import org.openspcoop2.protocol.sdk.constants.ArchiveType;

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

  35.     private Map<String, List<ArchiveModeType>> mappingExtensionsTypes = new HashMap<String, List<ArchiveModeType>>();
  36.     private Map<String, ArchiveType> mappingExtensionsArchiveType = new HashMap<String, ArchiveType>();
  37.     private List<String> exts = new ArrayList<>(); // per garantire l'ordine di inserimento
  38.     private String preferExtSingleObject = null;
  39.    
  40.     public void add(String ext,ArchiveModeType type) throws ProtocolException{
  41.         this.add(ext, false, type);
  42.     }
  43.     public void add(String ext,boolean preferUseForSingleObject,ArchiveModeType type) throws ProtocolException{
  44.         this.add(ext, preferUseForSingleObject, null, type);
  45.     }
  46.     public void add(String ext,ArchiveType archiveType,ArchiveModeType type) throws ProtocolException{
  47.         this.add(ext, false, archiveType, type);
  48.     }
  49.     public void add(String ext,boolean preferUseForSingleObject,ArchiveType archiveType,ArchiveModeType type) throws ProtocolException{
  50.         List<ArchiveModeType> l = new ArrayList<ArchiveModeType>();
  51.         l.add(type);
  52.         this.add(ext, preferUseForSingleObject, archiveType, l);
  53.     }
  54.     public void add(String ext,ArchiveModeType ... types) throws ProtocolException{
  55.         this.add(ext, false, types);
  56.     }
  57.     public void add(String ext,boolean preferUseForSingleObject,ArchiveModeType ... types) throws ProtocolException{
  58.         this.add(ext, preferUseForSingleObject, null, types);
  59.     }
  60.     public void add(String ext,ArchiveType archiveType,ArchiveModeType ... types) throws ProtocolException{
  61.         this.add(ext,false, archiveType, types);
  62.     }
  63.     public void add(String ext,boolean preferUseForSingleObject,ArchiveType archiveType,ArchiveModeType ... types) throws ProtocolException{
  64.         List<ArchiveModeType> l = new ArrayList<ArchiveModeType>();
  65.         for (int i = 0; i < types.length; i++) {
  66.             l.add(types[i]);
  67.         }
  68.         this.add(ext, preferUseForSingleObject, archiveType, l);
  69.     }
  70.     public void add(String ext,List<ArchiveModeType> listTypes) throws ProtocolException{
  71.         this.add(ext, false, null, listTypes);
  72.     }
  73.     public void add(String ext,boolean preferUseForSingleObject,List<ArchiveModeType> listTypes) throws ProtocolException{
  74.         this.add(ext, preferUseForSingleObject, null, listTypes);
  75.     }
  76.     public void add(String ext,ArchiveType archiveType, List<ArchiveModeType> listTypes) throws ProtocolException{
  77.         this.add(ext, false, archiveType, listTypes);
  78.     }
  79.     public void add(String ext,boolean preferUseForSingleObject,ArchiveType archiveType, List<ArchiveModeType> listTypes) throws ProtocolException{
  80.         List<ArchiveModeType> l = null;
  81.         if(this.mappingExtensionsTypes.containsKey(ext)){
  82.             l = this.mappingExtensionsTypes.remove(ext);
  83.         }else{
  84.             l = new ArrayList<ArchiveModeType>();
  85.         }
  86.        
  87.         for (ArchiveModeType type : listTypes) {
  88.            
  89.             if(l.contains(type)){
  90.                 throw new ProtocolException("Type["+type+"] already exists for extension ["+ext+"]");
  91.             }
  92.            
  93.             // Verificare che cmq il tipo non sia assegnato ad un altra extension!
  94.             // Questo DEVE essere permesso. Ad esempio uno zip gestisce diversi formati piu' specifici
  95. //          String extAlreadyExist = this.mappingTypeToExt(type);
  96. //          if(extAlreadyExist!=null){
  97. //              throw new ProtocolException("Type["+type+"] already exists for other extension ["+extAlreadyExist+"]");
  98. //          }
  99.            
  100.             l.add(type);
  101.            
  102.             if(preferUseForSingleObject) {
  103.                 if(this.preferExtSingleObject==null) {
  104.                     this.preferExtSingleObject = ext;
  105.                 }
  106.                 else {
  107.                     throw new ProtocolException("Extension["+this.preferExtSingleObject+"] already selected choice for single object");
  108.                 }
  109.             }
  110.         }
  111.         this.mappingExtensionsTypes.put(ext, l);
  112.         if(archiveType!=null){
  113.             this.mappingExtensionsArchiveType.put(ext, archiveType);
  114.         }
  115.         if(this.exts.contains(ext)==false){
  116.             this.exts.add(ext);
  117.         }
  118.     }
  119.    
  120.     public String mappingTypeToFirstExt(ArchiveModeType type){
  121.         // Viene ritornato il tipo al livello 0
  122.         return mappingTypeToExt(type, 0);
  123.     }
  124.     public String mappingTypeToExt(ArchiveModeType type,int index){
  125.         List<String> l = this.mappingTypeToExts(type);
  126.         if(l.size()>=(index+1)){
  127.             return l.get(index);
  128.         }else{
  129.             return null;
  130.         }
  131.     }
  132.     public List<String> mappingTypeToExts(ArchiveModeType type){
  133.         List<String> exts = new ArrayList<>();
  134.         for (int i = 0; i < this.exts.size(); i++) {
  135.             String ext = this.exts.get(i);
  136.             List<ArchiveModeType> types = this.mappingExtensionsTypes.get(ext);
  137.             if(types.contains(type)){
  138.                 exts.add(ext);
  139.             }
  140.         }
  141.         return exts;
  142.     }
  143.    
  144.     public ArchiveModeType mappingExtToFirstType(String ext){
  145.         // Viene ritornato il tipo al livello 0    
  146.         return this.mappingExtToType(ext, 0);  
  147.     }
  148.     public ArchiveModeType mappingExtToType(String ext,int index){
  149.         if(this.mappingExtensionsTypes.containsKey(ext)){
  150.             List<ArchiveModeType> l = this.mappingExtensionsTypes.get(ext);
  151.             if(l.size()>=(index+1)){
  152.                 return l.get(index);
  153.             }else{
  154.                 return null;
  155.             }
  156.         }else{
  157.             return null;
  158.         }
  159.     }
  160.     public List<ArchiveModeType> mappingExtToTypes(String ext){
  161.         if(this.mappingExtensionsTypes.containsKey(ext)){
  162.             return this.mappingExtensionsTypes.get(ext);            
  163.         }else{
  164.             return null;
  165.         }
  166.     }
  167.    
  168.     public List<String> getExtensions(){
  169.         return this.exts;
  170.     }
  171.    
  172.     public String getPreferExtSingleObject() {
  173.         return this.preferExtSingleObject;
  174.     }
  175.    
  176.     public List<ArchiveModeType> getTypes(String ext){
  177.         return this.mappingExtensionsTypes.get(ext);
  178.     }
  179.    
  180.     public List<ArchiveModeType> getAllTypes(){
  181.        
  182.         List<ArchiveModeType> typesAll = new ArrayList<ArchiveModeType>();
  183.         List<String> exts = this.getExtensions();
  184.         for (int i = 0; i < exts.size(); i++) {
  185.             List<ArchiveModeType> types = this.getTypes(exts.get(i));
  186.             for (ArchiveModeType type : types) {
  187.                 if(typesAll.contains(type)==false){
  188.                     typesAll.add(type);
  189.                 }
  190.             }
  191.         }
  192.         return typesAll;
  193.     }
  194.    
  195.     public String mappingArchiveTypeToExt(ArchiveType type){
  196.        
  197.         for (String ext : this.mappingExtensionsArchiveType.keySet()) {
  198.             ArchiveType check = this.mappingExtensionsArchiveType.get(ext);
  199.             if(check.equals(type)){
  200.                 return ext;
  201.             }
  202.         }          
  203.        
  204.         return null;
  205.     }
  206. }