View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.sample.domain.order.internal;
18  
19  import java.util.ArrayList;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.batch.item.ItemReader;
24  import org.springframework.batch.item.file.mapping.FieldSetMapper;
25  import org.springframework.batch.item.file.transform.FieldSet;
26  import org.springframework.batch.sample.domain.order.Address;
27  import org.springframework.batch.sample.domain.order.BillingInfo;
28  import org.springframework.batch.sample.domain.order.Customer;
29  import org.springframework.batch.sample.domain.order.LineItem;
30  import org.springframework.batch.sample.domain.order.Order;
31  import org.springframework.batch.sample.domain.order.ShippingInfo;
32  
33  /**
34   * @author peter.zozom
35   * 
36   */
37  public class OrderItemReader implements ItemReader<Order> {
38  	private static Log log = LogFactory.getLog(OrderItemReader.class);
39  
40  	private Order order;
41  
42  	private boolean recordFinished;
43  
44  	private FieldSetMapper<Order> headerMapper;
45  
46  	private FieldSetMapper<Customer> customerMapper;
47  
48  	private FieldSetMapper<Address> addressMapper;
49  
50  	private FieldSetMapper<BillingInfo> billingMapper;
51  
52  	private FieldSetMapper<LineItem> itemMapper;
53  
54  	private FieldSetMapper<ShippingInfo> shippingMapper;
55  
56  	private ItemReader<FieldSet> fieldSetReader;
57  
58  	/**
59  	 * @throws Exception
60  	 * @see org.springframework.batch.item.ItemReader#read()
61  	 */
62  	public Order read() throws Exception {
63  		recordFinished = false;
64  
65  		while (!recordFinished) {
66  			process(fieldSetReader.read());
67  		}
68  
69  		log.info("Mapped: " + order);
70  
71  		Order result = order;
72  		order = null;
73  
74  		return result;
75  	}
76  
77  	private void process(FieldSet fieldSet) throws Exception {
78  		// finish processing if we hit the end of file
79  		if (fieldSet == null) {
80  			log.debug("FINISHED");
81  			recordFinished = true;
82  			order = null;
83  			return;
84  		}
85  
86  		String lineId = fieldSet.readString(0);
87  
88  		// start a new Order
89  		if (Order.LINE_ID_HEADER.equals(lineId)) {
90  			log.debug("STARTING NEW RECORD");
91  			order = headerMapper.mapFieldSet(fieldSet);
92  		}
93  		else if (Order.LINE_ID_FOOTER.equals(lineId)) {
94  			log.debug("END OF RECORD");
95  
96  			// Do mapping for footer here, because mapper does not allow to pass
97  			// an Order object as input.
98  			// Mapper always creates new object
99  			order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE"));
100 			order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS"));
101 			order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS"));
102 
103 			// mark we are finished with current Order
104 			recordFinished = true;
105 		}
106 		else if (Customer.LINE_ID_BUSINESS_CUST.equals(lineId)) {
107 			log.debug("MAPPING CUSTOMER");
108 			if (order.getCustomer() == null) {
109 				Customer customer = customerMapper.mapFieldSet(fieldSet);
110 				customer.setBusinessCustomer(true);
111 				order.setCustomer(customer);
112 			}
113 		}
114 		else if (Customer.LINE_ID_NON_BUSINESS_CUST.equals(lineId)) {
115 			log.debug("MAPPING CUSTOMER");
116 			if (order.getCustomer() == null) {
117 				Customer customer = customerMapper.mapFieldSet(fieldSet);
118 				customer.setBusinessCustomer(false);
119 				order.setCustomer(customer);
120 			}
121 		}
122 		else if (Address.LINE_ID_BILLING_ADDR.equals(lineId)) {
123 			log.debug("MAPPING BILLING ADDRESS");
124 			order.setBillingAddress(addressMapper.mapFieldSet(fieldSet));
125 		}
126 		else if (Address.LINE_ID_SHIPPING_ADDR.equals(lineId)) {
127 			log.debug("MAPPING SHIPPING ADDRESS");
128 			order.setShippingAddress(addressMapper.mapFieldSet(fieldSet));
129 		}
130 		else if (BillingInfo.LINE_ID_BILLING_INFO.equals(lineId)) {
131 			log.debug("MAPPING BILLING INFO");
132 			order.setBilling(billingMapper.mapFieldSet(fieldSet));
133 		}
134 		else if (ShippingInfo.LINE_ID_SHIPPING_INFO.equals(lineId)) {
135 			log.debug("MAPPING SHIPPING INFO");
136 			order.setShipping(shippingMapper.mapFieldSet(fieldSet));
137 		}
138 		else if (LineItem.LINE_ID_ITEM.equals(lineId)) {
139 			log.debug("MAPPING LINE ITEM");
140 			if (order.getLineItems() == null) {
141 				order.setLineItems(new ArrayList<LineItem>());
142 			}
143 			order.getLineItems().add(itemMapper.mapFieldSet(fieldSet));
144 		}
145 		else {
146 			log.debug("Could not map LINE_ID=" + lineId);
147 		}
148 
149 	}
150 
151 	/**
152 	 * @param fieldSetReader reads lines from the file converting them to
153 	 *        {@link FieldSet}.
154 	 */
155 	public void setFieldSetReader(ItemReader<FieldSet> fieldSetReader) {
156 		this.fieldSetReader = fieldSetReader;
157 	}
158 
159 	public void setAddressMapper(FieldSetMapper<Address> addressMapper) {
160 		this.addressMapper = addressMapper;
161 	}
162 
163 	public void setBillingMapper(FieldSetMapper<BillingInfo> billingMapper) {
164 		this.billingMapper = billingMapper;
165 	}
166 
167 	public void setCustomerMapper(FieldSetMapper<Customer> customerMapper) {
168 		this.customerMapper = customerMapper;
169 	}
170 
171 	public void setHeaderMapper(FieldSetMapper<Order> headerMapper) {
172 		this.headerMapper = headerMapper;
173 	}
174 
175 	public void setItemMapper(FieldSetMapper<LineItem> itemMapper) {
176 		this.itemMapper = itemMapper;
177 	}
178 
179 	public void setShippingMapper(FieldSetMapper<ShippingInfo> shippingMapper) {
180 		this.shippingMapper = shippingMapper;
181 	}
182 
183 }