package uk.gov.indexer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.indexer.dao.DestinationDBUpdateDAO;
import uk.gov.indexer.dao.Entry;
import uk.gov.indexer.dao.SourceDBQueryDAO;
import uk.gov.indexer.monitoring.CloudwatchRecordsProcessedUpdater;

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

public class IndexerTask implements Runnable {
    private final Logger LOGGER = LoggerFactory.getLogger(IndexerTask.class);

    private final String register;
    private final DestinationDBUpdateDAO destinationDBUpdateDAO;
    private final SourceDBQueryDAO sourceDBQueryDAO;
    private final Optional<CloudwatchRecordsProcessedUpdater> cloudwatchUpdater;

    public IndexerTask(Optional<CloudwatchRecordsProcessedUpdater> cloudwatchUpdater, String register, SourceDBQueryDAO sourceDBQueryDAO, DestinationDBUpdateDAO destinationDBUpdateDAO) {
        this.register = register;
        this.sourceDBQueryDAO = sourceDBQueryDAO;
        this.destinationDBUpdateDAO = destinationDBUpdateDAO;
        this.cloudwatchUpdater = cloudwatchUpdater;
    }

    @Override
    public void run() {
        try {
            LOGGER.info("Starting update for: " + register);
            update();
            LOGGER.info("Finished for register: " + register);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }

    protected void update() {
        int from = destinationDBUpdateDAO.lastReadSerialNumber();
        List<Entry> newEntries;
        boolean noRecordsProcessed = true;
        while ((newEntries = sourceDBQueryDAO.read(from)).size() > 0) {
            noRecordsProcessed = false;
            destinationDBUpdateDAO.writeEntriesInBatch(register, newEntries);
            from = destinationDBUpdateDAO.lastReadSerialNumber();

            final int totalEntriesWritten = newEntries.size();
            LOGGER.info(String.format("Register '%s': Written %s entries after index '%s'.", register, totalEntriesWritten, from));

            cloudwatchUpdater.ifPresent(cwu -> cwu.update(totalEntriesWritten));
        }

        if (noRecordsProcessed && cloudwatchUpdater.isPresent()) {
            cloudwatchUpdater.get().update(0);
        }
    }
}

@@ -1418,74 +1418,58 @@
     
-int from = destinationDBUpdateDAO.lastReadSerialNumber
+List%3CEntry%3E entries = fetchNewEntries
 ();%0A
+%0A
         
 List
@@ -1468,187 +1468,104 @@
     
-List%3CEntry%3E newEntries;%0A        boolean noRecordsProcessed = true;%0A        while ((newEntries = sourceDBQueryDAO.read(from)).size() %3E 0) %7B%0A            noRecordsProcessed = false;%0A
+if (entries.isEmpty()) %7B%0A            updateCloudWatch(0);%0A        %7D else %7B%0A            do %7B%0A    
     
@@ -1625,20 +1625,17 @@
 gister, 
-newE
+e
 ntries);
@@ -1651,63 +1651,150 @@
     
-from = destinationDBUpdateDAO.lastReadS
+    LOGGER.info(String.format(%22Register '%25s': Written %25s entries from index '%25s'.%22, register, entries.size(), entries.get(0).s
 erial
-N
+_n
 umber
-(
+)
 );%0A
-%0A
     
@@ -1805,44 +1805,30 @@
     
-final int totalEntriesWritten = newE
+    updateCloudWatch(e
 ntri
@@ -1836,16 +1836,17 @@
 s.size()
+)
 ;%0A      
@@ -1855,98 +1855,121 @@
     
-LOGGER.info(String.format(%22Register '%25s': Written %25s entries after index '%25s'.%22, register,
+%7D while (!(entries = fetchNewEntries()).isEmpty());%0A        %7D%0A%0A    %7D%0A%0A    private void updateCloudWatch(final int
  tot
@@ -1988,23 +1988,12 @@
 tten
-, from));%0A%0A    
+) %7B%0A
     
@@ -2061,28 +2061,24 @@
 sWritten));%0A
-    
     %7D%0A%0A     
@@ -2080,132 +2080,138 @@
     
- 
-   if (noRecordsProcessed && cloudwatchUpdater.isPresent()) %7B%0A            cloudwatchUpdater.get().update(0);%0A    
+private List%3CEntry%3E fetchNewEntries() %7B%0A        return sourceDBQueryDAO.read(destinationDBUpdateDAO.lastReadSerialNumber());%0A
     %7D%0A
-    %7D
 %0A%7D%0A

Refactor update method to improve readability