package com.ejie.ab61.service.plantillas;

import com.ejie.ab61.dao.plantillas.RelPlaCamDao;
import com.ejie.ab61.model.plantillas.RelPlaCam;
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;

/**
 * RelPlaCamServiceImpl  
 * 
 *  
 */

@Service(value = "relPlaCamService")
public class RelPlaCamServiceImpl implements RelPlaCamService {

	@Autowired
	private RelPlaCamDao relPlaCamDao;

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

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

	/**
	 * Finds a single row in the RelPlaCam table.
	 *
	 * @param relPlaCam
	 *            RelPlaCam
	 * @return RelPlaCam
	 */
	public RelPlaCam find(RelPlaCam relPlaCam) {
		return (RelPlaCam) this.relPlaCamDao.find(relPlaCam);
	}

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

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

	/**
	 * Searches rows in the RelPlaCam table.
	 *
	 * @param filterRelPlaCam
	 *            RelPlaCam
	 * @param searchRelPlaCam
	 *            RelPlaCam
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelPlaCam>>
	 */
	public List<TableRowDto<RelPlaCam>> search(RelPlaCam filterRelPlaCam, RelPlaCam searchRelPlaCam,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.relPlaCamDao.search(filterRelPlaCam, searchRelPlaCam, jqGridRequestDto, startsWith);
	}

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

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

	/**
	 * Obtiene el numero de documentos existentes para el campo.
	 *
	 * @param idCampo
	 *            Integer
	 * @return int
	 */
	public Long obtenerNumDocumentosCampo(Integer idCampo) {
		return this.relPlaCamDao.obtenerNumDocumentosCampo(idCampo);
	}
}
