EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[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-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 
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
36ItemWriter<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}

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