Search in sources :

Example 6 with ImportHistory

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);
    }
}
Also used : MetaFile(com.axelor.meta.db.MetaFile) ImportHistory(com.axelor.apps.base.db.ImportHistory) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with ImportHistory

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;
}
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 8 with ImportHistory

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;
}
Also used : ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) ImportHistory(com.axelor.apps.base.db.ImportHistory)

Example 9 with 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;
}
Also used : FileWriter(java.io.FileWriter) MetaFile(com.axelor.meta.db.MetaFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) SimpleDateFormat(java.text.SimpleDateFormat) FileInputStream(java.io.FileInputStream) Date(java.util.Date) ImportHistory(com.axelor.apps.base.db.ImportHistory)

Example 10 with 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;
}
Also used : AxelorException(com.axelor.exception.AxelorException) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) ImportHistory(com.axelor.apps.base.db.ImportHistory)

Aggregations

ImportHistory (com.axelor.apps.base.db.ImportHistory)10 MetaFile (com.axelor.meta.db.MetaFile)7 File (java.io.File)7 ZipFile (java.util.zip.ZipFile)5 ImportConfiguration (com.axelor.apps.base.db.ImportConfiguration)4 AxelorException (com.axelor.exception.AxelorException)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 FECImport (com.axelor.apps.account.db.FECImport)1 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)1 ImportService (com.axelor.apps.base.service.imports.ImportService)1 FileWriter (java.io.FileWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1