package com.oguerreiro.resilient.service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.oguerreiro.resilient.domain.ResilientLog;
import com.oguerreiro.resilient.repository.ResilientLogRepository;
import com.oguerreiro.resilient.service.dto.ResilientLogDTO;
import com.oguerreiro.resilient.service.mapper.ResilientLogMapper;

/**
 * Service Implementation for managing {@link com.oguerreiro.resilient.domain.ResilientLog}.
 */
@Service
@Transactional
public class ResilientLogService extends AbstractResilientService<ResilientLogDTO, ResilientLog, Long> {

  private final ResilientLogRepository resilientLogRepository;

  private final ResilientLogMapper resilientLogMapper;

  public ResilientLogService(ResilientLogRepository inputDataUploadLogRepository,
      ResilientLogMapper inputDataUploadLogMapper) {
    super(ResilientLog.class, inputDataUploadLogRepository, inputDataUploadLogMapper);
    this.resilientLogRepository = inputDataUploadLogRepository;
    this.resilientLogMapper = inputDataUploadLogMapper;
  }

  /**
   * Get all the resilientLogs by owner.
   *
   * @return the list of entities.
   */
  @Transactional(readOnly = true)
  public List<ResilientLogDTO> findAllByOwnerId(Long ownerId) {
    log.debug("Request to get all by findAllByOwnerId : {}", ownerId);
    return resilientLogRepository.findAllByOwnerId(ownerId).stream().map(resilientLogMapper::toDto).collect(
        Collectors.toCollection(LinkedList::new));
  }

  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void saveWithNewTransaction(ResilientLog log) {
    this.resilientLogRepository.save(log);
  }

  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void deleteAllByOwnerIdWithNewTransaction(Long ownerId) {
    this.deleteAllByOwnerId(ownerId);
  }

  public void deleteAllByOwnerId(Long ownerId) {
    resilientLogRepository.deleteAllByOwnerId(ownerId);
  }
}
