Search in sources :

Example 6 with CSVImporter

use of com.axelor.data.csv.CSVImporter in project open-platform-demo by axelor.

the class CSVDataTest method testMulti.

@Test
public void testMulti() throws IOException {
    Importer importer = new CSVImporter("data/csv-multi-config.xml");
    importer.run(new ImportTask() {

        @Override
        public void configure() throws IOException {
            input("[sale.order]", new File("data/csv-multi/so1.csv"));
            input("[sale.order]", new File("data/csv-multi/so2.csv"));
        }
    });
}
Also used : IOException(java.io.IOException) CSVImporter(com.axelor.data.csv.CSVImporter) File(java.io.File) CSVImporter(com.axelor.data.csv.CSVImporter) Test(org.junit.jupiter.api.Test)

Example 7 with CSVImporter

use of com.axelor.data.csv.CSVImporter in project open-platform-demo by axelor.

the class CSVDataTest method testDefault.

@Test
public void testDefault() throws IOException {
    Importer importer = new CSVImporter("data/csv-config.xml", "data/csv");
    importer.run();
}
Also used : CSVImporter(com.axelor.data.csv.CSVImporter) CSVImporter(com.axelor.data.csv.CSVImporter) Test(org.junit.jupiter.api.Test)

Example 8 with CSVImporter

use of com.axelor.data.csv.CSVImporter in project open-platform-demo by axelor.

the class CSVImportTest method test.

@Test
public void test() throws ClassNotFoundException {
    final List<Model> records = new ArrayList<>();
    CSVImporter importer = new CSVImporter("data/csv-multi-config.xml");
    Map<String, Object> context = new HashMap<>();
    context.put("CUSTOMER_PHONE", "+3326253225");
    importer.setContext(context);
    importer.addListener(new Listener() {

        @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.run(new ImportTask() {

        @Override
        public void configure() throws IOException {
            input("[sale.order]", new File("data/csv-multi/so1.csv"));
            input("[sale.order]", new File("data/csv-multi/so2.csv"));
        }

        @Override
        public boolean handle(ImportException exception) {
            log.error("Import error : " + exception);
            return false;
        }

        @Override
        public boolean handle(IOException e) {
            log.error("IOException error : " + e);
            return true;
        }
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CSVImporter(com.axelor.data.csv.CSVImporter) IOException(java.io.IOException) Model(com.axelor.db.Model) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 9 with CSVImporter

use of com.axelor.data.csv.CSVImporter in project axelor-open-suite by axelor.

the class UnitCostCalculationServiceImpl method importUnitCostCalc.

@Override
public void importUnitCostCalc(MetaFile dataFile, UnitCostCalculation unitCostCalculation) throws IOException {
    File tempDir = Files.createTempDir();
    File csvFile = new File(tempDir, "unitcostcalc.csv");
    Files.copy(MetaFiles.getPath(dataFile).toFile(), csvFile);
    File configXmlFile = this.getConfigXmlFile();
    CSVImporter csvImporter = new CSVImporter(configXmlFile.getAbsolutePath(), tempDir.getAbsolutePath());
    Map<String, Object> context = new HashMap<>();
    context.put("_unitCostCalculation", unitCostCalculation.getId());
    csvImporter.setContext(context);
    csvImporter.run();
}
Also used : HashMap(java.util.HashMap) DMSFile(com.axelor.dms.db.DMSFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) CSVImporter(com.axelor.data.csv.CSVImporter)

Example 10 with CSVImporter

use of com.axelor.data.csv.CSVImporter in project axelor-open-suite by axelor.

the class ImporterCSV method process.

@Override
protected ImportHistory process(String bind, String data, Map<String, Object> importContext) throws IOException {
    CSVImporter importer = new CSVImporter(bind, data);
    ImporterListener listener = new ImporterListener(getConfiguration().getName());
    importer.addListener(listener);
    importer.setContext(importContext);
    importer.run();
    return addHistory(listener);
}
Also used : ImporterListener(com.axelor.apps.base.service.imports.listener.ImporterListener) CSVImporter(com.axelor.data.csv.CSVImporter)

Aggregations

CSVImporter (com.axelor.data.csv.CSVImporter)10 File (java.io.File)5 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)4 Model (com.axelor.db.Model)3 MetaFile (com.axelor.meta.db.MetaFile)3 ImporterListener (com.axelor.apps.base.service.imports.listener.ImporterListener)2 CSVConfig (com.axelor.data.csv.CSVConfig)2 ArrayList (java.util.ArrayList)2 Logger (org.slf4j.Logger)2 AppSettings (com.axelor.app.AppSettings)1 App (com.axelor.apps.base.db.App)1 AppRepository (com.axelor.apps.base.db.repo.AppRepository)1 IExceptionMessages (com.axelor.apps.base.exceptions.IExceptionMessages)1 FileUtils (com.axelor.common.FileUtils)1 Inflector (com.axelor.common.Inflector)1 Importer (com.axelor.data.Importer)1 Listener (com.axelor.data.Listener)1 CSVBind (com.axelor.data.csv.CSVBind)1 CSVInput (com.axelor.data.csv.CSVInput)1