EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.item.adapter]

COVERAGE SUMMARY FOR SOURCE FILE [PropertyExtractingDelegatingItemWriter.java]

nameclass, %method, %block, %line, %
PropertyExtractingDelegatingItemWriter.java100% (1/1)100% (4/4)100% (62/62)100% (14/14)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PropertyExtractingDelegatingItemWriter100% (1/1)100% (4/4)100% (62/62)100% (14/14)
PropertyExtractingDelegatingItemWriter (): void 100% (1/1)100% (3/3)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (6/6)100% (3/3)
setFieldsUsedAsTargetMethodArguments (String []): void 100% (1/1)100% (10/10)100% (2/2)
write (List): void 100% (1/1)100% (43/43)100% (8/8)

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 
17package org.springframework.batch.item.adapter;
18 
19import java.util.Arrays;
20import java.util.List;
21 
22import org.springframework.batch.item.ItemWriter;
23import org.springframework.beans.BeanWrapper;
24import org.springframework.beans.BeanWrapperImpl;
25import 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 */
35public 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        public void write(List<? extends T> items) throws Exception {
46                for (T item : items) {
47 
48                        // helper for extracting property values from a bean
49                        BeanWrapper beanWrapper = new BeanWrapperImpl(item);
50 
51                        Object[] methodArguments = new Object[fieldsUsedAsTargetMethodArguments.length];
52                        for (int i = 0; i < fieldsUsedAsTargetMethodArguments.length; i++) {
53                                methodArguments[i] = beanWrapper.getPropertyValue(fieldsUsedAsTargetMethodArguments[i]);
54                        }
55 
56                        invokeDelegateMethodWithArguments(methodArguments);
57 
58                }
59        }
60 
61        public void afterPropertiesSet() throws Exception {
62                super.afterPropertiesSet();
63                Assert.notEmpty(fieldsUsedAsTargetMethodArguments);
64        }
65 
66        /**
67         * @param fieldsUsedAsMethodArguments the values of the these item's fields
68         * will be used as arguments for the delegate method. Nested property values
69         * are supported, e.g. <code>address.city</code>
70         */
71        public void setFieldsUsedAsTargetMethodArguments(String[] fieldsUsedAsMethodArguments) {
72                this.fieldsUsedAsTargetMethodArguments = Arrays.asList(fieldsUsedAsMethodArguments).toArray(
73                                new String[fieldsUsedAsMethodArguments.length]);
74        }
75 
76}

[all classes][org.springframework.batch.item.adapter]
EMMA 2.0.5312 (C) Vladimir Roubtsov