package com.ejie.ab39a.service;

import java.util.List;
import java.util.Map;

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.constantes.Constantes;
import com.ejie.ab39a.dao.AccionesPgDao;
import com.ejie.ab39a.dao.PlanesGestionDao;
import com.ejie.ab39a.model.AccionesPg;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * AccionesPg.
 * 
 *  
 */
@Service(value = "accionesPgService")
public class AccionesPgServiceImpl implements AccionesPgService {

	@Autowired
	private AccionesPgDao accionesPgDao;

	@Autowired
	private PlanesGestionDao planesGestionDao;

	/**
	 * Inserts a single row in the AccionesPg table.
	 *
	 * @param accionesPg
	 *            AccionesPg
	 * @return AccionesPg
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public AccionesPg add(AccionesPg accionesPg) {
		this.planesGestionDao.updateActualizar(accionesPg);
		return this.accionesPgDao.add(accionesPg);

	}

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

	/**
	 * Updates a single row in the AccionesPg table.
	 *
	 * @param accionesPg
	 *            AccionesPg
	 * @return AccionesPg
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public AccionesPg guardarAcciones(AccionesPg accionesPg) {
		this.planesGestionDao.updateActualizar(accionesPg);
		return this.accionesPgDao.guardarAcciones(accionesPg);
	}

	/**
	 * Updates a single row in the AccionesPg table.
	 *
	 * @param accionesPg
	 *            AccionesPg
	 * @param modo
	 *            String modo
	 * @return List<AccionesPg>
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public AccionesPg anyadirAccionPgNueva(AccionesPg accionesPg, String modo) {

		// ElementosPg elem = new ElementosPg();
		// elem.setCodelempg(accionesPg.getCodelempg());
		// ElementosPg elemAux = elementosPgDao.find(elem);
		//
		// if (elemAux != null) {
		// PlanesGestion pg = new PlanesGestion();
		// pg.setCodplang(elemAux.getPlanesGestion().getCodplang());
		// planesGestionDao.updateActualizar(pg);
		// }
		this.planesGestionDao.updateActualizar(accionesPg);
		return this.accionesPgDao.anyadirAccionPgNueva(accionesPg, modo);
	}

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

	/**
	 * Deletes a single row in the AccionesPg table.
	 *
	 * @param accionesPg
	 *            AccionesPg
	 */
	@Override
	@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class, timeout = Constantes.MAX_TIMEOUT)
	public void remove(AccionesPg accionesPg) {

		this.planesGestionDao.updateActualizar(accionesPg);
		this.accionesPgDao.remove(accionesPg);
	}

	/**
	 * Deletes a single row in the AccionesPg table.
	 *
	 * @param accionesPg
	 *            AccionesPg
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void removeAccionesHijos(Integer idElemento) {
		this.accionesPgDao.removeAccionesHijos(idElemento);
	}

	/**
	 * Deletes a single row in the AccionesPg table.
	 *
	 * @param accionesPg
	 *            AccionesPg
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
	public void removeAccionesPgPe(Integer idElemento) {
		this.accionesPgDao.removeAccionesPgPe(idElemento);
	}

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

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

	/**
	 * Finds rows in the AccionesPg table using like.
	 *
	 * @param accionesPg
	 *            AccionesPg
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<AccionesPg>
	 */
	@Override
	public Map<String, List<AccionesPg>> buscarAccionesPgArbol(Integer idP) {
		return this.accionesPgDao.buscarAccionesPgArbol(idP);
	}

}
