package com.ejie.ab63.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab63.dao.HabitacionViviendaDao;
import com.ejie.ab63.model.HabitacionVivienda;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * Ab6341s00ServiceImpl  
 * 
 *  
 */

@Service(value = "habitacionViviendaService")
public class HabitacionViviendaServiceImpl implements HabitacionViviendaService {

	@Autowired
	private HabitacionViviendaDao ab6341s00Dao;

	@Autowired
	private ReloadableResourceBundleMessageSource appMessageSource;

	/**
	 * Inserts a single row in the Ab6341s00 table.
	 *
	 * @param ab6341s00
	 *            Ab6341s00
	 * @return Ab6341s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public HabitacionVivienda add(HabitacionVivienda ab6341s00) {
		return this.ab6341s00Dao.add(ab6341s00);
	}

	/**
	 * Updates a single row in the Ab6341s00 table.
	 *
	 * @param ab6341s00
	 *            Ab6341s00
	 * @return Ab6341s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public HabitacionVivienda update(HabitacionVivienda ab6341s00) {
		return this.ab6341s00Dao.update(ab6341s00);
	}

	/**
	 * Finds a single row in the Ab6341s00 table.
	 *
	 * @param ab6341s00
	 *            Ab6341s00
	 * @return Ab6341s00
	 */
	public HabitacionVivienda find(HabitacionVivienda ab6341s00) {
		return (HabitacionVivienda) this.ab6341s00Dao.find(ab6341s00);
	}

	/**
	 * Deletes a single row in the Ab6341s00 table.
	 *
	 * @param ab6341s00
	 *            Ab6341s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(HabitacionVivienda ab6341s00) {
		this.ab6341s00Dao.remove(ab6341s00);
	}

	/**
	 * Finds a list of rows in the Ab6341s00 table.
	 *
	 * @param ab6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<Ab6341s00>
	 */
	public List<HabitacionVivienda> findAll(HabitacionVivienda ab6341s00, JQGridRequestDto jqGridRequestDto) {
		return (List<HabitacionVivienda>) this.ab6341s00Dao.findAll(ab6341s00, jqGridRequestDto);
	}

	/**
	 * Finds rows in the Ab6341s00 table using like.
	 *
	 * @param ab6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<Ab6341s00>
	 */
	public List<HabitacionVivienda> findAllLike(HabitacionVivienda ab6341s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return (List<HabitacionVivienda>) this.ab6341s00Dao.findAllLike(ab6341s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Ab6341s00 table.
	 *
	 * @param filterAb6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(HabitacionVivienda filterAb6341s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		this.ab6341s00Dao.removeMultiple(filterAb6341s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Ab6341s00 table.
	 *
	 * @param filterAb6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Ab6341s00>
	 */
	public JQGridResponseDto<HabitacionVivienda> filter(HabitacionVivienda filterAb6341s00,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<HabitacionVivienda> listaAb6341s00 = this.ab6341s00Dao.findAllLike(filterAb6341s00, jqGridRequestDto,
				false);
		Long recordNum = this.ab6341s00Dao
				.findAllLikeCount(filterAb6341s00 != null ? filterAb6341s00 : new HabitacionVivienda(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<HabitacionVivienda>> reorderSelection = this.ab6341s00Dao.reorderSelection(filterAb6341s00,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<HabitacionVivienda>(jqGridRequestDto, recordNum, listaAb6341s00,
					reorderSelection);
		}

		for (HabitacionVivienda habitacionVivienda : listaAb6341s00) {

			if (habitacionVivienda.getAccesible() != null) {

				if ("0".equals(habitacionVivienda.getAccesible())) {

					habitacionVivienda.setAccesible(
							appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

				} else {

					habitacionVivienda.setAccesible(
							appMessageSource.getMessage("afirmativo", null, LocaleContextHolder.getLocale()));

				}
			} else {

				habitacionVivienda
						.setAccesible(appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

			}

			if (habitacionVivienda.getIlumNatural() != null) {

				if ("0".equals(habitacionVivienda.getIlumNatural())) {

					habitacionVivienda.setIlumNatural(
							appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

				} else {

					habitacionVivienda.setIlumNatural(
							appMessageSource.getMessage("afirmativo", null, LocaleContextHolder.getLocale()));

				}
			} else {
				habitacionVivienda
						.setIlumNatural(appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

			}

			if (habitacionVivienda.getVentilacionPatio() != null) {

				if ("0".equals(habitacionVivienda.getVentilacionPatio())) {

					habitacionVivienda.setVentilacionPatio(
							appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

				} else {

					habitacionVivienda.setVentilacionPatio(
							appMessageSource.getMessage("afirmativo", null, LocaleContextHolder.getLocale()));

				}
			} else {
				habitacionVivienda.setVentilacionPatio(
						appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

			}

			if (habitacionVivienda.getUsoTuristico() != null) {

				if ("0".equals(habitacionVivienda.getUsoTuristico())) {

					habitacionVivienda.setUsoTuristico(
							appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

				} else {

					habitacionVivienda.setUsoTuristico(
							appMessageSource.getMessage("afirmativo", null, LocaleContextHolder.getLocale()));

				}
			} else {

				habitacionVivienda.setUsoTuristico(
						appMessageSource.getMessage("negativo", null, LocaleContextHolder.getLocale()));

			}

		}
		return new JQGridResponseDto<HabitacionVivienda>(jqGridRequestDto, recordNum, listaAb6341s00);
	}

	/**
	 * Searches rows in the Ab6341s00 table.
	 *
	 * @param filterAb6341s00
	 *            Ab6341s00
	 * @param searchAb6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Ab6341s00>>
	 */
	public List<TableRowDto<HabitacionVivienda>> search(HabitacionVivienda filterAb6341s00,
			HabitacionVivienda searchAb6341s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.ab6341s00Dao.search(filterAb6341s00, searchAb6341s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in Ab6341s00 table.
	 *
	 * @param filterAb6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(HabitacionVivienda filterAb6341s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return this.ab6341s00Dao.reorderSelection(filterAb6341s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

	/**
	 * Finder method in the hierarchical the Ab6341s00 table.
	 *
	 * @param filterAb6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<JerarquiaDto<Ab6341s00>>
	 */
	public JQGridResponseDto<JerarquiaDto<HabitacionVivienda>> jerarquia(HabitacionVivienda filterAb6341s00,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<JerarquiaDto<HabitacionVivienda>> listaAb6341s00 = this.ab6341s00Dao.findAllLikeJerarquia(filterAb6341s00,
				jqGridRequestDto);
		Long recordNum = this.ab6341s00Dao.findAllLikeCountJerarquia(filterAb6341s00, jqGridRequestDto);
		return new JQGridResponseDto<JerarquiaDto<HabitacionVivienda>>(jqGridRequestDto, recordNum, listaAb6341s00);
	}

	/**
	 * Finder method for siblings rows in the hierarchical the Ab6341s00 table.
	 *
	 * @param filterAb6341s00
	 *            Ab6341s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return JQGridResponseDto<JerarquiaDto<Ab6341s00>>
	 */
	public JQGridResponseDto<JerarquiaDto<HabitacionVivienda>> jerarquiaChildren(HabitacionVivienda filterAb6341s00,
			JQGridRequestDto jqGridRequestDto) {
		JQGridResponseDto<JerarquiaDto<HabitacionVivienda>> jqGridResponseDto = new JQGridResponseDto<JerarquiaDto<HabitacionVivienda>>();
		jqGridResponseDto.addAdditionalParam(JQGridResponseDto.CHILDREN,
				this.ab6341s00Dao.findAllChild(filterAb6341s00, jqGridRequestDto));
		return jqGridResponseDto;
	}

}
