ConjunctionExpressionImpl.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.generic_project.expression.impl;

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

  24. import org.openspcoop2.generic_project.beans.IField;
  25. import org.openspcoop2.generic_project.beans.IModel;
  26. import org.openspcoop2.generic_project.exception.ExpressionException;
  27. import org.openspcoop2.generic_project.exception.ExpressionNotImplementedException;
  28. import org.openspcoop2.generic_project.expression.impl.formatter.IObjectFormatter;

  29. /**
  30.  * ConjunctionExpressionImpl
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class ConjunctionExpressionImpl extends AbstractBaseExpressionImpl {

  37.     private List<AbstractBaseExpressionImpl> lista = new ArrayList<>();
  38.     private boolean andConjunction = false;

  39.     public ConjunctionExpressionImpl(IObjectFormatter objectFormatter){
  40.         super(objectFormatter);
  41.     }
  42.    
  43.     public void addExpression(AbstractBaseExpressionImpl xml){
  44.         this.lista.add(xml);
  45.     }
  46.    
  47.     public List<AbstractBaseExpressionImpl> getLista() {
  48.         return this.lista;
  49.     }

  50.     public void setLista(List<AbstractBaseExpressionImpl> lista) {
  51.         this.lista = lista;
  52.     }

  53.     public boolean isAndConjunction() {
  54.         return this.andConjunction;
  55.     }

  56.     public void setAndConjunction(boolean andConjunction) {
  57.         this.andConjunction = andConjunction;
  58.     }

  59.     @Override
  60.     public String toString(){
  61.         StringBuilder bf = new StringBuilder();
  62.         if(isNot()){
  63.             bf.append("( NOT ");
  64.         }
  65.         bf.append("( ");
  66.         int index = 0;
  67.         for (Iterator<AbstractBaseExpressionImpl> iterator = this.lista.iterator(); iterator.hasNext();) {
  68.             AbstractBaseExpressionImpl exp = iterator.next();
  69.             if(index>0){
  70.                 if(this.andConjunction)
  71.                     bf.append(" AND ");
  72.                 else
  73.                     bf.append(" OR ");
  74.             }
  75.             bf.append(exp.toString());
  76.             index++;
  77.         }
  78.         bf.append(" )");
  79.         if(isNot()){
  80.             bf.append(" )");
  81.         }
  82.         return bf.toString();
  83.     }
  84.    
  85.     @Override
  86.     public boolean inUseField(IField field)
  87.             throws ExpressionNotImplementedException, ExpressionException {
  88.         if(this.lista==null){
  89.             return false;
  90.         }
  91.         for (Iterator<AbstractBaseExpressionImpl> iterator = this.lista.iterator(); iterator.hasNext();) {
  92.             AbstractBaseExpressionImpl expr = iterator.next();
  93.             if(expr.inUseField(field)){
  94.                 return true;
  95.             }          
  96.         }
  97.         return false;
  98.     }

  99.     @Override
  100.     public List<Object> getFieldValues(IField field) throws ExpressionNotImplementedException, ExpressionException{
  101.         List<Object> fields = null;
  102.         if(this.lista==null){
  103.             return fields;
  104.         }
  105.         fields = new ArrayList<>();
  106.         for (Iterator<AbstractBaseExpressionImpl> iterator = this.lista.iterator(); iterator.hasNext();) {
  107.             AbstractBaseExpressionImpl expr = iterator.next();
  108.             List<Object> tmp = expr.getFieldValues(field);
  109.             if(tmp!=null){
  110.                 fields.addAll(tmp);
  111.             }          
  112.         }
  113.         if(!fields.isEmpty()){
  114.             return fields;
  115.         }
  116.         else {
  117.             fields = null;
  118.         }
  119.         return fields;
  120.     }
  121.        
  122.     @Override
  123.     public boolean inUseModel(IModel<?> model)
  124.             throws ExpressionNotImplementedException, ExpressionException {
  125.         if(this.lista==null){
  126.             return false;
  127.         }
  128.         for (Iterator<AbstractBaseExpressionImpl> iterator = this.lista.iterator(); iterator.hasNext();) {
  129.             AbstractBaseExpressionImpl expr = iterator.next();
  130.             if(expr.inUseModel(model)){
  131.                 return true;
  132.             }          
  133.         }
  134.         return false;
  135.     }

  136.    
  137.    
  138.     @Override
  139.     public List<IField> getFields()
  140.             throws ExpressionNotImplementedException, ExpressionException {
  141.         List<IField> fields = null;
  142.         if(this.lista==null){
  143.             return fields;
  144.         }
  145.         fields = new ArrayList<>();
  146.         for (Iterator<AbstractBaseExpressionImpl> iterator = this.lista.iterator(); iterator.hasNext();) {
  147.             AbstractBaseExpressionImpl expr = iterator.next();
  148.             List<IField> tmp = expr.getFields();
  149.             if(tmp!=null){
  150.                 fields.addAll(tmp);
  151.             }
  152.         }
  153.         if(!fields.isEmpty()){
  154.             return fields;
  155.         }
  156.         else {
  157.             fields = null;
  158.         }
  159.         return fields;
  160.     }
  161. }