ConditionsEngine.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.web.lib.mvc.properties.utils;

  21. import java.util.List;

  22. import org.openspcoop2.core.mvc.properties.Condition;
  23. import org.openspcoop2.core.mvc.properties.Conditions;
  24. import org.openspcoop2.core.mvc.properties.Config;
  25. import org.openspcoop2.core.mvc.properties.Defined;
  26. import org.openspcoop2.core.mvc.properties.Equals;
  27. import org.openspcoop2.core.mvc.properties.Item;
  28. import org.openspcoop2.core.mvc.properties.Section;
  29. import org.openspcoop2.core.mvc.properties.Selected;
  30. import org.openspcoop2.core.mvc.properties.Subsection;
  31. import org.openspcoop2.web.lib.mvc.ServletUtils;
  32. import org.openspcoop2.web.lib.mvc.properties.beans.BaseItemBean;
  33. import org.openspcoop2.web.lib.mvc.properties.beans.ConfigBean;
  34. import org.openspcoop2.web.lib.mvc.properties.exception.ConditionException;

  35. /***
  36.  *
  37.  * Motore che serve a decidere le condizioni di visualizzazione di un elemento
  38.  *
  39.  * @author Pintori Giuliano (pintori@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  *
  43.  */
  44. public class ConditionsEngine {

  45.     public static boolean resolve(Conditions conditions, ConfigBean configBean) throws ConditionException {
  46.         try {
  47.             if(conditions == null)
  48.                 return true;
  49.            
  50.             boolean isAnd = conditions.getAnd();
  51.             boolean isNot = conditions.getNot();

  52.             // Valore di partenza dell'esito totale e'
  53.             // TRUE se devo controllare l'and delle condizioni
  54.             // FALSE se devo controllare l'or delle condizioni
  55.             boolean esito = isAnd ? true : false;

  56.             for (Condition condition : conditions.getConditionList()) {
  57.                 boolean resCondition = resolve(condition,configBean);

  58.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  59.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  60.             }

  61.             // eventuale NOT della condizione
  62.             return isNot ? !esito : esito;
  63.         }catch(Exception e) {
  64.             throw new ConditionException("Errore durante la risoluzione delle Conditions: " + conditions.toString(), e);
  65.         }
  66.     }

  67.     public static boolean resolve(Condition condition, ConfigBean configBean) throws ConditionException {
  68.         try {
  69.             boolean isAnd = condition.getAnd();
  70.             boolean isNot = condition.getNot();

  71.             // Valore di partenza dell'esito totale e'
  72.             // TRUE se devo controllare l'and delle condizioni
  73.             // FALSE se devo controllare l'or delle condizioni
  74.             boolean esito = isAnd ? true : false;

  75.             for (Defined defined : condition.getDefinedList()) {
  76.                 boolean resCondition = resolveDefined(defined,configBean);

  77.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  78.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  79.             }

  80.             for (Equals equals: condition.getEqualsList()) {
  81.                 boolean resCondition = resolveEquals(EqualsType.EQUALS, equals,configBean);

  82.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  83.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  84.             }
  85.            
  86.             for (Equals equals: condition.getLessThenList()) {
  87.                 boolean resCondition = resolveEquals(EqualsType.LESS_THEN, equals,configBean);

  88.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  89.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  90.             }
  91.            
  92.             for (Equals equals: condition.getLessEqualsList()) {
  93.                 boolean resCondition = resolveEquals(EqualsType.LESS_EQUALS, equals,configBean);

  94.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  95.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  96.             }
  97.            
  98.             for (Equals equals: condition.getGreaterThenList()) {
  99.                 boolean resCondition = resolveEquals(EqualsType.GREATER_THEN, equals,configBean);

  100.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  101.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  102.             }
  103.            
  104.             for (Equals equals: condition.getGreaterEqualsList()) {
  105.                 boolean resCondition = resolveEquals(EqualsType.GREATER_EQUALS, equals,configBean);

  106.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  107.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  108.             }

  109.             for (Equals equals: condition.getStartsWithList()) {
  110.                 boolean resCondition = resolveEquals(EqualsType.STARTS_WITH, equals,configBean);

  111.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  112.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  113.             }
  114.            
  115.             for (Equals equals: condition.getEndsWithList()) {
  116.                 boolean resCondition = resolveEquals(EqualsType.ENDS_WITH, equals,configBean);

  117.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  118.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  119.             }
  120.            
  121.             for (Selected selected : condition.getSelectedList()) {
  122.                 boolean resCondition = resolveSelected(selected,configBean);

  123.                 // aggiorno l'esito in base all'operazione da aggregare AND o OR
  124.                 esito = isAnd ? (esito && resCondition) : (esito || resCondition);
  125.             }

  126.             // eventuale NOT della condizione
  127.             return isNot ? !esito : esito;
  128.         }catch(Exception e) {
  129.             throw new ConditionException("Errore durante la risoluzione della Condition: " + condition.toString(), e);
  130.         }
  131.     }

  132.     public static boolean resolveSelected(Selected selected, ConfigBean configBean) throws ConditionException {
  133.         try {
  134.             String elementName = selected.getName();
  135.             boolean isNot = selected.getNot();
  136.             BaseItemBean<?> item = configBean.getItem(elementName);

  137.             boolean esito = item.isVisible() &&  ServletUtils.isCheckBoxEnabled(item.getValue());

  138.             // eventuale NOT della condizione
  139.             return isNot ? !esito : esito;
  140.         }catch(Exception e) {
  141.             throw new ConditionException("Errore durante la risoluzione della condizione Selected: " + selected.toString(), e);
  142.         }
  143.     }

  144.     public static boolean resolveEquals(EqualsType equalsType, Equals equals, ConfigBean configBean) throws ConditionException {
  145.         try {
  146.             String elementName = equals.getName();
  147.             boolean isNot = equals.getNot();
  148.             String value = equals.getValue();

  149.             BaseItemBean<?> item = configBean.getItem(elementName);

  150.             boolean opValue = false;
  151.             if(item.isVisible()) {
  152.                 switch (equalsType) {
  153.                 case EQUALS:
  154.                     opValue = value.equals(item.getValue());
  155.                     break;
  156.                 case LESS_THEN:
  157.                     opValue = (item.getValue()!=null && value.compareTo(item.getValue())<0);
  158.                     break;
  159.                 case LESS_EQUALS:
  160.                     opValue = value.equals(item.getValue()) || (item.getValue()!=null && value.compareTo(item.getValue())<0);
  161.                     break;
  162.                 case GREATER_THEN:
  163.                     opValue = (item.getValue()!=null && value.compareTo(item.getValue())>0);
  164.                     break;
  165.                 case GREATER_EQUALS:
  166.                     opValue = value.equals(item.getValue()) || (item.getValue()!=null && value.compareTo(item.getValue())>0);
  167.                     break;
  168.                 case STARTS_WITH:
  169.                     opValue = item.getValue()!=null && value!=null && item.getValue().startsWith(value);
  170.                     break;
  171.                 case ENDS_WITH:
  172.                     opValue = item.getValue()!=null && value!=null && item.getValue().endsWith(value);
  173.                     break;
  174.                 }
  175.             }
  176.            
  177.             boolean esito = item.isVisible() && opValue;

  178.             // eventuale NOT della condizione
  179.             return isNot ? !esito : esito;
  180.         }catch(Exception e) {
  181.             throw new ConditionException("Errore durante la risoluzione della condizione Equals: " + equals.toString(), e);
  182.         }
  183.     }

  184.     public static boolean resolveDefined(Defined defined, ConfigBean configBean) throws ConditionException {
  185.         try {
  186.             String elementName = defined.getName();
  187.             boolean isNot = defined.getNot();

  188.             BaseItemBean<?> item = configBean.getItem(elementName);

  189.             boolean esito = item.isVisible() && item.getValue() != null;

  190.             // eventuale NOT della condizione
  191.             return isNot ? !esito : esito;
  192.         }catch(Exception e) {
  193.             throw new ConditionException("Errore durante la risoluzione della condizione Defined: " + defined.toString(), e);
  194.         }
  195.     }

  196.     public static boolean controllaSezioniDaNascondere(Config config,ConfigBean cbTmp)  throws ConditionException{
  197.         List<Section> sectionList = config.getSectionList();
  198.        
  199.         boolean show = true;
  200.         for (int i= 0; i < sectionList.size() ; i++) {
  201.             Section section = sectionList.get(i);
  202.             show = showSection(section,"s"+i,cbTmp);
  203.         }
  204.        
  205.         return show;
  206.     }
  207.    
  208.     private static boolean showSection(Section section, String sectionIdx,ConfigBean cbTmp) throws ConditionException{
  209.         BaseItemBean<?> itemSection = cbTmp.getItem(sectionIdx);
  210.        
  211.         // 1. se la sezione e' nascosta nascondo tutti gli elementi
  212.         boolean show = itemSection.isVisible();

  213.         if(!show) {
  214.             if(section.getItemList() != null) {
  215.                 for (Item item : section.getItemList()) {
  216.                     BaseItemBean<?> item3 = cbTmp.getItem(item.getName());
  217.                     item3.setVisible(show);
  218.                 }
  219.             }
  220.             if(section.getSubsectionList() != null) {
  221.                 for (int i= 0; i < section.getSubsectionList().size() ; i++) {
  222.                     Subsection subSection  = section.getSubsectionList().get(i);
  223.                     showSubsection(subSection,sectionIdx+ "_ss"+i,cbTmp,show);
  224.                 }
  225.             }
  226.         } else {
  227.             boolean allItemHidden = true;
  228.             boolean allSubSectionHidden = true;
  229.            
  230.             if(section.getItemList() != null) {
  231.                 for (Item item : section.getItemList()) {
  232.                     BaseItemBean<?> item3 = cbTmp.getItem(item.getName());
  233.                     if(item3.isVisible()) {
  234.                         allItemHidden = false;
  235.                         break;
  236.                     }
  237.                 }
  238.             }
  239.            
  240.             if(section.getSubsectionList() != null) {
  241.                 for (int i= 0; i < section.getSubsectionList().size() ; i++) {
  242.                     Subsection subSection  = section.getSubsectionList().get(i);
  243.                     if(showSubsection(subSection,sectionIdx+ "_ss"+i,cbTmp,show)) {
  244.                         allSubSectionHidden = allSubSectionHidden && false;
  245.                     }
  246.                 }
  247.             }
  248.            
  249.             show = !(allItemHidden && allSubSectionHidden);
  250.         }
  251.        
  252.      
  253.         // 2. se tutti gli elementi sono nascosti nascondo la sezione
  254.         itemSection.setVisible(show);
  255.        
  256.         return show;
  257.     }

  258.     private static boolean showSubsection(Subsection subSection, String subsectionIdx, ConfigBean cbTmp, boolean showParent) throws ConditionException {
  259.         BaseItemBean<?> itemSubSection = cbTmp.getItem(subsectionIdx);
  260.        
  261.         // 1. se la sezione e' nascosta nascondo tutti gli elementi
  262.         boolean show = !showParent ? false : itemSubSection.isVisible();
  263.        
  264.         if(!show) {
  265.             for (Item item : subSection.getItemList()) {
  266.                 BaseItemBean<?> item3 = cbTmp.getItem(item.getName());
  267.                 item3.setVisible(show);
  268.             }
  269.         } else {
  270.             boolean allHidden = true;
  271.             for (Item item : subSection.getItemList()) {
  272.                 BaseItemBean<?> item3 = cbTmp.getItem(item.getName());
  273.                 if(item3.isVisible()) {
  274.                     allHidden = false;
  275.                     break;
  276.                 }
  277.             }
  278.            
  279.             show = !allHidden;
  280.         }
  281.         // 2. se tutti gli elementi sono nascosti nascondo la sezione
  282.         itemSubSection.setVisible(show);
  283.        
  284.         return show;
  285.     }
  286. }

  287. enum EqualsType{
  288.    
  289.     EQUALS, LESS_EQUALS, LESS_THEN, GREATER_EQUALS, GREATER_THEN, STARTS_WITH, ENDS_WITH
  290.    
  291. }