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 package org.springframework.batch.sample.domain.trade.internal;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.hibernate.SessionFactory;
22 import org.springframework.batch.repeat.RepeatContext;
23 import org.springframework.batch.repeat.RepeatListener;
24 import org.springframework.batch.repeat.RepeatStatus;
25 import org.springframework.batch.sample.domain.trade.CustomerCredit;
26 import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
27
28 /**
29 * @author Lucas Ward
30 * @author Dave Syer
31 *
32 */
33 public class HibernateCreditDao implements
34 CustomerCreditDao, RepeatListener {
35
36 private int failOnFlush = -1;
37 private List<Throwable> errors = new ArrayList<Throwable>();
38 private SessionFactory sessionFactory;
39
40 public void setSessionFactory(SessionFactory sessionFactory) {
41 this.sessionFactory = sessionFactory;
42 }
43
44 /**
45 * Public accessor for the errors property.
46 *
47 * @return the errors - a list of Throwable instances
48 */
49 public List<Throwable> getErrors() {
50 return errors;
51 }
52
53 /*
54 * (non-Javadoc)
55 *
56 * @see org.springframework.batch.sample.domain.trade.internal.CustomerCreditWriter#write(org.springframework.batch.sample.domain.CustomerCredit)
57 */
58 public void writeCredit(CustomerCredit customerCredit) {
59 if (customerCredit.getId() == failOnFlush) {
60 // try to insert one with a duplicate ID
61 CustomerCredit newCredit = new CustomerCredit();
62 newCredit.setId(customerCredit.getId());
63 newCredit.setName(customerCredit.getName());
64 newCredit.setCredit(customerCredit.getCredit());
65 sessionFactory.getCurrentSession().save(newCredit);
66 } else {
67 sessionFactory.getCurrentSession().update(customerCredit);
68 }
69 }
70
71 /*
72 * (non-Javadoc)
73 *
74 * @see org.springframework.batch.io.OutputSource#write(java.lang.Object)
75 */
76 public void write(Object output) {
77 writeCredit((CustomerCredit) output);
78 }
79
80 /**
81 * Public setter for the failOnFlush property.
82 *
83 * @param failOnFlush
84 * the ID of the record you want to fail on flush (for testing)
85 */
86 public void setFailOnFlush(int failOnFlush) {
87 this.failOnFlush = failOnFlush;
88 }
89
90 public void onError(RepeatContext context, Throwable e) {
91 errors.add(e);
92 }
93
94 /* (non-Javadoc)
95 * @see org.springframework.batch.repeat.RepeatInterceptor#after(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.ExitStatus)
96 */
97 public void after(RepeatContext context, RepeatStatus result) {
98 }
99
100 /* (non-Javadoc)
101 * @see org.springframework.batch.repeat.RepeatInterceptor#before(org.springframework.batch.repeat.RepeatContext)
102 */
103 public void before(RepeatContext context) {
104 }
105
106 /* (non-Javadoc)
107 * @see org.springframework.batch.repeat.RepeatInterceptor#close(org.springframework.batch.repeat.RepeatContext)
108 */
109 public void close(RepeatContext context) {
110 }
111
112 /* (non-Javadoc)
113 * @see org.springframework.batch.repeat.RepeatInterceptor#open(org.springframework.batch.repeat.RepeatContext)
114 */
115 public void open(RepeatContext context) {
116 }
117
118 }