Search in sources :

Example 1 with CSVImporter

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

the class DataImportServiceImpl method importData.

private MetaFile importData(List<CSVInput> inputs) throws IOException {
    if (CollectionUtils.isEmpty(inputs)) {
        return null;
    }
    CSVConfig config = new CSVConfig();
    config.setInputs(inputs);
    if (!CollectionUtils.isEmpty(adapterMap.values())) {
        config.getAdapters().addAll(adapterMap.values());
    }
    CSVImporter importer = new CSVImporter(config, dataDir.getAbsolutePath());
    ImporterListener listener = new ImporterListener("importData");
    importer.addListener(listener);
    importer.setContext(importContext);
    importer.run();
    if (!listener.isImported()) {
        MetaFile logFile = this.createImportLogFile(listener);
        return logFile;
    }
    return null;
}
Also used : ImporterListener(com.axelor.apps.base.service.imports.listener.ImporterListener) MetaFile(com.axelor.meta.db.MetaFile) CSVImporter(com.axelor.data.csv.CSVImporter) CSVConfig(com.axelor.data.csv.CSVConfig)

Example 2 with CSVImporter

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

the class DataBackupRestoreService method restore.

/* Restore the Data using provided zip File and prepare Log File and Return it*/
public File restore(MetaFile zipedBackupFile) {
    Logger LOG = LoggerFactory.getLogger(getClass());
    File tempDir = Files.createTempDir();
    String dirPath = tempDir.getAbsolutePath();
    StringBuilder sb = new StringBuilder();
    try {
        unZip(zipedBackupFile, dirPath);
        String configFName = tempDir.getAbsolutePath() + File.separator + DataBackupServiceImpl.CONFIG_FILE_NAME;
        CSVImporter csvImporter = new CSVImporter(configFName, tempDir.getAbsolutePath());
        csvImporter.addListener(new Listener() {

            String modelName;

            StringBuilder sb1 = new StringBuilder();

            @Override
            public void handle(Model bean, Exception e) {
                if (e.getMessage() != null && !e.getMessage().equals("null")) {
                    if (bean != null) {
                        sb1.append(bean.getClass().getSimpleName() + " : \n" + e.getMessage() + "\n\n");
                    } else {
                        sb1.append(e.getMessage() + "\n\n");
                    }
                }
            }

            @Override
            public void imported(Model model) {
                modelName = model.getClass().getSimpleName();
            }

            @Override
            public void imported(Integer total, Integer count) {
                String str = "", strError = "";
                if (!StringUtils.isBlank(sb1)) {
                    strError = "Errors : \n" + sb1.toString();
                }
                str = "Total Records :  {" + total + "} - Success Records :  {" + count + "}  \n";
                if (total != 0 && count != 0) {
                    sb.append(modelName + " : \n");
                }
                sb.append(strError).append(str + "-----------------------------------------\n");
                sb1.setLength(0);
            }
        });
        csvImporter.run();
        LOG.info("Data Restore Completed");
        FileUtils.cleanDirectory(new File(tempDir.getAbsolutePath()));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmSS");
        String logFileName = "DataBackupLog_" + LocalDateTime.now().format(formatter) + ".log";
        File file = new File(tempDir.getAbsolutePath(), logFileName);
        PrintWriter pw = new PrintWriter(file);
        pw.write(sb.toString());
        pw.close();
        return file;
    } catch (IOException e) {
        TraceBackService.trace(e);
        return null;
    }
}
Also used : Listener(com.axelor.data.Listener) IOException(java.io.IOException) Logger(org.slf4j.Logger) CSVImporter(com.axelor.data.csv.CSVImporter) IOException(java.io.IOException) Model(com.axelor.db.Model) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) DateTimeFormatter(java.time.format.DateTimeFormatter) PrintWriter(java.io.PrintWriter)

Example 3 with CSVImporter

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

the class AppServiceImpl method refreshApp.

@Override
public void refreshApp() throws IOException {
    File dataDir = Files.createTempDirectory(null).toFile();
    File imgDir = new File(dataDir, "img");
    imgDir.mkdir();
    CSVConfig csvConfig = new CSVConfig();
    csvConfig.setInputs(new ArrayList<>());
    List<MetaModel> metaModels = metaModelRepo.all().filter("self.name != 'App' and self.name like 'App%' and self.packageName =  ?1", App.class.getPackage().getName()).fetch();
    final List<String> appFieldTargetList = Stream.of(JPA.fields(App.class)).filter(p -> p.getType() == PropertyType.ONE_TO_ONE).filter(p -> p.getName().startsWith("app")).map(Property::getTarget).map(Class::getName).collect(Collectors.toList());
    log.debug("Total app models: {}", metaModels.size());
    for (MetaModel metaModel : metaModels) {
        if (!appFieldTargetList.contains(metaModel.getFullName())) {
            log.debug("Not a App class : {}", metaModel.getName());
            continue;
        }
        Class<?> klass;
        try {
            klass = Class.forName(metaModel.getFullName());
        } catch (ClassNotFoundException e) {
            continue;
        }
        Object obj = null;
        Query query = JPA.em().createQuery("SELECT id FROM " + metaModel.getName());
        try {
            obj = query.setMaxResults(1).getSingleResult();
        } catch (Exception ex) {
        }
        if (obj != null) {
            continue;
        }
        log.debug("App without app record: {}", metaModel.getName());
        String csvName = "base_" + inflector.camelize(klass.getSimpleName(), true) + ".csv";
        String pngName = inflector.dasherize(klass.getSimpleName()) + ".png";
        CSVInput input = new CSVInput();
        input.setFileName(csvName);
        input.setTypeName(App.class.getName());
        input.setCallable("com.axelor.csv.script.ImportApp:importApp");
        input.setSearch("self.code =:code");
        input.setSeparator(';');
        csvConfig.getInputs().add(input);
        CSVInput appInput = new CSVInput();
        appInput.setFileName(csvName);
        appInput.setTypeName(klass.getName());
        appInput.setSearch("self.app.code =:code");
        appInput.setSeparator(';');
        CSVBind appBind = new CSVBind();
        appBind.setColumn("code");
        appBind.setField("app");
        appBind.setSearch("self.code = :code");
        appInput.getBindings().add(appBind);
        csvConfig.getInputs().add(appInput);
        InputStream stream = klass.getResourceAsStream("/data-init/input/" + csvName);
        copyStream(stream, new File(dataDir, csvName));
        stream = klass.getResourceAsStream("/data-init/input/img/" + pngName);
        copyStream(stream, new File(imgDir, pngName));
    }
    if (!csvConfig.getInputs().isEmpty()) {
        CSVImporter importer = new CSVImporter(csvConfig, dataDir.getAbsolutePath());
        importer.run();
    }
}
Also used : App(com.axelor.apps.base.db.App) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Arrays(java.util.Arrays) FileUtils(com.axelor.common.FileUtils) URL(java.net.URL) MetaScanner(com.axelor.meta.MetaScanner) Inject(com.google.inject.Inject) CSVInput(com.axelor.data.csv.CSVInput) LoggerFactory(org.slf4j.LoggerFactory) Property(com.axelor.db.mapper.Property) Scanner(java.util.Scanner) AppRepository(com.axelor.apps.base.db.repo.AppRepository) Mapper(com.axelor.db.mapper.Mapper) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) AxelorException(com.axelor.exception.AxelorException) App(com.axelor.apps.base.db.App) I18n(com.axelor.i18n.I18n) Inflector(com.axelor.common.Inflector) XMLImporter(com.axelor.data.xml.XMLImporter) PropertyType(com.axelor.db.mapper.PropertyType) JPA(com.axelor.db.JPA) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) Files(java.nio.file.Files) Collection(java.util.Collection) CSVImporter(com.axelor.data.csv.CSVImporter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) IExceptionMessages(com.axelor.apps.base.exceptions.IExceptionMessages) CSVBind(com.axelor.data.csv.CSVBind) CSVConfig(com.axelor.data.csv.CSVConfig) AppSettings(com.axelor.app.AppSettings) ByteStreams(com.google.common.io.ByteStreams) Pattern(java.util.regex.Pattern) Importer(com.axelor.data.Importer) Singleton(com.google.inject.Singleton) InputStream(java.io.InputStream) Query(javax.persistence.Query) InputStream(java.io.InputStream) CSVInput(com.axelor.data.csv.CSVInput) CSVImporter(com.axelor.data.csv.CSVImporter) CSVConfig(com.axelor.data.csv.CSVConfig) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) MetaModel(com.axelor.meta.db.MetaModel) CSVBind(com.axelor.data.csv.CSVBind) File(java.io.File) Property(com.axelor.db.mapper.Property)

Example 4 with CSVImporter

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

the class DataTest method test.

@Test
public void test() {
    CSVImporter importer = new CSVImporter("src/main/resources/data-demo/input-config.xml", "src/main/resources/data-demo/input");
    importer.run();
}
Also used : CSVImporter(com.axelor.data.csv.CSVImporter) Test(org.junit.jupiter.api.Test)

Example 5 with CSVImporter

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

the class CSVDataTest method testData.

@Test
public void testData() throws IOException {
    Importer importer = new CSVImporter("data/csv-config-types.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)

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