Search in sources :

Example 71 with MetaFile

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

the class UnitCostCalculationServiceImpl method importUnitCostCalc.

@Override
public void importUnitCostCalc(MetaFile dataFile, UnitCostCalculation unitCostCalculation) throws IOException {
    File tempDir = Files.createTempDir();
    File csvFile = new File(tempDir, "unitcostcalc.csv");
    Files.copy(MetaFiles.getPath(dataFile).toFile(), csvFile);
    File configXmlFile = this.getConfigXmlFile();
    CSVImporter csvImporter = new CSVImporter(configXmlFile.getAbsolutePath(), tempDir.getAbsolutePath());
    Map<String, Object> context = new HashMap<>();
    context.put("_unitCostCalculation", unitCostCalculation.getId());
    csvImporter.setContext(context);
    csvImporter.run();
}
Also used : HashMap(java.util.HashMap) DMSFile(com.axelor.dms.db.DMSFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) CSVImporter(com.axelor.data.csv.CSVImporter)

Example 72 with MetaFile

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

the class InvoicePrintServiceImpl method printAndSave.

public File printAndSave(Invoice invoice, Integer reportType, String format, String locale) throws AxelorException {
    ReportSettings reportSettings = prepareReportSettings(invoice, reportType, format, locale);
    MetaFile metaFile;
    reportSettings.toAttach(invoice);
    File file = reportSettings.generate().getFile();
    try {
        MetaFiles metaFiles = Beans.get(MetaFiles.class);
        metaFile = metaFiles.upload(file);
        metaFile.setFileName(String.format("%s.%s", reportSettings.getOutputName(), format));
        invoice.setPrintedPDF(metaFile);
        return MetaFiles.getPath(metaFile).toFile();
    } catch (IOException e) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICE_PRINTING_IO_ERROR) + " " + e.getLocalizedMessage());
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) MetaFiles(com.axelor.meta.MetaFiles) ReportSettings(com.axelor.apps.report.engine.ReportSettings) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

Example 73 with MetaFile

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

the class ImportCityServiceImpl method importFromGeonamesAutoConfig.

/**
 * Imports cities from a predefined Geonames configuration.
 *
 * @param downloadFileName
 * @param typeSelect
 * @return
 */
public Map<String, Object> importFromGeonamesAutoConfig(String downloadFileName, 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);
        MetaFile zipImportDataFile = this.downloadZip(downloadFileName, GEONAMES_FILE.ZIP);
        MetaFile dumpImportDataFile = this.downloadZip(downloadFileName, GEONAMES_FILE.DUMP);
        importHistoryList.add(this.importCity(typeSelect + "-zip", zipImportDataFile));
        importHistoryList.add(this.importCity(typeSelect + "-dump", dumpImportDataFile));
    } 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 74 with MetaFile

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

the class ImportCityServiceImpl method getDataCsvFile.

/**
 * Creates geonames-city.csv data file from geonames.txt data file
 *
 * @param dataFile
 * @return
 */
protected File getDataCsvFile(MetaFile dataFile) {
    File csvFile = null;
    try {
        File tempDir = java.nio.file.Files.createTempDirectory(null).toFile();
        csvFile = new File(tempDir, "geonames_city.csv");
        Files.copy(MetaFiles.getPath(dataFile).toFile(), csvFile);
    } catch (Exception e) {
        TraceBackService.trace(e);
        printWriter.append(e.getLocalizedMessage() + " at " + e.getStackTrace()[0] + "\n");
    }
    return csvFile;
}
Also used : 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)

Example 75 with MetaFile

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

the class ImportCityServiceImpl method downloadZip.

/**
 * Extracts file from the zip
 *
 * @param downloadFileName : zip fileName to download from internet
 * @return
 * @return
 * @throws AxelorException if hostname is not valid or if file does not exist
 */
@Override
public MetaFile downloadZip(String downloadFileName, GEONAMES_FILE geonamesFile) throws AxelorException {
    String downloadUrl = getDownloadUrl(geonamesFile);
    MetaFile metaFile = null;
    try {
        File tempDir = java.nio.file.Files.createTempDirectory(null).toFile();
        File downloadFile = new File(tempDir, downloadFileName);
        File cityTextFile = new File(tempDir, CITYTEXTFILE);
        URLService.fileUrl(downloadFile, downloadUrl + downloadFileName, null, null);
        LOG.debug("path for downloaded zip file : {}", downloadFile.getPath());
        StringBuilder buffer = null;
        try (ZipFile zipFile = new ZipFile(downloadFile.getPath());
            FileWriter writer = new FileWriter(cityTextFile)) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.getName().equals(downloadFileName.replace("zip", "txt"))) {
                    BufferedReader stream = new BufferedReader(new InputStreamReader(zipFile.getInputStream(entry)));
                    switch(geonamesFile) {
                        case DUMP:
                            buffer = this.extractDataDumpImport(stream);
                            break;
                        case ZIP:
                            buffer = this.extractDataZipImport(stream);
                            break;
                        default:
                            printWriter.append(I18n.get(IExceptionMessage.INVALID_GEONAMES_IMPORT_FILE) + "\n");
                    }
                    cityTextFile.createNewFile();
                    writer.flush();
                    writer.write(buffer.toString().replace("\"", ""));
                    LOG.debug("Length of file : {}", cityTextFile.length());
                    break;
                }
            }
        }
        metaFile = metaFiles.upload(cityTextFile);
        FileUtils.forceDelete(tempDir);
    } catch (UnknownHostException hostExp) {
        printWriter.append(I18n.get(IExceptionMessage.SERVER_CONNECTION_ERROR) + "\n");
    } catch (IOException e) {
        printWriter.append(String.format(I18n.get(IExceptionMessage.NO_DATA_FILE_FOUND), downloadUrl) + "\n");
    }
    return metaFile;
}
Also used : ZipFile(java.util.zip.ZipFile) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) FileWriter(java.io.FileWriter) ZipEntry(java.util.zip.ZipEntry) BufferedReader(java.io.BufferedReader) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

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