I managed to find a workarround this way:
- Write a custom FieldExtractor
- This one has a Map associating for all Date or Calendar fields with theire formatting.
- It first convert non null fields to String
- Then delegates the formating.
Java
Code:
/**
* @author <b>(sylvain.mougenot) </b>
* @version 1.00, 9 déc. 2009
*/
public class BeanWrapperFieldExtractorDateFormatting<T>
implements FieldExtractor<T>, InitializingBean {
private String[] names;
private Map<String, String> dateFormatting;
/**
* @param names field names to be extracted by the {@link #extract(Object)} method.
*/
public void setNames(final String[] names) {
this.names = names;
}
/**
* @param dateFormatting the dateFormatting to set
*/
public void setDateFormatting(final Map<String, String> dateFormatting) {
this.dateFormatting = dateFormatting;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.file.transform.FieldExtractor#extract(java.lang.Object)
*/
public Object[] extract(final T item) {
final List<Object> values = new ArrayList<Object>();
final BeanWrapper bw = new BeanWrapperImpl(item);
for (final String propertyName : this.names) {
Object propertyValue = bw.getPropertyValue(propertyName);
if (propertyValue != null) {
final String myFormat = dateFormatting.get(propertyName);
// Do we have a format
if (myFormat != null) {
// only format Date and Calendar Objects
if (propertyValue instanceof Date) {
propertyValue = DateFormatUtils.format((Date) propertyValue, myFormat);
} else if (propertyValue instanceof Calendar) {
propertyValue = DateFormatUtils.format(((Calendar) propertyValue).getTime(), myFormat);
}
}
}
values.add(propertyValue);
}
return values.toArray();
}
Bean config
Code:
<bean
class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<property name="fieldExtractor">
<bean
class="fr.mediapost.sirh.batch.distributeur.util.BeanWrapperFieldExtractorDateFormatting">
<!-- Bean de type DistributeurData -->
<property name="names"
value="dateEntreeSoci,typeContrat,dateSortieEtab,motifSortie,blanc,soccle,mtfcatehr,mtfcodehr" />
<property name="dateFormatting">
<map key-type="java.lang.String" value-type="java.lang.String">
<entry key="dateEntreeSoci" value="yyyy-MM-dd" />
<entry key="dateSortieEtab" value="yyyy-MM-dd" />
</map>
</property>
</bean>
</property>
<property name="format"
value="%10.10s%3.3S0001%10.10s%3.3S%3$10.10s%10sOO %3.3s%6.6S%6.6S%5$22sEMBAUC%2$-6.6S%5$10sDISTRIB%5$82s0%5$70s" />
</bean>