Skip to content

Commit a727c46

Browse files
author
adrianpusty
committed
getProperties method moved from RowSet to BeanWrapperRowMapper, header array added as field to BeanWrapperRowMapper
1 parent 8c1f3ba commit a727c46

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/mapping/BeanWrapperRowMapper.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public class BeanWrapperRowMapper<T> extends DefaultPropertyEditorRegistrar
9999

100100
private boolean strict = true;
101101

102+
private String[] header;
103+
102104
@Override
103105
public void setBeanFactory(BeanFactory beanFactory) {
104106
this.beanFactory = beanFactory;
@@ -138,6 +140,10 @@ public void setTargetType(Class<? extends T> type) {
138140
this.type = type;
139141
}
140142

143+
public void setHeader(String[] header) {
144+
this.header = header;
145+
}
146+
141147
/**
142148
* Check that precisely one of type or prototype bean name is specified.
143149
* @throws IllegalStateException if neither is set or both properties are set.
@@ -165,13 +171,35 @@ public void afterPropertiesSet() throws Exception {
165171
public T mapRow(RowSet rs) throws BindException {
166172
T copy = getBean();
167173
DataBinder binder = createBinder(copy);
168-
binder.bind(new MutablePropertyValues(getBeanProperties(copy, rs.getProperties())));
174+
binder.bind(new MutablePropertyValues(getBeanProperties(copy, getProperties(rs))));
169175
if (binder.getBindingResult().hasErrors()) {
170176
throw new BindException(binder.getBindingResult());
171177
}
172178
return copy;
173179
}
174180

181+
/**
182+
* Construct name-value pairs from the column names and string values. {@code null}
183+
* values are omitted.
184+
* @param rowSet rowSet
185+
* @return some properties representing the row set.
186+
* @throws IllegalStateException if the column name meta data is not available.
187+
*/
188+
public Properties getProperties(RowSet rowSet) {
189+
if (this.header == null) {
190+
throw new IllegalStateException("Cannot create properties without meta data");
191+
}
192+
193+
Properties props = new Properties();
194+
for (int i = 0; i < rowSet.getCurrentRow().length; i++) {
195+
String value = rowSet.getCurrentRow()[i];
196+
if (value != null) {
197+
props.setProperty(this.header[i], value);
198+
}
199+
}
200+
return props;
201+
}
202+
175203
/**
176204
* Create a binder for the target object. The binder will then be used to bind the
177205
* properties form a field set into the target object. This implementation creates a

spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/support/rowset/DefaultRowSet.java

-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.batch.extensions.excel.support.rowset;
1818

1919
import java.util.Iterator;
20-
import java.util.Properties;
2120

2221
import org.springframework.batch.extensions.excel.Sheet;
2322

@@ -69,21 +68,4 @@ public String[] getCurrentRow() {
6968
return this.currentRow;
7069
}
7170

72-
@Override
73-
public Properties getProperties() {
74-
final String[] names = this.metaData.getColumnNames();
75-
if (names == null) {
76-
throw new IllegalStateException("Cannot create properties without meta data");
77-
}
78-
79-
Properties props = new Properties();
80-
for (int i = 0; i < this.currentRow.length; i++) {
81-
String value = this.currentRow[i];
82-
if (value != null) {
83-
props.setProperty(names[i], value);
84-
}
85-
}
86-
return props;
87-
}
88-
8971
}

spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/support/rowset/RowSet.java

-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.batch.extensions.excel.support.rowset;
1818

19-
import java.util.Properties;
20-
2119
/**
2220
* Used by the {@code org.springframework.batch.item.excel.AbstractExcelItemReader} to
2321
* abstract away the complexities of the underlying Excel API implementations.
@@ -51,13 +49,4 @@ public interface RowSet {
5149
* @return the row as a {@code String[]}
5250
*/
5351
String[] getCurrentRow();
54-
55-
/**
56-
* Construct name-value pairs from the column names and string values. {@code null}
57-
* values are omitted.
58-
* @return some properties representing the row set.
59-
* @throws IllegalStateException if the column name meta data is not available.
60-
*/
61-
Properties getProperties();
62-
6352
}

spring-batch-excel/src/test/java/org/springframework/batch/extensions/excel/BeanPropertyItemReaderTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public class BeanPropertyItemReaderTest {
3939
@BeforeEach
4040
public void setup() throws Exception {
4141
ExecutionContext executionContext = new ExecutionContext();
42+
String[] header = {"id", "lastName", "firstName", "position", "birthYear", "debutYear"};
4243

4344
List<String[]> rows = new ArrayList<>();
44-
rows.add(new String[] { "id", "lastName", "firstName", "position", "birthYear", "debutYear" });
45+
rows.add(header);
4546
rows.add(new String[] { "AbduKa00", "Abdul-Jabbar", "Karim", "rb", "1974", "1996" });
4647
rows.add(new String[] { "AbduRa00", "Abdullah", "Rabih", "rb", "1975", "1999" });
4748
MockSheet sheet = new MockSheet("players", rows);
@@ -51,6 +52,7 @@ public void setup() throws Exception {
5152
BeanWrapperRowMapper<Player> rowMapper = new BeanWrapperRowMapper<>();
5253
rowMapper.setTargetType(Player.class);
5354
rowMapper.afterPropertiesSet();
55+
rowMapper.setHeader(header);
5456

5557
this.reader.setLinesToSkip(1); // Skip first row as that is the header
5658
this.reader.setRowMapper(rowMapper);

spring-batch-excel/src/test/java/org/springframework/batch/extensions/excel/BeanPropertyWithStaticHeaderItemReaderTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*/
3737
public class BeanPropertyWithStaticHeaderItemReaderTest {
3838

39+
private static final String[] HEADER = new String[] { "id", "lastName", "firstName", "position", "birthYear", "debutYear" };
3940
private MockExcelItemReader<Player> reader;
4041

4142
@BeforeEach
@@ -51,13 +52,13 @@ public void setup() throws Exception {
5152

5253
BeanWrapperRowMapper<Player> rowMapper = new BeanWrapperRowMapper<>();
5354
rowMapper.setTargetType(Player.class);
55+
rowMapper.setHeader(HEADER);
5456
rowMapper.afterPropertiesSet();
5557

5658
this.reader.setRowMapper(rowMapper);
5759

5860
DefaultRowSetFactory factory = new DefaultRowSetFactory();
59-
factory.setColumnNameExtractor(new StaticColumnNameExtractor(
60-
new String[] { "id", "lastName", "firstName", "position", "birthYear", "debutYear" }));
61+
factory.setColumnNameExtractor(new StaticColumnNameExtractor(HEADER));
6162
this.reader.setRowSetFactory(factory);
6263
this.reader.afterPropertiesSet();
6364
this.reader.open(executionContext);

spring-batch-excel/src/test/java/org/springframework/batch/extensions/excel/mapping/BeanWrapperRowMapperTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
public class BeanWrapperRowMapperTest {
4141

42+
private static final String[] HEADER = {"id", "lastName", "firstName", "position", "birthYear", "debutYear"};
43+
4244
@Test
4345
public void givenNoNameWhenInitCompleteThenIllegalStateShouldOccur() {
4446
Assertions.assertThatThrownBy(() -> {
@@ -51,10 +53,11 @@ public void givenNoNameWhenInitCompleteThenIllegalStateShouldOccur() {
5153
public void givenAValidRowWhenMappingThenAValidPlayerShouldBeConstructed() throws Exception {
5254
BeanWrapperRowMapper<Player> mapper = new BeanWrapperRowMapper<>();
5355
mapper.setTargetType(Player.class);
56+
mapper.setHeader(HEADER);
5457
mapper.afterPropertiesSet();
5558

5659
List<String[]> rows = new ArrayList<>();
57-
rows.add(new String[] { "id", "lastName", "firstName", "position", "birthYear", "debutYear" });
60+
rows.add(HEADER);
5861
rows.add(new String[] { "AbduKa00", "Abdul-Jabbar", "Karim", "rb", "1974", "1996" });
5962
MockSheet sheet = new MockSheet("players", rows);
6063

@@ -82,9 +85,10 @@ public void givenAValidRowWhenMappingThenAValidPlayerShouldBeConstructedBasedOnP
8285

8386
ApplicationContext ctx = new AnnotationConfigApplicationContext(TestConfig.class);
8487
BeanWrapperRowMapper<Player> mapper = ctx.getBean("playerRowMapper", BeanWrapperRowMapper.class);
88+
mapper.setHeader(HEADER);
8589

8690
List<String[]> rows = new ArrayList<>();
87-
rows.add(new String[] { "id", "lastName", "firstName", "position", "birthYear", "debutYear" });
91+
rows.add(HEADER);
8892
rows.add(new String[] { "AbduKa00", "Abdul-Jabbar", "Karim", "rb", "1974", "1996" });
8993
MockSheet sheet = new MockSheet("players", rows);
9094

0 commit comments

Comments
 (0)