Search in sources :

Example 11 with FileField

use of com.axelor.apps.base.db.FileField in project axelor-open-suite by axelor.

the class DataImportServiceImpl method createData.

private String[] createData(String[] dataRow, FileTab fileTab, boolean isConfig, Mapper mapper) throws ClassNotFoundException {
    Map<String, Object> map = isConfig ? fieldMap : titleMap;
    List<String> dataList = new ArrayList<String>();
    for (int fieldIndex = 0; fieldIndex < fileTab.getFileFieldList().size(); fieldIndex++) {
        FileField fileField = fileTab.getFileFieldList().get(fieldIndex);
        String key = null;
        if (isConfig) {
            key = validatorService.getField(fileField);
        } else {
            key = fileField.getColumnTitle();
        }
        if (!fileField.getIsMatchWithFile() || fileField.getImportType() == FileFieldRepository.IMPORT_TYPE_IGNORE_EMPTY) {
            continue;
        }
        int cell = 0;
        if (map.containsKey(key)) {
            cell = (int) map.get(key);
        }
        cell = (!isConfig && !fileTab.getAdvancedImport().getIsHeader()) ? fieldIndex : cell;
        if (Strings.isNullOrEmpty(dataRow[cell])) {
            dataList.add(!Strings.isNullOrEmpty(fileField.getDefaultIfNotFound()) ? fileField.getDefaultIfNotFound().trim() : "");
            continue;
        }
        String dataCell = dataRow[cell].trim();
        this.checkAndWriteData(dataCell, fileTab.getMetaModel(), fileField, mapper, dataList);
    }
    return dataList.stream().toArray(String[]::new);
}
Also used : ArrayList(java.util.ArrayList) FileField(com.axelor.apps.base.db.FileField)

Example 12 with FileField

use of com.axelor.apps.base.db.FileField in project axelor-open-suite by axelor.

the class DataImportServiceImpl method createHeader.

private String[] createHeader(String[] row, FileTab fileTab, boolean isConfig, Mapper mapper) throws ClassNotFoundException {
    Map<String, Object> map = isConfig ? fieldMap : titleMap;
    int rowCount = row.length;
    for (int cell = 0; cell < rowCount; cell++) {
        if (Strings.isNullOrEmpty(row[cell])) {
            continue;
        }
        String value = row[cell].trim();
        map.put(isConfig ? value.contains("(") ? value.split("\\(")[0] : value : value, cell);
    }
    validatorService.sortFileFieldList(fileTab.getFileFieldList());
    List<String> headers = new ArrayList<>();
    List<CSVBind> allBindings = new ArrayList<CSVBind>();
    int cnt = 0;
    Map<String, Object> searchMap = new HashMap<String, Object>();
    for (FileField fileField : fileTab.getFileFieldList()) {
        if (fileField.getImportType() == FileFieldRepository.IMPORT_TYPE_IGNORE_EMPTY) {
            continue;
        }
        String key = (isConfig) ? validatorService.getField(fileField) : fileField.getColumnTitle();
        String column = ("cell" + (cnt + 1));
        if (!CollectionUtils.isEmpty(fileTab.getSearchFieldSet()) && fileTab.getSearchFieldSet().contains(fileField) && fileTab.getImportType() != FileFieldRepository.IMPORT_TYPE_NEW) {
            searchMap.put(validatorService.getField(fileField), column);
        }
        if ((map.containsKey(key) || (!isConfig && !fileTab.getAdvancedImport().getIsHeader())) && fileField.getIsMatchWithFile()) {
            headers.add(column);
        }
        allBindings = this.createCSVBinding(column, fileField, mapper, allBindings);
        cnt++;
    }
    CSVBind fileTabBind = new CSVBind();
    fileTabBind.setField("fileTabId");
    fileTabBind.setExpression(fileTab.getId().toString());
    allBindings.add(fileTabBind);
    for (Entry<String, Object> entry : searchMap.entrySet()) {
        String field = entry.getKey(), cond1 = "", cond2 = "", condAnd = "";
        if (field.contains(".")) {
            int index = field.lastIndexOf(46);
            cond1 = " (:" + entry.getValue() + " IS NOT NULL AND";
            cond2 = " OR self." + field.substring(0, index) + " IS NULL)";
        }
        if (!Strings.isNullOrEmpty(csvInput.getSearch())) {
            condAnd = csvInput.getSearch() + " AND";
        }
        csvInput.setSearch(condAnd + cond1 + " self." + field + " = :" + entry.getValue() + cond2);
    }
    csvInput.setBindings(allBindings);
    return headers.stream().toArray(String[]::new);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileField(com.axelor.apps.base.db.FileField) CSVBind(com.axelor.data.csv.CSVBind)

Aggregations

FileField (com.axelor.apps.base.db.FileField)12 ArrayList (java.util.ArrayList)5 Mapper (com.axelor.db.mapper.Mapper)4 Transactional (com.google.inject.persist.Transactional)4 Property (com.axelor.db.mapper.Property)3 FileTab (com.axelor.apps.base.db.FileTab)2 Model (com.axelor.db.Model)2 AxelorException (com.axelor.exception.AxelorException)2 MetaField (com.axelor.meta.db.MetaField)2 HashMap (java.util.HashMap)2 AdvancedImport (com.axelor.apps.base.db.AdvancedImport)1 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)1 FileTabRepository (com.axelor.apps.base.db.repo.FileTabRepository)1 IExceptionMessage (com.axelor.apps.base.exceptions.IExceptionMessage)1 DataReaderFactory (com.axelor.apps.tool.reader.DataReaderFactory)1 DataReaderService (com.axelor.apps.tool.reader.DataReaderService)1 Inflector (com.axelor.common.Inflector)1 CSVBind (com.axelor.data.csv.CSVBind)1 TraceBackRepository (com.axelor.exception.db.repo.TraceBackRepository)1 TraceBackService (com.axelor.exception.service.TraceBackService)1