UnixTimestampIntervalField.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.Date;

  22. import org.openspcoop2.generic_project.exception.ExpressionException;
  23. import org.openspcoop2.generic_project.expression.impl.sql.ISQLFieldConverter;
  24. import org.openspcoop2.utils.sql.ISQLQueryObject;
  25. import org.openspcoop2.utils.sql.SQLObjectFactory;
  26. import org.openspcoop2.utils.sql.SQLQueryObjectException;

  27. /**
  28.  * TimestampIntervalField
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class UnixTimestampIntervalField extends CustomField {
  35.    
  36.     public UnixTimestampIntervalField(String fieldName,ISQLFieldConverter fieldConverter,boolean appendTablePrefix, IField maxInterval, IField minInterval) throws ExpressionException, SQLQueryObjectException{
  37.         super(fieldName, Long.class, buildFunction(fieldConverter,appendTablePrefix,maxInterval,minInterval), fieldName, "", "");
  38.     }
  39.    
  40.     private static String buildFunction(ISQLFieldConverter fieldConverter, boolean appendTablePrefix, IField maxInterval, IField minInterval) throws ExpressionException, SQLQueryObjectException{
  41.         if(maxInterval==null){
  42.             throw new ExpressionException("MaxInterval is null");
  43.         }
  44.         if(minInterval==null){
  45.             throw new ExpressionException("MinInterval is null");
  46.         }
  47.         String dateClassName = Date.class.getName() + "";
  48.         String maxClassName = maxInterval.getFieldType().getName() + "";
  49.         if(!dateClassName.equals(maxClassName)){
  50.             throw new ExpressionException("MaxInterval with wrong type, expected:"+Date.class.getName()+" found:"+maxInterval.getFieldType());
  51.         }
  52.         String minClassName = minInterval.getFieldType().getName() + "";
  53.         if(!dateClassName.equals(minClassName)){
  54.             throw new ExpressionException("MinInterval with wrong type, expected:"+Date.class.getName()+" found:"+minInterval.getFieldType());
  55.         }
  56.         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(fieldConverter.getDatabaseType());
  57.         return sqlQueryObject.getDiffUnixTimestamp(fieldConverter.toColumn(maxInterval, appendTablePrefix), fieldConverter.toColumn(minInterval, appendTablePrefix));
  58.     }
  59.    
  60.     private static int counter = 0;
  61.     private static synchronized int getNextCounter(){
  62.         counter = counter+1;
  63.         if(counter>1000){
  64.             counter = 0; // ruoto
  65.         }
  66.         return counter;
  67.     }
  68.    
  69.     private String alias = null;
  70.     public boolean existsAlias(){
  71.         return this.alias!=null;
  72.     }
  73.     public String getAlias(){
  74.         return this.alias;
  75.     }
  76.     public synchronized void buildAlias(){
  77.         if(this.alias==null){
  78.             this.alias = "TS"+getNextCounter();
  79.         }
  80.     }
  81. }