package com.ejie.m62fb.control;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ejie.m62fb.model.M62fbProvincias;

import com.ejie.m62fb.service.M62fbProvinciasService;
import com.ejie.x38.control.exception.ResourceNotFoundException;
import com.ejie.x38.dto.JQGridJSONModel;
import com.ejie.x38.dto.Pagination;

/**
 * M62fbProvinciasController generated by UDA, 22-oct-2012 12:14:20.
 * 
XXXX
 */

@Controller
@RequestMapping(value = "/m62fbprovincias")
public class M62fbProvinciasController {

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

	@Autowired
	private M62fbProvinciasService m62fbProvinciasService;

	/* para poder recuperar textos de i18n */
	/* i18n */
	@Resource
	private ReloadableResourceBundleMessageSource messageSource;

	/**
	 * Method 'getProvincias'.
	 * 
	 * @param id
	 *            String
	 * @param dsO
	 *            String
	 * @param autonomiaId
	 *            String
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 */
	@RequestMapping(value = "provincias", method = RequestMethod.GET)
	public @ResponseBody
	List<HashMap<String, String>> getProvincias(Locale locale) {
		try {
			// M62fbProvincias filterProvincia = new M62fbProvincias(id, dsO);
			List<M62fbProvincias> provincias = this.m62fbProvinciasService
					.findAll(null, null);
			if (provincias == null) {
				throw new Exception("No data Found.");
			}
			ArrayList<HashMap<String, String>> returnData = new ArrayList<HashMap<String, String>>();
			HashMap<String, String> opc = new HashMap<String, String>();
			Iterator modelosIter = provincias.iterator();
			while (modelosIter.hasNext()) {
				M62fbProvincias modelObject = (M62fbProvincias) modelosIter
						.next();
				opc.put("value", modelObject.getCodProvincia());
				opc.put("label",
						modelObject.getDesProviCas().get(locale.getLanguage()));
				returnData.add(opc);
				opc = new HashMap<String, String>();
			}
			return returnData;
		} catch (Exception e) {
			throw new ResourceNotFoundException("No data Found.");
		}
	}

	/**
	 * Method 'getById'.
	 * 
	 * @param codProvincia
	 *            String
	 * @param desProviCas
	 *            String
	 * @param desProviEus
	 *            String
	 * @param decProviCas
	 *            String
	 * @param decProviEus
	 *            String
	 * @param fecAlta
	 *            Date
	 * @param dirIpUsuario
	 *            String
	 * @param fehUltModif
	 *            Date
	 * @param codUsuUltModif
	 *            String
	 * @param ideRegistro
	 *            Integer
	 * @return m62fbProvincias M62fbProvincias
	 */
	@RequestMapping(value = "/{codProvincia}/{desProviCas}", method = RequestMethod.GET)
	public @ResponseBody
	M62fbProvincias getById(@PathVariable String codProvincia,
			@PathVariable String desProviCas, @PathVariable String desProviEus,
			@PathVariable String decProviCas, @PathVariable String decProviEus,
			@PathVariable Date fecAlta, @PathVariable String dirIpUsuario,
			@PathVariable Date fehUltModif,
			@PathVariable String codUsuUltModif,
			@PathVariable Integer ideRegistro) {
		M62fbProvincias m62fbProvincias = new M62fbProvincias();
		m62fbProvincias.setCodProvincia(codProvincia);
		// m62fbProvincias.setDesProviCas(desProviCas);
		m62fbProvincias = this.m62fbProvinciasService.find(m62fbProvincias);
		M62fbProvinciasController.logger
				.info("[GET - findBy_PK] : Obtener M62fbProvincias por PK");
		return m62fbProvincias;
	}

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

	/**
	 * Method 'getAllJQGrid'.
	 * 
	 * @param filterM62fbProvincias
	 *            M62fbProvincias
	 * @param pagination
	 *            Pagination
	 * @return JQGridJSONModel
	 */
	@RequestMapping(method = RequestMethod.GET, headers = { "JQGridModel=true" })
	public @ResponseBody
	JQGridJSONModel getAllJQGrid(
			@ModelAttribute M62fbProvincias filterM62fbProvincias,
			@ModelAttribute Pagination pagination) {
		List<M62fbProvincias> m62fbProvinciass = this.m62fbProvinciasService
				.findAll(filterM62fbProvincias, pagination);
		Long recordNum = this.m62fbProvinciasService
				.findAllCount(filterM62fbProvincias);
		M62fbProvinciasController.logger
				.info("[GET - jqGrid] : Obtener M62fbProvincias");
		return new JQGridJSONModel(pagination, recordNum, m62fbProvinciass);
	}

}
