package com.ejie.ab39a.service;

import com.ejie.ab39a.dao.ProvinciaDao;
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.Provincia;

/**
 * Provincia.
 * 
 *  
 */
@Service(value = "provinciaService")
public class ProvinciaServiceImpl implements ProvinciaService {

	@Autowired
	private ProvinciaDao provinciaDao;

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

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

	/**
	 * Finds a single row in the Provincia table.
	 *
	 * @param provincia
	 *            Provincia
	 * @return Provincia
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public Provincia find(Provincia provincia) {
		return this.provinciaDao.find(provincia);
	}

	/**
	 * Deletes a single row in the Provincia table.
	 *
	 * @param provincia
	 *            Provincia
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void remove(Provincia provincia) {
		this.provinciaDao.remove(provincia);
	}

	/**
	 * Finds a list of rows in the Provincia table.
	 *
	 * @param provincia
	 *            Provincia
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<Provincia>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public List<Provincia> findAll(Provincia provincia, JQGridRequestDto jqGridRequestDto) {
		return this.provinciaDao.findAll(provincia, jqGridRequestDto);
	}

	/**
	 * Finds a list of rows in the Provincia table.
	 *
	 * @param provincia
	 *            Provincia
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<Provincia>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public List<Provincia> findAllNavIp(Provincia provincia, JQGridRequestDto jqGridRequestDto) {
		return this.provinciaDao.findAllNavIp(provincia, jqGridRequestDto);
	}

	/**
	 * Finds rows in the Provincia table using like.
	 *
	 * @param provincia
	 *            Provincia
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<Provincia>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public List<Provincia> findAllLike(Provincia provincia, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.provinciaDao.findAllLike(provincia, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Provincia table.
	 *
	 * @param filterProvincia
	 *            Provincia
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void removeMultiple(Provincia filterProvincia, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		this.provinciaDao.removeMultiple(filterProvincia, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Provincia table.
	 *
	 * @param filterProvincia
	 *            Provincia
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Provincia>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public JQGridResponseDto<Provincia> filter(Provincia filterProvincia, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		List<Provincia> listaProvincia = this.provinciaDao.findAllLike(filterProvincia, jqGridRequestDto, false);
		Long recordNum = this.provinciaDao.findAllLikeCount(filterProvincia != null ? filterProvincia : new Provincia(),
				false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Provincia>> reorderSelection = this.provinciaDao.reorderSelection(filterProvincia,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Provincia>(jqGridRequestDto, recordNum, listaProvincia, reorderSelection);
		}
		return new JQGridResponseDto<Provincia>(jqGridRequestDto, recordNum, listaProvincia);
	}

	/**
	 * Reorder the selection made in Provincia table.
	 *
	 * @param filterProvincia
	 *            Provincia
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public Object reorderSelection(Provincia filterProvincia, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.provinciaDao.reorderSelection(filterProvincia, jqGridRequestDto, startsWith);
	}

}
