package com.ejie.ab63.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab63.dao.DispensaEstablecimientoDao;
import com.ejie.ab63.model.DispensaEstablecimiento;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * Ab6320s00ServiceImpl  
 * 
 *  
 */

@Service(value = "dispensaEstablecimientoService")
public class DispensaEstablecimientoServiceImpl implements DispensaEstablecimientoService {

	@Autowired
	private DispensaEstablecimientoDao dispensaEstablecimientoDao;

	@Autowired
	private ReloadableResourceBundleMessageSource appMessageSource;

	/**
	 * Inserts a single row in the Ab6320s00 table.
	 *
	 * @param dispensaEstablecimiento
	 *            Ab6320s00
	 * @return Ab6320s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public DispensaEstablecimiento add(DispensaEstablecimiento dispensaEstablecimiento) {
		return this.dispensaEstablecimientoDao.add(dispensaEstablecimiento);
	}

	/**
	 * Updates a single row in the Ab6320s00 table.
	 *
	 * @param ab6320s00
	 *            Ab6320s00
	 * @return Ab6320s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public DispensaEstablecimiento update(DispensaEstablecimiento ab6320s00) {
		return this.dispensaEstablecimientoDao.update(ab6320s00);
	}

	/**
	 * Finds a single row in the Ab6320s00 table.
	 *
	 * @param ab6320s00
	 *            Ab6320s00
	 * @return Ab6320s00
	 */
	public DispensaEstablecimiento find(DispensaEstablecimiento ab6320s00) {
		return (DispensaEstablecimiento) this.dispensaEstablecimientoDao.find(ab6320s00);
	}

	/**
	 * Deletes a single row in the Ab6320s00 table.
	 *
	 * @param ab6320s00
	 *            Ab6320s00
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(DispensaEstablecimiento ab6320s00) {
		this.dispensaEstablecimientoDao.remove(ab6320s00);
	}

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the Ab6320s00 table.
	 *
	 * @param filterAb6320s00
	 *            Ab6320s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(DispensaEstablecimiento filterAb6320s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		this.dispensaEstablecimientoDao.removeMultiple(filterAb6320s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the Ab6320s00 table.
	 *
	 * @param filterAb6320s00
	 *            Ab6320s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<Ab6320s00>
	 */
	public JQGridResponseDto<DispensaEstablecimiento> filter(DispensaEstablecimiento filterAb6320s00,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<DispensaEstablecimiento> listaAb6320s00 = this.dispensaEstablecimientoDao.findAllLike(filterAb6320s00,
				jqGridRequestDto, false);
		for (DispensaEstablecimiento dispensaEstablecimiento : listaAb6320s00) {
			if (null != dispensaEstablecimiento && dispensaEstablecimiento.getFolderNumber() == null) {
				dispensaEstablecimiento.setFolderNumber(appMessageSource.getMessage("formDatosEspecificos.sinDispensa",
						null, LocaleContextHolder.getLocale()));
			}

		}

		Long recordNum = this.dispensaEstablecimientoDao
				.findAllLikeCount(filterAb6320s00 != null ? filterAb6320s00 : new DispensaEstablecimiento(), false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<DispensaEstablecimiento>> reorderSelection = this.dispensaEstablecimientoDao
					.reorderSelection(filterAb6320s00, jqGridRequestDto, startsWith);
			return new JQGridResponseDto<DispensaEstablecimiento>(jqGridRequestDto, recordNum, listaAb6320s00,
					reorderSelection);
		}
		return new JQGridResponseDto<DispensaEstablecimiento>(jqGridRequestDto, recordNum, listaAb6320s00);
	}

	/**
	 * Searches rows in the Ab6320s00 table.
	 *
	 * @param filterAb6320s00
	 *            Ab6320s00
	 * @param searchAb6320s00
	 *            Ab6320s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<Ab6320s00>>
	 */
	public List<TableRowDto<DispensaEstablecimiento>> search(DispensaEstablecimiento filterAb6320s00,
			DispensaEstablecimiento searchAb6320s00, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.dispensaEstablecimientoDao.search(filterAb6320s00, searchAb6320s00, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in Ab6320s00 table.
	 *
	 * @param filterAb6320s00
	 *            Ab6320s00
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(DispensaEstablecimiento filterAb6320s00, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		return this.dispensaEstablecimientoDao.reorderSelection(filterAb6320s00, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

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

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

	@Override
	public DispensaEstablecimiento findByExpediente(DispensaEstablecimiento dispensa) {

		return this.dispensaEstablecimientoDao.findByExpediente(dispensa);
	}

	@Override
	public boolean existsDispensaForExp(String folderId) {
		DispensaEstablecimiento dispensaEstablecimiento = new DispensaEstablecimiento();
		dispensaEstablecimiento.setFolderId(folderId);
		dispensaEstablecimiento = findByExpediente(dispensaEstablecimiento);
		return dispensaEstablecimiento != null && dispensaEstablecimiento.getIdDispensasEstab() != null;
	}

}
