Object CSV Mapper for Java
QuickOCM - Parse and convert your CSV files to your models in your java application.
<dependency> <groupId>io.github.mkhanal</groupId> <artifactId>quickocm</artifactId> <version>1.0</version> </dependency<
Assume your CSV, (first line are headers)
user name , age , Date Of JOiNinG , Referrer Code , Street Name , House No , City
phillips , 23 , 19/09/1999 , referrer1 , south street , 12 , Timbak 1
sri krishna , 25 , 20/12/2012 , referrer2 , north street , 24 , Timbak 2
Jaquiline , 44 , , referrer3 , east street , 36 , Timbak 3
Who so ever , 20000 , 22/02/2010 , referrer4 , west street , 48 , Timbak 4
To parse this, you need :
public class User {
//Getter, Setter, Default Constructor
@ImportField(mandatory = true, name = "User Name", type = "String")
private String name;
@ImportField(mandatory = true, type = "int") //default -> header name = field name
private Integer age;
@ImportField(type = "Date", name = "date of joining") //case insensitive headers
private Date joiningDate;
@ImportField(type = "String", name = "Referrer Code", nested = "code")
private Referrer referrer;
@ImportFields(importFields = {
@ImportField(type = "String", name = "Street Name", nested = "streetName"),
@ImportField(type = "int", name = "House No", nested = "houseNo"),
@ImportField(type = "String", name = "City", nested = "city")})
private Address completeAddress;
private String createdBy; //logged in user not present in csv file, can be pulled from session
}
public class Referrer {
//Getter, Setter, Default Constructor
private String code;
}
public class Address {
//Getter, Setter, Default Constructor
private Integer houseNo;
private String streetName;
private String city;
}
For each row in CSV, your handler will be invoked. So create a handler
public class UserRecordHandler implements RecordHandler<User> {
//Getter
private List<User> uploadedUsers = new ArrayList<User>();
@Override
public void execute(User user, int i, Map map) {
user.setCreatedBy((String) map.get("loggedInUser"));
uploadedUsers.add(user);
}
}
This is it. The below code shows a simple test
public class UserRecordHandlerTest {
private CsvParser<User> csvParser;
@Before
public void setUp() throws Exception {
Map<String, String> supplementaryInfo = new HashMap<String, String>() {{
put("loggedInUser", "User123");
}};
csvParser = new CsvParser<User>(supplementaryInfo);
}
@Test
public void shouldParseCsvFileAndGetAllUsers() throws Exception {
String csvContent =
"User Name , Age , Date Of JOiNinG, Referrer Code, Street Name , House No, City\n" +
"phillips , 23 , 19/09/1999 , referrer1 , south street, 12 , Timbak 1\n" +
"sri krishna, 25 , 20/12/2012 , referrer2 , north street, 24 , Timbak 2\n" +
"Jaquiline , 44 , , referrer3 , east street , 36 , Timbak 3\n" +
"Who so ever, 20000, 22/02/2010 , referrer4 , west street , 48 , Timbak 4";
InputStream inputStream = new ByteInputStream(csvContent.getBytes(), csvContent.length());
UserRecordHandler userRecordHandler = new UserRecordHandler();
csvParser.process(inputStream, User.class, userRecordHandler);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
List<User> uploadedUsers = userRecordHandler.getUploadedUsers();
assertEquals(4, uploadedUsers.size());
assertThat(uploadedUsers.get(0), isSameUser(new User(
"phillips", 23, format.parse("19/09/1999"), "referrer1", "south street", 12, "Timbak 1", "User123")));
assertThat(uploadedUsers.get(1), isSameUser(new User(
"sri krishna", 25, format.parse("20/12/2012"), "referrer2", "north street", 24, "Timbak 2", "User123")));
assertThat(uploadedUsers.get(2), isSameUser(new User(
"Jaquiline", 44, null, "referrer3", "east street", 36, "Timbak 3", "User123")));
assertThat(uploadedUsers.get(3), isSameUser(new User(
"Who so ever", 20000, format.parse("22/02/2010"), "referrer4", "west street", 48, "Timbak 4", "User123")));
}
}
The code shows that for each record, how the record handler was executed with the Object in question. Also note how extra information like audit info was passed through supplementaryInfo map