package it.csi.demo.democomp.business.business.dao.refappdao.dao.impl;

import it.csi.demo.democomp.business.business.dao.refappdao.dao.*;
import it.csi.demo.democomp.business.business.dao.refappdao.dto.*;
import it.csi.demo.democomp.business.business.dao.refappdao.qbe.*;
import it.csi.demo.democomp.business.business.dao.refappdao.metadata.*;
import it.csi.demo.democomp.business.business.dao.refappdao.exceptions.*;
import it.csi.demo.democomp.business.business.dao.util.*;
import it.csi.demo.democomp.business.business.dao.qbe.*;
import java.util.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import it.csi.util.performance.StopWatch;
import org.apache.log4j.Logger;
import java.util.Map;
import java.util.TreeMap;

/*PROTECTED REGION ID(R259030129) ENABLED START*/
// aggiungere qui eventuali import custom. 
/*PROTECTED REGION END*/

/**
 * @generated
 */
public class ComuneDaoImpl extends AbstractDAO
		implements
			ParameterizedRowMapper<ComuneDto>,
			ComuneDao {
	protected static Logger log = Logger.getLogger(Constants.APPLICATION_CODE);
	/**
	 * @generated
	 */
	protected NamedParameterJdbcTemplate jdbcTemplate;

	/**
	 * @generated
	 */
	public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	/**
	 * Method 'mapRow'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return ComuneDto
	 * @generated
	 */
	public ComuneDto mapRow(ResultSet rs, int row) throws SQLException {
		ComuneDto dto = new ComuneDto();
		dto = mapRow_internal(dto, rs, row);
		return dto;
	}

	/**
	 * Method 'mapRow_internal'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return ComuneDto
	 * @generated
	 */
	public ComuneDto mapRow_internal(ComuneDto objectToFill, ResultSet rs,
			int row) throws SQLException {
		ComuneDto dto = objectToFill;

		ComuneDaoRowMapper comuneDaoRowMapper = new ComuneDaoRowMapper(null);

		return comuneDaoRowMapper.mapRow_internal(objectToFill, rs, row);
	}

	protected ComuneDaoRowMapper findByPrimaryKeyRowMapper = new ComuneDaoRowMapper(
			null);
	protected ComuneDaoRowMapper findAllRowMapper = new ComuneDaoRowMapper(null);
	protected ComuneDaoRowMapper findByProvRowMapper = new ComuneDaoRowMapper(
			null);

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 * @generated
	 */
	public String getTableName() {
		return "REFAPP_D_COMUNE";
	}

	/** 
	 * Returns all rows from the REFAPP_D_COMUNE table that match the primary key criteria
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	public ComuneDto findByPrimaryKey(ComunePk pk) throws ComuneDaoException {

		final StringBuilder sql = new StringBuilder(
				"SELECT ID_COMUNE,ID_PROVINCIA,DESCRIZIONE FROM "
						+ getTableName() + " WHERE ID_COMUNE = :ID_COMUNE ");

		MapSqlParameterSource params = new MapSqlParameterSource();

		params.addValue("ID_COMUNE", pk.getIdComune(), java.sql.Types.DECIMAL);

		List<ComuneDto> list = null;

		StopWatch stopWatch = new StopWatch(Constants.APPLICATION_CODE);
		try {
			stopWatch.start();
			list = jdbcTemplate.query(sql.toString(), params,
					findByPrimaryKeyRowMapper);
		} catch (RuntimeException ex) {
			log.error("[ComuneDaoImpl::findByPrimaryKey] esecuzione query", ex);
			throw new ComuneDaoException("Query failed", ex);
		} finally {
			stopWatch.dumpElapsed("ComuneDaoImpl", "findByPrimaryKey",
					"esecuzione query", sql.toString());
			log.debug("[ComuneDaoImpl::findByPrimaryKey] END");
		}

		return list.size() == 0 ? null : list.get(0);

	}

	/** 
	 * Returns all rows from the REFAPP_D_COMUNE table that match the criteria ''.
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	public List<ComuneDto> findAll() throws ComuneDaoException {
		final StringBuilder sql = new StringBuilder(
				"SELECT ID_COMUNE,ID_PROVINCIA,DESCRIZIONE FROM "
						+ getTableName());

		MapSqlParameterSource params = new MapSqlParameterSource();

		List<ComuneDto> list = null;

		StopWatch stopWatch = new StopWatch(Constants.APPLICATION_CODE);
		try {
			stopWatch.start();
			list = jdbcTemplate.query(sql.toString(), params, findAllRowMapper);

		} catch (RuntimeException ex) {
			log.error("[ComuneDaoImpl::findAll] esecuzione query", ex);
			throw new ComuneDaoException("Query failed", ex);
		} finally {
			stopWatch.dumpElapsed("ComuneDaoImpl", "findAll",
					"esecuzione query", sql.toString());
			log.debug("[ComuneDaoImpl::findAll] END");
		}

		return list;

	}

	/** 
	 * Implementazione del finder findByProv
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	public List<ComuneDto> findFindByProv(
			it.csi.demo.democomp.business.business.dao.refappdao.dto.ComuneDto input)
			throws ComuneDaoException {
		StringBuilder sql = new StringBuilder();
		MapSqlParameterSource paramMap = new MapSqlParameterSource();

		sql.append("SELECT ID_COMUNE,ID_PROVINCIA,DESCRIZIONE ");
		sql.append(" FROM REFAPP_D_COMUNE");
		sql.append(" WHERE ");
		/*PROTECTED REGION ID(R-176827106) ENABLED START*/

		// personalizzare l'elenco dei parametri da passare al jdbctemplate (devono corrispondere in tipo e
		// numero ai parametri definiti nella queryString)
		sql.append("ID_PROVINCIA = :idProvincia");
		/*PROTECTED REGION END*/

		/*PROTECTED REGION ID(R-1061786740) ENABLED START*/
		//***aggiungere tutte le condizioni

		paramMap.addValue("idProvincia", input.getIdProvincia());

		/*PROTECTED REGION END*/
		List<ComuneDto> list = null;
		StopWatch stopWatch = new StopWatch(Constants.APPLICATION_CODE);
		try {
			stopWatch.start();
			list = jdbcTemplate.query(sql.toString(), paramMap,
					findByProvRowMapper);

		} catch (RuntimeException ex) {
			log.error("[ComuneDaoImpl::findFindByProv] esecuzione query", ex);
			throw new ComuneDaoException("Query failed", ex);
		} finally {
			stopWatch.dumpElapsed("ComuneDaoImpl", "findFindByProv",
					"esecuzione query", sql.toString());
			log.debug("[ComuneDaoImpl::findFindByProv] END");
		}
		return list;
	}

	/// flexible row mapper. 
	public class ComuneDaoRowMapper
			implements
				org.springframework.jdbc.core.RowMapper {

		private java.util.HashMap<String, String> columnsToReadMap = new java.util.HashMap<String, String>();
		private boolean mapAllColumns = true;

		/**
		 * @param columnsToRead elenco delle colonne da includere nel mapping (per query
		 *        incomplete, esempio distinct, custom select...)
		 */
		public ComuneDaoRowMapper(String[] columnsToRead) {
			if (columnsToRead != null) {
				mapAllColumns = false;
				for (int i = 0; i < columnsToRead.length; i++)
					columnsToReadMap.put(columnsToRead[i], columnsToRead[i]);
			}
		}

		/**
		 * Method 'mapRow'
		 * 
		 * @param rs
		 * @param row
		 * @throws SQLException
		 * @return ComuneDto
		 * @generated
		 */
		public ComuneDto mapRow(ResultSet rs, int row) throws SQLException {
			ComuneDto dto = new ComuneDto();
			dto = mapRow_internal(dto, rs, row);
			return dto;
		}

		/**
		 * Method 'mapRow_internal'
		 * 
		 * @param rs
		 * @param row
		 * @throws SQLException
		 * @return ComuneDto
		 * @generated
		 */
		public ComuneDto mapRow_internal(ComuneDto objectToFill, ResultSet rs,
				int row) throws SQLException

		{
			ComuneDto dto = objectToFill;

			if (mapAllColumns || columnsToReadMap.get("ID_COMUNE") != null)
				dto.setIdComune(rs.getBigDecimal("ID_COMUNE"));

			if (mapAllColumns || columnsToReadMap.get("ID_PROVINCIA") != null)
				dto.setIdProvincia(rs.getBigDecimal("ID_PROVINCIA"));

			if (mapAllColumns || columnsToReadMap.get("DESCRIZIONE") != null)
				dto.setDescrizione(rs.getString("DESCRIZIONE"));

			return dto;
		}

	}

}
