Integration
The data import feature can be easily integrated in the business application.
-
com.axelor.data.Importer
- the importer api interface -
com.axelor.data.ImportTask
- abstract class to configure import tasks -
com.axelor.data.Listener
- data import event listener -
com.axelor.data.Launcher
- base class to create command line task launcher -
com.axelor.data.csv.CSVImporter
- csv data importer -
com.axelor.data.xml.XMLImporter
- xml data importer
The ImportTask
class can be used to advanced usages like providing input data
from arbitrary data streams.
Here is an example:
package com.axelor.data;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.axelor.data.csv.CSVImporter;
import com.axelor.db.Model;
import com.axelor.test.GuiceModules;
import com.axelor.test.GuiceRunner;
import com.google.inject.Injector;
@RunWith(GuiceRunner.class)
@GuiceModules(DataModule.class)
public class CSVTest {
private Logger log = LoggerFactory.getLogger(getClass());
@Inject private Injector injector;
@Test
public void testCsv() throws ClassNotFoundException {
final Importer importer = new CSVImporter(
injector, "/path/to/csv-config.xml"); (1)
final List<Model> records = new ArrayList<>();
final Map<String, Object> context = new HashMap<>();
context.put("CUSTOMER_PHONE", "+3326253225");
importer.addListener(new Listener() { (2)
@Override
public void imported(Model bean) {
log.info("Bean saved : {}(id={})",
bean.getClass().getSimpleName(),
bean.getId());
records.add(bean);
}
@Override
public void imported(Integer total, Integer success) {
log.info("Record (total): " + total);
log.info("Record (success): " + success);
}
@Override
public void handle(Model bean, Exception e) {
}
});
importer.runTask(new ImportTask() { (3)
@Override
public void configure() throws IOException {
// provide input data
input("customers", new File("customers.csv")); (4)
input("sales", new File("sales.csv"));
}
@Override
public boolean handle(ImportException exception) { (5)
log.error("Import error : " + exception);
return false;
}
@Override
public boolean handle(IOException e) { (6)
log.error("IOException error : " + e);
return true;
}
});
}
}
1 | create a csv importer (subject to change in v4) |
2 | add a data import event listener (optional, for statistics) |
3 | run a data import task with the given task instance |
4 | provide data input streams |
5 | handle data import error, continue data import if returns true |
6 | handle input errors, continue data import if returns true |
Same way, you can use XMLImporter
class.