Search in sources :

Example 1 with Listener

use of com.axelor.data.Listener 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 2 with Listener

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

the class WkfModelServiceImpl method importWkfModels.

@Override
public String importWkfModels(MetaFile metaFile, boolean isTranslate, String sourceLanguage, String targetLanguage) throws AxelorException {
    if (metaFile == null) {
        return null;
    }
    String extension = Files.getFileExtension(metaFile.getFileName());
    if (extension == null || !extension.equals("xml")) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(ITranslation.INVALID_WKF_MODEL_XML));
    }
    try {
        InputStream inputStream = getClass().getResourceAsStream(IMPORT_CONFIG_PATH);
        File configFile = File.createTempFile("config", ".xml");
        FileOutputStream fout = new FileOutputStream(configFile);
        IOUtil.copyCompletely(inputStream, fout);
        File xmlFile = MetaFiles.getPath(metaFile).toFile();
        File tempDir = Files.createTempDir();
        File importFile = new File(tempDir, "wkfModels.xml");
        Files.copy(xmlFile, importFile);
        if (isTranslate) {
            importFile = this.translateNodeName(importFile, sourceLanguage, targetLanguage);
        }
        XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
        final StringBuilder log = new StringBuilder();
        Listener listner = new Listener() {

            @Override
            public void imported(Integer imported, Integer total) {
            }

            @Override
            public void imported(Model arg0) {
            }

            @Override
            public void handle(Model arg0, Exception err) {
                log.append("Error in import: " + err.getStackTrace().toString());
            }
        };
        importer.addListener(listner);
        importer.run();
        FileUtils.forceDelete(configFile);
        FileUtils.forceDelete(tempDir);
        FileUtils.forceDelete(xmlFile);
        MetaFileRepository metaFileRepository = Beans.get(MetaFileRepository.class);
        metaFileRepository.remove(metaFile);
        return log.toString();
    } catch (Exception e) {
        return e.getMessage();
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) MetaFileRepository(com.axelor.meta.db.repo.MetaFileRepository) Listener(com.axelor.data.Listener) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) WkfModel(com.axelor.apps.bpm.db.WkfModel) MetaModel(com.axelor.meta.db.MetaModel) Model(com.axelor.db.Model) XMLImporter(com.axelor.data.xml.XMLImporter) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) AxelorException(com.axelor.exception.AxelorException)

Example 3 with Listener

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

the class WkfModelServiceImpl method getXMLImpoter.

protected XMLImporter getXMLImpoter(String configFile, String dataFile) {
    XMLImporter importer = new XMLImporter(configFile, dataFile);
    final StringBuilder log = new StringBuilder();
    Listener listner = new Listener() {

        @Override
        public void imported(Integer imported, Integer total) {
        }

        @Override
        public void imported(Model arg0) {
        }

        @Override
        public void handle(Model arg0, Exception err) {
            log.append("Error in import: " + err.getStackTrace().toString());
        }
    };
    importer.addListener(listner);
    return importer;
}
Also used : Listener(com.axelor.data.Listener) WkfModel(com.axelor.apps.bpm.db.WkfModel) MetaModel(com.axelor.meta.db.MetaModel) Model(com.axelor.db.Model) XMLImporter(com.axelor.data.xml.XMLImporter) AxelorException(com.axelor.exception.AxelorException)

Example 4 with Listener

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

the class ConfiguratorCreatorImportServiceImpl method importConfiguratorCreators.

@Transactional(rollbackOn = { Exception.class })
@Override
public String importConfiguratorCreators(InputStream xmlInputStream, String configFilePath) throws IOException {
    InputStream inputStream = this.getClass().getResourceAsStream(configFilePath);
    File configFile = File.createTempFile("config", ".xml");
    FileOutputStream fout = new FileOutputStream(configFile);
    IOUtil.copyCompletely(inputStream, fout);
    File tempDir = Files.createTempDir();
    File importFile = new File(tempDir, "configurator-creator.xml");
    FileUtils.copyInputStreamToFile(xmlInputStream, importFile);
    XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
    final StringBuilder importLog = new StringBuilder();
    Listener listener = new Listener() {

        @Override
        public void imported(Integer imported, Integer total) {
            importLog.append("Total records: " + total + ", Total imported: " + total);
        }

        @Override
        public void imported(Model arg0) {
            try {
                completeAfterImport(arg0);
            } catch (AxelorException e) {
                importLog.append("Error in import: " + Arrays.toString(e.getStackTrace()));
            }
        }

        @Override
        public void handle(Model arg0, Exception err) {
            importLog.append("Error in import: " + Arrays.toString(err.getStackTrace()));
        }
    };
    importer.addListener(listener);
    importer.run();
    FileUtils.forceDelete(configFile);
    FileUtils.forceDelete(tempDir);
    return importLog.toString();
}
Also used : AxelorException(com.axelor.exception.AxelorException) Listener(com.axelor.data.Listener) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) Model(com.axelor.db.Model) XMLImporter(com.axelor.data.xml.XMLImporter) File(java.io.File) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) Transactional(com.google.inject.persist.Transactional)

Example 5 with Listener

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

the class AppLoaderImportServiceImpl method importApp.

protected File importApp(AppLoader appLoader, File dataDir) throws IOException, FileNotFoundException {
    File logFile = appLoader.getImportLog() != null ? MetaFiles.getPath(appLoader.getImportLog()).toFile() : MetaFiles.createTempFile("import-", "log").toFile();
    PrintWriter pw = new PrintWriter(logFile);
    for (File confiFile : getAppImportConfigFiles(dataDir)) {
        XMLImporter xmlImporter = new XMLImporter(confiFile.getAbsolutePath(), dataDir.getAbsolutePath());
        xmlImporter.setContext(getImportContext(appLoader));
        xmlImporter.addListener(new Listener() {

            @Override
            public void imported(Integer total, Integer success) {
            }

            @Override
            public void imported(Model bean) {
            }

            @Override
            public void handle(Model bean, Exception e) {
                pw.write("Error Importing: " + bean);
                e.printStackTrace(pw);
            }
        });
        xmlImporter.run();
    }
    pw.flush();
    pw.close();
    return logFile;
}
Also used : Listener(com.axelor.data.Listener) Model(com.axelor.db.Model) XMLImporter(com.axelor.data.xml.XMLImporter) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Aggregations

Listener (com.axelor.data.Listener)5 Model (com.axelor.db.Model)5 XMLImporter (com.axelor.data.xml.XMLImporter)4 File (java.io.File)4 AxelorException (com.axelor.exception.AxelorException)3 IOException (java.io.IOException)3 WkfModel (com.axelor.apps.bpm.db.WkfModel)2 MetaFile (com.axelor.meta.db.MetaFile)2 MetaModel (com.axelor.meta.db.MetaModel)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 CSVImporter (com.axelor.data.csv.CSVImporter)1 MetaFileRepository (com.axelor.meta.db.repo.MetaFileRepository)1 Transactional (com.google.inject.persist.Transactional)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Logger (org.slf4j.Logger)1