001/*
002 * Copyright 2006-2013 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 */
016
017package org.springframework.batch.item.adapter;
018
019import java.util.Arrays;
020import java.util.List;
021
022import org.springframework.batch.item.ItemWriter;
023import org.springframework.beans.BeanWrapper;
024import org.springframework.beans.BeanWrapperImpl;
025import org.springframework.util.Assert;
026
027/**
028 * Delegates processing to a custom method - extracts property values from item
029 * object and uses them as arguments for the delegate method.
030 *
031 * @see ItemWriterAdapter
032 *
033 * @author Robert Kasanicky
034 */
035public class PropertyExtractingDelegatingItemWriter<T> extends AbstractMethodInvokingDelegator<T> implements
036ItemWriter<T> {
037
038        private String[] fieldsUsedAsTargetMethodArguments;
039
040        /**
041         * Extracts values from item's fields named in
042         * fieldsUsedAsTargetMethodArguments and passes them as arguments to the
043         * delegate method.
044         */
045        @Override
046        public void write(List<? extends T> items) throws Exception {
047                for (T item : items) {
048
049                        // helper for extracting property values from a bean
050                        BeanWrapper beanWrapper = new BeanWrapperImpl(item);
051
052                        Object[] methodArguments = new Object[fieldsUsedAsTargetMethodArguments.length];
053                        for (int i = 0; i < fieldsUsedAsTargetMethodArguments.length; i++) {
054                                methodArguments[i] = beanWrapper.getPropertyValue(fieldsUsedAsTargetMethodArguments[i]);
055                        }
056
057                        invokeDelegateMethodWithArguments(methodArguments);
058
059                }
060        }
061
062        @Override
063        public void afterPropertiesSet() throws Exception {
064                super.afterPropertiesSet();
065                Assert.notEmpty(fieldsUsedAsTargetMethodArguments, "fieldsUsedAsTargetMethodArguments must not be empty");
066        }
067
068        /**
069         * @param fieldsUsedAsMethodArguments the values of the these item's fields
070         * will be used as arguments for the delegate method. Nested property values
071         * are supported, e.g. <code>address.city</code>
072         */
073        public void setFieldsUsedAsTargetMethodArguments(String[] fieldsUsedAsMethodArguments) {
074                this.fieldsUsedAsTargetMethodArguments = Arrays.asList(fieldsUsedAsMethodArguments).toArray(
075                                new String[fieldsUsedAsMethodArguments.length]);
076        }
077
078}