Replace if statements with switch statements where appropriate
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2021 the original author or authors.
|
||||
* Copyright 2006-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,6 +33,7 @@ import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author peter.zozom
|
||||
* @author Mahmoud Ben Hassine
|
||||
*
|
||||
*/
|
||||
public class OrderItemReader implements ItemReader<Order> {
|
||||
@@ -89,65 +90,67 @@ public class OrderItemReader implements ItemReader<Order> {
|
||||
|
||||
String lineId = fieldSet.readString(0);
|
||||
|
||||
if (Order.LINE_ID_HEADER.equals(lineId)) {
|
||||
log.debug("STARTING NEW RECORD");
|
||||
order = headerMapper.mapFieldSet(fieldSet);
|
||||
}
|
||||
else if (Order.LINE_ID_FOOTER.equals(lineId)) {
|
||||
log.debug("END OF RECORD");
|
||||
switch (lineId) {
|
||||
case Order.LINE_ID_HEADER -> {
|
||||
log.debug("STARTING NEW RECORD");
|
||||
order = headerMapper.mapFieldSet(fieldSet);
|
||||
}
|
||||
case Order.LINE_ID_FOOTER -> {
|
||||
log.debug("END OF RECORD");
|
||||
|
||||
// Do mapping for footer here, because mapper does not allow to pass
|
||||
// an Order object as input.
|
||||
// Mapper always creates new object
|
||||
order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE"));
|
||||
order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS"));
|
||||
order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS"));
|
||||
// Do mapping for footer here, because mapper does not allow to pass
|
||||
// an Order object as input.
|
||||
// Mapper always creates new object
|
||||
order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE"));
|
||||
order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS"));
|
||||
order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS"));
|
||||
|
||||
// mark we are finished with current Order
|
||||
recordFinished = true;
|
||||
}
|
||||
else if (Customer.LINE_ID_BUSINESS_CUST.equals(lineId)) {
|
||||
log.debug("MAPPING CUSTOMER");
|
||||
if (order.getCustomer() == null) {
|
||||
Customer customer = customerMapper.mapFieldSet(fieldSet);
|
||||
customer.setBusinessCustomer(true);
|
||||
order.setCustomer(customer);
|
||||
// mark we are finished with current Order
|
||||
recordFinished = true;
|
||||
}
|
||||
}
|
||||
else if (Customer.LINE_ID_NON_BUSINESS_CUST.equals(lineId)) {
|
||||
log.debug("MAPPING CUSTOMER");
|
||||
if (order.getCustomer() == null) {
|
||||
Customer customer = customerMapper.mapFieldSet(fieldSet);
|
||||
customer.setBusinessCustomer(false);
|
||||
order.setCustomer(customer);
|
||||
case Customer.LINE_ID_BUSINESS_CUST -> {
|
||||
log.debug("MAPPING CUSTOMER");
|
||||
if (order.getCustomer() == null) {
|
||||
Customer customer = customerMapper.mapFieldSet(fieldSet);
|
||||
customer.setBusinessCustomer(true);
|
||||
order.setCustomer(customer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (Address.LINE_ID_BILLING_ADDR.equals(lineId)) {
|
||||
log.debug("MAPPING BILLING ADDRESS");
|
||||
order.setBillingAddress(addressMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
else if (Address.LINE_ID_SHIPPING_ADDR.equals(lineId)) {
|
||||
log.debug("MAPPING SHIPPING ADDRESS");
|
||||
order.setShippingAddress(addressMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
else if (BillingInfo.LINE_ID_BILLING_INFO.equals(lineId)) {
|
||||
log.debug("MAPPING BILLING INFO");
|
||||
order.setBilling(billingMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
else if (ShippingInfo.LINE_ID_SHIPPING_INFO.equals(lineId)) {
|
||||
log.debug("MAPPING SHIPPING INFO");
|
||||
order.setShipping(shippingMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
else if (LineItem.LINE_ID_ITEM.equals(lineId)) {
|
||||
log.debug("MAPPING LINE ITEM");
|
||||
if (order.getLineItems() == null) {
|
||||
order.setLineItems(new ArrayList<>());
|
||||
case Customer.LINE_ID_NON_BUSINESS_CUST -> {
|
||||
log.debug("MAPPING CUSTOMER");
|
||||
if (order.getCustomer() == null) {
|
||||
Customer customer = customerMapper.mapFieldSet(fieldSet);
|
||||
customer.setBusinessCustomer(false);
|
||||
order.setCustomer(customer);
|
||||
}
|
||||
}
|
||||
order.getLineItems().add(itemMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Could not map LINE_ID=" + lineId);
|
||||
case Address.LINE_ID_BILLING_ADDR -> {
|
||||
log.debug("MAPPING BILLING ADDRESS");
|
||||
order.setBillingAddress(addressMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
case Address.LINE_ID_SHIPPING_ADDR -> {
|
||||
log.debug("MAPPING SHIPPING ADDRESS");
|
||||
order.setShippingAddress(addressMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
case BillingInfo.LINE_ID_BILLING_INFO -> {
|
||||
log.debug("MAPPING BILLING INFO");
|
||||
order.setBilling(billingMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
case ShippingInfo.LINE_ID_SHIPPING_INFO -> {
|
||||
log.debug("MAPPING SHIPPING INFO");
|
||||
order.setShipping(shippingMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
case LineItem.LINE_ID_ITEM -> {
|
||||
log.debug("MAPPING LINE ITEM");
|
||||
if (order.getLineItems() == null) {
|
||||
order.setLineItems(new ArrayList<>());
|
||||
}
|
||||
order.getLineItems().add(itemMapper.mapFieldSet(fieldSet));
|
||||
}
|
||||
default -> {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Could not map LINE_ID=" + lineId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user