Subversion Repositories SmartDukaan

Rev

Rev 23511 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23506 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
4
import java.util.List;
5
 
6
import javax.servlet.http.HttpServletRequest;
7
 
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Value;
12
import org.springframework.scheduling.annotation.Scheduled;
13
import org.springframework.stereotype.Controller;
14
import org.springframework.transaction.annotation.Transactional;
15
import org.springframework.ui.Model;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestMethod;
18
 
19
import com.spice.profitmandi.common.enumuration.SchemeType;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.web.client.RestClient;
22
import com.spice.profitmandi.dao.entity.fofo.Purchase;
23
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
24
import com.spice.profitmandi.service.scheme.SchemeService;
25
import com.spice.profitmandi.web.util.MVCResponseSender;
26
 
27
@Controller
28
@Transactional
29
public class CronController {
30
 
31
	@Autowired
32
	PurchaseRepository purchaseRepository;
33
 
34
	@Autowired
35
	SchemeService schemeService;
36
 
37
	@Autowired
38
	private MVCResponseSender mvcResponseSender;
39
 
40
	@Value("${prod}")
41
	private boolean prod;
42
 
43
 
44
	private static final Logger LOGGER = LoggerFactory.getLogger(CronController.class);
45
 
46
	@Scheduled(cron = "0 45 6 * * *")
47
	public void executeJob() throws Exception {
48
		if(prod) {
49
			RestClient rc = new RestClient(SchemeType.HTTP, "localhost", 8080);
50
			String uri = "/cron/process-schemes";
51
			rc.get(uri, null);
52
		}
53
	}
54
 
55
	@RequestMapping(value = "/cron/process-schemes", method = RequestMethod.GET)
56
	public String createScheme(HttpServletRequest request, Model model) throws Exception{
57
		LocalDateTime fromDate = LocalDateTime.now().minusDays(15);
58
		LOGGER.info("Started execution at {}", LocalDateTime.now());
59
		List<Purchase> purchases = purchaseRepository.selectFromPurchaseCompleteDate(fromDate);
60
		for (Purchase purchase : purchases) {
61
			try {
62
				schemeService.processSchemeIn(purchase.getId(), purchase.getFofoId());
63
			} catch (Exception e) {
64
				LOGGER.error("Error while processing purchase {} for scheme In ", purchase.getId());
65
				e.printStackTrace();
66
 
67
			}
68
		}
69
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
70
		return "response";
71
	}
72
 
73
}