package com.ejie.ab39a.service;

import com.ejie.ab39a.dao.PlanificacionDao;
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.Planificacion;

/**
 * Planificacion.
 * 
 *  
 */
@Service(value = "planificacionService")
public class PlanificacionServiceImpl implements PlanificacionService {

	@Autowired
	private PlanificacionDao planificacionDao;

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

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

	/**
	 * Finds a single row in the Planificacion table.
	 *
	 * @param planificacion
	 *            Planificacion
	 * @return Planificacion
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public Planificacion find(Planificacion planificacion) {
		return this.planificacionDao.find(planificacion);
	}

	/**
	 * Deletes a single row in the Planificacion table.
	 *
	 * @param planificacion
	 *            Planificacion
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void remove(Planificacion planificacion) {
		this.planificacionDao.remove(planificacion);
	}

	/**
	 * Finds a list of rows in the Planificacion table.
	 *
	 * @param planificacion
	 *            Planificacion
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<Planificacion>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public List<Planificacion> findAll(Planificacion planificacion, JQGridRequestDto jqGridRequestDto) {
		return this.planificacionDao.findAll(planificacion, jqGridRequestDto);
	}

	/**
	 * Finds rows in the Planificacion table using like.
	 *
	 * @param planificacion
	 *            Planificacion
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<Planificacion>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public List<Planificacion> findAllLike(Planificacion planificacion, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return this.planificacionDao.findAllLike(planificacion, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Planificacion table.
	 *
	 * @param filterPlanificacion
	 *            Planificacion
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void removeMultiple(Planificacion filterPlanificacion, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		this.planificacionDao.removeMultiple(filterPlanificacion, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Planificacion table.
	 *
	 * @param filterPlanificacion
	 *            Planificacion
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Planificacion>
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public JQGridResponseDto<Planificacion> filter(Planificacion filterPlanificacion, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		List<Planificacion> listaPlanificacion = this.planificacionDao.findAllLike(filterPlanificacion,
				jqGridRequestDto, false);
		Long recordNum = this.planificacionDao
				.findAllLikeCount(filterPlanificacion != null ? filterPlanificacion : new Planificacion(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<Planificacion>> reorderSelection = this.planificacionDao
					.reorderSelection(filterPlanificacion, jqGridRequestDto, startsWith);
			return new JQGridResponseDto<Planificacion>(jqGridRequestDto, recordNum, listaPlanificacion,
					reorderSelection);
		}
		return new JQGridResponseDto<Planificacion>(jqGridRequestDto, recordNum, listaPlanificacion);
	}

	/**
	 * Reorder the selection made in Planificacion table.
	 *
	 * @param filterPlanificacion
	 *            Planificacion
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
	public Object reorderSelection(Planificacion filterPlanificacion, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return this.planificacionDao.reorderSelection(filterPlanificacion, jqGridRequestDto, startsWith);
	}

	/**
	 * Método que devuelve el máximo id de la tabla Planificación
	 * 
	 * @return Planificacion
	 */
	@Override
	public Planificacion getMaxId() {
		return this.planificacionDao.getMaxId();
	}

	/**
	 * Método que añade y da de baja lógica el registro anterior
	 * 
	 * @param planificacion
	 * @return Planificacion
	 */
	@Override
	public Planificacion addAndDelete(Planificacion planificacion) {
		Planificacion planiBaja = this.planificacionDao.getMaxId();
		Planificacion planifAlta = this.planificacionDao.add(planificacion);
		this.planificacionDao.remove(planiBaja);
		return planifAlta;
	}

	/**
	 * Devuelve una objeto planificacion con lso años de la planificacion activa
	 *
	 * @return Planificacion Objeto con lso años de inicio y fin de la
	 *         planificacion activa
	 * 
	 */
	@Override
	public Planificacion getAnioPlanificacion() {
		return planificacionDao.getAnioPlanificacion();
	}

}
