package com.sldlt.scheduled;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.sldlt.downloader.service.NAVPSDownloader;
import com.sldlt.navps.dto.NAVPSEntryDto;
import com.sldlt.navps.service.FundService;
import com.sldlt.navps.service.NAVPSService;

@Component
public class DownloaderScheduledTask {

    private static Logger LOG = Logger.getLogger(DownloaderScheduledTask.class);

    @Autowired
    private NAVPSDownloader navpsDownloader;

    @Autowired
    private FundService fundService;

    @Autowired
    private NAVPSService navpsService;

    @Scheduled(cron = "0 0 0/2 * * ?")
    public void run() {
        LOG.debug("Running downloader for " + LocalDateTime.now().toString());
        List<NAVPSEntryDto> allNavpsList = navpsDownloader.findAvailableFunds().stream()
                .map(fund -> fundService.saveFund(fund).getCode()).map(fundCode -> {
                    List<NAVPSEntryDto> result = navpsDownloader.fetchNAVPSFromPage(fundCode, LocalDate.of(2017, 2, 1),
                            LocalDate.of(2017, 2, 9));
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                    }
                    return result;
                }).flatMap(List::stream).collect(Collectors.toList());
        navpsService.saveNAVPS(allNavpsList);
    }

}

@@ -59,36 +59,8 @@
 me;%0A
-import java.time.LocalDate;%0A
 impo
@@ -1257,88 +1257,8 @@
 Code
-, LocalDate.of(2017, 2, 1),%0A                            LocalDate.of(2017, 2, 9)
 );%0A 

remove test date range