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 */ 016package org.springframework.batch.sample.domain.trade.internal; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import org.hibernate.SessionFactory; 022import org.springframework.batch.repeat.RepeatContext; 023import org.springframework.batch.repeat.RepeatListener; 024import org.springframework.batch.repeat.RepeatStatus; 025import org.springframework.batch.sample.domain.trade.CustomerCredit; 026import org.springframework.batch.sample.domain.trade.CustomerCreditDao; 027 028/** 029 * @author Lucas Ward 030 * @author Dave Syer 031 * 032 */ 033public class HibernateCreditDao implements 034 CustomerCreditDao, RepeatListener { 035 036 private int failOnFlush = -1; 037 private List<Throwable> errors = new ArrayList<Throwable>(); 038 private SessionFactory sessionFactory; 039 040 public void setSessionFactory(SessionFactory sessionFactory) { 041 this.sessionFactory = sessionFactory; 042 } 043 044 /** 045 * Public accessor for the errors property. 046 * 047 * @return the errors - a list of Throwable instances 048 */ 049 public List<Throwable> getErrors() { 050 return errors; 051 } 052 053 /* 054 * (non-Javadoc) 055 * 056 * @see org.springframework.batch.sample.domain.trade.internal.CustomerCreditWriter#write(org.springframework.batch.sample.domain.CustomerCredit) 057 */ 058 @Override 059 public void writeCredit(CustomerCredit customerCredit) { 060 if (customerCredit.getId() == failOnFlush) { 061 // try to insert one with a duplicate ID 062 CustomerCredit newCredit = new CustomerCredit(); 063 newCredit.setId(customerCredit.getId()); 064 newCredit.setName(customerCredit.getName()); 065 newCredit.setCredit(customerCredit.getCredit()); 066 sessionFactory.getCurrentSession().save(newCredit); 067 } else { 068 sessionFactory.getCurrentSession().update(customerCredit); 069 } 070 } 071 072 /* 073 * (non-Javadoc) 074 * 075 * @see org.springframework.batch.io.OutputSource#write(java.lang.Object) 076 */ 077 public void write(Object output) { 078 writeCredit((CustomerCredit) output); 079 } 080 081 /** 082 * Public setter for the failOnFlush property. 083 * 084 * @param failOnFlush 085 * the ID of the record you want to fail on flush (for testing) 086 */ 087 public void setFailOnFlush(int failOnFlush) { 088 this.failOnFlush = failOnFlush; 089 } 090 091 @Override 092 public void onError(RepeatContext context, Throwable e) { 093 errors.add(e); 094 } 095 096 /* (non-Javadoc) 097 * @see org.springframework.batch.repeat.RepeatInterceptor#after(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.ExitStatus) 098 */ 099 @Override 100 public void after(RepeatContext context, RepeatStatus result) { 101 } 102 103 /* (non-Javadoc) 104 * @see org.springframework.batch.repeat.RepeatInterceptor#before(org.springframework.batch.repeat.RepeatContext) 105 */ 106 @Override 107 public void before(RepeatContext context) { 108 } 109 110 /* (non-Javadoc) 111 * @see org.springframework.batch.repeat.RepeatInterceptor#close(org.springframework.batch.repeat.RepeatContext) 112 */ 113 @Override 114 public void close(RepeatContext context) { 115 } 116 117 /* (non-Javadoc) 118 * @see org.springframework.batch.repeat.RepeatInterceptor#open(org.springframework.batch.repeat.RepeatContext) 119 */ 120 @Override 121 public void open(RepeatContext context) { 122 } 123 124}