The IO add-on gives simple way to export and import data in multiple formats.

Dependencies

The IO add-on provides a module for importing and export CSV files with SuperCSV:

<dependency>
    <groupId>org.seedstack.addons.io</groupId>
    <artifactId>io-supercsv</artifactId>
</dependency>
Show version
dependencies {
    compile("org.seedstack.addons.io:io-supercsv:3.0.0")
}

It also provides a module for exporting with JasperReports:

<dependency>
    <groupId>org.seedstack.addons.io</groupId>
    <artifactId>io-jasper</artifactId>
</dependency>
Show version
dependencies {
    compile("org.seedstack.addons.io:io-jasper:3.0.0")
}

CSV files

Writing

To export a POJO to a CSV file, make sure the io-supercsv module is in your classpath. We will export the following POJO:

public class CustomerBean {
    private String firstName;
    private String lastName;
    private int age;
}

Add a customerbean.csv.properties file in META-INF/templates directory:

columns=firstName,lastName,age
firstName.name=First name
lastName.name=Last name
age.name=Age
age.type=int

In your code, inject a renderer:

public class SomeClass {
	@Render("customerbean")
	private Renderer renderer;
	
	private List<CustomerBean> customers;
	
	public void exportCustomers(OuputStream os) {
	    renderer.render(os, customers);
	}
}

Or programmatically obtain the required renderer:

public class SomeClass {
	@Inject
	private Renderers renderers;

	public void exportCustomers(OuputStream os, String name) {
		Renderer renderer = renderers.getRendererFor(name);
	    renderer.render(os, customers);
	}
}

Reading

To import the POJO, the configuration is the same as export configuration. Then inject a Parser with the @Parse annotation:

public class SomeClass {
	@Parse("customerbean")
	private Parser<CustomerBean> parser;
	
	private List<CustomerBean> customers;
	
	public void importCustomers(InputStream is) {
	    customers = parser.parse(is, CustomerBean.class);
	} 
}

Or use Parsers to programatically obtain the required parser:

public class SomeClass {
	@Inject
	private Parsers parsers;

	public void importCustomers(InputStream is, String name) {
		Parser parser = parsers.getParserFor(name);
	    renderer.render(os, customers);
		customers = parser.parse(is, CustomerBean.class);
	}
}

JasperReports

JasperReports can generate multiple file formats from a common JRXML template. One of the most useful format is PDF, as in the following example:

public class SomeClass {
	@Render("pdftemplate")
	private Renderer renderer;
	private List<CustomerBean> customers;
	
	public void exportCustomers(OuputStream os) {
	    renderer.render(os, customers, "application/pdf", parameters);
	}
}

You can pass any Jasper parameter (like SUBREPORT_DIR) using the fourth parameter or render()which is a Map<String, Object>.

The Jasper module does not provide a parser.