001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.sample.domain.trade;
018
019import static org.springframework.batch.sample.domain.trade.CustomerOperation.*;
020
021import org.springframework.batch.item.ItemProcessor;
022
023/**
024 * @author Lucas Ward
025 *
026 */
027public class CustomerUpdateProcessor implements ItemProcessor<CustomerUpdate, CustomerUpdate>{
028
029        private CustomerDao customerDao;
030        private InvalidCustomerLogger invalidCustomerLogger;
031        
032        @Override
033        public CustomerUpdate process(CustomerUpdate item) throws Exception {
034                
035                if(item.getOperation() == DELETE){
036                        //delete is not supported
037                        invalidCustomerLogger.log(item);
038                        return null;
039                }
040                
041                CustomerCredit customerCredit = customerDao.getCustomerByName(item.getCustomerName());
042                
043                if(item.getOperation() == ADD && customerCredit == null){
044                        return item;
045                }
046                else if(item.getOperation() == ADD && customerCredit != null){
047                        //veto processing
048                        invalidCustomerLogger.log(item);
049                        return null;
050                }
051                
052                if(item.getOperation() == UPDATE && customerCredit != null){
053                        return item;
054                }
055                else if(item.getOperation() == UPDATE && customerCredit == null){
056                        //veto processing
057                        invalidCustomerLogger.log(item);
058                        return null;
059                }
060                
061                //if an item makes it through all these checks it can be assumed to be bad, logged, and skipped
062                invalidCustomerLogger.log(item);
063                return null;
064        }
065        
066        public void setCustomerDao(CustomerDao customerDao) {
067                this.customerDao = customerDao;
068        }
069        
070        public void setInvalidCustomerLogger(
071                        InvalidCustomerLogger invalidCustomerLogger) {
072                this.invalidCustomerLogger = invalidCustomerLogger;
073        }
074
075}