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  package org.springframework.batch.sample.domain.multiline;
17  
18  import org.springframework.batch.item.file.mapping.FieldSetMapper;
19  import org.springframework.batch.item.file.transform.FieldSet;
20  import org.springframework.beans.factory.InitializingBean;
21  import org.springframework.util.Assert;
22  import org.springframework.validation.BindException;
23  
24  /**
25   * Delegating mapper to convert form a vanilla {@link FieldSetMapper} to one
26   * that returns {@link AggregateItem} instances for consumption by the
27   * {@link AggregateItemReader}.
28   * 
29   * @author Dave Syer
30   * 
31   */
32  public class AggregateItemFieldSetMapper<T> implements FieldSetMapper<AggregateItem<T>>, InitializingBean {
33  
34  	private FieldSetMapper<T> delegate;
35  
36  	private String end = "END";
37  
38  	private String begin = "BEGIN";
39  
40  	/**
41  	 * Public setter for the delegate.
42  	 * @param delegate the delegate to set
43  	 */
44  	public void setDelegate(FieldSetMapper<T> delegate) {
45  		this.delegate = delegate;
46  	}
47  
48  	/**
49  	 * Public setter for the end field value. If the {@link FieldSet} input has
50  	 * a first field with this value that signals the start of an aggregate
51  	 * record.
52  	 * 
53  	 * @param end the end to set
54  	 */
55  	public void setEnd(String end) {
56  		this.end = end;
57  	}
58  
59  	/**
60  	 * Public setter for the begin value. If the {@link FieldSet} input has a
61  	 * first field with this value that signals the end of an aggregate record.
62  	 * 
63  	 * @param begin the begin to set
64  	 */
65  	public void setBegin(String begin) {
66  		this.begin = begin;
67  	}
68  
69  	/**
70  	 * Check mandatory properties (delegate).
71  	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
72  	 */
73  	public void afterPropertiesSet() throws Exception {
74  		Assert.notNull(delegate, "A FieldSetMapper delegate must be provided.");
75  	}
76  
77  	/**
78  	 * Build an {@link AggregateItem} based on matching the first column in the
79  	 * input {@link FieldSet} to check for begin and end delimiters. If the
80  	 * current record is neither a begin nor an end marker then it is mapped
81  	 * using the delegate.
82  	 * @param fieldSet a {@link FieldSet} to map
83  	 * 
84  	 * @return an {@link AggregateItem} that wraps the return value from the
85  	 * delegate
86  	 * @throws BindException if one of the delegates does
87  	 */
88  	public AggregateItem<T> mapFieldSet(FieldSet fieldSet) throws BindException {
89  
90  		if (fieldSet.readString(0).equals(begin)) {
91  			return AggregateItem.getHeader();
92  		}
93  		if (fieldSet.readString(0).equals(end)) {
94  			return AggregateItem.getFooter();
95  		}
96  
97  		return new AggregateItem<T>(delegate.mapFieldSet(fieldSet));
98  
99  	}
100 
101 }