ConfigUtils.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.config.utils;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.core.config.Proprieta;
  24. import org.openspcoop2.utils.SortedMap;
  25. import org.openspcoop2.utils.UtilsException;

  26. /**
  27.  * ConfigUtils
  28.  *
  29.  * @author Stefano Corallo (corallo@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class ConfigUtils {

  34.     private static final String PROPS_SEPARATOR_VALUES = "!,!";
  35.     public static SortedMap<List<String>> toSortedListMap(List<Proprieta> proprieta) throws UtilsException{
  36.         SortedMap<List<String>> map = new SortedMap<List<String>> ();
  37.         if(proprieta!=null && !proprieta.isEmpty()) {
  38.             for (Proprieta p : proprieta) {
  39.                 List<String> l = map.get(p.getNome());
  40.                 if(l==null) {
  41.                     l = new ArrayList<>();
  42.                     map.add(p.getNome(), l);
  43.                 }
  44.                 String valore = p.getValore();
  45.                 if(valore.contains(PROPS_SEPARATOR_VALUES)) {
  46.                     String [] split = valore.split(PROPS_SEPARATOR_VALUES);
  47.                     if(split==null || split.length<=0) {
  48.                         l.add(valore);
  49.                     }
  50.                     else {
  51.                         for (String v : split) {
  52.                             l.add(v);
  53.                         }
  54.                     }
  55.                 }
  56.                 else {
  57.                     l.add(valore);
  58.                 }
  59.             }
  60.         }
  61.         return map;
  62.     }
  63.     public static void addFromSortedListMap(List<Proprieta> proprieta, SortedMap<List<String>> map){
  64.         List<String> keys = map.keys();
  65.         if(keys!=null && !keys.isEmpty()) {
  66.             for (String nome : keys) {
  67.                 List<String> valori = map.get(nome);
  68.                 if(valori!=null && !valori.isEmpty()) {
  69.                     if(valori.size()==1) {
  70.                         String valore = valori.get(0);
  71.                         Proprieta proprietaAutorizzazioneContenuto = new Proprieta();
  72.                         proprietaAutorizzazioneContenuto.setNome(nome);
  73.                         proprietaAutorizzazioneContenuto.setValore(valore);
  74.                         proprieta.add(proprietaAutorizzazioneContenuto);
  75.                     }
  76.                     else {
  77.                         StringBuilder sb = new StringBuilder();
  78.                         for (String valore : valori) {
  79.                             if(sb.length()>0) {
  80.                                 sb.append(PROPS_SEPARATOR_VALUES);
  81.                             }
  82.                             sb.append(valore);
  83.                         }  
  84.                         Proprieta proprietaAutorizzazioneContenuto = new Proprieta();
  85.                         proprietaAutorizzazioneContenuto.setNome(nome);
  86.                         proprietaAutorizzazioneContenuto.setValore(sb.toString());
  87.                         proprieta.add(proprietaAutorizzazioneContenuto);
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93.    
  94. }