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.adaptadores.ab61WebServiceAdaptadorService;
import com.ejie.ab63.clientesWS.ab61WebService.Ab61Exception;
import com.ejie.ab63.dao.plantillas.PlantillasDao;
import com.ejie.ab63.model.plantillas.Plantillas;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * PlantillasServiceImpl  
 * 
 *  
 */

@Service(value = "plantillasService")
public class PlantillasServiceImpl implements PlantillasService {

	@Autowired
	private PlantillasDao plantillasDao;

	@Autowired
	private ab61WebServiceAdaptadorService ab61WebServiceAdaptadorService;

	/**
	 * Finds a single row in the Plantillas table.
	 *
	 * @param plantillas
	 *            Plantillas
	 * @return Plantillas
	 */
	public Plantillas find(Plantillas plantillas) {
		return (Plantillas) this.plantillasDao.find(plantillas);
	}

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

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

	/**
	 * Searches rows in the Plantillas table.
	 *
	 * @param filterPlantillas
	 *            Plantillas
	 * @param searchPlantillas
	 *            Plantillas
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Plantillas>>
	 */
	public List<TableRowDto<Plantillas>> search(Plantillas filterPlantillas, Plantillas searchPlantillas,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.plantillasDao.search(filterPlantillas, searchPlantillas, jqGridRequestDto, startsWith);
	}

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

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

	/**
	 * llama al webService que realiza la tarea indicada
	 * 
	 * @param idPlantilla
	 *            Integer
	 * @param procedureId
	 *            String
	 * @param folderId
	 *            String
	 * @param n38Login
	 *            String
	 * @return String
	 * @throws Ab61Exception
	 */
	public String aportarDocumento(Integer idPlantilla, String procedureId, String folderId, String n38Login,
			String usuario) throws Ab61Exception {

		return ab61WebServiceAdaptadorService.aportarDocumento(idPlantilla, procedureId, folderId, n38Login, usuario);
	}

}
