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(R-1419423315) ENABLED START*/
// aggiungere qui eventuali import custom. 
/*PROTECTED REGION END*/

/**
 * @generated
 */
public class ProvinciaDaoImpl extends AbstractDAO
		implements
			ParameterizedRowMapper<ProvinciaDto>,
			ProvinciaDao {
	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 ProvinciaDto
	 * @generated
	 */
	public ProvinciaDto mapRow(ResultSet rs, int row) throws SQLException {
		ProvinciaDto dto = new ProvinciaDto();
		dto = mapRow_internal(dto, rs, row);
		return dto;
	}

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

		ProvinciaDaoRowMapper provinciaDaoRowMapper = new ProvinciaDaoRowMapper(
				null);

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

	protected ProvinciaDaoRowMapper findByPrimaryKeyRowMapper = new ProvinciaDaoRowMapper(
			null);
	protected ProvinciaDaoRowMapper findAllRowMapper = new ProvinciaDaoRowMapper(
			null);
	protected ProvinciaDaoRowMapper findByRegRowMapper = new ProvinciaDaoRowMapper(
			null);

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

	/** 
	 * Returns all rows from the REFAPP_D_PROVINCIA table that match the primary key criteria
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	public ProvinciaDto findByPrimaryKey(ProvinciaPk pk)
			throws ProvinciaDaoException {

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

		MapSqlParameterSource params = new MapSqlParameterSource();

		params.addValue("ID_PROVINCIA", pk.getIdProvincia(),
				java.sql.Types.DECIMAL);

		List<ProvinciaDto> list = null;

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

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

	}

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

		MapSqlParameterSource params = new MapSqlParameterSource();

		List<ProvinciaDto> list = null;

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

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

		return list;

	}

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

		sql.append("SELECT ID_PROVINCIA,DESCRIZIONE,ID_REGIONE ");
		sql.append(" FROM REFAPP_D_PROVINCIA");
		sql.append(" WHERE ");
		/*PROTECTED REGION ID(R-38574697) 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_REGIONE = :idregione");
		/*PROTECTED REGION END*/

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

		paramMap.addValue("idregione", input.getIdRegione());

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

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

	/// flexible row mapper. 
	public class ProvinciaDaoRowMapper
			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 ProvinciaDaoRowMapper(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 ProvinciaDto
		 * @generated
		 */
		public ProvinciaDto mapRow(ResultSet rs, int row) throws SQLException {
			ProvinciaDto dto = new ProvinciaDto();
			dto = mapRow_internal(dto, rs, row);
			return dto;
		}

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

		{
			ProvinciaDto dto = objectToFill;

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

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

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

			return dto;
		}

	}

}
