package com.ejie.m62fb.service.registroVPP;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import com.ejie.m62fb.utils.constants.IberErrorsMsgConstants;
import com.ejie.m62fb.utils.error.UFException;
import com.ejie.m62fb.dao.registroVPP.InquilinoAdDao;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.TableRowDto;
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.model.Inquilino;
import com.ejie.m62fb.model.InquilinoAd;

/**
 * InquilinoAdServiceImpl generated by UDA, 08-oct-2015 9:02:54.
XXXX
 */

@Service(value = "inquilinoAdService")
public class InquilinoAdServiceImpl implements InquilinoAdService {

	@Autowired
	private InquilinoAdDao inquilinoAdDao;

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

	/**
	 * Updates a single row in the InquilinoAd table.
	 *
	 * @param inquilinoAd InquilinoAd
	 * @return InquilinoAd
	 */
	@Transactional(rollbackFor = Throwable.class)
@PreAuthorize("hasPermission('INQUILINO_AD_SERVICE','SecureService','write')")
	public InquilinoAd update(InquilinoAd inquilinoAd) {
		
		// find for update
		InquilinoAd inquilinoasBD = this.inquilinoAdDao.findForUpdate(inquilinoAd);
		// actualizar datos modificables
		inquilinoasBD.setPersona(inquilinoAd.getPersona());
		inquilinoasBD.getPersona().setDireccion(
				inquilinoasBD.getPersona().getDireccion());
		inquilinoasBD.setIndAlojAdaptada(inquilinoAd.getIndAlojAdaptada());

		return this.inquilinoAdDao.update(inquilinoasBD);
	 }

	/**
	 * Finds a single row in the InquilinoAd table.
	 *
	 * @param inquilinoAd InquilinoAd
	 * @return InquilinoAd
	 */
@PreAuthorize("hasPermission('INQUILINO_AD_SERVICE','SecureService','read')")
	public InquilinoAd find(InquilinoAd inquilinoAd) {
		return (InquilinoAd) this.inquilinoAdDao.find(inquilinoAd);
	}
	
	/**
	 * Deletes a single row in the InquilinoAd table.
	 *
	 * @param inquilinoAd InquilinoAd
	 */
	@Transactional(rollbackFor = Throwable.class)
@PreAuthorize("hasPermission('INQUILINO_AD_SERVICE','SecureService','delete')")
	public void remove(InquilinoAd inquilinoAd) {
		this.inquilinoAdDao.remove(inquilinoAd);
	}

    
	/**
	 * Finds rows in the InquilinoAd table using like.
	 *
	 * @param inquilinoAd InquilinoAd
	 * @param jqGridRequestDto JQGridRequestDto
     * @param startsWith Boolean	 
	 * @return List<InquilinoAd>
	 */
@PreAuthorize("hasPermission('INQUILINO_AD_SERVICE','SecureService','read')")
	public List<InquilinoAd> findAllLike(InquilinoAd inquilinoAd, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		return (List<InquilinoAd>) this.inquilinoAdDao.findAllLike(inquilinoAd, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */
	 
        
	/**
	 * Filter method in the InquilinoAd table.
	 *
	 * @param filterInquilinoAd InquilinoAd
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return JQGridResponseDto<InquilinoAd>
	 */	
@PreAuthorize("hasPermission('INQUILINO_AD_SERVICE','SecureService','read')")
	public JQGridResponseDto< InquilinoAd> filter(InquilinoAd filterInquilinoAd, JQGridRequestDto jqGridRequestDto,  Boolean startsWith){
		List<InquilinoAd> listaInquilinoAd =  this.inquilinoAdDao.findAllLike(filterInquilinoAd, jqGridRequestDto, false);
		Long recordNum =  this.inquilinoAdDao.findAllLikeCount(filterInquilinoAd != null ? filterInquilinoAd: new InquilinoAd (),false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds()!=null){
			List< TableRowDto< InquilinoAd>> reorderSelection = this.inquilinoAdDao.reorderSelection(filterInquilinoAd, jqGridRequestDto, startsWith);
			return new JQGridResponseDto<InquilinoAd>(jqGridRequestDto, recordNum, listaInquilinoAd, reorderSelection);
		}
		return new JQGridResponseDto<InquilinoAd>(jqGridRequestDto, recordNum, listaInquilinoAd);   
	}
    
    
    /**
	 * Reorder the selection made in InquilinoAd table.
	 *
	 * @param filterInquilinoAd InquilinoAd
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return Object
	 */	
@PreAuthorize("hasPermission('INQUILINO_AD_SERVICE','SecureService','read')")
    public Object reorderSelection(InquilinoAd filterInquilinoAd, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		return this.inquilinoAdDao.reorderSelection(filterInquilinoAd, jqGridRequestDto, startsWith);
	}

    /**
     * Devuelve si existen contratos de alojamiento dotacional y de Etxelaguntza para el inquilino recibido
     * en el contrato que estamos dando de alta.
     * 
     * @param inquilinoAd
     * 			Integer
     * @param idContrato
	 *            Integer
     * @return
     */
	public Boolean existsContract4Inquilino(Integer idPersona, Integer idContrato) {
		return this.inquilinoAdDao.existsContract4Inquilino(idPersona, idContrato);
	}
    
	
}

