package com.ejie.ab39a.service;
import com.ejie.ab39a.dao.MunicipioDao;
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.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab39a.model.Municipio;

/**
 * Municipio.
 *  
 */
@Service(value = "municipioService")
public class MunicipioServiceImpl implements MunicipioService {

	@Autowired
	private MunicipioDao municipioDao;

	/**
	 * Inserts a single row in the Municipio table.
	 *
	 * @param municipio Municipio
	 * @return Municipio
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public Municipio add(Municipio municipio) {
		return this.municipioDao.add(municipio);
	}

	/**
	 * Updates a single row in the Municipio table.
	 *
	 * @param municipio Municipio
	 * @return Municipio
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public Municipio update(Municipio municipio) {
		return this.municipioDao.update(municipio);
	 }

	/**
	 * Finds a single row in the Municipio table.
	 *
	 * @param municipio Municipio
	 * @return Municipio
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public Municipio find(Municipio municipio) {
		return this.municipioDao.find(municipio);
	}
	
	/**
	 * Deletes a single row in the Municipio table.
	 *
	 * @param municipio Municipio
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void remove(Municipio municipio) {
		this.municipioDao.remove(municipio);
	}

	/**
	 * Finds a list of rows in the Municipio table.
	 *
	 * @param municipio Municipio
	 * @param jqGridRequestDto JQGridRequestDto
	 * @return List<Municipio>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public List<Municipio> findAll(Municipio municipio, JQGridRequestDto jqGridRequestDto){
		return this.municipioDao.findAll(municipio, jqGridRequestDto);
	}
    
	/**
	 * Finds rows in the Municipio table using like.
	 *
	 * @param municipio Municipio
	 * @param jqGridRequestDto JQGridRequestDto
     * @param startsWith Boolean	 
	 * @return List<Municipio>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED) 
	public List<Municipio> findAllLike(Municipio municipio, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		return this.municipioDao.findAllLike(municipio, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */
	 
	/**
	 * Removes rows from the Municipio table.
	 *
	 * @param filterMunicipio Municipio
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 */	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void removeMultiple(Municipio filterMunicipio, JQGridRequestDto jqGridRequestDto,  Boolean startsWith){
		this.municipioDao.removeMultiple(filterMunicipio, jqGridRequestDto, startsWith);
	}
        
	/**
	 * Filter method in the Municipio table.
	 *
	 * @param filterMunicipio Municipio
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return JQGridResponseDto<Municipio>
	 */
	@Override	
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public JQGridResponseDto< Municipio> filter(Municipio filterMunicipio, JQGridRequestDto jqGridRequestDto,  Boolean startsWith){
		List<Municipio> listaMunicipio =  this.municipioDao.findAllLike(filterMunicipio, jqGridRequestDto, false);
		Long recordNum =  this.municipioDao.findAllLikeCount(filterMunicipio != null ? filterMunicipio: new Municipio (),false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds()!=null){
			List< TableRowDto< Municipio>> reorderSelection = this.municipioDao.reorderSelection(filterMunicipio, jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Municipio>(jqGridRequestDto, recordNum, listaMunicipio, reorderSelection);
		}
		return new JQGridResponseDto<Municipio>(jqGridRequestDto, recordNum, listaMunicipio);   
	}
    
    /**
	 * Reorder the selection made in Municipio table.
	 *
	 * @param filterMunicipio Municipio
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return Object
	 */	
	@Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public Object reorderSelection(Municipio filterMunicipio, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		return this.municipioDao.reorderSelection(filterMunicipio, jqGridRequestDto, startsWith);
	}
    	
}
