use of com.axelor.apps.tool.reader.DataReaderService 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.tool.reader.DataReaderService in project axelor-open-suite by axelor.
the class DmnImportServiceImpl method importDmnTable.
@Override
public void importDmnTable(MetaFile dataFile, WkfDmnModel dmnModel) throws AxelorException {
String extension = Files.getFileExtension(dataFile.getFileName());
if (extension == null || (!extension.equals("xlsx") && !extension.equals("xls"))) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, IExceptionMessage.INVALID_IMPORT_FILE);
}
DataReaderService reader = dataReaderFactory.getDataReader(extension);
reader.initialize(dataFile, null);
this.process(reader, dmnModel);
}
use of com.axelor.apps.tool.reader.DataReaderService in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method apply.
@Override
public boolean apply(AdvancedImport advancedImport) throws AxelorException, ClassNotFoundException {
if (advancedImport.getImportFile() == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.ADVANCED_IMPORT_NO_IMPORT_FILE));
}
String extension = Files.getFileExtension(advancedImport.getImportFile().getFileName());
if (extension == null || (!extension.equals("xlsx") && !extension.equals("xls") && !extension.equals("csv"))) {
return false;
}
DataReaderService reader = dataReaderFactory.getDataReader(extension);
reader.initialize(advancedImport.getImportFile(), advancedImport.getFileSeparator());
return this.process(reader, advancedImport);
}
use of com.axelor.apps.tool.reader.DataReaderService in project axelor-open-suite by axelor.
the class ValidatorService method validate.
public boolean validate(AdvancedImport advancedImport) throws AxelorException, IOException, ClassNotFoundException {
if (advancedImport.getImportFile() == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.ADVANCED_IMPORT_NO_IMPORT_FILE));
}
if (advancedImport.getAttachment() != null && !Files.getFileExtension(advancedImport.getAttachment().getFileName()).equals("zip")) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.ADVANCED_IMPORT_ATTACHMENT_FORMAT));
}
String extension = Files.getFileExtension(advancedImport.getImportFile().getFileName());
if (extension == null || (!extension.equals("xlsx") && !extension.equals("xls") && !extension.equals("csv"))) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.ADVANCED_IMPORT_FILE_FORMAT_INVALID));
}
DataReaderService reader = dataReaderFactory.getDataReader(extension);
reader.initialize(advancedImport.getImportFile(), advancedImport.getFileSeparator());
return validate(reader, advancedImport);
}
use of com.axelor.apps.tool.reader.DataReaderService 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;
}
Aggregations