Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
30209 amit.gupta 1
package com.smartdukaan.cron.scheduled;
2
 
3
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
4
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
5
import org.apache.logging.log4j.LogManager;
6
import org.apache.logging.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Service;
9
 
10
import java.time.LocalDate;
11
import java.time.LocalDateTime;
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.concurrent.ExecutionException;
16
import java.util.concurrent.ExecutorService;
17
import java.util.concurrent.Executors;
18
import java.util.concurrent.Future;
19
 
20
@Service
21
public class OppoImeiActivationService {
22
 
23
	private static final Logger LOGGER = LogManager.getLogger(OppoImeiActivationService.class);
24
	@Autowired
25
	ActivatedImeiRepository activatedImeiRepository;
26
 
27
	public void updateActivationDate(List<String> imeis) throws Exception {
30352 amit.gupta 28
		//TODO:Change bucket size
29
		int bucketSize = 25;
30
		int chromeThreads = imeis.size() / bucketSize;
30209 amit.gupta 31
		ExecutorService excecutorService = Executors.newFixedThreadPool(chromeThreads);
32
		List<CheckOppoWarrantyTask> tasks = new ArrayList<>();
30315 amit.gupta 33
		for (int i = 0; i < imeis.size() / bucketSize; i++) {
30352 amit.gupta 34
			CheckOppoWarrantyTask task = new CheckOppoWarrantyTask(imeis);
30209 amit.gupta 35
			tasks.add(task);
36
		}
30355 amit.gupta 37
		LOGGER.info("Total tasks {}", tasks.size());
30209 amit.gupta 38
		List<Future<Map<String, LocalDate>>> futures = excecutorService.invokeAll(tasks);
39
		List<String> foundImeis = new ArrayList<>();
40
		futures.stream().forEach(x -> {
41
			try {
42
				x.get().entrySet().forEach(y -> {
43
					foundImeis.add(y.getKey());
44
					System.out.println("Serial Number " + y.getKey() + "Date " + y.getValue());
30352 amit.gupta 45
					ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(y.getKey());
30209 amit.gupta 46
					if (activatedImeiRepository.selectBySerialNumber(y.getKey()) == null) {
30352 amit.gupta 47
						activatedImei = new ActivatedImei();
30209 amit.gupta 48
						activatedImei.setSerialNumber(y.getKey());
49
						activatedImei.setCreateTimestamp(LocalDateTime.now());
50
						activatedImeiRepository.persist(activatedImei);
51
					}
30352 amit.gupta 52
					if (y.getValue() != null) {
53
						activatedImei.setActivationTimestamp(y.getValue().atStartOfDay());
54
					}
30209 amit.gupta 55
				});
56
			} catch (InterruptedException e) {
57
				e.printStackTrace();
58
			} catch (ExecutionException e) {
59
				e.printStackTrace();
60
			}
61
		});
62
		imeis.removeAll(foundImeis);
63
		LOGGER.info("Could not break captcha for imeis - " + foundImeis);
64
	}
65
}