Search in sources :

Example 6 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class DataBackupCreateService method getRelationalFieldHeader.

protected String getRelationalFieldHeader(Property property, CSVInput csvInput, String relationship) {
    csvInput.setSearch("self.importId = :importId");
    CSVBind csvBind = new CSVBind();
    String columnName = property.getName() + "_importId";
    String search = relationship.equalsIgnoreCase("ONE") ? "self.importId = :" + columnName : "self.importId in :" + columnName;
    if (property.getTarget() != null && property.getTarget().getPackage().equals(Package.getPackage("com.axelor.meta.db")) && !property.getTarget().getTypeName().equals("com.axelor.meta.db.MetaFile")) {
        columnName = property.getName() + "_name";
        search = relationship.equalsIgnoreCase("ONE") ? "self.name = :" + columnName : "self.name in :" + columnName;
    }
    csvBind.setColumn(columnName);
    csvBind.setField(property.getName());
    csvBind.setSearch(search);
    csvBind.setUpdate(true);
    if (relationship.equalsIgnoreCase("MANY")) {
        csvBind.setExpression(columnName + ".split('\\\\|') as List");
    }
    csvInput.getBindings().add(csvBind);
    referenceFlag = true;
    if (property.isRequired()) {
        notNullReferenceFlag = true;
    }
    return columnName;
}
Also used : CSVBind(com.axelor.data.csv.CSVBind)

Example 7 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class DataImportServiceImpl method createBindForMatchWithFile.

private void createBindForMatchWithFile(String column, int importType, String expression, String adapter, String relationship, CSVBind parentBind, Property childProp) {
    if (importType != FileFieldRepository.IMPORT_TYPE_FIND) {
        if (!Strings.isNullOrEmpty(expression) && expression.contains(BIND_CALLABLE_META_FILE) && importType == FileFieldRepository.IMPORT_TYPE_NEW) {
            parentBind.setExpression(expression);
            return;
        }
        CSVBind subBind = this.createCSVBind(column, childProp.getName(), null, expression, adapter, null);
        this.setImportIf(childProp, subBind, column);
        parentBind.getBindings().add(subBind);
    } else {
        if (!Strings.isNullOrEmpty(relationship) && relationship.equals(MANY_TO_MANY)) {
            parentBind.setColumn(column);
            parentBind.setExpression(expression);
        }
    }
}
Also used : CSVBind(com.axelor.data.csv.CSVBind)

Example 8 with CSVBind

use of com.axelor.data.csv.CSVBind 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)

Example 9 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class DataImportServiceImpl method createCSVSubBinding.

private void createCSVSubBinding(String[] subFields, int index, String column, Property parentProp, FileField fileField, CSVBind parentBind, CSVBind dummyBind, boolean isSameParentExist) throws ClassNotFoundException {
    if (index < subFields.length) {
        if (parentProp.getTarget() == null) {
            return;
        }
        int importType = fileField.getImportType();
        String relationship = fileField.getRelationship();
        fullFieldName += "." + subFields[index];
        Mapper mapper = advancedImportService.getMapper(parentProp.getTarget().getName());
        Property childProp = mapper.getProperty(subFields[index]);
        if (childProp != null && childProp.getTarget() != null) {
            CSVBind subBind = null;
            if (subBindMap.containsKey(fullFieldName)) {
                subBind = subBindMap.get(fullFieldName);
            } else if (importType != FileFieldRepository.IMPORT_TYPE_FIND) {
                subBind = this.createCSVBind(null, childProp.getName(), null, null, null, true);
                subBind.setBindings(new ArrayList<>());
                parentBind.getBindings().add(subBind);
                subBindMap.put(fullFieldName, subBind);
            }
            if (importType == FileFieldRepository.IMPORT_TYPE_FIND_NEW && subBind != null) {
                String fieldName = (isSameParentExist ? fullFieldName.replaceFirst(fileField.getImportField().getName() + ".", "") : fullFieldName) + "." + subFields[index + 1];
                this.setSearch(column, fieldName, fileField, parentBind, isSameParentExist);
            }
            this.createCSVSubBinding(subFields, index + 1, column, childProp, fileField, isSameParentExist && importType == FileFieldRepository.IMPORT_TYPE_FIND ? parentBind : subBind, dummyBind, isSameParentExist);
        } else {
            String expression = this.setExpression(column, fileField, childProp);
            String adapter = null;
            String dateFormat = fileField.getDateFormat();
            if (Strings.isNullOrEmpty(expression) && !Strings.isNullOrEmpty(dateFormat)) {
                adapter = this.getAdapter(childProp.getJavaType().getSimpleName(), dateFormat.trim());
            }
            if (!fileField.getIsMatchWithFile()) {
                this.createBindForNotMatchWithFile(column, importType, dummyBind, expression, adapter, parentBind, childProp);
            } else {
                this.createBindForMatchWithFile(column, importType, expression, adapter, relationship, parentBind, childProp);
            }
            String fieldName = isSameParentExist && importType == FileFieldRepository.IMPORT_TYPE_FIND ? fullFieldName.replaceFirst(fileField.getImportField().getName() + ".", "") : childProp.getName();
            this.setSearch(column, fieldName, fileField, parentBind, isSameParentExist);
            if (importType != FileFieldRepository.IMPORT_TYPE_FIND) {
                parentBind.setUpdate(false);
            }
        }
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) ArrayList(java.util.ArrayList) CSVBind(com.axelor.data.csv.CSVBind) Property(com.axelor.db.mapper.Property)

Aggregations

CSVBind (com.axelor.data.csv.CSVBind)9 Property (com.axelor.db.mapper.Property)3 ArrayList (java.util.ArrayList)3 Mapper (com.axelor.db.mapper.Mapper)2 AppSettings (com.axelor.app.AppSettings)1 App (com.axelor.apps.base.db.App)1 FileField (com.axelor.apps.base.db.FileField)1 AppRepository (com.axelor.apps.base.db.repo.AppRepository)1 IExceptionMessages (com.axelor.apps.base.exceptions.IExceptionMessages)1 FileUtils (com.axelor.common.FileUtils)1 Inflector (com.axelor.common.Inflector)1 Importer (com.axelor.data.Importer)1 CSVConfig (com.axelor.data.csv.CSVConfig)1 CSVImporter (com.axelor.data.csv.CSVImporter)1 CSVInput (com.axelor.data.csv.CSVInput)1 XMLImporter (com.axelor.data.xml.XMLImporter)1 JPA (com.axelor.db.JPA)1 Model (com.axelor.db.Model)1 PropertyType (com.axelor.db.mapper.PropertyType)1 AxelorException (com.axelor.exception.AxelorException)1