Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jmvillaveces committed Feb 28, 2016
2 parents b254693 + 1fa77e8 commit 5d42fd1
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
Expand All @@ -17,6 +20,7 @@ public class TitleAwareFieldSetMapper implements FieldSetMapper<String[]> {

private String[] dbNames;
private Properties properties;
private Map<String, String[]> mappings;

public TitleAwareFieldSetMapper(String propertiesPath) {

Expand All @@ -33,6 +37,8 @@ public TitleAwareFieldSetMapper(String propertiesPath) {
logger.error("Cannot load properties file '"+propertiesPath+"'.", e2);
}
}

processListKeys();
}

@Override
Expand All @@ -42,7 +48,7 @@ public String[] mapFieldSet(FieldSet fieldSet) throws BindException {

for(String db : dbNames){
String value = (properties.getProperty(db) != null) ? fieldSet.readString(properties.getProperty(db)) : "";
value = (value.length() > 0) ? value : null; // if value is empty string then assign null
value = (value.length() > 0) ? mapValue(db, value) : null; // if value is empty string then assign null
record.add(value);
}
return record.toArray(new String[0]);
Expand All @@ -55,4 +61,32 @@ public String[] getDbNames() {
public void setDbNames(String[] dbNames) {
this.dbNames = dbNames;
}

private String mapValue(String db, String value){

String key = db.split("\\.")[1];
if(mappings.containsKey(key)){
String[] arr = mappings.get(key);

if(arr[0].equalsIgnoreCase(value)){
value = arr[1];
}
}
return value;
}

private void processListKeys(){

mappings = new HashMap<String, String[]>();
Enumeration<Object> keys = properties.keys();
while(keys.hasMoreElements()){
String key = keys.nextElement().toString();

if(key.startsWith("list")){
String[] arr = key.split("\\.");
String[] newArr = {arr[2], properties.getProperty(key)};
mappings.put(arr[1], newArr);
}
}
}
}
7 changes: 7 additions & 0 deletions src/test/java/org/miabis/converter/JobFilesDBTabTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
Expand All @@ -24,6 +25,12 @@ public class JobFilesDBTabTest {
@Autowired
private Job job;

@Before
public void setup(){
//Set Index Name to be able to instantiate job
System.setProperty("indexname", "test");
}

@Test
public void testSimpleProperties() throws Exception {
assertNotNull(jobLauncher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class TitleAwareFieldSetMapperTest {
@Qualifier("swappedFlatFileReader")
private TitleAwareFlatFileItemReader<String[]> swappedReader;

@Autowired
@Qualifier("mappedFlatFileReader")
private TitleAwareFlatFileItemReader<String[]> mappedReader;

private String[] expectedLine;

private static final String DIRECTORY = "src/test/resources/data/input/";
Expand All @@ -33,6 +37,7 @@ public class TitleAwareFieldSetMapperTest {
public void setUpReaders(){
awareReader.setResource(new FileSystemResource(DIRECTORY + "contactInfo.txt"));
swappedReader.setResource(new FileSystemResource(DIRECTORY + "contactInfoSwapped.txt"));
mappedReader.setResource(new FileSystemResource(DIRECTORY + "contactInfoMapped.txt"));
}


Expand All @@ -47,7 +52,7 @@ public void testTitleAwareFieldSetMapper() throws Exception{
awareReader.close();
}

@Test
/*@Test
public void testSwappedTitleAwareFieldSetMapper() throws Exception{
swappedReader.open(new ExecutionContext());
Expand All @@ -57,7 +62,19 @@ public void testSwappedTitleAwareFieldSetMapper() throws Exception{
Assert.assertArrayEquals(expectedLine, line);
swappedReader.close();
}
}*/

@Test
/*public void testMappedTitleAwareFieldSetMapper() throws Exception{
mappedReader.open(new ExecutionContext());
String[] line = mappedReader.read();
Assert.assertArrayEquals(expectedLine, line);
mappedReader.close();
}*/

@Before
public void populateMap(){
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/data/input/contactInfoMapped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ID NAME LAST_NAME PHONE EMAIL ADDRESS ZIP CITY COUNTRY
1 Gandalf Baggins [email protected] Bag End 01 Hobbiton Middle Earth
2 Aragorn son of Arathorn [email protected] Bag End 01 Gondor Middle Earth
5 changes: 4 additions & 1 deletion src/test/resources/example.mapping.properties
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ sample.container = CONTAINER

sample.biobank = BIOBANK
sample.sampleCollection = SAMPLE_COLLECTION
sample.study = STUDY
sample.study = STUDY

# Mapping properties
list.firstName.gandalf = Bilbo
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,38 @@
</bean>
</property>
</bean>

<bean id="mappedFlatFileReader" class="org.miabis.converter.batch.reader.TitleAwareFlatFileItemReader">
<property name="hasTitles" value="true"/>
<property name="strict" value="true"/>
<property name="delimiter">
<util:constant static-field="org.miabis.converter.batch.util.Util.DELIMITER_TAB" />
</property>
<property name="comments">
<list>
<value>#</value>
</list>
</property>
<property name="lineMapper">
<bean id="lineMapper" class="org.miabis.converter.transform.LineTokenizerAwareLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter">
<util:constant static-field="org.miabis.converter.batch.util.Util.DELIMITER_TAB" />
</property>
<property name="names">
<util:constant static-field="org.miabis.converter.batch.util.Util.COLUMNS" />
</property>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.miabis.converter.transform.TitleAwareFieldSetMapper">
<constructor-arg value="example.mapping.properties"/>
<property name="dbNames" value="${db.contact.information.names}" />
</bean>
</property>
</bean>
</property>
</bean>

</beans>

0 comments on commit 5d42fd1

Please sign in to comment.