ComparatorExpressionSQL.java

/*
 * GovWay - A customizable API Gateway 
 * https://govway.org
 * 
 * Copyright (c) 2005-2024 Link.it srl (https://link.it).
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package org.openspcoop2.generic_project.expression.impl.sql;

import java.util.List;
import java.util.Map;

import org.openspcoop2.generic_project.beans.IField;
import org.openspcoop2.generic_project.exception.ExpressionException;
import org.openspcoop2.generic_project.expression.impl.Comparator;
import org.openspcoop2.generic_project.expression.impl.ComparatorExpressionImpl;
import org.openspcoop2.generic_project.expression.impl.formatter.IObjectFormatter;
import org.openspcoop2.generic_project.utils.IDGenerator;
import org.openspcoop2.utils.sql.ISQLQueryObject;

/**
 * ComparatorExpressionSQL
 * 
 * @author Poli Andrea (apoli@link.it)
 * @author $Author$
 * @version $Rev$, $Date$
 */
public class ComparatorExpressionSQL extends ComparatorExpressionImpl implements ISQLExpression {

	private ISQLFieldConverter sqlFieldConverter;
	
	public ComparatorExpressionSQL(ISQLFieldConverter sqlFieldConverter, IObjectFormatter objectFormatter,
			IField field, Object object, Comparator comparator) {
		super(objectFormatter, field, object, comparator);
		this.sqlFieldConverter = sqlFieldConverter;
	}
	
	private String toSqlEngine(SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
		StringBuilder bf = new StringBuilder();
		if(isNot()){
			bf.append("( NOT ");
		}
		bf.append("( ");
		bf.append(this.sqlFieldConverter.toColumn(this.getField(),true));
		bf.append(" ");
		bf.append(this.getComparator().getOperatore(this.sqlFieldConverter));
		if(this.getObject()!=null){
			bf.append(" ");
			switch (mode) {
			case STANDARD:
				try{
					bf.append(super.getObjectFormatter().toSQLString(this.getObject()));
				}catch(Exception e){
					return "ERROR: "+e.getMessage();
				}
				break;
			case PREPARED_STATEMENT:
				bf.append("?");
				if(oggettiPreparedStatement!=null) {
					oggettiPreparedStatement.add(this.getObject());
				}
				break;
			case JPA:
				String id = IDGenerator.getUniqueID(this.getField());
				bf.append(":"+id);
				if(oggettiJPA!=null) {
					oggettiJPA.put(id, this.getObject());
				}
				break;
			}
		}
		bf.append(" )");
		if(isNot()){
			bf.append(" )");
		}
		return bf.toString();
	}

	private void toSqlEngine(ISQLQueryObject sqlQueryObject,SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
		try{
			String s = toSqlEngine(mode, oggettiPreparedStatement, oggettiJPA);
			s = s.substring(1,s.length()-2);
			sqlQueryObject.addWhereCondition(s);
		}catch(Exception e){
			throw new ExpressionException(e);
		}
	}

	@Override
	public String toSql() throws ExpressionException {
		return toSqlEngine(SQLMode.STANDARD, null, null);
	}

	@Override
	public String toSqlPreparedStatement(List<Object> oggetti)
			throws ExpressionException {
		return toSqlEngine(SQLMode.PREPARED_STATEMENT, oggetti, null);
	}

	@Override
	public String toSqlJPA(Map<String, Object> oggetti)
			throws ExpressionException {
		return toSqlEngine(SQLMode.JPA, null, oggetti);
	}
	
	@Override
	public void toSql(ISQLQueryObject sqlQueryObject) throws ExpressionException {
		toSqlEngine(sqlQueryObject,SQLMode.STANDARD, null, null);
	}

	@Override
	public void toSqlPreparedStatement(ISQLQueryObject sqlQueryObject,List<Object> oggetti)
			throws ExpressionException {
		toSqlEngine(sqlQueryObject,SQLMode.PREPARED_STATEMENT, oggetti, null);
	}

	@Override
	public void toSqlJPA(ISQLQueryObject sqlQueryObject,Map<String, Object> oggetti)
			throws ExpressionException {
		toSqlEngine(sqlQueryObject,SQLMode.JPA, null, oggetti);
	}
}