package com.ejie.m62fb.service.registroVPP;
import org.springframework.security.access.prepost.PreAuthorize;

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.utils.constants.IberErrorsMsgConstants;
import com.ejie.m62fb.utils.error.UFException;
import com.ejie.m62fb.dao.registroVPP.InquilinoDao;
import com.ejie.m62fb.model.Inquilino;
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 = "inquilinoService")
public class InquilinoServiceImpl implements InquilinoService {

	@Autowired
	private InquilinoDao inquilinoDao;

	/**
	 * Inserts a single row in the Inquilino table.
	 * 
	 * @param inquilino
	 *            Inquilino
	 * @return Inquilino
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','create')")
	public Inquilino add(Inquilino inquilino) {
		Inquilino inquilinoBBDD = null;
		// 1. El inquilino no debe estar dado de alta en el mismo contrato en
		// Etxelaguntza.
		inquilinoBBDD = this.find(inquilino);
		if (inquilinoBBDD != null) {
			throw new UFException(IberErrorsMsgConstants.EXISTE_INQUILINO);
		} else {
			return this.inquilinoDao.add(inquilino);
		}
	}

	/**
	 * Updates a single row in the Inquilino table.
	 * 
	 * @param inquilino
	 *            Inquilino
	 * @return Inquilino
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','write')")
	public Inquilino update(Inquilino inquilino) {
		// find for update
		Inquilino inquilinoBD = this.inquilinoDao.findForUpdate(inquilino);
		// actualizar datos modificables
		inquilinoBD.setPersona(inquilino.getPersona());
		inquilinoBD.getPersona().setDireccion(
				inquilino.getPersona().getDireccion());
		inquilinoBD.setIndVivAdaptada(inquilino.getIndVivAdaptada());

		return this.inquilinoDao.update(inquilinoBD);
	}

	/**
	 * Finds a single row in the Inquilino table.
	 * 
	 * @param inquilino
	 *            Inquilino
	 * @return Inquilino
	 */
	@Override
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','read')")
	public Inquilino find(Inquilino inquilino) {
		return this.inquilinoDao.find(inquilino);
	}

	/**
	 * Deletes a single row in the Inquilino table.
	 * 
	 * @param inquilino
	 *            Inquilino
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','delete')")
	public void remove(Inquilino inquilino) {
		this.inquilinoDao.remove(inquilino);
	}

	/**
	 * Finds a list of rows in the Inquilino table.
	 * 
	 * @param inquilino
	 *            Inquilino
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<Inquilino>
	 */
	@Override
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','read')")
	public List<Inquilino> findAll(Inquilino inquilino,
			JQGridRequestDto jqGridRequestDto) {
		return this.inquilinoDao.findAll(inquilino, jqGridRequestDto);
	}

	/**
	 * Finds rows in the Inquilino table using like.
	 * 
	 * @param inquilino
	 *            Inquilino
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<Inquilino>
	 */
	@Override
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','read')")
	public List<Inquilino> findAllLike(Inquilino inquilino,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.inquilinoDao.findAllLike(inquilino, jqGridRequestDto,
				startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */
	/**
	 * Filter method in the Inquilino table.
	 * 
	 * @param filterInquilino
	 *            Inquilino
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Inquilino>
	 */
	@Override
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','read')")
	public JQGridResponseDto<Inquilino> filter(Inquilino filterInquilino,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<Inquilino> listaPropietario = this.inquilinoDao.findAllLike(
				filterInquilino, jqGridRequestDto, false);
		Long recordNum = this.inquilinoDao.findAllLikeCount(
				filterInquilino != null ? filterInquilino : new Inquilino(),
				false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Inquilino>> reorderSelection = this.inquilinoDao
					.reorderSelection(filterInquilino, jqGridRequestDto,
							startsWith);
			return new JQGridResponseDto<Inquilino>(jqGridRequestDto,
					recordNum, listaPropietario, reorderSelection);
		}
		return new JQGridResponseDto<Inquilino>(jqGridRequestDto, recordNum,
				listaPropietario);
	}

	/**
	 * Reorder the selection made in Inquilino table.
	 * 
	 * @param filterInquilino
	 *            Inquilino
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	@Override
@PreAuthorize("hasPermission('INQUILINO_SERVICE','SecureService','read')")
	public Object reorderSelection(Inquilino filterInquilino,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.inquilinoDao.reorderSelection(filterInquilino,
				jqGridRequestDto, startsWith);
	}
}
