FunctionField.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.beans;

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

  23. import org.openspcoop2.generic_project.exception.ExpressionException;

  24. /**
  25.  * FunctionField
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class FunctionField {

  32.     // campi per la funct
  33.     private String functionValue;
  34.     private Class<?> functionValueType;
  35.     private List<IField> fields = new ArrayList<IField>();
  36.     private String operator;
  37.    
  38.     // function
  39.     private Function function;
  40.     private boolean customFunction;
  41.     private String prefixFunctionCustom;
  42.     private String suffixFunctionCustom;
  43.    
  44.     // alias
  45.     private String alias;
  46.    
  47.    
  48.     private void checkAlias() throws ExpressionException{
  49.         if(this.function!=null && this.alias!=null){
  50.            
  51.             // hsql le utilizza come chiavi
  52.            
  53.             if("count".equalsIgnoreCase(this.alias) && Function.COUNT.equals(this.function) ){
  54.                 throw new ExpressionException("Alias ["+this.alias+"] is not allowed with the function COUNT");
  55.             }
  56.            
  57.             if("count".equalsIgnoreCase(this.alias) && Function.COUNT_DISTINCT.equals(this.function) ){
  58.                 throw new ExpressionException("Alias ["+this.alias+"] is not allowed with the function COUNT");
  59.             }
  60.            
  61.             if("avg".equalsIgnoreCase(this.alias) && Function.AVG.equals(this.function) ){
  62.                 throw new ExpressionException("Alias ["+this.alias+"] is not allowed with the function AVG");
  63.             }
  64.            
  65.             if("avg".equalsIgnoreCase(this.alias) && Function.AVG_DOUBLE.equals(this.function) ){
  66.                 throw new ExpressionException("Alias ["+this.alias+"] is not allowed with the function AVG_DOUBLE");
  67.             }
  68.            
  69.             if("max".equalsIgnoreCase(this.alias) && Function.MAX.equals(this.function) ){
  70.                 throw new ExpressionException("Alias ["+this.alias+"] is not allowed with the function MAX");
  71.             }
  72.            
  73.             if("min".equalsIgnoreCase(this.alias) && Function.MIN.equals(this.function) ){
  74.                 throw new ExpressionException("Alias ["+this.alias+"] is not allowed with the function MIN");
  75.             }
  76.            
  77.             if("sum".equalsIgnoreCase(this.alias) && Function.SUM.equals(this.function) ){
  78.                 throw new ExpressionException("Alias ["+this.alias+"] is not allowed with the function SUM");
  79.             }
  80.         }
  81.     }
  82.    
  83.    
  84.     /**
  85.      * Esempio di utilizzo di una funzione conosciuta su di un field
  86.      **/
  87.     public FunctionField(IField field,Function function,String alias) throws ExpressionException{
  88.         if(field==null){
  89.             throw new ExpressionException("IField is null");
  90.         }
  91.         if(function==null){
  92.             throw new ExpressionException("Function is null");
  93.         }
  94.         if(alias==null){
  95.             throw new ExpressionException("Alias is null");
  96.         }

  97.         this.fields.add(field);
  98.         this.function = function;
  99.         this.alias = alias;
  100.         this.customFunction = false;
  101.         this.checkAlias();
  102.     }
  103.    
  104.     /**
  105.      * Esempio di utilizzo di una funzione conosciuta su piu' fields uniti da uno stesso operatore (F1 operator F2 operator ... FN)
  106.      **/
  107.     public FunctionField(Function function,String alias,String operator,IField ... field) throws ExpressionException{

  108.         if(function==null){
  109.             throw new ExpressionException("Function is null");
  110.         }
  111.         if(alias==null){
  112.             throw new ExpressionException("Alias is null");
  113.         }
  114.         if(operator==null){
  115.             throw new ExpressionException("Operator is null");
  116.         }

  117.         if(field==null || field.length<=0){
  118.             throw new ExpressionException("IField is null");
  119.         }
  120.         if(field.length<2){
  121.             throw new ExpressionException("IField less then 2. With operator constructor is required almost two field");
  122.         }
  123.         Class<?> cField = null;
  124.         for (int i = 0; i < field.length; i++) {
  125.             if(i==0){
  126.                 cField = field[i].getFieldType();
  127.             }
  128.             else{
  129.                 String cFieldName = cField.getName() + "";
  130.                 String fieldName = field[i].getFieldType().getName() + "";
  131.                 if(cFieldName.equals(fieldName)==false){
  132.                     throw new ExpressionException("Fields aren't same type [0]="+cField.getClass().getName()+" ["+i+"]="+field[i].getFieldType().getName());
  133.                 }
  134.             }
  135.             this.fields.add(field[i]);
  136.         }
  137.         this.function = function;
  138.         this.alias = alias;
  139.         this.operator = operator;
  140.         this.customFunction = false;
  141.         this.checkAlias();
  142.     }
  143.    
  144.     /**
  145.      * Esempio di utilizzo di una funzione conosciuta su dei valori input based (functionValue)
  146.      **/
  147.     public FunctionField(String functionValue,Class<?> functionValueType,Function function,String alias) throws ExpressionException{
  148.         if(functionValue==null){
  149.             throw new ExpressionException("functionValue is null");
  150.         }
  151.         if(functionValueType==null){
  152.             throw new ExpressionException("functionValueType is null");
  153.         }
  154.         if(function==null){
  155.             throw new ExpressionException("Function is null");
  156.         }
  157.         if(alias==null){
  158.             throw new ExpressionException("Alias is null");
  159.         }
  160.         this.functionValue = functionValue;
  161.         this.functionValueType = functionValueType;
  162.         this.function = function;
  163.         this.alias = alias;
  164.         this.customFunction = false;
  165.         this.checkAlias();
  166.     }
  167.    
  168.     /**
  169.      * Esempio di utilizzo di una funzione custom su di un field
  170.      **/
  171.     public FunctionField(IField field,String prefixFunctionCustom,String suffixFunctionCustom,String alias) throws ExpressionException{
  172.         if(field==null){
  173.             throw new ExpressionException("IField is null");
  174.         }
  175.         if(prefixFunctionCustom==null){
  176.             throw new ExpressionException("prefixFunctionCustom is null");
  177.         }
  178.         if(suffixFunctionCustom==null){
  179.             throw new ExpressionException("suffixFunctionCustom is null");
  180.         }
  181.         if(alias==null){
  182.             throw new ExpressionException("Alias is null");
  183.         }
  184.         this.fields.add(field);
  185.         this.prefixFunctionCustom = prefixFunctionCustom;
  186.         this.suffixFunctionCustom = suffixFunctionCustom;
  187.         this.alias = alias;
  188.         this.customFunction = true;
  189.         this.checkAlias();
  190.     }
  191.    
  192.     /**
  193.      * Esempio di utilizzo di una funzione custom su piu' fields uniti da uno stesso operatore (F1 operator F2 operator ... FN)
  194.      **/
  195.     public FunctionField(String prefixFunctionCustom,String suffixFunctionCustom,String alias,String operator,IField ... field) throws ExpressionException{

  196.         if(prefixFunctionCustom==null){
  197.             throw new ExpressionException("prefixFunctionCustom is null");
  198.         }
  199.         if(suffixFunctionCustom==null){
  200.             throw new ExpressionException("suffixFunctionCustom is null");
  201.         }
  202.         if(alias==null){
  203.             throw new ExpressionException("Alias is null");
  204.         }
  205.         if(operator==null){
  206.             throw new ExpressionException("Operator is null");
  207.         }

  208.         if(field==null || field.length<=0){
  209.             throw new ExpressionException("IField is null");
  210.         }
  211.         if(field.length<2){
  212.             throw new ExpressionException("IField less then 2. With operator constructor is required almost two field");
  213.         }
  214.         Class<?> cField = null;
  215.         for (int i = 0; i < field.length; i++) {
  216.             if(i==0){
  217.                 cField = field[i].getFieldType();
  218.             }
  219.             else{
  220.                 String cFieldName = cField.getName() + "";
  221.                 String fieldName = field[i].getFieldType().getName() + "";
  222.                 if(cFieldName.equals(fieldName)==false){
  223.                     throw new ExpressionException("Fields aren't same type [0]="+cField.getClass().getName()+" ["+i+"]="+field[i].getFieldType().getName());
  224.                 }
  225.             }
  226.             this.fields.add(field[i]);
  227.         }
  228.         this.prefixFunctionCustom = prefixFunctionCustom;
  229.         this.suffixFunctionCustom = suffixFunctionCustom;
  230.         this.alias = alias;
  231.         this.operator = operator;
  232.         this.customFunction = true;
  233.         this.checkAlias();
  234.     }
  235.    
  236.     /**
  237.      * Esempio di utilizzo di una funzione custom su dei valori input based (functionValue)
  238.      **/
  239.     public FunctionField(String functionValue,Class<?> functionValueType,String prefixFunctionCustom,String suffixFunctionCustom,String alias) throws ExpressionException{
  240.         if(functionValue==null){
  241.             throw new ExpressionException("functionValue is null");
  242.         }
  243.         if(functionValueType==null){
  244.             throw new ExpressionException("functionValueType is null");
  245.         }
  246.         if(prefixFunctionCustom==null){
  247.             throw new ExpressionException("prefixFunctionCustom is null");
  248.         }
  249.         if(suffixFunctionCustom==null){
  250.             throw new ExpressionException("suffixFunctionCustom is null");
  251.         }
  252.         if(alias==null){
  253.             throw new ExpressionException("Alias is null");
  254.         }
  255.         this.functionValue = functionValue;
  256.         this.functionValueType = functionValueType;
  257.         this.prefixFunctionCustom = prefixFunctionCustom;
  258.         this.suffixFunctionCustom = suffixFunctionCustom;
  259.         this.alias = alias;
  260.         this.customFunction = true;
  261.         this.checkAlias();
  262.     }
  263.    
  264.    
  265.    
  266.     public String getFunctionValue() {
  267.         return this.functionValue;
  268.     }
  269.     public Class<?> getFunctionValueType() {
  270.         return this.functionValueType;
  271.     }
  272.     public List<IField> getFields() {
  273.         return this.fields;
  274.     }
  275.     public String getOperator() {
  276.         return this.operator;
  277.     }
  278.     public Class<?> getFieldType(){
  279.         if(this.functionValueType!=null){
  280.             return this.functionValueType;
  281.         }
  282.         else{
  283.             if(this.function!=null && Function.AVG_DOUBLE.equals(this.function)){
  284.                 return Double.class;
  285.             }
  286.             else{
  287.                 // devono essere tutti dello stesso tipo
  288.                 return this.fields.get(0).getFieldType();
  289.             }
  290.         }
  291.     }
  292.    
  293.    
  294.     public boolean isCustomFunction() {
  295.         return this.customFunction;
  296.     }
  297.     public Function getFunction() {
  298.         return this.function;
  299.     }
  300.     public String getPrefixFunctionCustom() {
  301.         return this.prefixFunctionCustom;
  302.     }
  303.     public String getSuffixFunctionCustom() {
  304.         return this.suffixFunctionCustom;
  305.     }
  306.    
  307.        
  308.     public String getAlias() {
  309.         return this.alias;
  310.     }



  311.    
  312.     @Override
  313.     public boolean equals(Object o){
  314.         if(o instanceof FunctionField){
  315.             return this.equals((FunctionField)o);
  316.         }
  317.         else{
  318.             return false;
  319.         }
  320.     }
  321.     public boolean equals(FunctionField o){
  322.         if(o==null){
  323.             return false;
  324.         }
  325.        
  326.         if(this.functionValue==null){
  327.             if(o.getFunctionValue()!=null){
  328.                 return false;
  329.             }
  330.         }
  331.         else {
  332.             if(this.functionValue.equals(o.getFunctionValue())==false){
  333.                 return false;
  334.             }
  335.         }
  336.        
  337.         if(this.functionValueType==null){
  338.             if(o.getFunctionValueType()!=null){
  339.                 return false;
  340.             }
  341.         }
  342.         else {
  343.             if(o.getFunctionValueType()==null){
  344.                 return false;
  345.             }
  346.             String functionValueTypeName = this.functionValueType.getName() + "";
  347.             String oFunctionValueTypeName = o.getFunctionValueType().getName() + "";
  348.             if(functionValueTypeName.equals(oFunctionValueTypeName)==false){
  349.                 return false;
  350.             }
  351.         }
  352.        
  353.         if(this.getFields().size()!=o.getFields().size()){
  354.             return false;
  355.         }
  356.         for (IField field : this.fields) {
  357.             boolean find = false;
  358.             for (IField fieldParam : o.getFields()) {
  359.                 if(fieldParam.equals(field)){
  360.                     find = true;
  361.                     break;
  362.                 }
  363.             }
  364.             if(!find){
  365.                 return false;
  366.             }
  367.         }
  368.         for (IField fieldParam : o.getFields()) {
  369.             boolean find = false;
  370.             for (IField field : this.fields) {
  371.                 if(fieldParam.equals(field)){
  372.                     find = true;
  373.                     break;
  374.                 }
  375.             }
  376.             if(!find){
  377.                 return false;
  378.             }
  379.         }

  380.        
  381.         if(o.getOperator()==null){
  382.             if(this.operator!=null)
  383.                 return false;
  384.         }
  385.         else{
  386.             if(o.getOperator().equals(this.operator)==false){
  387.                 return false;
  388.             }
  389.         }
  390.        
  391.         if(this.customFunction != o.isCustomFunction() ){
  392.             return false;
  393.         }

  394.         if(this.function==null){
  395.             if(o.getFunction()!=null){
  396.                 return false;
  397.             }
  398.         }
  399.         else {
  400.             if(o.getFunction()==null){
  401.                 return false;
  402.             }
  403.             if(this.function.name().equals(o.getFunction().name())==false){
  404.                 return false;
  405.             }
  406.         }

  407.         if(this.prefixFunctionCustom==null){
  408.             if(o.getPrefixFunctionCustom()!=null){
  409.                 return false;
  410.             }
  411.         }
  412.         else {
  413.             if(this.prefixFunctionCustom.equals(o.getPrefixFunctionCustom())==false){
  414.                 return false;
  415.             }
  416.         }
  417.        
  418.         if(this.suffixFunctionCustom==null){
  419.             if(o.getSuffixFunctionCustom()!=null){
  420.                 return false;
  421.             }
  422.         }
  423.         else {
  424.             if(this.suffixFunctionCustom.equals(o.getSuffixFunctionCustom())==false){
  425.                 return false;
  426.             }
  427.         }
  428.        
  429.         if(this.alias==null){
  430.             if(o.getAlias()!=null){
  431.                 return false;
  432.             }
  433.         }
  434.         else {
  435.             if(this.alias.equals(o.getAlias())==false){
  436.                 return false;
  437.             }
  438.         }
  439.        
  440.        
  441.         return true;
  442.     }
  443.    
  444.     @Override
  445.     public int hashCode() {
  446.         return super.hashCode();
  447.     }
  448.    
  449.     @Override
  450.     public String toString(){
  451.                
  452.         StringBuilder bf = new StringBuilder();
  453.        
  454.         bf.append("- Alias: "+this.alias);
  455.         bf.append("\n");
  456.         if(this.customFunction){
  457.             bf.append("- CustomFunction: "+this.prefixFunctionCustom +" @VALUE@ "+this.suffixFunctionCustom);
  458.         }else{
  459.             bf.append("- Function: "+this.function);
  460.         }
  461.         bf.append("\n");
  462.         if(this.functionValue!=null){
  463.             bf.append("- FunctionValue (type:"+this.functionValueType+"): "+this.functionValue);
  464.         }
  465.         else{
  466.             if(this.operator!=null){
  467.                 bf.append("- Operator: "+this.operator);
  468.                 bf.append("\n");
  469.             }
  470.             if(this.fields.size()==1){
  471.                 bf.append(this.fields.get(0).toString());
  472.             }
  473.             else{
  474.                 for (int i = 0; i < this.fields.size(); i++) {
  475.                     bf.append("- Field["+i+"]: "+this.fields.get(i));
  476.                     bf.append("\n");
  477.                 }
  478.             }
  479.         }
  480.                
  481.         return bf.toString();
  482.     }
  483. }