package com.ejie.m62fb.service.registroVPP;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.m62fb.dao.registroVPP.InquilinoElementoDao;
import com.ejie.m62fb.model.InquilinoElemento;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * InquilinoServiceImpl generated by UDA, 21-oct-2015 14:10:38.
 * 
XXXX
 */

@Service(value = "inquilinoElementoService")
public class InquilinoElementoServiceImpl implements InquilinoElementoService {

	@Autowired
	private InquilinoElementoDao inquilinoElementoDao;

	/**
	 * Inserts a single row in the InquilinoElemento table.
	 * 
	 * @param inquilinoElemento
	 *            InquilinoElemento
	 * @return InquilinoElemento
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
	public InquilinoElemento add(InquilinoElemento inquilinoElemento) {
		return this.inquilinoElementoDao.add(inquilinoElemento);
	}

	/**
	 * Updates a single row in the InquilinoElemento table.
	 * 
	 * @param inquilinoElemento
	 *            InquilinoElemento
	 * @return InquilinoElemento
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
	public InquilinoElemento update(InquilinoElemento inquilinoElemento) {
		// find for update
		InquilinoElemento inquilinoElementoBD = this.inquilinoElementoDao
				.findForUpdate(inquilinoElemento);
		// actualizar datos modificables
		inquilinoElementoBD.setPersona(inquilinoElemento.getPersona());
		inquilinoElementoBD.getPersona().setDireccion(
				inquilinoElemento.getPersona().getDireccion());
		inquilinoElementoBD.setIndVivAdaptada(inquilinoElemento
				.getIndVivAdaptada());
		// update
		return this.inquilinoElementoDao.update(inquilinoElementoBD);
	}

	/**
	 * Finds a single row in the InquilinoElemento table.
	 * 
	 * @param inquilinoElemento
	 *            InquilinoElemento
	 * @return InquilinoElemento
	 */
	@Override
	public InquilinoElemento find(InquilinoElemento inquilinoElemento) {
		return this.inquilinoElementoDao.find(inquilinoElemento);
	}

	/**
	 * Deletes a single row in the InquilinoElemento table.
	 * 
	 * @param inquilinoElemento
	 *            InquilinoElemento
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void remove(InquilinoElemento inquilinoElemento) {
		this.inquilinoElementoDao.remove(inquilinoElemento);
	}

	/**
	 * Finds a list of rows in the InquilinoElemento table.
	 * 
	 * @param inquilinoElemento
	 *            InquilinoElemento
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<InquilinoElemento>
	 */
	@Override
	public List<InquilinoElemento> findAll(InquilinoElemento inquilinoElemento,
			JQGridRequestDto jqGridRequestDto) {
		return this.inquilinoElementoDao.findAll(inquilinoElemento,
				jqGridRequestDto);
	}

	/**
	 * Finds rows in the InquilinoElemento table using like.
	 * 
	 * @param inquilinoElemento
	 *            InquilinoElemento
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<InquilinoElemento>
	 */
	@Override
	public List<InquilinoElemento> findAllLike(
			InquilinoElemento inquilinoElemento,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.inquilinoElementoDao.findAllLike(inquilinoElemento,
				jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */
	/**
	 * Filter method in the InquilinoElemento table.
	 * 
	 * @param filterInquilinoElemento
	 *            InquilinoElemento
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<InquilinoElemento>
	 */
	@Override
	public JQGridResponseDto<InquilinoElemento> filter(
			InquilinoElemento filterInquilinoElemento,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<InquilinoElemento> listaPropietario = this.inquilinoElementoDao
				.findAllLike(filterInquilinoElemento, jqGridRequestDto, false);
		Long recordNum = this.inquilinoElementoDao.findAllLikeCount(
				filterInquilinoElemento != null ? filterInquilinoElemento
						: new InquilinoElemento(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<InquilinoElemento>> reorderSelection = this.inquilinoElementoDao
					.reorderSelection(filterInquilinoElemento,
							jqGridRequestDto, startsWith);
			return new JQGridResponseDto<InquilinoElemento>(jqGridRequestDto,
					recordNum, listaPropietario, reorderSelection);
		}
		return new JQGridResponseDto<InquilinoElemento>(jqGridRequestDto,
				recordNum, listaPropietario);
	}

	/**
	 * Reorder the selection made in InquilinoElemento table.
	 * 
	 * @param filterInquilinoElemento
	 *            InquilinoElemento
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	@Override
	public Object reorderSelection(InquilinoElemento filterInquilinoElemento,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.inquilinoElementoDao.reorderSelection(
				filterInquilinoElemento, jqGridRequestDto, startsWith);
	}
}