package com.ejie.ab79.dao;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab79.model.RelDenIns;
import com.ejie.ab79.model.inspecciones.Inspecciones;

/**
 * RelDenInsDaoImpl  
 * 
 *  
 */

@Repository
@Transactional
public class RelDenInsDaoImpl implements RelDenInsDao {

	private JdbcTemplate jdbcTemplate;

	/**
	 * Method use to set the datasource.
	 *
	 * @param dataSource
	 *            DataSource
	 * @return
	 */
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	/*
	 * OPERACIONES CRUD
	 */

	/**
	 * Inserts a single row in the RelDenIns table.
	 *
	 * @param relDenIns
	 *            RelDenIns
	 * @return RelDenIns
	 */
	@Override
	public RelDenIns add(RelDenIns relDenIns) {

		int idRelDenIns = this.jdbcTemplate.queryForObject("SELECT AB61.S_AB6150T00.NEXTVAL FROM DUAL", Integer.class);
		relDenIns.setIdRelDenIns(idRelDenIns);

		String query = "INSERT INTO REL_DEN_INS (ID_REL_DEN_INS, NUM_DENUNCIA, ID_INSPECCION, IDENTIFICADOR_INSPECCION, FECHA_REGISTRO, USUARIO_REGISTRO) VALUES (?,?,?,?,SYSDATE,?)";
		this.jdbcTemplate.update(query, relDenIns.getIdRelDenIns(), relDenIns.getNumDenuncia(),
				relDenIns.getIdInspeccion(), relDenIns.getIdentificadorInspeccion(), relDenIns.getUsuarioRegistro());
		return relDenIns;
	}

	@Override
	public void removeByInspeccion(Inspecciones inspeccion) {
		String query = "DELETE FROM REL_DEN_INS WHERE ID_INSPECCION=?";
		this.jdbcTemplate.update(query, inspeccion.getIdInspeccion());
	}

	/**
	 * StringBuilder initilization value
	 */
	public static final int STRING_BUILDER_INIT = 4096;

}
