Search in sources :

Example 11 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class InventoryController method exportInventory.

public void exportInventory(ActionRequest request, ActionResponse response) {
    try {
        Inventory inventory = request.getContext().asType(Inventory.class);
        inventory = Beans.get(InventoryRepository.class).find(inventory.getId());
        String name = I18n.get("Inventory") + " " + inventory.getInventorySeq();
        MetaFile metaFile = Beans.get(InventoryService.class).exportInventoryAsCSV(inventory);
        response.setView(ActionView.define(name).add("html", "ws/rest/com.axelor.meta.db.MetaFile/" + metaFile.getId() + "/content/download?v=" + metaFile.getVersion()).map());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : InventoryService(com.axelor.apps.stock.service.InventoryService) MetaFile(com.axelor.meta.db.MetaFile) Inventory(com.axelor.apps.stock.db.Inventory) BirtException(org.eclipse.birt.core.exception.BirtException) NoResultException(javax.persistence.NoResultException) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException)

Example 12 with MetaFile

use of com.axelor.meta.db.MetaFile 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 13 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class DataBackupRestoreService method unZip.

private boolean unZip(MetaFile zipMetaFile, String destinationDirectoryPath) throws IOException {
    File zipFile = MetaFiles.getPath(zipMetaFile).toFile();
    try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)))) {
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;
        while ((ze = zis.getNextEntry()) != null) {
            try (FileOutputStream fout = new FileOutputStream(new File(destinationDirectoryPath, ze.getName()))) {
                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }
            }
            zis.closeEntry();
        }
        return true;
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream)

Example 14 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class DataBackupServiceImpl method startBackup.

@Transactional(rollbackOn = { Exception.class })
protected void startBackup(DataBackup dataBackup) throws Exception {
    final AuditableRunner runner = Beans.get(AuditableRunner.class);
    final Callable<Boolean> job = new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            Logger LOG = LoggerFactory.getLogger(getClass());
            DataBackup obj = Beans.get(DataBackupRepository.class).find(dataBackup.getId());
            obj = createService.create(obj);
            MetaFile logFile = obj.getLogMetaFile();
            MetaFile zipFile = obj.getBackupMetaFile();
            int status = obj.getStatusSelect();
            obj = dataBackupRepository.find(obj.getId());
            if (status != DataBackupRepository.DATA_BACKUP_STATUS_ERROR) {
                obj.setBackupMetaFile(zipFile);
                obj.setStatusSelect(DataBackupRepository.DATA_BACKUP_STATUS_CREATED);
            } else {
                obj.setStatusSelect(DataBackupRepository.DATA_BACKUP_STATUS_ERROR);
            }
            obj.setLogMetaFile(logFile);
            dataBackupRepository.save(obj);
            LOG.info("Data BackUp Saved");
            return true;
        }
    };
    runner.run(job);
}
Also used : DataBackupRepository(com.axelor.apps.base.db.repo.DataBackupRepository) DataBackup(com.axelor.apps.base.db.DataBackup) MetaFile(com.axelor.meta.db.MetaFile) Logger(org.slf4j.Logger) AuditableRunner(com.axelor.auth.AuditableRunner) Callable(java.util.concurrent.Callable) Transactional(com.google.inject.persist.Transactional)

Example 15 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class AppController method generateAccessTemplate.

public void generateAccessTemplate(ActionRequest request, ActionResponse response) throws AxelorException {
    MetaFile accesssFile = Beans.get(AccessTemplateService.class).generateTemplate();
    if (accesssFile == null) {
        return;
    }
    response.setView(ActionView.define(I18n.get("Export file")).model(App.class.getName()).add("html", "ws/rest/com.axelor.meta.db.MetaFile/" + accesssFile.getId() + "/content/download?v=" + accesssFile.getVersion()).param("download", "true").map());
}
Also used : App(com.axelor.apps.base.db.App) MetaFile(com.axelor.meta.db.MetaFile) AccessTemplateService(com.axelor.apps.base.service.app.AccessTemplateService)

Aggregations

MetaFile (com.axelor.meta.db.MetaFile)87 File (java.io.File)50 FileInputStream (java.io.FileInputStream)25 IOException (java.io.IOException)24 AxelorException (com.axelor.exception.AxelorException)21 MetaFiles (com.axelor.meta.MetaFiles)18 Transactional (com.google.inject.persist.Transactional)17 ArrayList (java.util.ArrayList)13 Path (java.nio.file.Path)12 InputStream (java.io.InputStream)10 ZipFile (java.util.zip.ZipFile)9 FileOutputStream (java.io.FileOutputStream)8 MetaFileRepository (com.axelor.meta.db.repo.MetaFileRepository)7 ImportHistory (com.axelor.apps.base.db.ImportHistory)6 DMSFile (com.axelor.dms.db.DMSFile)6 HashMap (java.util.HashMap)6 ZipEntry (java.util.zip.ZipEntry)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ZipInputStream (java.util.zip.ZipInputStream)4 App (com.axelor.apps.base.db.App)3