1 | /* |
2 | * Copyright 2006-2013 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 | |
17 | package org.springframework.batch.item.adapter; |
18 | |
19 | import java.util.Arrays; |
20 | import java.util.List; |
21 | |
22 | import org.springframework.batch.item.ItemWriter; |
23 | import org.springframework.beans.BeanWrapper; |
24 | import org.springframework.beans.BeanWrapperImpl; |
25 | import org.springframework.util.Assert; |
26 | |
27 | /** |
28 | * Delegates processing to a custom method - extracts property values from item |
29 | * object and uses them as arguments for the delegate method. |
30 | * |
31 | * @see ItemWriterAdapter |
32 | * |
33 | * @author Robert Kasanicky |
34 | */ |
35 | public class PropertyExtractingDelegatingItemWriter<T> extends AbstractMethodInvokingDelegator<T> implements |
36 | ItemWriter<T> { |
37 | |
38 | private String[] fieldsUsedAsTargetMethodArguments; |
39 | |
40 | /** |
41 | * Extracts values from item's fields named in |
42 | * fieldsUsedAsTargetMethodArguments and passes them as arguments to the |
43 | * delegate method. |
44 | */ |
45 | @Override |
46 | public void write(List<? extends T> items) throws Exception { |
47 | for (T item : items) { |
48 | |
49 | // helper for extracting property values from a bean |
50 | BeanWrapper beanWrapper = new BeanWrapperImpl(item); |
51 | |
52 | Object[] methodArguments = new Object[fieldsUsedAsTargetMethodArguments.length]; |
53 | for (int i = 0; i < fieldsUsedAsTargetMethodArguments.length; i++) { |
54 | methodArguments[i] = beanWrapper.getPropertyValue(fieldsUsedAsTargetMethodArguments[i]); |
55 | } |
56 | |
57 | invokeDelegateMethodWithArguments(methodArguments); |
58 | |
59 | } |
60 | } |
61 | |
62 | @Override |
63 | public void afterPropertiesSet() throws Exception { |
64 | super.afterPropertiesSet(); |
65 | Assert.notEmpty(fieldsUsedAsTargetMethodArguments); |
66 | } |
67 | |
68 | /** |
69 | * @param fieldsUsedAsMethodArguments the values of the these item's fields |
70 | * will be used as arguments for the delegate method. Nested property values |
71 | * are supported, e.g. <code>address.city</code> |
72 | */ |
73 | public void setFieldsUsedAsTargetMethodArguments(String[] fieldsUsedAsMethodArguments) { |
74 | this.fieldsUsedAsTargetMethodArguments = Arrays.asList(fieldsUsedAsMethodArguments).toArray( |
75 | new String[fieldsUsedAsMethodArguments.length]); |
76 | } |
77 | |
78 | } |