package com.ejie.ab39a.service;
import com.ejie.ab39a.dao.ElementoDao;
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.Elemento;

/**
 * Elemento.
 *  
 */
@Service(value = "elementoService")
public class ElementoServiceImpl implements ElementoService {

	@Autowired
	private ElementoDao elementoDao;

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

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

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

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

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