Search in sources :

Example 1 with XMLImporter

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

the class WkfModelServiceImpl method importStandardBPM.

@Override
public void importStandardBPM() {
    List<App> appList = Beans.get(AppRepository.class).all().filter("self.active = true").fetch();
    if (CollectionUtils.isEmpty(appList)) {
        return;
    }
    String configFileName = "/data-import/import-wkf-models.xml";
    File configFile = null;
    try {
        configFile = File.createTempFile("config", ".xml");
        FileOutputStream fout = new FileOutputStream(configFile);
        InputStream inputStream = this.getClass().getResourceAsStream(configFileName);
        IOUtil.copyCompletely(inputStream, fout);
    } catch (Exception e) {
        TraceBackService.trace(e);
    }
    if (configFile == null) {
        return;
    }
    try {
        String dataFileName = "/data-wkf-models/input/";
        File tempDir = Files.createTempDir();
        File dataFile = new File(tempDir, "wkfModels.xml");
        XMLImporter importer = getXMLImpoter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
        for (App app : appList) {
            String fileName = dataFileName + app.getCode() + ".xml";
            InputStream dataInputStream = this.getClass().getResourceAsStream(fileName);
            if (dataInputStream == null) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(dataFile);
            IOUtil.copyCompletely(dataInputStream, fout);
            importer.run();
        }
    } catch (Exception e) {
        TraceBackService.trace(e);
    }
}
Also used : App(com.axelor.apps.base.db.App) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) AppRepository(com.axelor.apps.base.db.repo.AppRepository) XMLImporter(com.axelor.data.xml.XMLImporter) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) AxelorException(com.axelor.exception.AxelorException)

Example 2 with XMLImporter

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

the class ImporterXML method process.

@Override
protected ImportHistory process(String bind, String data, Map<String, Object> importContext) throws IOException {
    XMLImporter importer = new XMLImporter(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) XMLImporter(com.axelor.data.xml.XMLImporter)

Example 3 with XMLImporter

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

the class EbicsController method importEbicsUsers.

@Transactional
public void importEbicsUsers(ActionRequest request, ActionResponse response) {
    String config = "/data-import/import-ebics-user-config.xml";
    try {
        InputStream inputStream = this.getClass().getResourceAsStream(config);
        File configFile = File.createTempFile("config", ".xml");
        FileOutputStream fout = new FileOutputStream(configFile);
        IOUtil.copyCompletely(inputStream, fout);
        Path path = MetaFiles.getPath((String) ((Map) request.getContext().get("dataFile")).get("filePath"));
        File tempDir = Files.createTempDir();
        File importFile = new File(tempDir, "ebics-user.xml");
        Files.copy(path.toFile(), importFile);
        XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
        ImporterListener listner = new ImporterListener("EbicsUser");
        importer.addListener(listner);
        importer.run();
        FileUtils.forceDelete(configFile);
        FileUtils.forceDelete(tempDir);
        response.setValue("importLog", listner.getImportLog());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) ImporterListener(com.axelor.apps.base.service.imports.listener.ImporterListener) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) XMLImporter(com.axelor.data.xml.XMLImporter) IOException(java.io.IOException) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Map(java.util.Map) Transactional(com.google.inject.persist.Transactional)

Example 4 with XMLImporter

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

the class PrintTemplateController method importPrintTemplate.

public void importPrintTemplate(ActionRequest request, ActionResponse response) {
    String config = "/data-import/import-print-template-config.xml";
    try {
        InputStream inputStream = this.getClass().getResourceAsStream(config);
        File configFile = File.createTempFile("config", ".xml");
        FileOutputStream fout = new FileOutputStream(configFile);
        IOUtil.copyCompletely(inputStream, fout);
        Path path = MetaFiles.getPath((String) ((Map) request.getContext().get("dataFile")).get("filePath"));
        File tempDir = Files.createTempDir();
        File importFile = new File(tempDir, "print-template.xml");
        Files.copy(path.toFile(), importFile);
        XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
        ImporterListener listner = new ImporterListener("PrintTemplate");
        importer.addListener(listner);
        importer.run();
        FileUtils.forceDelete(configFile);
        FileUtils.forceDelete(tempDir);
        response.setValue("importLog", listner.getImportLog());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Path(java.nio.file.Path) ImporterListener(com.axelor.apps.base.service.imports.listener.ImporterListener) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) XMLImporter(com.axelor.data.xml.XMLImporter) File(java.io.File) Map(java.util.Map) AxelorException(com.axelor.exception.AxelorException)

Example 5 with XMLImporter

use of com.axelor.data.xml.XMLImporter 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)

Aggregations

XMLImporter (com.axelor.data.xml.XMLImporter)11 File (java.io.File)7 AxelorException (com.axelor.exception.AxelorException)5 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 Listener (com.axelor.data.Listener)4 Model (com.axelor.db.Model)4 IOException (java.io.IOException)4 ImporterListener (com.axelor.apps.base.service.imports.listener.ImporterListener)3 MetaFile (com.axelor.meta.db.MetaFile)3 Test (org.junit.jupiter.api.Test)3 WkfModel (com.axelor.apps.bpm.db.WkfModel)2 MetaModel (com.axelor.meta.db.MetaModel)2 Transactional (com.google.inject.persist.Transactional)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 App (com.axelor.apps.base.db.App)1 AppRepository (com.axelor.apps.base.db.repo.AppRepository)1 MetaFileRepository (com.axelor.meta.db.repo.MetaFileRepository)1 FileInputStream (java.io.FileInputStream)1