package com.ejie.ab61.service.procedimientos;
import com.ejie.ab61.dao.procedimientos.AreasDao;
import com.ejie.ab61.model.procedimientos.Areas;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
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;

/**
 * AreasServiceImpl  
 *  
 */

@Service(value = "areasService")
public class AreasServiceImpl implements AreasService {

	@Autowired
	private AreasDao areasDao;

	/**
	 * Inserts a single row in the Areas table.
	 *
	 * @param areas Areas
	 * @return Areas
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Areas add(Areas areas) {
		return this.areasDao.add(areas);
	}

	/**
	 * Updates a single row in the Areas table.
	 *
	 * @param areas Areas
	 * @return Areas
	 */
	@Transactional(rollbackFor = Throwable.class)
	public Areas update(Areas areas) {
		return this.areasDao.update(areas);
	 }

	/**
	 * Finds a single row in the Areas table.
	 *
	 * @param areas Areas
	 * @return Areas
	 */
	public Areas find(Areas areas) {
		return (Areas) this.areasDao.find(areas);
	}
	
	/**
	 * Deletes a single row in the Areas table.
	 *
	 * @param areas Areas
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(Areas areas) {
		this.areasDao.remove(areas);
	}

	/**
	 * Finds a list of rows in the Areas table.
	 *
	 * @param areas Areas
	 * @param jqGridRequestDto JQGridRequestDto
	 * @return List<Areas>
	 */
	public List<Areas> findAll(Areas areas, JQGridRequestDto jqGridRequestDto){
		return (List<Areas>) this.areasDao.findAll(areas, jqGridRequestDto);
	}
    
	/**
	 * Finds rows in the Areas table using like.
	 *
	 * @param areas Areas
	 * @param jqGridRequestDto JQGridRequestDto
     * @param startsWith Boolean	 
	 * @return List<Areas>
	 */
	public List<Areas> findAllLike(Areas areas, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		return (List<Areas>) this.areasDao.findAllLike(areas, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */
	 
	/**
	 * Removes rows from the Areas table.
	 *
	 * @param filterAreas Areas
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 */	
	public void removeMultiple(Areas filterAreas, JQGridRequestDto jqGridRequestDto,  Boolean startsWith){
		this.areasDao.removeMultiple(filterAreas, jqGridRequestDto, startsWith);
	}
        
	/**
	 * Filter method in the Areas table.
	 *
	 * @param filterAreas Areas
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return JQGridResponseDto<Areas>
	 */	
	public JQGridResponseDto< Areas> filter(Areas filterAreas, JQGridRequestDto jqGridRequestDto,  Boolean startsWith){
		List<Areas> listaAreas =  this.areasDao.findAllLike(filterAreas, jqGridRequestDto, false);
		Long recordNum =  this.areasDao.findAllLikeCount(filterAreas != null ? filterAreas: new Areas (),false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds()!=null){
			List< TableRowDto< Areas>> reorderSelection = this.areasDao.reorderSelection(filterAreas, jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Areas>(jqGridRequestDto, recordNum, listaAreas, reorderSelection);
		}
		return new JQGridResponseDto<Areas>(jqGridRequestDto, recordNum, listaAreas);   
	}
    
    /**
	 * Searches rows in the Areas table.
	 *
	 * @param filterAreas Areas
	 * @param searchAreas Areas
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return List<TableRowDto<Areas>>
	 */	
    public List< TableRowDto< Areas>> search(Areas filterAreas, Areas searchAreas, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		return this.areasDao.search(filterAreas, searchAreas, jqGridRequestDto, startsWith);
	}
    
    /**
	 * Reorder the selection made in Areas table.
	 *
	 * @param filterAreas Areas
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return Object
	 */	
    public Object reorderSelection(Areas filterAreas, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		return this.areasDao.reorderSelection(filterAreas, jqGridRequestDto, startsWith);
	}
    
    /*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */
	 
	/**
	 * Finder method in the hierarchical the Areas table.
	 *
	 * @param filterAreas Areas
	 * @param jqGridRequestDto JQGridRequestDto
	 * @param startsWith Boolean
	 * @return JQGridResponseDto<JerarquiaDto<Areas>>
	 */	
	public JQGridResponseDto<JerarquiaDto<Areas>> jerarquia(Areas filterAreas, JQGridRequestDto jqGridRequestDto, Boolean startsWith){
		List<JerarquiaDto<Areas>> listaAreas =  this.areasDao.findAllLikeJerarquia(filterAreas, jqGridRequestDto);
		Long recordNum = this.areasDao.findAllLikeCountJerarquia(filterAreas, jqGridRequestDto);
		return new JQGridResponseDto<JerarquiaDto<Areas>>(jqGridRequestDto, recordNum, listaAreas);
	}
	
	/**
	 * Finder method for siblings rows in the hierarchical the Areas table.
	 *
	 * @param filterAreas Areas
	 * @param jqGridRequestDto JQGridRequestDto
	 * @return JQGridResponseDto<JerarquiaDto<Areas>>
	 */	
	public JQGridResponseDto<JerarquiaDto<Areas>> jerarquiaChildren(Areas filterAreas, JQGridRequestDto jqGridRequestDto){
		JQGridResponseDto<JerarquiaDto<Areas>> jqGridResponseDto = new JQGridResponseDto<JerarquiaDto<Areas>>();
		jqGridResponseDto.addAdditionalParam(JQGridResponseDto.CHILDREN, this.areasDao.findAllChild(filterAreas, jqGridRequestDto));
		return jqGridResponseDto;
	}
	
	
}

