use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method process.
@Transactional
public boolean process(DataReaderService reader, AdvancedImport advancedImport) throws AxelorException, ClassNotFoundException {
boolean isValid = true;
if (!CollectionUtils.isEmpty(advancedImport.getFileTabList())) {
advancedImport.getFileTabList().stream().forEach(tab -> tab.clearFileFieldList());
advancedImport.clearFileTabList();
}
String[] sheets = reader.getSheetNames();
boolean isConfig = advancedImport.getIsConfigInFile();
boolean isTabConfig = advancedImport.getIsFileTabConfigAdded();
boolean isHeader = advancedImport.getIsHeader();
int linesToIgnore = advancedImport.getNbOfFirstLineIgnore();
int tabConfigRowCount = 0;
int startIndex = isConfig ? 0 : linesToIgnore;
int fileTabSequence = 1;
for (String sheet : sheets) {
int totalLines = reader.getTotalLines(sheet);
if (totalLines == 0) {
continue;
}
FileTab fileTab = new FileTab();
fileTab.setName(sheet);
fileTab.setSequence(fileTabSequence);
fileTabSequence++;
String[] objectRow = reader.read(sheet, startIndex, 0);
if (objectRow == null) {
isValid = false;
break;
}
if (isConfig && isTabConfig) {
tabConfigRowCount = getTabConfigRowCount(sheet, reader, totalLines, objectRow);
}
isValid = this.applyObject(objectRow, fileTab, isConfig, linesToIgnore, isTabConfig);
if (!isValid) {
break;
}
List<FileField> fileFieldList = new ArrayList<>();
List<Integer> ignoreFields = new ArrayList<Integer>();
for (int line = isConfig ? 1 : linesToIgnore; line < totalLines; line++) {
String[] row = reader.read(sheet, line, isConfig ? 0 : objectRow.length);
if (row == null) {
continue;
}
if (isConfig) {
this.applyWithConfig(row, line, fileFieldList, ignoreFields, fileTab, isTabConfig, tabConfigRowCount);
} else {
this.applyWithoutConfig(row, (line - linesToIgnore), fileFieldList, fileTab, isHeader);
}
}
if (isConfig) {
fileFieldList.removeIf(field -> field.getImportField() == null);
if (!fileTab.getImportType().equals(FileFieldRepository.IMPORT_TYPE_NEW)) {
fileTab = this.setSearchField(fileTab, searchFieldList, fileFieldList);
}
}
advancedImport.addFileTabListItem(fileTab);
advancedImportRepository.save(advancedImport);
}
return isValid;
}
use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method resetRelationalFields.
@SuppressWarnings("unchecked")
public boolean resetRelationalFields(List<FileTab> fileTabList) throws ClassNotFoundException {
boolean isResetValue = false;
for (FileTab fileTab : fileTabList) {
Map<String, Object> jsonContextMap = dataImportService.createJsonContext(fileTab);
JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
String fieldName = inflector.camelize(fileTab.getMetaModel().getName(), true) + "Set";
List<Object> recordList = (List<Object>) jsonContext.get(fieldName);
if (CollectionUtils.isEmpty(recordList)) {
continue;
}
isResetValue = true;
Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(fileTab.getMetaModel().getFullName());
this.resetPropertyValue(modelKlass, recordList);
this.resetSubPropertyValue(modelKlass, jsonContext);
}
return isResetValue;
}
use of com.axelor.apps.base.db.FileTab in project axelor-open-suite by axelor.
the class ValidatorService method validateFields.
private void validateFields(int line, boolean isConfig, FileTab fileTab) throws IOException, ClassNotFoundException {
List<String> relationalFieldList = fileTab.getFileFieldList().stream().filter(field -> !Strings.isNullOrEmpty(field.getSubImportField())).map(field -> field.getImportField().getName() + "." + field.getSubImportField()).collect(Collectors.toList());
for (FileField fileField : fileTab.getFileFieldList()) {
MetaField importField = fileField.getImportField();
if (importField != null && Strings.isNullOrEmpty(fileField.getSubImportField())) {
if (importField.getRelationship() != null) {
logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_4, importField.getName(), line);
}
this.validateImportRequiredField(line, Class.forName(fileTab.getMetaModel().getFullName()), importField.getName(), fileField, null);
this.validateDateField(line, fileField);
} else if (!Strings.isNullOrEmpty(fileField.getSubImportField())) {
Mapper mapper = advancedImportService.getMapper(importField.getMetaModel().getFullName());
Property parentProp = mapper.getProperty(importField.getName());
if (parentProp == null) {
return;
}
Property subProperty = this.getAndValidateSubField(line, parentProp, fileField, false);
if (subProperty != null) {
this.validateImportRequiredField(line, subProperty.getEntity(), subProperty.getName(), fileField, relationalFieldList);
this.validateDateField(line, fileField);
}
}
}
}
use of com.axelor.apps.base.db.FileTab 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