|
1 | 1 | /* |
2 | | - * Copyright 2006-2021 the original author or authors. |
| 2 | + * Copyright 2006-2023 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
33 | 33 |
|
34 | 34 | /** |
35 | 35 | * @author peter.zozom |
| 36 | + * @author Mahmoud Ben Hassine |
36 | 37 | * |
37 | 38 | */ |
38 | 39 | public class OrderItemReader implements ItemReader<Order> { |
@@ -89,65 +90,67 @@ private void process(FieldSet fieldSet) throws Exception { |
89 | 90 |
|
90 | 91 | String lineId = fieldSet.readString(0); |
91 | 92 |
|
92 | | - if (Order.LINE_ID_HEADER.equals(lineId)) { |
93 | | - log.debug("STARTING NEW RECORD"); |
94 | | - order = headerMapper.mapFieldSet(fieldSet); |
95 | | - } |
96 | | - else if (Order.LINE_ID_FOOTER.equals(lineId)) { |
97 | | - log.debug("END OF RECORD"); |
98 | | - |
99 | | - // Do mapping for footer here, because mapper does not allow to pass |
100 | | - // an Order object as input. |
101 | | - // Mapper always creates new object |
102 | | - order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE")); |
103 | | - order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS")); |
104 | | - order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS")); |
105 | | - |
106 | | - // mark we are finished with current Order |
107 | | - recordFinished = true; |
108 | | - } |
109 | | - else if (Customer.LINE_ID_BUSINESS_CUST.equals(lineId)) { |
110 | | - log.debug("MAPPING CUSTOMER"); |
111 | | - if (order.getCustomer() == null) { |
112 | | - Customer customer = customerMapper.mapFieldSet(fieldSet); |
113 | | - customer.setBusinessCustomer(true); |
114 | | - order.setCustomer(customer); |
| 93 | + switch (lineId) { |
| 94 | + case Order.LINE_ID_HEADER -> { |
| 95 | + log.debug("STARTING NEW RECORD"); |
| 96 | + order = headerMapper.mapFieldSet(fieldSet); |
115 | 97 | } |
116 | | - } |
117 | | - else if (Customer.LINE_ID_NON_BUSINESS_CUST.equals(lineId)) { |
118 | | - log.debug("MAPPING CUSTOMER"); |
119 | | - if (order.getCustomer() == null) { |
120 | | - Customer customer = customerMapper.mapFieldSet(fieldSet); |
121 | | - customer.setBusinessCustomer(false); |
122 | | - order.setCustomer(customer); |
| 98 | + case Order.LINE_ID_FOOTER -> { |
| 99 | + log.debug("END OF RECORD"); |
| 100 | + |
| 101 | + // Do mapping for footer here, because mapper does not allow to pass |
| 102 | + // an Order object as input. |
| 103 | + // Mapper always creates new object |
| 104 | + order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE")); |
| 105 | + order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS")); |
| 106 | + order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS")); |
| 107 | + |
| 108 | + // mark we are finished with current Order |
| 109 | + recordFinished = true; |
123 | 110 | } |
124 | | - } |
125 | | - else if (Address.LINE_ID_BILLING_ADDR.equals(lineId)) { |
126 | | - log.debug("MAPPING BILLING ADDRESS"); |
127 | | - order.setBillingAddress(addressMapper.mapFieldSet(fieldSet)); |
128 | | - } |
129 | | - else if (Address.LINE_ID_SHIPPING_ADDR.equals(lineId)) { |
130 | | - log.debug("MAPPING SHIPPING ADDRESS"); |
131 | | - order.setShippingAddress(addressMapper.mapFieldSet(fieldSet)); |
132 | | - } |
133 | | - else if (BillingInfo.LINE_ID_BILLING_INFO.equals(lineId)) { |
134 | | - log.debug("MAPPING BILLING INFO"); |
135 | | - order.setBilling(billingMapper.mapFieldSet(fieldSet)); |
136 | | - } |
137 | | - else if (ShippingInfo.LINE_ID_SHIPPING_INFO.equals(lineId)) { |
138 | | - log.debug("MAPPING SHIPPING INFO"); |
139 | | - order.setShipping(shippingMapper.mapFieldSet(fieldSet)); |
140 | | - } |
141 | | - else if (LineItem.LINE_ID_ITEM.equals(lineId)) { |
142 | | - log.debug("MAPPING LINE ITEM"); |
143 | | - if (order.getLineItems() == null) { |
144 | | - order.setLineItems(new ArrayList<>()); |
| 111 | + case Customer.LINE_ID_BUSINESS_CUST -> { |
| 112 | + log.debug("MAPPING CUSTOMER"); |
| 113 | + if (order.getCustomer() == null) { |
| 114 | + Customer customer = customerMapper.mapFieldSet(fieldSet); |
| 115 | + customer.setBusinessCustomer(true); |
| 116 | + order.setCustomer(customer); |
| 117 | + } |
145 | 118 | } |
146 | | - order.getLineItems().add(itemMapper.mapFieldSet(fieldSet)); |
147 | | - } |
148 | | - else { |
149 | | - if (log.isDebugEnabled()) { |
150 | | - log.debug("Could not map LINE_ID=" + lineId); |
| 119 | + case Customer.LINE_ID_NON_BUSINESS_CUST -> { |
| 120 | + log.debug("MAPPING CUSTOMER"); |
| 121 | + if (order.getCustomer() == null) { |
| 122 | + Customer customer = customerMapper.mapFieldSet(fieldSet); |
| 123 | + customer.setBusinessCustomer(false); |
| 124 | + order.setCustomer(customer); |
| 125 | + } |
| 126 | + } |
| 127 | + case Address.LINE_ID_BILLING_ADDR -> { |
| 128 | + log.debug("MAPPING BILLING ADDRESS"); |
| 129 | + order.setBillingAddress(addressMapper.mapFieldSet(fieldSet)); |
| 130 | + } |
| 131 | + case Address.LINE_ID_SHIPPING_ADDR -> { |
| 132 | + log.debug("MAPPING SHIPPING ADDRESS"); |
| 133 | + order.setShippingAddress(addressMapper.mapFieldSet(fieldSet)); |
| 134 | + } |
| 135 | + case BillingInfo.LINE_ID_BILLING_INFO -> { |
| 136 | + log.debug("MAPPING BILLING INFO"); |
| 137 | + order.setBilling(billingMapper.mapFieldSet(fieldSet)); |
| 138 | + } |
| 139 | + case ShippingInfo.LINE_ID_SHIPPING_INFO -> { |
| 140 | + log.debug("MAPPING SHIPPING INFO"); |
| 141 | + order.setShipping(shippingMapper.mapFieldSet(fieldSet)); |
| 142 | + } |
| 143 | + case LineItem.LINE_ID_ITEM -> { |
| 144 | + log.debug("MAPPING LINE ITEM"); |
| 145 | + if (order.getLineItems() == null) { |
| 146 | + order.setLineItems(new ArrayList<>()); |
| 147 | + } |
| 148 | + order.getLineItems().add(itemMapper.mapFieldSet(fieldSet)); |
| 149 | + } |
| 150 | + default -> { |
| 151 | + if (log.isDebugEnabled()) { |
| 152 | + log.debug("Could not map LINE_ID=" + lineId); |
| 153 | + } |
151 | 154 | } |
152 | 155 | } |
153 | 156 | } |
|
0 commit comments