Search in sources :

Example 36 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class AdvancedImportServiceImpl method applyWithConfig.

private void applyWithConfig(String[] row, int line, List<FileField> fileFieldList, List<Integer> ignoreFields, FileTab fileTab, Boolean isTabConfig, int tabConfigRowCount) throws AxelorException, ClassNotFoundException {
    Mapper mapper = getMapper(fileTab.getMetaModel().getFullName());
    FileField fileField = null;
    int index = 0;
    for (int i = 0; i < row.length; i++) {
        if (Strings.isNullOrEmpty(row[i])) {
            continue;
        }
        index = line;
        String value = row[i].trim();
        if (line == 1) {
            this.readFields(value, i, fileFieldList, ignoreFields, mapper, fileTab);
            continue;
        }
        if (ignoreFields.contains(i)) {
            continue;
        }
        if (fileFieldList.size() < i) {
            break;
        } else if (!isTabConfig && fileFieldList.size() <= i) {
            break;
        }
        if (row[0] == null) {
            fileField = fileFieldList.get(i - 1);
        } else if (!isTabConfig && row[0] != null) {
            fileField = fileFieldList.get(i);
        }
        if (isTabWithoutConfig) {
            fileField = fileFieldList.get(i);
        }
        if (line == 2) {
            fileField.setColumnTitle(value);
            continue;
        }
        if (isTabConfig && i > 0 && row[0] != null && line >= 3 && line <= 11) {
            fileField = fileFieldList.get(i - 1);
            this.setFileFieldConfig(row, i, fileField);
        }
        if (isTabConfig && i > 0 && row[0] == null) {
            line += -(tabConfigRowCount + 2);
            this.setSampleLines(line, value, fileField);
            line = index;
        } else if (!isTabConfig) {
            line += -2;
            this.setSampleLines(line, value, fileField);
            line = index;
        }
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) FileField(com.axelor.apps.base.db.FileField)

Example 37 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class ValidatorService method getAndValidateSubField.

public Property getAndValidateSubField(String[] subFields, int index, Integer rowNum, Property parentProp, String field, boolean isLog) throws IOException, ClassNotFoundException {
    Property subProperty = null;
    if (parentProp.getTarget() != null) {
        Mapper mapper = advancedImportService.getMapper(parentProp.getTarget().getName());
        Property childProp = mapper.getProperty(subFields[index]);
        if (childProp == null) {
            if (!isLog) {
                logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_7, field, rowNum);
            }
        }
        if (childProp != null && childProp.getTarget() != null) {
            if (index != subFields.length - 1) {
                subProperty = this.getAndValidateSubField(subFields, index + 1, rowNum, childProp, field, isLog);
            } else {
                subProperty = childProp;
                if (!isLog) {
                    logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_4, field, rowNum);
                }
            }
        } else {
            subProperty = childProp;
        }
    }
    return subProperty;
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Property(com.axelor.db.mapper.Property)

Example 38 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class ValidatorService method validateFields.

private void validateFields(int line, boolean isConfig, FileTab fileTab) throws IOException, ClassNotFoundException {
    List<String> relationalFieldList = fileTab.getFileFieldList().stream().filter(field -> !Strings.isNullOrEmpty(field.getSubImportField())).map(field -> field.getImportField().getName() + "." + field.getSubImportField()).collect(Collectors.toList());
    for (FileField fileField : fileTab.getFileFieldList()) {
        MetaField importField = fileField.getImportField();
        if (importField != null && Strings.isNullOrEmpty(fileField.getSubImportField())) {
            if (importField.getRelationship() != null) {
                logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_4, importField.getName(), line);
            }
            this.validateImportRequiredField(line, Class.forName(fileTab.getMetaModel().getFullName()), importField.getName(), fileField, null);
            this.validateDateField(line, fileField);
        } else if (!Strings.isNullOrEmpty(fileField.getSubImportField())) {
            Mapper mapper = advancedImportService.getMapper(importField.getMetaModel().getFullName());
            Property parentProp = mapper.getProperty(importField.getName());
            if (parentProp == null) {
                return;
            }
            Property subProperty = this.getAndValidateSubField(line, parentProp, fileField, false);
            if (subProperty != null) {
                this.validateImportRequiredField(line, subProperty.getEntity(), subProperty.getName(), fileField, relationalFieldList);
                this.validateDateField(line, fileField);
            }
        }
    }
}
Also used : MetaJsonFieldRepository(com.axelor.meta.db.repo.MetaJsonFieldRepository) Arrays(java.util.Arrays) Inject(com.google.inject.Inject) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) Property(com.axelor.db.mapper.Property) HashMap(java.util.HashMap) Mapper(com.axelor.db.mapper.Mapper) StringUtils(org.apache.commons.lang3.StringUtils) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) FileField(com.axelor.apps.base.db.FileField) Strings(com.google.common.base.Strings) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) Files(com.google.common.io.Files) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) LocalTime(java.time.LocalTime) I18n(com.axelor.i18n.I18n) BigInteger(java.math.BigInteger) Inflector(com.axelor.common.Inflector) MetaJsonField(com.axelor.meta.db.MetaJsonField) DataReaderFactory(com.axelor.apps.tool.reader.DataReaderFactory) MetaFiles(com.axelor.meta.MetaFiles) Model(com.axelor.db.Model) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) MetaField(com.axelor.meta.db.MetaField) BufferedWriter(java.io.BufferedWriter) TraceBackService(com.axelor.exception.service.TraceBackService) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) FileTabRepository(com.axelor.apps.base.db.repo.FileTabRepository) File(java.io.File) DataReaderService(com.axelor.apps.tool.reader.DataReaderService) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) FileFieldRepository(com.axelor.apps.base.db.repo.FileFieldRepository) IExceptionMessage(com.axelor.apps.base.exceptions.IExceptionMessage) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) FileTab(com.axelor.apps.base.db.FileTab) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) Mapper(com.axelor.db.mapper.Mapper) MetaField(com.axelor.meta.db.MetaField) FileField(com.axelor.apps.base.db.FileField) Property(com.axelor.db.mapper.Property)

Example 39 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class ValidatorService method validateDataRequiredField.

private boolean validateDataRequiredField(String[] row, int cell, int line, Class<?> model, String fieldName, FileField fileField) throws IOException, ClassNotFoundException {
    boolean flag = false;
    String field = getField(fileField);
    int importType = fileField.getImportType();
    Mapper mapper = advancedImportService.getMapper(model.getName());
    Property prop = mapper.getProperty(fieldName);
    if (prop != null) {
        if (prop.isRequired() && Strings.isNullOrEmpty(row[cell]) && importType != FileFieldRepository.IMPORT_TYPE_FIND) {
            logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_8, field, line);
        } else if (importType == FileFieldRepository.IMPORT_TYPE_IGNORE_EMPTY) {
            flag = true;
            return flag;
        }
    }
    return flag;
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Property(com.axelor.db.mapper.Property)

Example 40 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class ValidatorService method validateObjectRequiredFields.

private void validateObjectRequiredFields(FileTab fileTab) throws ClassNotFoundException, IOException, AxelorException {
    if (CollectionUtils.isEmpty(fileTab.getFileFieldList())) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.ADVANCED_IMPORT_NO_FIELDS), fileTab.getName()));
    }
    List<String> fieldList = new ArrayList<String>();
    for (FileField fileField : fileTab.getFileFieldList()) {
        if (fileField.getImportField() != null) {
            fieldList.add(fileField.getImportField().getName());
        } else {
            logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_2, fileField.getColumnTitle(), null);
        }
    }
    if (fileTab.getImportType() == FileFieldRepository.IMPORT_TYPE_FIND) {
        return;
    }
    if (fileTab.getMetaModel() != null) {
        Mapper mapper = advancedImportService.getMapper(fileTab.getMetaModel().getFullName());
        Model obj = null;
        try {
            obj = (Model) Class.forName(fileTab.getMetaModel().getFullName()).newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            TraceBackService.trace(e);
        }
        for (Property prop : mapper.getProperties()) {
            if (prop.isRequired() && !fieldList.contains(prop.getName())) {
                if (obj != null && mapper.get(obj, prop.getName()) != null) {
                    continue;
                }
                logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_3, prop.getName(), null);
            }
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Mapper(com.axelor.db.mapper.Mapper) ArrayList(java.util.ArrayList) Model(com.axelor.db.Model) FileField(com.axelor.apps.base.db.FileField) Property(com.axelor.db.mapper.Property)

Aggregations

Mapper (com.axelor.db.mapper.Mapper)44 Property (com.axelor.db.mapper.Property)19 AxelorException (com.axelor.exception.AxelorException)13 Model (com.axelor.db.Model)12 MetaField (com.axelor.meta.db.MetaField)10 ArrayList (java.util.ArrayList)10 MetaModel (com.axelor.meta.db.MetaModel)7 File (java.io.File)6 IOException (java.io.IOException)6 FileField (com.axelor.apps.base.db.FileField)5 Transactional (com.google.inject.persist.Transactional)5 TraceBackService (com.axelor.exception.service.TraceBackService)4 MetaFiles (com.axelor.meta.MetaFiles)4 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)4 List (java.util.List)4 Inflector (com.axelor.common.Inflector)3 MetaFile (com.axelor.meta.db.MetaFile)3 MetaJsonField (com.axelor.meta.db.MetaJsonField)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Strings (com.google.common.base.Strings)3