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