Search in sources :

Example 11 with FileTab

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

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

the class AdvancedImportServiceImpl method resetRelationalFields.

@SuppressWarnings("unchecked")
public boolean resetRelationalFields(List<FileTab> fileTabList) throws ClassNotFoundException {
    boolean isResetValue = false;
    for (FileTab fileTab : fileTabList) {
        Map<String, Object> jsonContextMap = dataImportService.createJsonContext(fileTab);
        JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
        String fieldName = inflector.camelize(fileTab.getMetaModel().getName(), true) + "Set";
        List<Object> recordList = (List<Object>) jsonContext.get(fieldName);
        if (CollectionUtils.isEmpty(recordList)) {
            continue;
        }
        isResetValue = true;
        Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(fileTab.getMetaModel().getFullName());
        this.resetPropertyValue(modelKlass, recordList);
        this.resetSubPropertyValue(modelKlass, jsonContext);
    }
    return isResetValue;
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) ArrayList(java.util.ArrayList) List(java.util.List) FileTab(com.axelor.apps.base.db.FileTab)

Example 13 with FileTab

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

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

the class ValidatorService method validate.

@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public boolean validate(DataReaderService reader, AdvancedImport advancedImport) throws IOException, ClassNotFoundException, AxelorException {
    boolean isLog = false;
    String[] sheets = reader.getSheetNames();
    this.validateTab(sheets, advancedImport);
    boolean isConfig = advancedImport.getIsConfigInFile();
    boolean isTabConfig = advancedImport.getIsFileTabConfigAdded();
    sortFileTabList(advancedImport.getFileTabList());
    for (FileTab fileTab : advancedImport.getFileTabList()) {
        if (!Arrays.stream(sheets).anyMatch(sheet -> sheet.equals(fileTab.getName()))) {
            continue;
        }
        fieldMap = new HashMap<>();
        titleMap = new HashMap<>();
        String sheet = fileTab.getName();
        logService.initialize(sheet);
        this.validateModel(fileTab);
        int tabConfigRowCount = 0;
        int totalLines = reader.getTotalLines(fileTab.getName());
        if (isConfig) {
            String[] objectRow = reader.read(sheet, 0, 0);
            if (isTabConfig) {
                tabConfigRowCount = advancedImportService.getTabConfigRowCount(sheet, reader, totalLines, objectRow);
            }
            this.validateObject(objectRow, fileTab, isTabConfig);
        }
        this.validateSearch(fileTab);
        this.validateObjectRequiredFields(fileTab);
        this.validateFieldAndData(reader, sheet, fileTab, isConfig, isTabConfig, tabConfigRowCount);
        this.validateActions(fileTab);
        if (fileTab.getValidationLog() != null) {
            fileTab.setValidationLog(null);
        }
        if (logService.isLogGenerated()) {
            logService.write();
            logService.close();
            File logFile = logService.getLogFile();
            fileTab.setValidationLog(metaFiles.upload(new FileInputStream(logFile), sheet + "_err.xlsx"));
            logFile.delete();
            isLog = true;
        } else {
            createCustomObjectSet(fileTab.getClass().getName(), fileTab.getMetaModel().getFullName(), 0);
            createCustomButton(fileTab.getClass().getName(), fileTab.getMetaModel().getFullName(), 1);
        }
        fileTabRepo.save(fileTab);
    }
    return isLog;
}
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) FileTab(com.axelor.apps.base.db.FileTab) File(java.io.File) FileInputStream(java.io.FileInputStream) Transactional(com.google.inject.persist.Transactional)

Aggregations

FileTab (com.axelor.apps.base.db.FileTab)14 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Model (com.axelor.db.Model)7 FileField (com.axelor.apps.base.db.FileField)6 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)5 Mapper (com.axelor.db.mapper.Mapper)5 Property (com.axelor.db.mapper.Property)5 Strings (com.google.common.base.Strings)5 Inject (com.google.inject.Inject)5 AdvancedImport (com.axelor.apps.base.db.AdvancedImport)4 DataReaderFactory (com.axelor.apps.tool.reader.DataReaderFactory)4 DataReaderService (com.axelor.apps.tool.reader.DataReaderService)4 Inflector (com.axelor.common.Inflector)4 AxelorException (com.axelor.exception.AxelorException)4 MetaField (com.axelor.meta.db.MetaField)4 MetaModel (com.axelor.meta.db.MetaModel)4 JsonContext (com.axelor.rpc.JsonContext)4 Files (com.google.common.io.Files)4 Arrays (java.util.Arrays)4