package com.ejie.m62fb.control;

import com.ejie.m62fb.service.M62fbDisponibilidadVivsService;
import com.ejie.m62fb.service.M62fbr45s0s00Service;
import com.ejie.m62fb.service.M62fbSituacionOfeService;
import com.ejie.m62fb.service.M62fbRegTenenciaService;
import com.ejie.x38.control.exception.ResourceNotFoundException;
import com.ejie.x38.control.exception.ServiceUnavailableException;
import com.ejie.x38.dto.JQGridJSONModel;
import com.ejie.x38.dto.Pagination;
import com.ejie.x38.util.ObjectConversionManager;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import com.ejie.m62fb.model.M62fbDisponibilidadVivs;
import com.ejie.m62fb.model.M62fbr45s0s00;

/**
 * M62fbr45s0s00Controller generated by UDA, 05-nov-2012 14:56:10.
 * 
XXXX
 */

@Controller
@RequestMapping(value = "/ofertas")
public class M62fbr45s0s00Controller {

	private static final Logger logger = LoggerFactory
			.getLogger(M62fbr45s0s00Controller.class);

	@Autowired
	private M62fbr45s0s00Service m62fbr45s0s00Service;
	@Autowired
	private M62fbDisponibilidadVivsService m62fbDisponibilidadVivsService;
	@Autowired
	private M62fbSituacionOfeService m62fbSituacionOfeService;
	@Autowired
	private M62fbRegTenenciaService m62fbRegTenenciaService;

	/**
	 * Method 'getCreateForm'.
	 * 
	 * @param model
	 *            Model
	 * @return String
	 */
	@RequestMapping(value = "maint", method = RequestMethod.GET)
	public String getCreateForm(Model model) {
		M62fbr45s0s00Controller.logger.info("[GET - View] : m62fbr45s0s00");
		model.addAttribute("comboCodSitOferta",
				m62fbSituacionOfeService.findAll(null, null));
		model.addAttribute("comboCodRegTenencia",
				m62fbRegTenenciaService.findAll(null, null));

		return "m62fbr45s0s00";
	}

	/**
	 * Method 'getPorFiltroPersonalizado'.
	 * 
	 * @param idM62r45t0_search
	 *            Long
	 * @param nifAdjudicatario_search_label
	 *            Long
	 * @param codSitOferta_search
	 *            String
	 * @param fecOfertaDesde_search
	 *            Date
	 * @param fecOfertaHasta_search
	 *            Date
	 * @param fecAceptacionDesde_search
	 *            Date
	 * @param fecAceptacionHasta_search
	 *            Date
	 * @param fecFinalizacionDesde_search
	 *            Date
	 * @param fecFinalizacionHasta_search
	 *            Date
	 * @param fecDeclinacionDesde_search
	 *            Date
	 * @param fecDeclinacionHasta_search
	 *            Date
	 * @return List
	 */

	@RequestMapping(method = RequestMethod.GET, headers = { "JQGridModel=true" })
	public @ResponseBody
	Object getAllJQGrid(
			@RequestParam(value = "idM62r45t0", required = false) BigDecimal idR45,
			// @RequestParam(value = "nifAdjudicatario", required = false)
			// BigDecimal idPersona,
			@RequestParam(value = "codSitOferta", required = false) String codSitOferta,
			@RequestParam(value = "fecOfertaDesde", required = false) Date fecOfertaDesde,
			@RequestParam(value = "fecOfertaHasta", required = false) Date fecOfertaHasta,
			@RequestParam(value = "fecAceptacionDesde", required = false) Date fecAceptacionDesde,
			@RequestParam(value = "fecAceptacionHasta", required = false) Date fecAceptacionHasta,
			@RequestParam(value = "fecFinalizacionDesde", required = false) Date fecFinalizacionDesde,
			@RequestParam(value = "fecFinalizacionHasta", required = false) Date fecFinalizacionHasta,
			@RequestParam(value = "fecDeclinacionDesde", required = false) Date fecDeclinacionDesde,
			@RequestParam(value = "fecDeclinacionHasta", required = false) Date fecDeclinacionHasta,
			@RequestParam(value = "adjudicatario[idPersona]", required = false) BigDecimal idPersona,
			HttpServletRequest request) {
		try {
			Pagination pagination = null;

			if (request.getHeader("JQGridModel") != null
					&& request.getHeader("JQGridModel").equals("true")) {
				pagination = new Pagination();
				pagination.setPage(Long.valueOf(request.getParameter("page")));
				pagination.setRows(Long.valueOf(request.getParameter("rows")));
				pagination.setSort(request.getParameter("sidx"));
				pagination.setAscDsc(request.getParameter("sord"));
				List<M62fbr45s0s00> ofertas = this.m62fbr45s0s00Service
						.findAllPersonalizado(idR45, idPersona, codSitOferta,
								fecOfertaDesde, fecOfertaHasta,
								fecFinalizacionDesde, fecFinalizacionHasta,
								fecDeclinacionDesde, fecDeclinacionHasta,
								fecAceptacionDesde, fecAceptacionHasta,
								pagination);

				if (ofertas == null) {
					throw new Exception("No data Found.");
				}

				Long total = getPorFiltroPersonalizadoCount(idR45, idPersona,
						codSitOferta, fecOfertaDesde, fecOfertaHasta,
						fecAceptacionDesde, fecAceptacionHasta,
						fecFinalizacionDesde, fecFinalizacionHasta,
						fecDeclinacionDesde, fecDeclinacionHasta, request);
				JQGridJSONModel data = new JQGridJSONModel();
				data.setPage(request.getParameter("page"));
				data.setRecords(total.intValue());
				data.setTotal(total, pagination.getRows());
				data.setRows(ofertas);
				return data;
			} else {
				List<M62fbr45s0s00> ofertas = this.m62fbr45s0s00Service
						.findAllPersonalizado(idR45, idPersona, codSitOferta,
								fecOfertaDesde, fecOfertaHasta,
								fecAceptacionDesde, fecAceptacionHasta,
								fecFinalizacionDesde, fecFinalizacionHasta,
								fecDeclinacionDesde, fecDeclinacionHasta,
								pagination);
				if (ofertas == null) {
					throw new Exception("No data Found.");
				}
				return ofertas;
			}
		} catch (Exception e) {
			throw new ResourceNotFoundException("No data Found.");
		}
	}

	/**
	 * Method 'getPorFiltroPersonalizadoCount'.
	 * 
	 * @param idM62r45t0_search
	 *            BigDecimal
	 * @param nifAdjudicatario_search_label
	 *            Long
	 * @param codSitOferta_search
	 *            String
	 * @param fecOfertaDesde_search
	 *            Date
	 * @param fecOfertaHasta_search
	 *            Date
	 * @param fecAceptacionDesde_search
	 *            Date
	 * @param fecAceptacionHasta_search
	 *            Date
	 * @param fecFinalizacionDesde_search
	 *            Date
	 * @param fecFinalizacionHasta_search
	 *            Date
	 * @param fecDeclinacionDesde_search
	 *            Date
	 * @param fecDeclinacionHasta_search
	 *            Date
	 * @param request
	 *            HttpServletRequest
	 * @return Long
	 */
	@RequestMapping(value = "/filtrarOfertasCount", method = RequestMethod.GET)
	public @ResponseBody
	Long getPorFiltroPersonalizadoCount(
			@RequestParam(value = "idM62r45t0", required = false) BigDecimal idR45,
			@RequestParam(value = "nifAdjudicatario", required = false) BigDecimal idPersona,
			@RequestParam(value = "codSitOferta", required = false) String codSitOferta,
			@RequestParam(value = "fecOfertaDesde", required = false) Date fecOfertaDesde,
			@RequestParam(value = "fecOfertaHasta", required = false) Date fecOfertaHasta,
			@RequestParam(value = "fecAceptacionDesde", required = false) Date fecAceptacionDesde,
			@RequestParam(value = "fecAceptacionHasta", required = false) Date fecAceptacionHasta,
			@RequestParam(value = "fecFinalizacionDesde", required = false) Date fecFinalizacionDesde,
			@RequestParam(value = "fecFinalizacionHasta", required = false) Date fecFinalizacionHasta,
			@RequestParam(value = "fecDeclinacionDesde", required = false) Date fecDeclinacionDesde,
			@RequestParam(value = "fecDeclinacionHasta", required = false) Date fecDeclinacionHasta,
			HttpServletRequest request) {
		try {
			return m62fbr45s0s00Service.findAllPersonalizadoCount(idR45,
					idPersona, codSitOferta, fecOfertaDesde, fecOfertaHasta,
					fecAceptacionDesde, fecAceptacionHasta,
					fecFinalizacionDesde, fecFinalizacionHasta,
					fecDeclinacionDesde, fecDeclinacionHasta);
		} catch (Exception e) {
			throw new ServiceUnavailableException(
					"Count Service is not responding.");
		}
	}

	/**
	 * Method 'getById'.
	 * 
	 * @param idM62r45t0
	 *            Integer
	 * @return m62fbr45s0s00 M62fbr45s0s00
	 */
	@RequestMapping(value = "/{idM62r45t0}", method = RequestMethod.GET)
	public @ResponseBody
	M62fbr45s0s00 getById(@PathVariable BigDecimal idM62r45t0) {
		M62fbr45s0s00 m62fbr45s0s00 = new M62fbr45s0s00();
		m62fbr45s0s00.setIdM62r45t0(idM62r45t0);
		m62fbr45s0s00 = this.m62fbr45s0s00Service.find(m62fbr45s0s00);
		M62fbr45s0s00Controller.logger
				.info("[GET - findBy_PK] : Obtener M62fbr45s0s00 por PK");
		return m62fbr45s0s00;
	}

	/**
	 * Method 'getAll'.
	 * 
	 * @param filterM62fbr45s0s00
	 *            M62fbr45s0s00
	 * @return List
	 */
	@RequestMapping(method = RequestMethod.GET)
	public @ResponseBody
	List<M62fbr45s0s00> getAll(@ModelAttribute M62fbr45s0s00 filterM62fbr45s0s00) {
		M62fbr45s0s00Controller.logger
				.info("[GET - find_ALL] : Obtener M62fbr45s0s00 por filtro");
		return this.m62fbr45s0s00Service.findAll(filterM62fbr45s0s00, null);
	}

	/**
	 * Method 'edit'.
	 * 
	 * @param m62fbr45s0s00
	 *            M62fbr45s0s00
	 * @return M62fbr45s0s00
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public @ResponseBody
	M62fbr45s0s00 edit(@RequestBody M62fbr45s0s00 m62fbr45s0s00) {
		M62fbr45s0s00 m62fbr45s0s00Aux = this.m62fbr45s0s00Service
				.update(m62fbr45s0s00);
		M62fbr45s0s00Controller.logger
				.info("[PUT] : M62fbr45s0s00 actualizado correctamente");
		return m62fbr45s0s00Aux;
	}

	/**
	 * Method 'add'.
	 * 
	 * @param m62fbr45s0s00
	 *            M62fbr45s0s00
	 * @return M62fbr45s0s00
	 */
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody
	M62fbr45s0s00 add(@RequestBody M62fbr45s0s00 m62fbr45s0s00) {
		M62fbr45s0s00 m62fbr45s0s00Aux = this.m62fbr45s0s00Service
				.add(m62fbr45s0s00);
		M62fbr45s0s00Controller.logger
				.info("[POST] : M62fbr45s0s00 insertado correctamente");
		return m62fbr45s0s00Aux;
	}

	/**
	 * Method 'remove'.
	 * 
	 * @param idM62r45t0
	 *            Integer
	 * @param idPersona
	 *            Integer
	 * @param codSitOferta
	 *            String
	 * @param fecOferta
	 *            Date
	 * @param fecFinalizacion
	 *            Date
	 * @param fecDeclinacion
	 *            Date
	 * @param fecAceptacion
	 *            Date
	 * @param texObservaciones
	 *            String
	 * @param fehUltModif
	 *            Date
	 * @param codUsuUltModif
	 *            String
	 * @param ideRegistro
	 *            Integer
	 * @param fecAlta
	 *            Date
	 * @return m62fbr45s0s00
	 */
	@RequestMapping(value = "/{idM62r45t0}", method = RequestMethod.DELETE)
	@ResponseStatus(value = HttpStatus.OK)
	public @ResponseBody
	M62fbr45s0s00 remove(@PathVariable BigDecimal idM62r45t0) {
		M62fbr45s0s00 m62fbr45s0s00 = new M62fbr45s0s00();
		m62fbr45s0s00.setIdM62r45t0(idM62r45t0);

		this.m62fbr45s0s00Service.remove(m62fbr45s0s00);
		M62fbr45s0s00Controller.logger
				.info("[DELETE] : M62fbr45s0s00 borrado correctamente");
		return m62fbr45s0s00;
	}

	/**
	 * Method 'removeAll'.
	 * 
	 * @param m62fbr45s0s00Ids
	 *            List
	 * @return m62fbr45s0s00List
	 */
	@RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public @ResponseBody
	List<List<String>> removeMultiple(
			@RequestBody List<List<String>> m62fbr45s0s00Ids) {
		List<M62fbr45s0s00> m62fbr45s0s00List = new ArrayList<M62fbr45s0s00>();
		for (List<String> m62fbr45s0s00Id : m62fbr45s0s00Ids) {
			Iterator<String> iterator = m62fbr45s0s00Id.iterator();
			M62fbr45s0s00 m62fbr45s0s00 = new M62fbr45s0s00(); // NOPMD - Objeto
																// nuevo en la
																// lista
																// (parametro
																// del servicio)
			m62fbr45s0s00.setIdM62r45t0(ObjectConversionManager.convert(
					iterator.next(), BigDecimal.class));
			m62fbr45s0s00.setIdPersona(ObjectConversionManager.convert(
					iterator.next(), BigDecimal.class));
			m62fbr45s0s00.setNifAdjudicatario(ObjectConversionManager.convert(
					iterator.next(), String.class));
			m62fbr45s0s00.setCodSitOferta(ObjectConversionManager.convert(
					iterator.next(), String.class));
			m62fbr45s0s00.setFecOferta(ObjectConversionManager.convert(
					iterator.next(), java.util.Date.class));
			m62fbr45s0s00.setFecFinalizacion(ObjectConversionManager.convert(
					iterator.next(), java.util.Date.class));
			m62fbr45s0s00.setFecDeclinacion(ObjectConversionManager.convert(
					iterator.next(), java.util.Date.class));
			m62fbr45s0s00.setFecAceptacion(ObjectConversionManager.convert(
					iterator.next(), java.util.Date.class));
			m62fbr45s0s00.setTexObservaciones(ObjectConversionManager.convert(
					iterator.next(), String.class));
			m62fbr45s0s00.setFehUltModif(ObjectConversionManager.convert(
					iterator.next(), java.util.Date.class));
			m62fbr45s0s00.setCodUsuUltModif(ObjectConversionManager.convert(
					iterator.next(), String.class));
			m62fbr45s0s00.setIdeRegistro(ObjectConversionManager.convert(
					iterator.next(), java.lang.Integer.class));
			m62fbr45s0s00.setFecAlta(ObjectConversionManager.convert(
					iterator.next(), java.util.Date.class));
			m62fbr45s0s00List.add(m62fbr45s0s00);
		}
		this.m62fbr45s0s00Service.removeMultiple(m62fbr45s0s00List);
		M62fbr45s0s00Controller.logger
				.info("[POST - DELETE_ALL] : M62fbr45s0s00 borrados correctamente");
		return m62fbr45s0s00Ids;
	}

	/**
	 * Method 'getDescAdjudicatario'.
	 * 
	 * @param BigDecimal
	 *            incidencia
	 * @param request
	 *            HttpServletRequest
	 * @return Long
	 */
	@RequestMapping(value = "/descadjudicatario", method = RequestMethod.GET)
	public @ResponseBody
	String getDescAdjudicatario(
			@RequestParam(value = "idPersona", required = false) BigDecimal idPersona,
			HttpServletRequest request) {
		try {
			String data;
			data = m62fbr45s0s00Service.getDescAdjudicatario(idPersona);
			return data;
		} catch (Exception e) {
			throw new ServiceUnavailableException(
					"El Servicio de detalle de adjudicatarios no está disponible.");
		}
	}

	/**
	 * Method 'getAllJQGrid'.
	 * 
	 * @param filterM62fbr45s0s00
	 *            M62fbr45s0s00
	 * @param pagination
	 *            Pagination
	 * @return JQGridJSONModel
	 * @RequestMapping(method = RequestMethod.GET, headers = {
	 *                        "JQGridModel=true" }) public @ResponseBody
	 *                        JQGridJSONModel getAllJQGrid(
	 * @ModelAttribute M62fbr45s0s00 filterM62fbr45s0s00,
	 * @ModelAttribute Pagination pagination) { List<M62fbr45s0s00>
	 *                 m62fbr45s0s00s = this.m62fbr45s0s00Service.findAll(
	 *                 filterM62fbr45s0s00, pagination); Long recordNum =
	 *                 this.m62fbr45s0s00Service
	 *                 .findAllCount(filterM62fbr45s0s00);
	 *                 M62fbr45s0s00Controller.logger
	 *                 .info("[GET - jqGrid] : Obtener M62fbr45s0s00"); return
	 *                 new JQGridJSONModel(pagination, recordNum,
	 *                 m62fbr45s0s00s); }
	 */
	@RequestMapping(value = "/catalogoReport", method = RequestMethod.GET)
	public ModelAndView getCatalogoReport(
			Model model,
			@RequestParam(value = "idM62r45t0", required = false) BigDecimal idR45,
			@RequestParam(value = "nifAdjudicatario", required = false) BigDecimal idPersona,
			@RequestParam(value = "codSitOferta", required = false) String codSitOferta,
			@RequestParam(value = "fecOfertaDesde", required = false) Date fecOfertaDesde,
			@RequestParam(value = "fecOfertaHasta", required = false) Date fecOfertaHasta,
			@RequestParam(value = "fecAceptacionDesde", required = false) Date fecAceptacionDesde,
			@RequestParam(value = "fecAceptacionHasta", required = false) Date fecAceptacionHasta,
			@RequestParam(value = "fecFinalizacionDesde", required = false) Date fecFinalizacionDesde,
			@RequestParam(value = "fecFinalizacionHasta", required = false) Date fecFinalizacionHasta,
			@RequestParam(value = "fecDeclinacionDesde", required = false) Date fecDeclinacionDesde,
			@RequestParam(value = "fecDeclinacionHasta", required = false) Date fecDeclinacionHasta,
			HttpServletRequest request, HttpServletResponse response)
			throws ParseException {
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		Pagination pagination = null;

		if (request.getHeader("JQGridModel") != null
				&& request.getHeader("JQGridModel").equals("true")) {
			pagination = new Pagination();
			pagination.setPage(Long.valueOf(request.getParameter("page")));
			pagination.setRows(Long.valueOf(request.getParameter("rows")));
			pagination.setSort(request.getParameter("sidx"));
			pagination.setAscDsc(request.getParameter("sord"));
		}
		List<M62fbDisponibilidadVivs> lista = null;

		lista = this.m62fbDisponibilidadVivsService
				.findAllPersonalizadoVivEnOferExcel(idR45, idPersona,
						codSitOferta, fecOfertaDesde, fecOfertaHasta,
						fecAceptacionDesde, fecAceptacionHasta,
						fecFinalizacionDesde, fecFinalizacionHasta,
						fecDeclinacionDesde, fecDeclinacionHasta, pagination);
		model.addAttribute("excelOfertas", lista);

		return new ModelAndView("excelOfertas", "model", model);
	}

}
