SearchServiceLibrary.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.monitor.engine.config;

  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.monitor.engine.config.ricerche.ConfigurazioneRicerca;

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

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;

  40.     private BasicServiceLibrary basicServiceLibrary;
  41.     private List<ConfigurazioneRicerca> searchActionLibrary;
  42.     private List<ConfigurazioneRicerca> searchActionAllLibrary;

  43.     public BasicServiceLibrary getBasicServiceLibrary() {
  44.         return this.basicServiceLibrary;
  45.     }
  46.     public void setBasicServiceLibrary(BasicServiceLibrary basicServiceLibrary) {
  47.         this.basicServiceLibrary = basicServiceLibrary;
  48.     }
  49.     public List<ConfigurazioneRicerca> getSearchActionLibrary() {
  50.         return this.searchActionLibrary;
  51.     }
  52.     public void setSearchActionLibrary(List<ConfigurazioneRicerca> searchActionLibrary) {
  53.         this.searchActionLibrary = searchActionLibrary;
  54.     }
  55.     public List<ConfigurazioneRicerca> getSearchActionAllLibrary() {
  56.         return this.searchActionAllLibrary;
  57.     }
  58.     public void setSearchActionAllLibrary(
  59.             List<ConfigurazioneRicerca> searchActionAllLibrary) {
  60.         this.searchActionAllLibrary = searchActionAllLibrary;
  61.     }





  62.     // ****** UTILS ***************

  63.     @Override
  64.     public String toString(){
  65.         StringBuilder bf = new StringBuilder();

  66.         if(this.basicServiceLibrary==null){
  67.             bf.append("BasicServiceLibrary: notDefined");
  68.             bf.append("\n");
  69.         }
  70.         else{
  71.             bf.append(this.basicServiceLibrary.toString());
  72.         }

  73.            
  74.         if(this.getSearchActionLibrary()!=null){
  75.             for (int i = 0; i < this.getSearchActionLibrary().size(); i++) {
  76.                 bf.append("SearchActionLibrary["+i+"]: ");
  77.                 bf.append("id[").
  78.                 append(this.getSearchActionLibrary().get(i).getIdConfigurazioneRicerca()).
  79.                 append("] plugin.className[").
  80.                 append(this.getSearchActionLibrary().get(i).getPlugin().getClassName()).
  81.                 append("] plugin.description[").
  82.                 append(this.getSearchActionLibrary().get(i).getPlugin().getDescrizione()).
  83.                 append("] plugin.label[").
  84.                 append(this.getSearchActionLibrary().get(i).getPlugin().getLabel()).
  85.                 append("] label[").
  86.                 append(this.getSearchActionLibrary().get(i).getLabel()).
  87.                 append("] enabled[").
  88.                 append(this.getSearchActionLibrary().get(i).isEnabled()).
  89.                 append("]");
  90.                 bf.append("\n");
  91.             }
  92.         }else{
  93.             bf.append("SearchActionLibrary: ");
  94.             bf.append("-");
  95.         }
  96.         bf.append("\n");

  97.        
  98.         if(this.getSearchActionAllLibrary()!=null){
  99.             for (int i = 0; i < this.getSearchActionAllLibrary().size(); i++) {
  100.                 bf.append("SearchActionAllLibrary["+i+"]: ");
  101.                 bf.append("id[").
  102.                 append(this.getSearchActionAllLibrary().get(i).getIdConfigurazioneRicerca()).
  103.                 append("] plugin.className[").
  104.                 append(this.getSearchActionAllLibrary().get(i).getPlugin().getClassName()).
  105.                 append("] plugin.description[").
  106.                 append(this.getSearchActionAllLibrary().get(i).getPlugin().getDescrizione()).
  107.                 append("] plugin.label[").
  108.                 append(this.getSearchActionAllLibrary().get(i).getPlugin().getLabel()).
  109.                 append("] label[").
  110.                 append(this.getSearchActionAllLibrary().get(i).getLabel()).
  111.                 append("] enabled[").
  112.                 append(this.getSearchActionAllLibrary().get(i).isEnabled()).
  113.                 append("]");
  114.                 bf.append("\n");
  115.             }
  116.         }else{
  117.             bf.append("SearchActionAllLibrary: ");
  118.             bf.append("-");
  119.         }
  120.         bf.append("\n");


  121.         return bf.toString();
  122.     }


  123.     public List<ConfigurazioneRicerca> mergeServiceActionSearchLibrary(boolean onlyEnabled, boolean orderByLabel) throws Exception{

  124.         Map<String,ConfigurazioneRicerca> plugins = new HashMap<String,ConfigurazioneRicerca>();

  125.         // Leggo le risorse associate all'azione specifica
  126.         if(this.searchActionLibrary!=null && this.searchActionLibrary.size()>0){
  127.             for (ConfigurazioneRicerca configurazioneRicerca : this.searchActionLibrary) {
  128.                 if(!onlyEnabled || configurazioneRicerca.isEnabled()){
  129.                     plugins.put(configurazioneRicerca.getPlugin().getClassName(), configurazioneRicerca);
  130.                 }
  131.             }
  132.         }

  133.         // Leggo le risorse associate all'azione '*'
  134.         if(this.searchActionAllLibrary!=null && this.searchActionAllLibrary.size()>0){
  135.             for (ConfigurazioneRicerca configurazioneRicerca : this.searchActionAllLibrary) {

  136.                 String idRisorsa = configurazioneRicerca.getPlugin().getClassName();
  137.                 if(plugins.containsKey(idRisorsa)==false){

  138.                     // inserisco solo le risorse il cui idPlugin non siano giĆ  stati inseriti per l'azione specifica

  139.                     if(!onlyEnabled || configurazioneRicerca.isEnabled()){
  140.                         plugins.put(configurazioneRicerca.getPlugin().getClassName(), configurazioneRicerca);
  141.                     }
  142.                 }

  143.             }
  144.         }

  145.         List<ConfigurazioneRicerca> list = new ArrayList<ConfigurazioneRicerca>();
  146.         if(orderByLabel){
  147.             List<String> sortedKey = new ArrayList<>();
  148.             Map<String, ConfigurazioneRicerca> mapLabelToCorrelazioneRicerca = new HashMap<String, ConfigurazioneRicerca>();
  149.             int count = 0; // lo uso per gestire eventuali label identiche.
  150.             for (String className : plugins.keySet()) {
  151.                 ConfigurazioneRicerca cr = plugins.get(className);
  152.                 String labelKey = cr.getLabel()+count;
  153.                 sortedKey.add(labelKey);
  154.                 mapLabelToCorrelazioneRicerca.put(labelKey, cr);
  155.                 count++;
  156.             }
  157.             Collections.sort(sortedKey);
  158.             for (String sortKey : sortedKey) {
  159.                 list.add(mapLabelToCorrelazioneRicerca.get(sortKey));
  160.             }
  161.         }
  162.         else{
  163.             list.addAll(plugins.values());
  164.         }
  165.         return list;
  166.     }
  167.    

  168. }