| 23755 |
amit.gupta |
1 |
package com.smartdukaan.cron.migrations;
|
|
|
2 |
|
| 23824 |
amit.gupta |
3 |
import java.time.LocalDate;
|
| 23755 |
amit.gupta |
4 |
import java.time.LocalDateTime;
|
| 23824 |
amit.gupta |
5 |
import java.time.LocalTime;
|
| 23755 |
amit.gupta |
6 |
import java.util.Arrays;
|
| 23827 |
amit.gupta |
7 |
import java.util.Collections;
|
| 23755 |
amit.gupta |
8 |
import java.util.List;
|
|
|
9 |
|
|
|
10 |
import org.apache.commons.lang.StringUtils;
|
|
|
11 |
import org.apache.logging.log4j.LogManager;
|
|
|
12 |
import org.apache.logging.log4j.Logger;
|
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14 |
import org.springframework.stereotype.Component;
|
|
|
15 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
16 |
|
| 23824 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.entity.transaction.LineItem;
|
| 23755 |
amit.gupta |
18 |
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
|
|
|
19 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
20 |
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
|
| 23824 |
amit.gupta |
21 |
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
|
| 23755 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
23 |
|
|
|
24 |
@Component
|
|
|
25 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
26 |
public class RunOnceTasks {
|
|
|
27 |
|
|
|
28 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
29 |
|
|
|
30 |
@Autowired
|
| 23824 |
amit.gupta |
31 |
private LineItemRepository lineItemRepository;
|
|
|
32 |
|
|
|
33 |
@Autowired
|
| 23755 |
amit.gupta |
34 |
private OrderRepository orderRepository;
|
| 23767 |
amit.gupta |
35 |
|
| 23755 |
amit.gupta |
36 |
@Autowired
|
|
|
37 |
private LineItemImeisRepository lineItemImeisRepository;
|
|
|
38 |
|
|
|
39 |
public void migarateLineItemsToNewTable() {
|
|
|
40 |
LOGGER.info("Before Migrated LineItems Successfully");
|
| 23824 |
amit.gupta |
41 |
int lineItemImeiId = 0;
|
|
|
42 |
LocalDateTime startDate = null;
|
|
|
43 |
try {
|
|
|
44 |
lineItemImeiId = lineItemImeisRepository.selectMaxId();
|
|
|
45 |
LineItem lineItem = lineItemRepository.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
|
|
|
46 |
Order order = orderRepository.selectById(lineItem.getOrderId());
|
|
|
47 |
startDate = order.getBillingTimestamp();
|
|
|
48 |
} catch (Exception e) {
|
|
|
49 |
LOGGER.info("Running before first time");
|
| 23826 |
amit.gupta |
50 |
startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
|
| 23824 |
amit.gupta |
51 |
}
|
|
|
52 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
|
| 23827 |
amit.gupta |
53 |
Collections.reverse(orders);
|
|
|
54 |
|
| 23755 |
amit.gupta |
55 |
for (Order order : orders) {
|
| 23824 |
amit.gupta |
56 |
try {
|
| 23767 |
amit.gupta |
57 |
String serialNumbers = order.getLineItem().getSerialNumber();
|
|
|
58 |
if (!StringUtils.isEmpty(serialNumbers)) {
|
|
|
59 |
List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
|
|
|
60 |
for (String serialNumber : serialNumberList) {
|
|
|
61 |
int lineItemId = order.getLineItem().getId();
|
|
|
62 |
LineItemImei lineItemImei = new LineItemImei();
|
|
|
63 |
lineItemImei.setSerialNumber(serialNumber);
|
|
|
64 |
lineItemImei.setLineItemId(lineItemId);
|
|
|
65 |
lineItemImeisRepository.persist(lineItemImei);
|
|
|
66 |
}
|
|
|
67 |
} else {
|
|
|
68 |
LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
|
| 23755 |
amit.gupta |
69 |
}
|
| 23824 |
amit.gupta |
70 |
} catch (Exception e) {
|
|
|
71 |
LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(), e.getMessage());
|
| 23755 |
amit.gupta |
72 |
}
|
|
|
73 |
}
|
|
|
74 |
LOGGER.info("Migrated LineItems Successfully");
|
|
|
75 |
}
|
|
|
76 |
}
|