JDBCServizioApplicativoServiceSearchImpl.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.core.commons.search.dao.jdbc;

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

import java.sql.Connection;

import org.slf4j.Logger;

import org.openspcoop2.utils.sql.ISQLQueryObject;

import org.openspcoop2.generic_project.expression.impl.sql.ISQLFieldConverter;
import org.openspcoop2.generic_project.dao.jdbc.utils.IJDBCFetch;
import org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject;
import org.openspcoop2.generic_project.dao.jdbc.IJDBCServiceSearchWithId;
import org.openspcoop2.core.commons.search.IdServizioApplicativo;
import org.openspcoop2.core.commons.search.IdSoggetto;
import org.openspcoop2.generic_project.utils.UtilsTemplate;
import org.openspcoop2.generic_project.beans.CustomField;
import org.openspcoop2.generic_project.beans.InUse;
import org.openspcoop2.generic_project.beans.IField;
import org.openspcoop2.generic_project.beans.NonNegativeNumber;
import org.openspcoop2.generic_project.beans.UnionExpression;
import org.openspcoop2.generic_project.beans.Union;
import org.openspcoop2.generic_project.beans.FunctionField;
import org.openspcoop2.generic_project.exception.MultipleResultException;
import org.openspcoop2.generic_project.exception.NotFoundException;
import org.openspcoop2.generic_project.exception.NotImplementedException;
import org.openspcoop2.generic_project.exception.ServiceException;
import org.openspcoop2.generic_project.expression.IExpression;
import org.openspcoop2.generic_project.dao.jdbc.JDBCExpression;
import org.openspcoop2.generic_project.dao.jdbc.JDBCPaginatedExpression;

import org.openspcoop2.generic_project.dao.jdbc.JDBCServiceManagerProperties;
import org.openspcoop2.core.commons.search.dao.jdbc.converter.ServizioApplicativoFieldConverter;
import org.openspcoop2.core.commons.search.dao.jdbc.fetch.ServizioApplicativoFetch;
import org.openspcoop2.core.constants.CostantiDB;
import org.openspcoop2.core.constants.ProprietariProtocolProperty;
import org.openspcoop2.core.commons.search.dao.IDBSoggettoServiceSearch;
import org.openspcoop2.core.commons.search.dao.ISoggettoServiceSearch;
import org.openspcoop2.core.commons.search.ServizioApplicativoProprietaProtocollo;
import org.openspcoop2.core.commons.search.ServizioApplicativo;
import org.openspcoop2.core.commons.search.Soggetto;
import org.openspcoop2.core.commons.search.ServizioApplicativoRuolo;

/**     
 * JDBCServizioApplicativoServiceSearchImpl
 *
 * @author Poli Andrea (poli@link.it)
 * @author $Author$
 * @version $Rev$, $Date$
 */
public class JDBCServizioApplicativoServiceSearchImpl implements IJDBCServiceSearchWithId<ServizioApplicativo, IdServizioApplicativo, JDBCServiceManager> {

	private ServizioApplicativoFieldConverter _servizioApplicativoFieldConverter = null;
	public ServizioApplicativoFieldConverter getServizioApplicativoFieldConverter() {
		if(this._servizioApplicativoFieldConverter==null){
			this._servizioApplicativoFieldConverter = new ServizioApplicativoFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
		}		
		return this._servizioApplicativoFieldConverter;
	}
	@Override
	public ISQLFieldConverter getFieldConverter() {
		return this.getServizioApplicativoFieldConverter();
	}
	
	private ServizioApplicativoFetch servizioApplicativoFetch = new ServizioApplicativoFetch();
	public ServizioApplicativoFetch getServizioApplicativoFetch() {
		return this.servizioApplicativoFetch;
	}
	@Override
	public IJDBCFetch getFetch() {
		return getServizioApplicativoFetch();
	}
	
	
	private JDBCServiceManager jdbcServiceManager = null;

	@Override
	public void setServiceManager(JDBCServiceManager serviceManager) throws ServiceException{
		this.jdbcServiceManager = serviceManager;
	}
	
	@Override
	public JDBCServiceManager getServiceManager() throws ServiceException{
		return this.jdbcServiceManager;
	}
	public JDBCServiceManager getServiceManager(Connection connection, JDBCServiceManagerProperties jdbcProperties, Logger log) throws ServiceException{
		return new JDBCServiceManager(connection, jdbcProperties, log);
	}
	

	@Override
	public IdServizioApplicativo convertToId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ServizioApplicativo servizioApplicativo) throws NotImplementedException, ServiceException, Exception{
	
		IdServizioApplicativo idServizioApplicativo = new IdServizioApplicativo();
		idServizioApplicativo.setNome(servizioApplicativo.getNome());
        idServizioApplicativo.setIdSoggetto(servizioApplicativo.getIdSoggetto());
        return idServizioApplicativo;

	}
	
	@Override
	public ServizioApplicativo get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdServizioApplicativo id, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {
		Long id_servizioApplicativo = ( (id!=null && id.getId()!=null && id.getId()>0) ? id.getId() : this.findIdServizioApplicativo(jdbcProperties, log, connection, sqlQueryObject, id, true));
		return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, id_servizioApplicativo,idMappingResolutionBehaviour);
		
		
	}
	
	@Override
	public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdServizioApplicativo id) throws MultipleResultException, NotImplementedException, ServiceException,Exception {

		Long id_servizioApplicativo = this.findIdServizioApplicativo(jdbcProperties, log, connection, sqlQueryObject, id, false);
		return id_servizioApplicativo != null && id_servizioApplicativo > 0;
		
	}
	
	@Override
	public List<IdServizioApplicativo> findAllIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

		List<IdServizioApplicativo> list = new ArrayList<IdServizioApplicativo>();

		// TODO: implementazione non efficiente. 
		// Per ottenere una implementazione efficiente:
		// 1. Usare metodo select di questa classe indirizzando esattamente i field necessari a create l'ID logico
		// 2. Usare metodo getServizioApplicativoFetch() sul risultato della select per ottenere un oggetto ServizioApplicativo
		//	  La fetch con la map inserirĂ  nell'oggetto solo i valori estratti 
		// 3. Usare metodo convertToId per ottenere l'id

        List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
        
        for(Long id: ids) {
        	ServizioApplicativo servizioApplicativo = this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
			IdServizioApplicativo idServizioApplicativo = this.convertToId(jdbcProperties,log,connection,sqlQueryObject,servizioApplicativo);
        	list.add(idServizioApplicativo);
        }

        return list;
		
	}
	
	@Override
	public List<ServizioApplicativo> findAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

        List<ServizioApplicativo> list = new ArrayList<ServizioApplicativo>();
        
        // TODO: implementazione non efficiente. 
		// Per ottenere una implementazione efficiente:
		// 1. Usare metodo select di questa classe indirizzando esattamente i field necessari
		// 2. Usare metodo getServizioApplicativoFetch() sul risultato della select per ottenere un oggetto ServizioApplicativo
		//	  La fetch con la map inserirĂ  nell'oggetto solo i valori estratti 

        List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
        
        for(Long id: ids) {
        	list.add(this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour));
        }

        return list;      
		
	}
	
	@Override
	public ServizioApplicativo find(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) 
		throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {

        long id = this.findTableId(jdbcProperties, log, connection, sqlQueryObject, expression);
        if(id>0){
        	return this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
        }else{
        	throw new NotFoundException("Entry with id["+id+"] not found");
        }
		
	}
	
	@Override
	public NonNegativeNumber count(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws NotImplementedException, ServiceException,Exception {
		
		List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareCount(jdbcProperties, log, connection, sqlQueryObject, expression,
												this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model());
		
		sqlQueryObject.addSelectCountField(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model())+".id","tot",true);
		
		joinEngine(expression,sqlQueryObject);
		
		return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.count(jdbcProperties, log, connection, sqlQueryObject, expression,
																			this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(),listaQuery);
	}

	@Override
	public InUse inUse(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdServizioApplicativo id) throws NotFoundException, NotImplementedException, ServiceException,Exception {
		
		Long id_servizioApplicativo = this.findIdServizioApplicativo(jdbcProperties, log, connection, sqlQueryObject, id, true);
        return this.inUseEngine(jdbcProperties, log, connection, sqlQueryObject, id_servizioApplicativo);
		
	}

	@Override
	public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCPaginatedExpression paginatedExpression, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		return this.select(jdbcProperties, log, connection, sqlQueryObject,
								paginatedExpression, false, field);
	}
	
	@Override
	public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCPaginatedExpression paginatedExpression, boolean distinct, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		List<Map<String,Object>> map = 
			this.select(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, distinct, new IField[]{field});
		return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectSingleObject(map);
	}
	
	@Override
	public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCPaginatedExpression paginatedExpression, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		return this.select(jdbcProperties, log, connection, sqlQueryObject,
								paginatedExpression, false, field);
	}
	
	@Override
	public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCPaginatedExpression paginatedExpression, boolean distinct, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		
		org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,paginatedExpression,field);
		try{
		
			ISQLQueryObject sqlQueryObjectDistinct = 
						org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSqlQueryObjectForSelectDistinct(distinct,sqlQueryObject, paginatedExpression, log,
												this.getServizioApplicativoFieldConverter(), field);

			return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, sqlQueryObjectDistinct);
			
		}finally{
			org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,field);
		}
	}

	@Override
	public Object aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCExpression expression, FunctionField functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		Map<String,Object> map = 
			this.aggregate(jdbcProperties, log, connection, sqlQueryObject, expression, new FunctionField[]{functionField});
		return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectAggregateObject(map,functionField);
	}
	
	@Override
	public Map<String,Object> aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {													
		
		org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
		try{
			List<Map<String,Object>> list = selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
			return list.get(0);
		}finally{
			org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
		}
	}

	@Override
	public List<Map<String,Object>> groupBy(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		
		if(expression.getGroupByFields().isEmpty()){
			throw new ServiceException("GroupBy conditions not found in expression");
		}
		
		org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
		try{
			return selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
		}finally{
			org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
		}
	}
	

	@Override
	public List<Map<String,Object>> groupBy(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
													JDBCPaginatedExpression paginatedExpression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		
		if(paginatedExpression.getGroupByFields().isEmpty()){
			throw new ServiceException("GroupBy conditions not found in expression");
		}
		
		org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,paginatedExpression,functionField);
		try{
			return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression);
		}finally{
			org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,functionField);
		}
	}
	
	protected List<Map<String,Object>> selectEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
												IExpression expression) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		return selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression, null);
	}
	protected List<Map<String,Object>> selectEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
												IExpression expression, ISQLQueryObject sqlQueryObjectDistinct) throws ServiceException,NotFoundException,NotImplementedException,Exception {
		
		List<Object> listaQuery = new ArrayList<>();
		List<JDBCObject> listaParams = new ArrayList<>();
		List<Object> returnField = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSelect(jdbcProperties, log, connection, sqlQueryObject, 
        						expression, this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(), 
        						listaQuery,listaParams);
		
		joinEngine(expression,sqlQueryObject);
        
        List<Map<String,Object>> list = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.select(jdbcProperties, log, connection,
        								org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSqlQueryObjectForSelectDistinct(sqlQueryObject,sqlQueryObjectDistinct), 
        								expression, this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(),
        								listaQuery,listaParams,returnField);
		if(list!=null && !list.isEmpty()){
			return list;
		}
		else{
			throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
		}
	}
	
	@Override
	public List<Map<String,Object>> union(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
												Union union, UnionExpression ... unionExpression) throws ServiceException,NotFoundException,NotImplementedException,Exception {		
		
		List<ISQLQueryObject> sqlQueryObjectInnerList = new ArrayList<>();
		List<JDBCObject> jdbcObjects = new ArrayList<>();
		List<Class<?>> returnClassTypes = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareUnion(jdbcProperties, log, connection, sqlQueryObject, 
        						this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(), 
        						sqlQueryObjectInnerList, jdbcObjects, union, unionExpression);
		
		if(unionExpression!=null){
			for (int i = 0; i < unionExpression.length; i++) {
				UnionExpression ue = unionExpression[i];
				IExpression expression = ue.getExpression();
				joinEngine(expression,sqlQueryObjectInnerList.get(i));
			}
		}
        
        List<Map<String,Object>> list = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.union(jdbcProperties, log, connection, sqlQueryObject, 
        								this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(), 
        								sqlQueryObjectInnerList, jdbcObjects, returnClassTypes, union, unionExpression);
        if(list!=null && !list.isEmpty()){
			return list;
		}
		else{
			throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
		}								
	}
	
	@Override
	public NonNegativeNumber unionCount(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
												Union union, UnionExpression ... unionExpression) throws ServiceException,NotFoundException,NotImplementedException,Exception {		
		
		List<ISQLQueryObject> sqlQueryObjectInnerList = new ArrayList<>();
		List<JDBCObject> jdbcObjects = new ArrayList<>();
		List<Class<?>> returnClassTypes = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareUnionCount(jdbcProperties, log, connection, sqlQueryObject, 
        						this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(), 
        						sqlQueryObjectInnerList, jdbcObjects, union, unionExpression);
		
		if(unionExpression!=null){
			for (int i = 0; i < unionExpression.length; i++) {
				UnionExpression ue = unionExpression[i];
				IExpression expression = ue.getExpression();
				joinEngine(expression,sqlQueryObjectInnerList.get(i));
			}
		}
        
        NonNegativeNumber number = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.unionCount(jdbcProperties, log, connection, sqlQueryObject, 
        								this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(), 
        								sqlQueryObjectInnerList, jdbcObjects, returnClassTypes, union, unionExpression);
        if(number!=null && number.longValue()>=0){
			return number;
		}
		else{
			throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
		}
	}



	// -- ConstructorExpression	

	@Override
	public JDBCExpression newExpression(Logger log) throws NotImplementedException, ServiceException {
		try{
			return new JDBCExpression(this.getServizioApplicativoFieldConverter());
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}


	@Override
	public JDBCPaginatedExpression newPaginatedExpression(Logger log) throws NotImplementedException, ServiceException {
		try{
			return new JDBCPaginatedExpression(this.getServizioApplicativoFieldConverter());
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}
	
	@Override
	public JDBCExpression toExpression(JDBCPaginatedExpression paginatedExpression, Logger log) throws NotImplementedException, ServiceException {
		try{
			return new JDBCExpression(paginatedExpression);
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}

	@Override
	public JDBCPaginatedExpression toPaginatedExpression(JDBCExpression expression, Logger log) throws NotImplementedException, ServiceException {
		try{
			return new JDBCPaginatedExpression(expression);
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}
	
	
	
	// -- DB

	@Override
	public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdServizioApplicativo id, ServizioApplicativo obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
		_mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
				this.get(jdbcProperties,log,connection,sqlQueryObject,id,null));
	}
	
	@Override
	public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, ServizioApplicativo obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
		_mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
				this.get(jdbcProperties,log,connection,sqlQueryObject,tableId,null));
	}
	private void _mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ServizioApplicativo obj, ServizioApplicativo imgSaved) throws NotFoundException,NotImplementedException,ServiceException,Exception{
		if(imgSaved==null){
			return;
		}
		obj.setId(imgSaved.getId());
		if(obj.getIdSoggetto()!=null && 
				imgSaved.getIdSoggetto()!=null){
			obj.getIdSoggetto().setId(imgSaved.getIdSoggetto().getId());
		}
		if(obj.getServizioApplicativoRuoloList()!=null){
			List<org.openspcoop2.core.commons.search.ServizioApplicativoRuolo> listObj_ = obj.getServizioApplicativoRuoloList();
			for(org.openspcoop2.core.commons.search.ServizioApplicativoRuolo itemObj_ : listObj_){
				org.openspcoop2.core.commons.search.ServizioApplicativoRuolo itemAlreadySaved_ = null;
				if(imgSaved.getServizioApplicativoRuoloList()!=null){
					List<org.openspcoop2.core.commons.search.ServizioApplicativoRuolo> listImgSaved_ = imgSaved.getServizioApplicativoRuoloList();
					for(org.openspcoop2.core.commons.search.ServizioApplicativoRuolo itemImgSaved_ : listImgSaved_){
						boolean objEqualsToImgSaved_ = false;
						objEqualsToImgSaved_ = org.openspcoop2.generic_project.utils.Utilities.equals(itemObj_.getNome(),itemImgSaved_.getNome());
						if(objEqualsToImgSaved_){
							itemAlreadySaved_=itemImgSaved_;
							break;
						}
					}
				}
				if(itemAlreadySaved_!=null){
					itemObj_.setId(itemAlreadySaved_.getId());
				}
			}
		}
		if(obj.getServizioApplicativoProprietaProtocolloList()!=null){
			List<org.openspcoop2.core.commons.search.ServizioApplicativoProprietaProtocollo> listObj_ = obj.getServizioApplicativoProprietaProtocolloList();
			for(org.openspcoop2.core.commons.search.ServizioApplicativoProprietaProtocollo itemObj_ : listObj_){
				org.openspcoop2.core.commons.search.ServizioApplicativoProprietaProtocollo itemAlreadySaved_ = null;
				if(imgSaved.getServizioApplicativoProprietaProtocolloList()!=null){
					List<org.openspcoop2.core.commons.search.ServizioApplicativoProprietaProtocollo> listImgSaved_ = imgSaved.getServizioApplicativoProprietaProtocolloList();
					for(org.openspcoop2.core.commons.search.ServizioApplicativoProprietaProtocollo itemImgSaved_ : listImgSaved_){
						boolean objEqualsToImgSaved_ = false;
						objEqualsToImgSaved_ = org.openspcoop2.generic_project.utils.Utilities.equals(itemObj_.getName(),itemImgSaved_.getName());
						if(objEqualsToImgSaved_){
							itemAlreadySaved_=itemImgSaved_;
							break;
						}
					}
				}
				if(itemAlreadySaved_!=null){
					itemObj_.setId(itemAlreadySaved_.getId());
				}
			}
		}

	}
	
	@Override
	public ServizioApplicativo get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
		return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId), idMappingResolutionBehaviour);
	}
	
	private ServizioApplicativo getEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
	
		org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities = 
					new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
		
		ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();
				
		ServizioApplicativo servizioApplicativo = new ServizioApplicativo();
		

		// Object servizioApplicativo
		ISQLQueryObject sqlQueryObjectGet_servizioApplicativo = sqlQueryObjectGet.newSQLQueryObject();
		sqlQueryObjectGet_servizioApplicativo.setANDLogicOperator(true);
		sqlQueryObjectGet_servizioApplicativo.addFromTable(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model()));
		sqlQueryObjectGet_servizioApplicativo.addSelectField("id");
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().NOME,true));
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().TIPOLOGIA_FRUIZIONE,true));
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().TIPOLOGIA_EROGAZIONE,true));
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().TIPO,true));
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().AS_CLIENT,true));
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().TIPOAUTH,true));
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().UTENTE,true));
		sqlQueryObjectGet_servizioApplicativo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().TOKEN_POLICY,true));
		sqlQueryObjectGet_servizioApplicativo.addWhereCondition("id=?");

		// Get servizioApplicativo
		servizioApplicativo = (ServizioApplicativo) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_servizioApplicativo.createSQLQuery(), jdbcProperties.isShowSql(), ServizioApplicativo.model(), this.getServizioApplicativoFetch(),
			new JDBCObject(tableId,Long.class));


		// Object _servizioApplicativo_soggetto (recupero id)
		ISQLQueryObject sqlQueryObjectGet_servizioApplicativo_soggetto_readFkId = sqlQueryObjectGet.newSQLQueryObject();
		sqlQueryObjectGet_servizioApplicativo_soggetto_readFkId.addFromTable(CostantiDB.SERVIZI_APPLICATIVI);
		sqlQueryObjectGet_servizioApplicativo_soggetto_readFkId.addSelectField("id_soggetto");
		sqlQueryObjectGet_servizioApplicativo_soggetto_readFkId.addWhereCondition("id=?");
		sqlQueryObjectGet_servizioApplicativo_soggetto_readFkId.setANDLogicOperator(true);
		Long idFK_servizioApplicativo_soggetto = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_servizioApplicativo_soggetto_readFkId.createSQLQuery(), jdbcProperties.isShowSql(),Long.class,
				new JDBCObject(servizioApplicativo.getId(),Long.class));
		
		// Object _servizioApplicativo_soggetto
		ISQLQueryObject sqlQueryObjectGet_servizioApplicativo_soggetto = sqlQueryObjectGet.newSQLQueryObject();
		sqlQueryObjectGet_servizioApplicativo_soggetto.addFromTable(CostantiDB.SOGGETTI);
		sqlQueryObjectGet_servizioApplicativo_soggetto.addSelectField("tipo_soggetto");
		sqlQueryObjectGet_servizioApplicativo_soggetto.addSelectField("nome_soggetto");
		sqlQueryObjectGet_servizioApplicativo_soggetto.setANDLogicOperator(true);
		sqlQueryObjectGet_servizioApplicativo_soggetto.addWhereCondition("id=?");

		// Recupero _servizioApplicativo_soggetto
		org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_servizioApplicativo_soggetto = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] { 
			new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(idFK_servizioApplicativo_soggetto,Long.class)
		};
		List<Class<?>> listaFieldIdReturnType_servizioApplicativo_soggetto = new ArrayList<Class<?>>();
		listaFieldIdReturnType_servizioApplicativo_soggetto.add(String.class);
		listaFieldIdReturnType_servizioApplicativo_soggetto.add(String.class);
		List<Object> listaFieldId_servizioApplicativo_soggetto = jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_servizioApplicativo_soggetto.createSQLQuery(), jdbcProperties.isShowSql(),
				listaFieldIdReturnType_servizioApplicativo_soggetto, searchParams_servizioApplicativo_soggetto);
		// set _servizioApplicativo_soggetto
		IdSoggetto id_servizioApplicativo_soggetto = new IdSoggetto();
		id_servizioApplicativo_soggetto.setTipo((String)listaFieldId_servizioApplicativo_soggetto.get(0));
		id_servizioApplicativo_soggetto.setNome((String)listaFieldId_servizioApplicativo_soggetto.get(1));
		servizioApplicativo.setIdSoggetto(id_servizioApplicativo_soggetto);

		// Object servizioApplicativo_servizioApplicativoRuolo
		ISQLQueryObject sqlQueryObjectGet_servizioApplicativo_servizioApplicativoRuolo = sqlQueryObjectGet.newSQLQueryObject();
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoRuolo.setANDLogicOperator(true);
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoRuolo.addFromTable(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO));
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoRuolo.addSelectField("id");
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoRuolo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO.NOME,true));
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoRuolo.addWhereCondition("id_servizio_applicativo=?");

		// Get servizioApplicativo_servizioApplicativoRuolo
		java.util.List<Object> servizioApplicativo_servizioApplicativoRuolo_list = (java.util.List<Object>) jdbcUtilities.executeQuery(sqlQueryObjectGet_servizioApplicativo_servizioApplicativoRuolo.createSQLQuery(), jdbcProperties.isShowSql(), ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO, this.getServizioApplicativoFetch(),
			new JDBCObject(servizioApplicativo.getId(),Long.class));

		if(servizioApplicativo_servizioApplicativoRuolo_list != null) {
			for (Object servizioApplicativo_servizioApplicativoRuolo_object: servizioApplicativo_servizioApplicativoRuolo_list) {
				ServizioApplicativoRuolo servizioApplicativo_servizioApplicativoRuolo = (ServizioApplicativoRuolo) servizioApplicativo_servizioApplicativoRuolo_object;


				servizioApplicativo.addServizioApplicativoRuolo(servizioApplicativo_servizioApplicativoRuolo);
			}
		}

		// Object servizioApplicativo_servizioApplicativoProprietaProtocollo
		ISQLQueryObject sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo = sqlQueryObjectGet.newSQLQueryObject();
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.setANDLogicOperator(true);
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addFromTable(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO));
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addSelectField("id");
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO.NAME,true));
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO.VALUE_STRING,true));
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO.VALUE_NUMBER,true));
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO.VALUE_BOOLEAN,true));
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addWhereCondition("id_proprietario=?");
		sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.addWhereCondition("tipo_proprietario=?");

		// Get servizioApplicativo_servizioApplicativoProprietaProtocollo
		java.util.List<Object> servizioApplicativo_servizioApplicativoProprietaProtocollo_list = (java.util.List<Object>) jdbcUtilities.executeQuery(sqlQueryObjectGet_servizioApplicativo_servizioApplicativoProprietaProtocollo.createSQLQuery(), jdbcProperties.isShowSql(), ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO, this.getServizioApplicativoFetch(),
			new JDBCObject(servizioApplicativo.getId(),Long.class),
			new JDBCObject(ProprietariProtocolProperty.SERVIZIO_APPLICATIVO.name(),String.class));

		if(servizioApplicativo_servizioApplicativoProprietaProtocollo_list != null) {
			for (Object servizioApplicativo_servizioApplicativoProprietaProtocollo_object: servizioApplicativo_servizioApplicativoProprietaProtocollo_list) {
				ServizioApplicativoProprietaProtocollo servizioApplicativo_servizioApplicativoProprietaProtocollo = (ServizioApplicativoProprietaProtocollo) servizioApplicativo_servizioApplicativoProprietaProtocollo_object;


				servizioApplicativo.addServizioApplicativoProprietaProtocollo(servizioApplicativo_servizioApplicativoProprietaProtocollo);
			}
		}               
		
        return servizioApplicativo;  
	
	} 
	
	@Override
	public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
		return this._exists(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
	}
	
	private boolean _exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
	
		org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities = 
					new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
				
		boolean existsServizioApplicativo = false;

		sqlQueryObject = sqlQueryObject.newSQLQueryObject();
		sqlQueryObject.setANDLogicOperator(true);

		sqlQueryObject.addFromTable(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model()));
		sqlQueryObject.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().NOME,true));
		sqlQueryObject.addWhereCondition("id=?");


		// Exists servizioApplicativo
		existsServizioApplicativo = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
			new JDBCObject(tableId,Long.class));

		
        return existsServizioApplicativo;
	
	}
	
	private void joinEngine(IExpression expression, ISQLQueryObject sqlQueryObject) throws NotImplementedException, ServiceException, Exception{
	
		if(expression.inUseModel(ServizioApplicativo.model().ID_SOGGETTO,false)){
			String tableName1 = this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model());
			String tableName2 = this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model().ID_SOGGETTO);
			sqlQueryObject.addWhereCondition(tableName1+".id_soggetto="+tableName2+".id");
		}
		if(expression.inUseModel(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO,false)){
			String tableName1 = this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model());
			String tableName2 = this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO);
			sqlQueryObject.addWhereCondition(tableName1+".id="+tableName2+".id_servizio_applicativo");
		}
		if(expression.inUseModel(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO,false)){
			String tableName1 = this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model());
			String tableName2 = this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO);
			sqlQueryObject.addWhereCondition(tableName1+".id="+tableName2+".id_proprietario");
			sqlQueryObject.addWhereCondition(tableName2+".tipo_proprietario='"+ProprietariProtocolProperty.SERVIZIO_APPLICATIVO.name()+"'");
		}
	}
	
	protected java.util.List<Object> getRootTablePrimaryKeyValuesEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdServizioApplicativo id) throws NotFoundException, ServiceException, NotImplementedException, Exception{
	    // Identificativi
        java.util.List<Object> rootTableIdValues = new java.util.ArrayList<>();
        Long longId = this.findIdServizioApplicativo(jdbcProperties, log, connection, sqlQueryObject.newSQLQueryObject(), id, true);
		rootTableIdValues.add(longId);
        return rootTableIdValues;
	}
	
	protected Map<String, List<IField>> getMapTableToPKColumnEngine() throws NotImplementedException, Exception{
	
		ServizioApplicativoFieldConverter converter = this.getServizioApplicativoFieldConverter();
		Map<String, List<IField>> mapTableToPKColumn = new java.util.HashMap<>();
		UtilsTemplate<IField> utilities = new UtilsTemplate<>();

		// ServizioApplicativo.model()
		mapTableToPKColumn.put(converter.toTable(ServizioApplicativo.model()),
			utilities.newList(
				new CustomField("id", Long.class, "id", converter.toTable(ServizioApplicativo.model()))
			));

		// ServizioApplicativo.model().ID_SOGGETTO
		mapTableToPKColumn.put(converter.toTable(ServizioApplicativo.model().ID_SOGGETTO),
			utilities.newList(
				new CustomField("id", Long.class, "id", converter.toTable(ServizioApplicativo.model().ID_SOGGETTO))
			));

		// ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO
		mapTableToPKColumn.put(converter.toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO),
			utilities.newList(
				new CustomField("id", Long.class, "id", converter.toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_RUOLO))
			));

		// ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA
		mapTableToPKColumn.put(converter.toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO),
			utilities.newList(
				new CustomField("id", Long.class, "id", converter.toTable(ServizioApplicativo.model().SERVIZIO_APPLICATIVO_PROPRIETA_PROTOCOLLO))
			));

        return mapTableToPKColumn;		
	}
	
	@Override
	public List<Long> findAllTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression paginatedExpression) throws ServiceException, NotImplementedException, Exception {
		
		List<Long> list = new ArrayList<Long>();

		sqlQueryObject.setSelectDistinct(true);
		sqlQueryObject.setANDLogicOperator(true);
		sqlQueryObject.addSelectField(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model())+".id");
		Class<?> objectIdClass = Long.class;
		
		List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFindAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
												this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model());
		
		joinEngine(paginatedExpression,sqlQueryObject);
		
		List<Object> listObjects = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.findAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
																			this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(), objectIdClass, listaQuery);
		for(Object object: listObjects) {
			list.add((Long)object);
		}

        return list;
		
	}
	
	@Override
	public long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws ServiceException, NotFoundException, MultipleResultException, NotImplementedException, Exception {
	
		sqlQueryObject.setSelectDistinct(true);
		sqlQueryObject.setANDLogicOperator(true);
		sqlQueryObject.addSelectField(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model())+".id");
		Class<?> objectIdClass = Long.class;
		
		List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFind(jdbcProperties, log, connection, sqlQueryObject, expression,
												this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model());
		
		joinEngine(expression,sqlQueryObject);

		Object res = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.find(jdbcProperties, log, connection, sqlQueryObject, expression,
														this.getServizioApplicativoFieldConverter(), ServizioApplicativo.model(), objectIdClass, listaQuery);
		if(res!=null && (((Long) res).longValue()>0) ){
			return ((Long) res).longValue();
		}
		else{
			throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
		}
		
	}

	@Override
	public InUse inUse(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws ServiceException, NotFoundException, NotImplementedException, Exception {
		return this.inUseEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
	}

	private InUse inUseEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws ServiceException, NotFoundException, NotImplementedException, Exception {

		InUse inUse = new InUse();
		inUse.setInUse(false);
		
		/* 
		 * TODO: implement code that checks whether the object identified by the id parameter is used by other objects
		*/
		
		// Delete this line when you have implemented the method
		int throwNotImplemented = 1;
		if(throwNotImplemented==1){
		        throw new NotImplementedException("NotImplemented");
		}
		// Delete this line when you have implemented the method

        return inUse;

	}
	
	@Override
	public IdServizioApplicativo findId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, boolean throwNotFound)
			throws NotFoundException, ServiceException, NotImplementedException, Exception {
		
		org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities = 
			new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

		ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();               

		// Object _servizioApplicativo
		sqlQueryObjectGet.addFromTable(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model()));
		sqlQueryObjectGet.addSelectField(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().NOME,true));
		sqlQueryObjectGet.addSelectField("id_soggetto");
		sqlQueryObjectGet.setANDLogicOperator(true);
		sqlQueryObjectGet.addWhereCondition("id=?");

		// Recupero _servizioApplicativo
		org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_servizioApplicativo = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] { 
			new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(tableId,Long.class)
		};
		List<Class<?>> listaFieldIdReturnType_servizioApplicativo = new ArrayList<Class<?>>();
		listaFieldIdReturnType_servizioApplicativo.add(String.class);
		listaFieldIdReturnType_servizioApplicativo.add(Long.class);
		org.openspcoop2.core.commons.search.IdServizioApplicativo id_servizioApplicativo = null;
		List<Object> listaFieldId_servizioApplicativo = jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
				listaFieldIdReturnType_servizioApplicativo, searchParams_servizioApplicativo);
		if(listaFieldId_servizioApplicativo==null || listaFieldId_servizioApplicativo.size()<=0){
			if(throwNotFound){
				throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
			}
		}
		else{
			// set _servizioApplicativo
			id_servizioApplicativo = new org.openspcoop2.core.commons.search.IdServizioApplicativo();
			id_servizioApplicativo.setNome((String)listaFieldId_servizioApplicativo.get(0));
			
			Long idSoggettoFK = (Long) listaFieldId_servizioApplicativo.get(1);
			id_servizioApplicativo.
				setIdSoggetto(((IDBSoggettoServiceSearch)this.getServiceManager(connection, jdbcProperties, log).
						getSoggettoServiceSearch()).findId(idSoggettoFK, true));
		}
		
		return id_servizioApplicativo;
		
	}

	@Override
	public Long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdServizioApplicativo id, boolean throwNotFound)
			throws NotFoundException, ServiceException, NotImplementedException, Exception {
	
		return this.findIdServizioApplicativo(jdbcProperties,log,connection,sqlQueryObject,id,throwNotFound);
			
	}
	
	@Override
	public List<List<Object>> nativeQuery(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, 
											String sql,List<Class<?>> returnClassTypes,Object ... param) throws ServiceException,NotFoundException,NotImplementedException,Exception{
		
		return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeQuery(jdbcProperties, log, connection, sqlQueryObject,
																							sql,returnClassTypes,param);
														
	}
	
	protected Long findIdServizioApplicativo(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdServizioApplicativo id, boolean throwNotFound) throws NotFoundException, ServiceException, NotImplementedException, Exception {

		if(jdbcProperties==null) {
			throw new ServiceException("Param jdbcProperties is null");
		}
		if(sqlQueryObject==null) {
			throw new ServiceException("Param sqlQueryObject is null");
		}
		if(id==null) {
			throw new ServiceException("Param id is null");
		}
		
		org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities = 
			new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

		ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();

		if(id.getIdSoggetto()==null){
			throw new ServiceException("IdSoggetto non fornito");
		}
		
		// Recupero id soggetto
		ISoggettoServiceSearch soggettoServiceSearch = this.getServiceManager(connection, jdbcProperties, log).getSoggettoServiceSearch();
		Soggetto sa_id_soggetto = ((IDBSoggettoServiceSearch)soggettoServiceSearch).get(id.getIdSoggetto());
		
		// Object _servizioApplicativo
		sqlQueryObjectGet.addFromTable(this.getServizioApplicativoFieldConverter().toTable(ServizioApplicativo.model()));
		sqlQueryObjectGet.addSelectField("id");
		// Devono essere mappati nella where condition i metodi dell'oggetto id.getXXX
		sqlQueryObjectGet.setANDLogicOperator(true);
		sqlQueryObjectGet.setSelectDistinct(true);
		sqlQueryObjectGet.addWhereCondition(this.getServizioApplicativoFieldConverter().toColumn(ServizioApplicativo.model().NOME,true)+"=?");
		sqlQueryObjectGet.addWhereCondition("id_soggetto=?");

		// Recupero _servizioApplicativo
		org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_servizioApplicativo = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] { 
			new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(id.getNome(),String.class),
			new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(sa_id_soggetto.getId(),Long.class)
		};
		Long id_servizioApplicativo = null;
		try{
			id_servizioApplicativo = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
						Long.class, searchParams_servizioApplicativo);
		}catch(NotFoundException notFound){
			if(throwNotFound){
				throw new NotFoundException(notFound);
			}
		}
		if(id_servizioApplicativo==null || id_servizioApplicativo<=0){
			if(throwNotFound){
				throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
			}
		}
		
		return id_servizioApplicativo;
	}
}