use of com.axelor.apps.base.db.ImportHistory in project axelor-open-suite by axelor.
the class ImportCityController method importCity.
/**
* Import cities
*
* @param request
* @param response
* @throws InterruptedException
*/
@SuppressWarnings("unchecked")
public void importCity(ActionRequest request, ActionResponse response) {
try {
List<ImportHistory> importHistoryList = null;
Map<String, Object> importCityMap = null;
MetaFile errorFile = null;
String typeSelect = (String) request.getContext().get("typeSelect");
if (CityRepository.TYPE_SELECT_GEONAMES.equals(typeSelect)) {
String importTypeSelect = (String) request.getContext().get("importTypeSelect");
switch(importTypeSelect) {
case CityRepository.IMPORT_TYPE_SELECT_AUTO:
String downloadFileName = (String) request.getContext().get("autoImportTypeSelect");
importCityMap = Beans.get(ImportCityService.class).importFromGeonamesAutoConfig(downloadFileName, typeSelect);
break;
case CityRepository.IMPORT_TYPE_SELECT_MANUAL:
Map<String, Object> map = (LinkedHashMap<String, Object>) request.getContext().get("metaFile");
importCityMap = Beans.get(ImportCityService.class).importFromGeonamesManualConfig(map, typeSelect);
break;
default:
break;
}
}
if (importCityMap.containsKey("importHistoryList") && importCityMap.containsKey("errorFile")) {
importHistoryList = (List<ImportHistory>) importCityMap.get("importHistoryList");
errorFile = (MetaFile) importCityMap.get("errorFile");
if (errorFile != null) {
response.setFlash(I18n.get(IExceptionMessage.CITIES_IMPORT_FAILED));
response.setAttr("errorFile", "hidden", false);
response.setValue("errorFile", errorFile);
} else {
response.setAttr("$importHistoryList", "hidden", false);
response.setAttr("errorFile", "hidden", true);
response.setAttr("$importHistoryList", "value", importHistoryList);
response.setFlash(I18n.get(ITranslation.BASE_GEONAMES_CITY_IMPORT_COMPLETED));
}
}
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
use of com.axelor.apps.base.db.ImportHistory 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.apps.base.db.ImportHistory in project axelor-open-suite by axelor.
the class ImportCityServiceImpl method importCity.
/**
* {@inheritDoc}}
*
* @throws AxelorException
* @throws IOException
*/
@Override
public ImportHistory importCity(String typeSelect, MetaFile dataFile) throws AxelorException, IOException {
ImportHistory importHistory = null;
if (dataFile.getFileType().equals("text/plain") || dataFile.getFileType().equals("text/csv") || dataFile.getFileType().equals("application/zip")) {
if (dataFile.getFileType().equals("application/zip")) {
dataFile = this.extractCityZip(dataFile);
}
File configXmlFile = this.getConfigXmlFile(typeSelect);
File dataCsvFile = this.getDataCsvFile(dataFile);
importHistory = importCityData(configXmlFile, dataCsvFile);
this.deleteTempFiles(configXmlFile, dataCsvFile);
} else {
printWriter.append(I18n.get(IExceptionMessage.INVALID_DATA_FILE_EXTENSION) + "\n");
}
return importHistory;
}
use of com.axelor.apps.base.db.ImportHistory in project axelor-open-suite by axelor.
the class Importer method addHistory.
/**
* Adds a new log in the history table for data configuration.
*
* @param listener
* @return
* @throws IOException
*/
protected ImportHistory addHistory(ImporterListener listener) throws IOException {
ImportHistory importHistory = new ImportHistory(AuthUtils.getUser(), configuration.getDataMetaFile());
File logFile = File.createTempFile("importLog", ".log");
try (FileWriter writer = new FileWriter(logFile)) {
writer.write(listener.getImportLog());
}
MetaFile logMetaFile = metaFiles.upload(new FileInputStream(logFile), "importLog-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".log");
importHistory.setLogMetaFile(logMetaFile);
importHistory.setImportConfiguration(configuration);
return importHistory;
}
use of com.axelor.apps.base.db.ImportHistory in project axelor-open-suite by axelor.
the class Importer method run.
public ImportHistory run(Map<String, Object> importContext) throws AxelorException, IOException {
File bind = MetaFiles.getPath(configuration.getBindMetaFile()).toFile();
File data = MetaFiles.getPath(configuration.getDataMetaFile()).toFile();
if (!bind.exists()) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.IMPORTER_1));
}
if (!data.exists()) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.IMPORTER_2));
}
File fileWorkspace = createFinalWorkspace(configuration.getDataMetaFile());
ImportHistory importHistory = process(bind.getAbsolutePath(), fileWorkspace.getAbsolutePath(), importContext);
deleteFinalWorkspace(fileWorkspace);
return importHistory;
}
Aggregations