Search in sources :

Example 31 with MetaFile

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

the class ImportCityServiceImpl method extractCityZip.

protected MetaFile extractCityZip(MetaFile dataFile) throws AxelorException, IOException {
    ZipEntry entry;
    MetaFile metaFile;
    File txtFile = File.createTempFile("city", ".txt");
    String requiredFileName = dataFile.getFileName().replace(".zip", ".txt");
    byte[] buffer = new byte[1024];
    int len;
    boolean txtFileFound = false;
    try (ZipInputStream zipFileInStream = new ZipInputStream(new FileInputStream(MetaFiles.getPath(dataFile).toFile()))) {
        while ((entry = zipFileInStream.getNextEntry()) != null) {
            if (entry.getName().equals(requiredFileName)) {
                try (FileOutputStream fos = new FileOutputStream(txtFile)) {
                    while ((len = zipFileInStream.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                }
                txtFileFound = true;
                break;
            }
        }
        if (!txtFileFound) {
            printWriter.append(String.format(I18n.get(IExceptionMessage.NO_TEXT_FILE_FOUND), requiredFileName, dataFile.getFileName()) + "\n");
        }
    }
    metaFile = metaFiles.upload(txtFile);
    FileUtils.forceDelete(txtFile);
    return metaFile;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) MetaFile(com.axelor.meta.db.MetaFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream)

Example 32 with MetaFile

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

the class ImportCityServiceImpl method importFromGeonamesManualConfig.

/**
 * Imports cities from a custom Geonames file. This is useful for the countries not present in the
 * predefined list.
 *
 * @param map
 * @param typeSelect
 * @return
 */
public Map<String, Object> importFromGeonamesManualConfig(Map<String, Object> map, String typeSelect) {
    List<ImportHistory> importHistoryList = new ArrayList<>();
    Map<String, Object> importCityMap = new HashMap<>();
    try {
        File tempDir = Files.createTempDir();
        errorFile = new File(tempDir.getAbsolutePath(), "Error-File.txt");
        printWriter = new PrintWriter(errorFile);
        if (map != null) {
            MetaFile dataFile = metaFileRepo.find(Long.parseLong(map.get("id").toString()));
            importHistoryList.add(this.importCity(typeSelect + "-dump", dataFile));
        }
    } catch (Exception e) {
        printWriter.append(e.getLocalizedMessage() + " at " + e.getStackTrace()[0] + "\n");
    }
    printWriter.close();
    importCityMap.put("importHistoryList", importHistoryList);
    importCityMap.put("errorFile", this.getMetaFile(errorFile));
    return importCityMap;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetaFile(com.axelor.meta.db.MetaFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ImportHistory(com.axelor.apps.base.db.ImportHistory) PrintWriter(java.io.PrintWriter)

Example 33 with MetaFile

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

the class ImportDemoDataServiceImpl method importFileData.

private MetaFile importFileData(File dataFile, File configFile, String dataFileName, String configFileName) throws IOException, AxelorException {
    ImportConfiguration config = new ImportConfiguration();
    MetaFile configMetaFile = metaFiles.upload(new FileInputStream(configFile), configFileName + ".xml");
    MetaFile dataMetaFile = metaFiles.upload(new FileInputStream(dataFile), dataFileName + ".csv");
    config.setBindMetaFile(configMetaFile);
    config.setDataMetaFile(dataMetaFile);
    config.setTypeSelect("csv");
    ImportHistory importHistory = factoryImporter.createImporter(config).run();
    MetaFiles.getPath(configMetaFile).toFile().delete();
    MetaFiles.getPath(dataMetaFile).toFile().delete();
    return importHistory.getLogMetaFile();
}
Also used : ImportConfiguration(com.axelor.apps.base.db.ImportConfiguration) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream) ImportHistory(com.axelor.apps.base.db.ImportHistory)

Example 34 with MetaFile

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

the class BatchEmploymentContractExport method employmentContractExportSilae.

@Transactional(rollbackOn = { Exception.class })
public MetaFile employmentContractExportSilae(List<EmploymentContract> employmentContractList) throws IOException {
    List<String[]> list = new ArrayList<>();
    for (EmploymentContract employmentContract : employmentContractList) {
        Beans.get(EmploymentContractService.class).employmentContractExportSilae(employmentContract, list);
        total++;
        incrementDone();
    }
    File tempFile = MetaFiles.createTempFile(Beans.get(EmploymentContractService.class).employmentContractExportName(), ".csv").toFile();
    String[] headers = Beans.get(EmploymentContractService.class).employmentContractExportHeaders();
    CsvTool.csvWriter(tempFile.getParent(), tempFile.getName(), ';', headers, list);
    MetaFiles metaFiles = Beans.get(MetaFiles.class);
    MetaFile metaFile = metaFiles.upload(tempFile);
    tempFile.delete();
    return metaFile;
}
Also used : EmploymentContract(com.axelor.apps.hr.db.EmploymentContract) EmploymentContractService(com.axelor.apps.hr.service.EmploymentContractService) MetaFiles(com.axelor.meta.MetaFiles) ArrayList(java.util.ArrayList) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Example 35 with MetaFile

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

the class BatchPayrollPreparationExport method nibelisExport.

@Transactional(rollbackOn = { Exception.class })
public MetaFile nibelisExport(List<PayrollPreparation> payrollPreparationList) throws IOException, AxelorException {
    List<String[]> list = new ArrayList<>();
    for (PayrollPreparation payrollPreparation : payrollPreparationList) {
        payrollPreparation.addBatchListItem(batch);
        payrollPreparationService.exportNibelis(payrollPreparation, list);
        total++;
    }
    String fileName = Beans.get(PayrollPreparationService.class).getPayrollPreparationExportName();
    File file = MetaFiles.createTempFile(fileName, ".csv").toFile();
    CsvTool.csvWriter(file.getParent(), file.getName(), ';', Beans.get(PayrollPreparationService.class).getPayrollPreparationMeilleurGestionExportHeader(), list);
    FileInputStream inStream = new FileInputStream(file);
    MetaFile metaFile = Beans.get(MetaFiles.class).upload(inStream, file.getName());
    return metaFile;
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) ArrayList(java.util.ArrayList) PayrollPreparationService(com.axelor.apps.hr.service.PayrollPreparationService) MetaFile(com.axelor.meta.db.MetaFile) PayrollPreparation(com.axelor.apps.hr.db.PayrollPreparation) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream) Transactional(com.google.inject.persist.Transactional)

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