package com.ejie.m62fb.control;

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

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.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.ejie.m62fb.model.M62fbSituacionOfe;
import com.ejie.m62fb.service.M62fbSituacionOfeService;
import com.ejie.x38.control.exception.ResourceNotFoundException;
import com.ejie.x38.dto.JQGridJSONModel;
import com.ejie.x38.dto.Pagination;

/**
 * M62fbSituacionOfeController generated by UDA, 26-oct-2012 12:29:44.
 * 
XXXX
 */

@Controller
@RequestMapping(value = "/m62fbsituacionofe")
public class M62fbSituacionOfeController {

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

	@Autowired
	private M62fbSituacionOfeService m62fbSituacionOfeService;

	/**
	 * Method 'getCreateForm'.
	 * 
	 * @param model
	 *            Model
	 * @return String
	 */
	@RequestMapping(value = "maint", method = RequestMethod.GET)
	public String getCreateForm(Model model) {
		M62fbSituacionOfeController.logger
				.info("[GET - View] : m62fbsituacionofe");
		return "m62fbsituacionofe";
	}

	/**
	 * Method 'getSituaciones'.
	 * 
	 * @param id
	 *            String
	 * @param dsO
	 *            String
	 * @param autonomiaId
	 *            String
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 */
	@RequestMapping(value = "tiposdesituacionofe", method = RequestMethod.GET)
	public @ResponseBody
	List<HashMap<String, String>> getTiposDeSitOfe(Locale locale) {
		try {
			List<M62fbSituacionOfe> tiposDeSituacion = this.m62fbSituacionOfeService
					.findAll(null, null);
			if (tiposDeSituacion == 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 = tiposDeSituacion.iterator();
			while (modelosIter.hasNext()) {
				M62fbSituacionOfe modelObject = (M62fbSituacionOfe) modelosIter
						.next();
				opc.put("value", modelObject.getCodTabla());
				opc.put("label",
						modelObject.getDesTabla().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 codTabla
	 *            String
	 * @param desTablaCas
	 *            String
	 * @return m62fbSituacionOfe M62fbSituacionOfe
	 */
	@RequestMapping(value = "/{codTabla}", method = RequestMethod.GET)
	public @ResponseBody
	M62fbSituacionOfe getById(@PathVariable String codTabla,
			@PathVariable String desTablaCas) {
		M62fbSituacionOfe m62fbSituacionOfe = new M62fbSituacionOfe();
		m62fbSituacionOfe.setCodTabla(codTabla);
		// m62fbSituacionOfe.setDesTabla(desTablaCas);
		m62fbSituacionOfe = this.m62fbSituacionOfeService
				.find(m62fbSituacionOfe);
		M62fbSituacionOfeController.logger
				.info("[GET - findBy_PK] : Obtener M62fbSituacionOfe por PK");
		return m62fbSituacionOfe;
	}

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

	/**
	 * Method 'edit'.
	 * 
	 * @param m62fbSituacionOfe
	 *            M62fbSituacionOfe
	 * @return M62fbSituacionOfe
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public @ResponseBody
	M62fbSituacionOfe edit(@RequestBody M62fbSituacionOfe m62fbSituacionOfe) {
		M62fbSituacionOfe m62fbSituacionOfeAux = this.m62fbSituacionOfeService
				.update(m62fbSituacionOfe);
		M62fbSituacionOfeController.logger
				.info("[PUT] : M62fbSituacionOfe actualizado correctamente");
		return m62fbSituacionOfeAux;
	}

	/**
	 * Method 'add'.
	 * 
	 * @param m62fbSituacionOfe
	 *            M62fbSituacionOfe
	 * @return M62fbSituacionOfe
	 */
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody
	M62fbSituacionOfe add(@RequestBody M62fbSituacionOfe m62fbSituacionOfe) {
		M62fbSituacionOfe m62fbSituacionOfeAux = this.m62fbSituacionOfeService
				.add(m62fbSituacionOfe);
		M62fbSituacionOfeController.logger
				.info("[POST] : M62fbSituacionOfe insertado correctamente");
		return m62fbSituacionOfeAux;
	}

	/**
	 * Method 'remove'.
	 * 
	 * @param codTabla
	 *            String
	 * @param desTablaCas
	 *            String
	 * @return m62fbSituacionOfe
	 */
	@RequestMapping(value = "/{codTabla}", method = RequestMethod.DELETE)
	@ResponseStatus(value = HttpStatus.OK)
	public @ResponseBody
	M62fbSituacionOfe remove(@PathVariable String codTabla,
			@PathVariable String desTablaCas) {
		M62fbSituacionOfe m62fbSituacionOfe = new M62fbSituacionOfe();
		m62fbSituacionOfe.setCodTabla(codTabla);
		// m62fbSituacionOfe.setDesTabla(desTablaCas);
		this.m62fbSituacionOfeService.remove(m62fbSituacionOfe);
		M62fbSituacionOfeController.logger
				.info("[DELETE] : M62fbSituacionOfe borrado correctamente");
		return m62fbSituacionOfe;
	}

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

}
