package com.ejie.ab63.service.plantillas;

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.ab63.dao.plantillas.CamposPlantillaDao;
import com.ejie.ab63.model.plantillas.CamposPlantilla;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * CamposPlantillaServiceImpl  
 * 
 *  
 */

@Service(value = "camposPlantillaService")
public class CamposPlantillaServiceImpl implements CamposPlantillaService {

	@Autowired
	private CamposPlantillaDao camposPlantillaDao;

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

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

	/**
	 * Finds a single row in the CamposPlantilla table.
	 *
	 * @param camposPlantilla
	 *            CamposPlantilla
	 * @return CamposPlantilla
	 */
	public CamposPlantilla find(CamposPlantilla camposPlantilla) {
		return (CamposPlantilla) this.camposPlantillaDao.find(camposPlantilla);
	}

	/**
	 * Deletes a single row in the CamposPlantilla table.
	 *
	 * @param camposPlantilla
	 *            CamposPlantilla
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(CamposPlantilla camposPlantilla) {
		this.camposPlantillaDao.remove(camposPlantilla);
	}

	/**
	 * Finds a list of rows in the CamposPlantilla table.
	 *
	 * @param camposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<CamposPlantilla>
	 */
	public List<CamposPlantilla> findAll(CamposPlantilla camposPlantilla, JQGridRequestDto jqGridRequestDto) {
		return (List<CamposPlantilla>) this.camposPlantillaDao.findAll(camposPlantilla, jqGridRequestDto);
	}

	/**
	 * Finds rows in the CamposPlantilla table using like.
	 *
	 * @param camposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<CamposPlantilla>
	 */
	public List<CamposPlantilla> findAllLike(CamposPlantilla camposPlantilla, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return (List<CamposPlantilla>) this.camposPlantillaDao.findAllLike(camposPlantilla, jqGridRequestDto,
				startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the CamposPlantilla table.
	 *
	 * @param filterCamposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(CamposPlantilla filterCamposPlantilla, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		this.camposPlantillaDao.removeMultiple(filterCamposPlantilla, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the CamposPlantilla table.
	 *
	 * @param filterCamposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<CamposPlantilla>
	 */
	public JQGridResponseDto<CamposPlantilla> filter(CamposPlantilla filterCamposPlantilla,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<CamposPlantilla> listaCamposPlantilla = this.camposPlantillaDao.findAllLike(filterCamposPlantilla,
				jqGridRequestDto, false);
		Long recordNum = this.camposPlantillaDao
				.findAllLikeCount(filterCamposPlantilla != null ? filterCamposPlantilla : new CamposPlantilla(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<CamposPlantilla>> reorderSelection = this.camposPlantillaDao
					.reorderSelection(filterCamposPlantilla, jqGridRequestDto, startsWith);
			return new JQGridResponseDto<CamposPlantilla>(jqGridRequestDto, recordNum, listaCamposPlantilla,
					reorderSelection);
		}
		return new JQGridResponseDto<CamposPlantilla>(jqGridRequestDto, recordNum, listaCamposPlantilla);
	}

	/**
	 * Searches rows in the CamposPlantilla table.
	 *
	 * @param filterCamposPlantilla
	 *            CamposPlantilla
	 * @param searchCamposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<CamposPlantilla>>
	 */
	public List<TableRowDto<CamposPlantilla>> search(CamposPlantilla filterCamposPlantilla,
			CamposPlantilla searchCamposPlantilla, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.camposPlantillaDao.search(filterCamposPlantilla, searchCamposPlantilla, jqGridRequestDto,
				startsWith);
	}

	/**
	 * Reorder the selection made in CamposPlantilla table.
	 *
	 * @param filterCamposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(CamposPlantilla filterCamposPlantilla, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return this.camposPlantillaDao.reorderSelection(filterCamposPlantilla, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

	/**
	 * Finder method in the hierarchical the CamposPlantilla table.
	 *
	 * @param filterCamposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<JerarquiaDto<CamposPlantilla>>
	 */
	public JQGridResponseDto<JerarquiaDto<CamposPlantilla>> jerarquia(CamposPlantilla filterCamposPlantilla,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<JerarquiaDto<CamposPlantilla>> listaCamposPlantilla = this.camposPlantillaDao
				.findAllLikeJerarquia(filterCamposPlantilla, jqGridRequestDto);
		Long recordNum = this.camposPlantillaDao.findAllLikeCountJerarquia(filterCamposPlantilla, jqGridRequestDto);
		return new JQGridResponseDto<JerarquiaDto<CamposPlantilla>>(jqGridRequestDto, recordNum, listaCamposPlantilla);
	}

	/**
	 * Finder method for siblings rows in the hierarchical the CamposPlantilla
	 * table.
	 *
	 * @param filterCamposPlantilla
	 *            CamposPlantilla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return JQGridResponseDto<JerarquiaDto<CamposPlantilla>>
	 */
	public JQGridResponseDto<JerarquiaDto<CamposPlantilla>> jerarquiaChildren(CamposPlantilla filterCamposPlantilla,
			JQGridRequestDto jqGridRequestDto) {
		JQGridResponseDto<JerarquiaDto<CamposPlantilla>> jqGridResponseDto = new JQGridResponseDto<JerarquiaDto<CamposPlantilla>>();
		jqGridResponseDto.addAdditionalParam(JQGridResponseDto.CHILDREN,
				this.camposPlantillaDao.findAllChild(filterCamposPlantilla, jqGridRequestDto));
		return jqGridResponseDto;
	}

}
