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.trade;
18  
19  import static org.springframework.batch.sample.domain.trade.CustomerOperation.*;
20  
21  import org.springframework.batch.item.ItemProcessor;
22  
23  /**
24   * @author Lucas Ward
25   *
26   */
27  public class CustomerUpdateProcessor implements ItemProcessor<CustomerUpdate, CustomerUpdate>{
28  
29  	private CustomerDao customerDao;
30  	private InvalidCustomerLogger invalidCustomerLogger;
31  	
32  	public CustomerUpdate process(CustomerUpdate item) throws Exception {
33  		
34  		if(item.getOperation() == DELETE){
35  			//delete is not supported
36  			invalidCustomerLogger.log(item);
37  			return null;
38  		}
39  		
40  		CustomerCredit customerCredit = customerDao.getCustomerByName(item.getCustomerName());
41  		
42  		if(item.getOperation() == ADD && customerCredit == null){
43  			return item;
44  		}
45  		else if(item.getOperation() == ADD && customerCredit != null){
46  			//veto processing
47  			invalidCustomerLogger.log(item);
48  			return null;
49  		}
50  		
51  		if(item.getOperation() == UPDATE && customerCredit != null){
52  			return item;
53  		}
54  		else if(item.getOperation() == UPDATE && customerCredit == null){
55  			//veto processing
56  			invalidCustomerLogger.log(item);
57  			return null;
58  		}
59  		
60  		//if an item makes it through all these checks it can be assumed to be bad, logged, and skipped
61  		invalidCustomerLogger.log(item);
62  		return null;
63  	}
64  	
65  	public void setCustomerDao(CustomerDao customerDao) {
66  		this.customerDao = customerDao;
67  	}
68  	
69  	public void setInvalidCustomerLogger(
70  			InvalidCustomerLogger invalidCustomerLogger) {
71  		this.invalidCustomerLogger = invalidCustomerLogger;
72  	}
73  
74  }