CatalogueJobProgressService.java

package com.sintia.ffl.admin.optique.services.services;

import com.sintia.ffl.admin.optique.dal.entities.catalogue.CatalogueJobProgress;
import com.sintia.ffl.admin.optique.dal.repositories.catalogue.CatalogueJobProgressRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class CatalogueJobProgressService {

    @Autowired
    private CatalogueJobProgressRepository catalogueJobProgressRepository;

    public List<CatalogueJobProgress> findCatalogueJobProgressListByJobId(Long id) {
        return catalogueJobProgressRepository.findByIdJob(id);
    }

    public Optional<CatalogueJobProgress> findCatalogueJobProgressByJobIdAndCurrentTable(Long jobId, String currentTable) {
        return catalogueJobProgressRepository.findByIdJobAndCurrentTable(jobId, currentTable);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void createCatalogueJobProgress(Long jobId, String currentTable, int totalProgress) {
        Optional<CatalogueJobProgress> jobProgressInDb = findCatalogueJobProgressByJobIdAndCurrentTable(jobId, currentTable);
        if (jobProgressInDb.isPresent()) {
            return;
        }

        CatalogueJobProgress newJobProgress = new CatalogueJobProgress(jobId, currentTable, totalProgress);
        catalogueJobProgressRepository.save(newJobProgress);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateCatalogueJobCurrentProgress(Long jobId, String currentTable, int currentProgress) {
        Optional<CatalogueJobProgress> jobProgressInDb = findCatalogueJobProgressByJobIdAndCurrentTable(jobId, currentTable);
        if (jobProgressInDb.isEmpty()) {
            return ;
        }

        jobProgressInDb.get().setCurrentProgress(currentProgress);
        catalogueJobProgressRepository.save(jobProgressInDb.get());
    }
}