package com.ejie.ab39a.service;

import com.ejie.ab39a.dao.AccionesPgDao;
import com.ejie.ab39a.dao.ElementosPgDao;
import com.ejie.ab39a.dao.PlanesGestionDao;
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.AccionesPg;
import com.ejie.ab39a.model.ElementosPg;

/**
 * ElementosPg.
 * 
 *  
 */
@Service(value = "elementosPgService")
public class ElementosPgServiceImpl implements ElementosPgService {

	@Autowired
	private ElementosPgDao elementosPgDao;
	@Autowired
	private AccionesPgDao accionesPgDao;
	@Autowired
	private PlanesGestionDao planesGestionDao;

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

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

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

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

		AccionesPg accionesPg = new AccionesPg();
		accionesPg.setCodelempg(elementosPg.getCodelempg());
		this.planesGestionDao.updateActualizar(accionesPg);
		this.accionesPgDao.removeIdElem(elementosPg.getCodelempg());
		this.elementosPgDao.remove(elementosPg);
	}

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

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

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

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

}
