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;
}
Aggregations