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