Search in sources :

Example 1 with AdvancedImport

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

the class DataImportServiceImpl method process.

private List<CSVInput> process(DataReaderService reader, AdvancedImport advancedImport) throws AxelorException, IOException, ClassNotFoundException {
    String[] sheets = reader.getSheetNames();
    boolean isConfig = advancedImport.getIsConfigInFile();
    int linesToIgnore = advancedImport.getNbOfFirstLineIgnore();
    boolean isTabConfig = advancedImport.getIsFileTabConfigAdded();
    List<CSVInput> inputList = new ArrayList<CSVInput>();
    validatorService.sortFileTabList(advancedImport.getFileTabList());
    for (FileTab fileTab : advancedImport.getFileTabList()) {
        if (!Arrays.stream(sheets).anyMatch(sheet -> sheet.equals(fileTab.getName()))) {
            continue;
        }
        this.initializeVariables();
        String fileName = createDataFileName(fileTab);
        csvInput = this.createCSVInput(fileTab, fileName);
        ifList = new ArrayList<String>();
        try (CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(dataDir, fileName)), CSV_SEPRATOR)) {
            int totalLines = reader.getTotalLines(fileTab.getName());
            if (totalLines == 0) {
                continue;
            }
            Mapper mapper = advancedImportService.getMapper(fileTab.getMetaModel().getFullName());
            List<String[]> allLines = new ArrayList<String[]>();
            int startIndex = isConfig ? 1 : linesToIgnore;
            String[] row = reader.read(fileTab.getName(), startIndex, 0);
            String[] headers = this.createHeader(row, fileTab, isConfig, mapper);
            allLines.add(headers);
            int tabConfigRowCount = 0;
            if (isTabConfig) {
                String[] objectRow = reader.read(fileTab.getName(), 0, 0);
                tabConfigRowCount = advancedImportService.getTabConfigRowCount(fileTab.getName(), reader, totalLines, objectRow);
            }
            startIndex = isConfig ? tabConfigRowCount + 3 : fileTab.getAdvancedImport().getIsHeader() ? linesToIgnore + 1 : linesToIgnore;
            for (int line = startIndex; line < totalLines; line++) {
                String[] dataRow = reader.read(fileTab.getName(), line, row.length);
                if (dataRow == null || Arrays.stream(dataRow).allMatch(StringUtils::isBlank)) {
                    continue;
                }
                String[] data = this.createData(dataRow, fileTab, isConfig, mapper);
                allLines.add(data);
            }
            csvWriter.writeAll(allLines);
            csvWriter.flush();
        }
        inputList.add(csvInput);
        importContext.put("ifConditions" + fileTab.getId(), ifList);
        importContext.put("jsonContextValues" + fileTab.getId(), createJsonContext(fileTab));
        importContext.put("actionsToApply" + fileTab.getId(), fileTab.getActions());
        XStream stream = XStreamUtils.createXStream();
        stream.processAnnotations(CSVConfig.class);
        LOG.debug("CSV Config created :" + "\n" + stream.toXML(csvInput));
    }
    return inputList;
}
Also used : XStream(com.thoughtworks.xstream.XStream) Arrays(java.util.Arrays) Date(java.util.Date) Inject(com.google.inject.Inject) CSVInput(com.axelor.data.csv.CSVInput) LoggerFactory(org.slf4j.LoggerFactory) Mapper(com.axelor.db.mapper.Mapper) JsonContext(com.axelor.rpc.JsonContext) FileField(com.axelor.apps.base.db.FileField) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) Inflector(com.axelor.common.Inflector) ZipEntry(java.util.zip.ZipEntry) TranslationService(com.axelor.apps.tool.service.TranslationService) ZipException(java.util.zip.ZipException) MethodHandles(java.lang.invoke.MethodHandles) CSVImporter(com.axelor.data.csv.CSVImporter) StringUtils(com.axelor.common.StringUtils) DataAdapter(com.axelor.data.adapter.DataAdapter) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) CSVBind(com.axelor.data.csv.CSVBind) MetaSelectRepository(com.axelor.meta.db.repo.MetaSelectRepository) Entry(java.util.Map.Entry) FileTab(com.axelor.apps.base.db.FileTab) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) ZipInputStream(java.util.zip.ZipInputStream) MetaSelectItem(com.axelor.meta.db.MetaSelectItem) Property(com.axelor.db.mapper.Property) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) ImporterListener(com.axelor.apps.base.service.imports.listener.ImporterListener) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) AxelorException(com.axelor.exception.AxelorException) Files(com.google.common.io.Files) CollectionUtils(org.apache.commons.collections.CollectionUtils) XStreamUtils(com.axelor.data.XStreamUtils) MetaSelectItemRepository(com.axelor.meta.db.repo.MetaSelectItemRepository) DataReaderFactory(com.axelor.apps.tool.reader.DataReaderFactory) MetaFiles(com.axelor.meta.MetaFiles) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) FileWriter(java.io.FileWriter) TraceBackService(com.axelor.exception.service.TraceBackService) FileOutputStream(java.io.FileOutputStream) CSVWriter(com.opencsv.CSVWriter) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) JavaTimeAdapter(com.axelor.data.adapter.JavaTimeAdapter) FileInputStream(java.io.FileInputStream) File(java.io.File) DataReaderService(com.axelor.apps.tool.reader.DataReaderService) MetaFile(com.axelor.meta.db.MetaFile) FileFieldRepository(com.axelor.apps.base.db.repo.FileFieldRepository) CSVConfig(com.axelor.data.csv.CSVConfig) MetaSelect(com.axelor.meta.db.MetaSelect) Context(com.axelor.rpc.Context) XStream(com.thoughtworks.xstream.XStream) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) CSVInput(com.axelor.data.csv.CSVInput) CSVWriter(com.opencsv.CSVWriter) Mapper(com.axelor.db.mapper.Mapper) FileTab(com.axelor.apps.base.db.FileTab) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

Example 2 with AdvancedImport

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

the class ValidatorService method validateFieldAndData.

private void validateFieldAndData(DataReaderService reader, String sheet, FileTab fileTab, boolean isConfig, boolean isTabConfig, int tabConfigRowCount) throws ClassNotFoundException, IOException {
    AdvancedImport advancedImport = fileTab.getAdvancedImport();
    Map<String, Object> map = isConfig ? fieldMap : titleMap;
    int linesToIgnore = advancedImport.getNbOfFirstLineIgnore();
    int startIndex = isConfig ? 1 : linesToIgnore;
    String[] row = reader.read(sheet, startIndex, 0);
    if (row == null) {
        return;
    }
    sortFileFieldList(fileTab.getFileFieldList());
    int rowCount = row.length;
    for (int cell = 0; cell < rowCount; cell++) {
        String value = row[cell];
        if (Strings.isNullOrEmpty(value)) {
            continue;
        }
        value = value.trim();
        map.put(isConfig ? value.contains("(") ? value.split("\\(")[0] : value : value, cell);
        if (cell == row.length - 1) {
            this.validateFields(startIndex, isConfig, fileTab);
        }
    }
    if (!advancedImport.getIsValidateValue()) {
        return;
    }
    int totalLines = reader.getTotalLines(sheet);
    startIndex = isConfig ? tabConfigRowCount + 3 : fileTab.getAdvancedImport().getIsHeader() ? linesToIgnore + 1 : linesToIgnore;
    for (int line = startIndex; line < totalLines; line++) {
        String[] dataRow = reader.read(sheet, line, row.length);
        if (dataRow == null) {
            continue;
        }
        this.validateData(dataRow, line, isConfig, fileTab);
    }
}
Also used : AdvancedImport(com.axelor.apps.base.db.AdvancedImport)

Example 3 with AdvancedImport

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

the class AdvancedImportController method importData.

public void importData(ActionRequest request, ActionResponse response) {
    try {
        AdvancedImport advancedImport = request.getContext().asType(AdvancedImport.class);
        if (advancedImport.getId() != null) {
            advancedImport = Beans.get(AdvancedImportRepository.class).find(advancedImport.getId());
        }
        MetaFile logFile = Beans.get(DataImportService.class).importData(advancedImport);
        if (logFile != null) {
            response.setValue("errorLog", logFile);
        } else {
            response.setValue("errorLog", null);
            response.setFlash(I18n.get(IExceptionMessage.ADVANCED_IMPORT_IMPORT_DATA));
            response.setSignal("refresh-app", true);
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : DataImportService(com.axelor.apps.base.service.advanced.imports.DataImportService) MetaFile(com.axelor.meta.db.MetaFile) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) AxelorException(com.axelor.exception.AxelorException)

Example 4 with AdvancedImport

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

the class AdvancedImportController method validate.

public void validate(ActionRequest request, ActionResponse response) {
    try {
        AdvancedImport advancedImport = request.getContext().asType(AdvancedImport.class);
        if (advancedImport.getId() != null) {
            advancedImport = Beans.get(AdvancedImportRepository.class).find(advancedImport.getId());
        }
        boolean isLog = Beans.get(ValidatorService.class).validate(advancedImport);
        if (isLog) {
            response.setFlash(I18n.get(IExceptionMessage.ADVANCED_IMPORT_CHECK_LOG));
            response.setReload(true);
        } else {
            response.setValue("statusSelect", 1);
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : ValidatorService(com.axelor.apps.base.service.advanced.imports.ValidatorService) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) AxelorException(com.axelor.exception.AxelorException)

Example 5 with AdvancedImport

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

the class AdvancedImportController method apply.

public void apply(ActionRequest request, ActionResponse response) throws AxelorException {
    try {
        AdvancedImport advancedImport = request.getContext().asType(AdvancedImport.class);
        if (advancedImport.getId() != null) {
            advancedImport = Beans.get(AdvancedImportRepository.class).find(advancedImport.getId());
        }
        boolean isValid = Beans.get(AdvancedImportService.class).apply(advancedImport);
        if (isValid) {
            response.setReload(true);
        } else {
            response.setFlash(I18n.get(IExceptionMessage.ADVANCED_IMPORT_FILE_FORMAT_INVALID));
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : AdvancedImportService(com.axelor.apps.base.service.advanced.imports.AdvancedImportService) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) AxelorException(com.axelor.exception.AxelorException)

Aggregations

AdvancedImport (com.axelor.apps.base.db.AdvancedImport)8 AxelorException (com.axelor.exception.AxelorException)6 FileField (com.axelor.apps.base.db.FileField)2 FileTab (com.axelor.apps.base.db.FileTab)2 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)2 AdvancedImportService (com.axelor.apps.base.service.advanced.imports.AdvancedImportService)2 DataReaderFactory (com.axelor.apps.tool.reader.DataReaderFactory)2 DataReaderService (com.axelor.apps.tool.reader.DataReaderService)2 Inflector (com.axelor.common.Inflector)2 Model (com.axelor.db.Model)2 Mapper (com.axelor.db.mapper.Mapper)2 Property (com.axelor.db.mapper.Property)2 TraceBackService (com.axelor.exception.service.TraceBackService)2 MetaFiles (com.axelor.meta.MetaFiles)2 MetaFile (com.axelor.meta.db.MetaFile)2 Strings (com.google.common.base.Strings)2 Files (com.google.common.io.Files)2 Inject (com.google.inject.Inject)2 FileTabRepository (com.axelor.apps.base.db.repo.FileTabRepository)1 IExceptionMessage (com.axelor.apps.base.exceptions.IExceptionMessage)1