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();
}
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());
}
}
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;
}
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;
}
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;
}
Aggregations