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.multiline; 017 018import org.springframework.batch.item.file.mapping.FieldSetMapper; 019import org.springframework.batch.item.file.transform.FieldSet; 020import org.springframework.beans.factory.InitializingBean; 021import org.springframework.util.Assert; 022import org.springframework.validation.BindException; 023 024/** 025 * Delegating mapper to convert form a vanilla {@link FieldSetMapper} to one 026 * that returns {@link AggregateItem} instances for consumption by the 027 * {@link AggregateItemReader}. 028 * 029 * @author Dave Syer 030 * 031 */ 032public class AggregateItemFieldSetMapper<T> implements FieldSetMapper<AggregateItem<T>>, InitializingBean { 033 034 private FieldSetMapper<T> delegate; 035 036 private String end = "END"; 037 038 private String begin = "BEGIN"; 039 040 /** 041 * Public setter for the delegate. 042 * @param delegate the delegate to set 043 */ 044 public void setDelegate(FieldSetMapper<T> delegate) { 045 this.delegate = delegate; 046 } 047 048 /** 049 * Public setter for the end field value. If the {@link FieldSet} input has 050 * a first field with this value that signals the start of an aggregate 051 * record. 052 * 053 * @param end the end to set 054 */ 055 public void setEnd(String end) { 056 this.end = end; 057 } 058 059 /** 060 * Public setter for the begin value. If the {@link FieldSet} input has a 061 * first field with this value that signals the end of an aggregate record. 062 * 063 * @param begin the begin to set 064 */ 065 public void setBegin(String begin) { 066 this.begin = begin; 067 } 068 069 /** 070 * Check mandatory properties (delegate). 071 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() 072 */ 073 @Override 074 public void afterPropertiesSet() throws Exception { 075 Assert.notNull(delegate, "A FieldSetMapper delegate must be provided."); 076 } 077 078 /** 079 * Build an {@link AggregateItem} based on matching the first column in the 080 * input {@link FieldSet} to check for begin and end delimiters. If the 081 * current record is neither a begin nor an end marker then it is mapped 082 * using the delegate. 083 * @param fieldSet a {@link FieldSet} to map 084 * 085 * @return an {@link AggregateItem} that wraps the return value from the 086 * delegate 087 * @throws BindException if one of the delegates does 088 */ 089 @Override 090 public AggregateItem<T> mapFieldSet(FieldSet fieldSet) throws BindException { 091 092 if (fieldSet.readString(0).equals(begin)) { 093 return AggregateItem.getHeader(); 094 } 095 if (fieldSet.readString(0).equals(end)) { 096 return AggregateItem.getFooter(); 097 } 098 099 return new AggregateItem<T>(delegate.mapFieldSet(fieldSet)); 100 101 } 102 103}