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

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

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

		RegioneDaoRowMapper regioneDaoRowMapper = new RegioneDaoRowMapper(null);

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

	protected RegioneDaoRowMapper findByPrimaryKeyRowMapper = new RegioneDaoRowMapper(
			null);
	protected RegioneDaoRowMapper findAllRowMapper = new RegioneDaoRowMapper(
			null);

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

	/** 
	 * Returns all rows from the REFAPP_D_REGIONE table that match the primary key criteria
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	public RegioneDto findByPrimaryKey(RegionePk pk) throws RegioneDaoException {

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

		MapSqlParameterSource params = new MapSqlParameterSource();

		params.addValue("ID_REGIONE", pk.getIdRegione(), java.sql.Types.DECIMAL);

		List<RegioneDto> list = null;

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

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

	}

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

		MapSqlParameterSource params = new MapSqlParameterSource();

		List<RegioneDto> list = null;

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

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

		return list;

	}

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

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

		{
			RegioneDto dto = objectToFill;

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

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

			return dto;
		}

	}

}
