Search in sources :

Example 1 with StringUtils

use of com.axelor.common.StringUtils 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)

Aggregations

AdvancedImport (com.axelor.apps.base.db.AdvancedImport)1 FileField (com.axelor.apps.base.db.FileField)1 FileTab (com.axelor.apps.base.db.FileTab)1 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)1 ImporterListener (com.axelor.apps.base.service.imports.listener.ImporterListener)1 DataReaderFactory (com.axelor.apps.tool.reader.DataReaderFactory)1 DataReaderService (com.axelor.apps.tool.reader.DataReaderService)1 TranslationService (com.axelor.apps.tool.service.TranslationService)1 Inflector (com.axelor.common.Inflector)1 StringUtils (com.axelor.common.StringUtils)1 XStreamUtils (com.axelor.data.XStreamUtils)1 DataAdapter (com.axelor.data.adapter.DataAdapter)1 JavaTimeAdapter (com.axelor.data.adapter.JavaTimeAdapter)1 CSVBind (com.axelor.data.csv.CSVBind)1 CSVConfig (com.axelor.data.csv.CSVConfig)1 CSVImporter (com.axelor.data.csv.CSVImporter)1 CSVInput (com.axelor.data.csv.CSVInput)1 Model (com.axelor.db.Model)1 Mapper (com.axelor.db.mapper.Mapper)1 Property (com.axelor.db.mapper.Property)1