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