Subversion Repositories SmartDukaan

Rev

Rev 30209 | Rev 30352 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.scheduled;

import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Service
@Transactional(rollbackFor = Throwable.class)
public class OppoImeiActivationService {

        private static final Logger LOGGER = LogManager.getLogger(OppoImeiActivationService.class);
        @Autowired
        ActivatedImeiRepository activatedImeiRepository;

        public void updateActivationDate(List<String> imeis) throws Exception {
                int chromeThreads = 7;
                int bucketSize = 4;
                ExecutorService excecutorService = Executors.newFixedThreadPool(chromeThreads);
                List<CheckOppoWarrantyTask> tasks = new ArrayList<>();
                for (int i = 0; i < imeis.size() / bucketSize; i++) {
                        CheckOppoWarrantyTask task = new CheckOppoWarrantyTask(imeis.subList(i * bucketSize, (i + 1) * bucketSize));
                        tasks.add(task);
                }
                List<Future<Map<String, LocalDate>>> futures = excecutorService.invokeAll(tasks);
                List<String> foundImeis = new ArrayList<>();
                futures.stream().forEach(x -> {
                        try {
                                x.get().entrySet().forEach(y -> {
                                        foundImeis.add(y.getKey());
                                        System.out.println("Serial Number " + y.getKey() + "Date " + y.getValue());
                                        if (y.getValue() == null) return;
                                        if (activatedImeiRepository.selectBySerialNumber(y.getKey()) == null) {
                                                ActivatedImei activatedImei = new ActivatedImei();
                                                activatedImei.setSerialNumber(y.getKey());
                                                activatedImei.setActivationTimestamp(y.getValue().atStartOfDay());
                                                activatedImei.setCreateTimestamp(LocalDateTime.now());
                                                activatedImeiRepository.persist(activatedImei);
                                        }
                                });
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        } catch (ExecutionException e) {
                                e.printStackTrace();
                        }
                });
                imeis.removeAll(foundImeis);
                LOGGER.info("Could not break captcha for imeis - " + foundImeis);
        }
}