Search in sources :

Example 6 with FileField

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

the class AdvancedImportServiceImpl method process.

@Transactional
public boolean process(DataReaderService reader, AdvancedImport advancedImport) throws AxelorException, ClassNotFoundException {
    boolean isValid = true;
    if (!CollectionUtils.isEmpty(advancedImport.getFileTabList())) {
        advancedImport.getFileTabList().stream().forEach(tab -> tab.clearFileFieldList());
        advancedImport.clearFileTabList();
    }
    String[] sheets = reader.getSheetNames();
    boolean isConfig = advancedImport.getIsConfigInFile();
    boolean isTabConfig = advancedImport.getIsFileTabConfigAdded();
    boolean isHeader = advancedImport.getIsHeader();
    int linesToIgnore = advancedImport.getNbOfFirstLineIgnore();
    int tabConfigRowCount = 0;
    int startIndex = isConfig ? 0 : linesToIgnore;
    int fileTabSequence = 1;
    for (String sheet : sheets) {
        int totalLines = reader.getTotalLines(sheet);
        if (totalLines == 0) {
            continue;
        }
        FileTab fileTab = new FileTab();
        fileTab.setName(sheet);
        fileTab.setSequence(fileTabSequence);
        fileTabSequence++;
        String[] objectRow = reader.read(sheet, startIndex, 0);
        if (objectRow == null) {
            isValid = false;
            break;
        }
        if (isConfig && isTabConfig) {
            tabConfigRowCount = getTabConfigRowCount(sheet, reader, totalLines, objectRow);
        }
        isValid = this.applyObject(objectRow, fileTab, isConfig, linesToIgnore, isTabConfig);
        if (!isValid) {
            break;
        }
        List<FileField> fileFieldList = new ArrayList<>();
        List<Integer> ignoreFields = new ArrayList<Integer>();
        for (int line = isConfig ? 1 : linesToIgnore; line < totalLines; line++) {
            String[] row = reader.read(sheet, line, isConfig ? 0 : objectRow.length);
            if (row == null) {
                continue;
            }
            if (isConfig) {
                this.applyWithConfig(row, line, fileFieldList, ignoreFields, fileTab, isTabConfig, tabConfigRowCount);
            } else {
                this.applyWithoutConfig(row, (line - linesToIgnore), fileFieldList, fileTab, isHeader);
            }
        }
        if (isConfig) {
            fileFieldList.removeIf(field -> field.getImportField() == null);
            if (!fileTab.getImportType().equals(FileFieldRepository.IMPORT_TYPE_NEW)) {
                fileTab = this.setSearchField(fileTab, searchFieldList, fileFieldList);
            }
        }
        advancedImport.addFileTabListItem(fileTab);
        advancedImportRepository.save(advancedImport);
    }
    return isValid;
}
Also used : ArrayList(java.util.ArrayList) FileField(com.axelor.apps.base.db.FileField) FileTab(com.axelor.apps.base.db.FileTab) Transactional(com.google.inject.persist.Transactional)

Example 7 with FileField

use of com.axelor.apps.base.db.FileField 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 8 with FileField

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

the class FileTabServiceImpl method updateFields.

@Override
public FileTab updateFields(FileTab fileTab) throws ClassNotFoundException {
    MetaModel model = fileTab.getMetaModel();
    if (model == null || CollectionUtils.isEmpty(fileTab.getFileFieldList())) {
        return fileTab;
    }
    Beans.get(ValidatorService.class).sortFileFieldList(fileTab.getFileFieldList());
    for (FileField fileField : fileTab.getFileFieldList()) {
        MetaField importField = metaFieldRepo.all().filter("self.label = ?1 AND self.metaModel.id = ?2", fileField.getColumnTitle(), model.getId()).fetchOne();
        if (importField != null) {
            String relationship = importField.getRelationship();
            if (!Strings.isNullOrEmpty(relationship) && relationship.equals("OneToMany")) {
                continue;
            }
            fileField.setImportField(importField);
            if (!Strings.isNullOrEmpty(relationship)) {
                String subImportField = this.getSubImportField(importField);
                fileField.setSubImportField(subImportField);
            }
            fileField = fileFieldService.fillType(fileField);
            if (!Strings.isNullOrEmpty(relationship) && !fileField.getTargetType().equals("MetaFile")) {
                fileField.setImportType(FileFieldRepository.IMPORT_TYPE_FIND);
            } else {
                if (!Strings.isNullOrEmpty(fileField.getTargetType()) && fileField.getTargetType().equals("MetaFile")) {
                    fileField.setImportType(FileFieldRepository.IMPORT_TYPE_NEW);
                }
            }
            fileField.setFullName(fileFieldService.computeFullName(fileField));
        } else {
            fileField.setImportField(null);
            fileField.setSubImportField(null);
        }
    }
    return fileTab;
}
Also used : MetaModel(com.axelor.meta.db.MetaModel) MetaField(com.axelor.meta.db.MetaField) FileField(com.axelor.apps.base.db.FileField)

Example 9 with FileField

use of com.axelor.apps.base.db.FileField 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 10 with FileField

use of com.axelor.apps.base.db.FileField 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

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