use of com.axelor.apps.base.db.FileField 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.FileField in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method applyWithConfig.
private void applyWithConfig(String[] row, int line, List<FileField> fileFieldList, List<Integer> ignoreFields, FileTab fileTab, Boolean isTabConfig, int tabConfigRowCount) throws AxelorException, ClassNotFoundException {
Mapper mapper = getMapper(fileTab.getMetaModel().getFullName());
FileField fileField = null;
int index = 0;
for (int i = 0; i < row.length; i++) {
if (Strings.isNullOrEmpty(row[i])) {
continue;
}
index = line;
String value = row[i].trim();
if (line == 1) {
this.readFields(value, i, fileFieldList, ignoreFields, mapper, fileTab);
continue;
}
if (ignoreFields.contains(i)) {
continue;
}
if (fileFieldList.size() < i) {
break;
} else if (!isTabConfig && fileFieldList.size() <= i) {
break;
}
if (row[0] == null) {
fileField = fileFieldList.get(i - 1);
} else if (!isTabConfig && row[0] != null) {
fileField = fileFieldList.get(i);
}
if (isTabWithoutConfig) {
fileField = fileFieldList.get(i);
}
if (line == 2) {
fileField.setColumnTitle(value);
continue;
}
if (isTabConfig && i > 0 && row[0] != null && line >= 3 && line <= 11) {
fileField = fileFieldList.get(i - 1);
this.setFileFieldConfig(row, i, fileField);
}
if (isTabConfig && i > 0 && row[0] == null) {
line += -(tabConfigRowCount + 2);
this.setSampleLines(line, value, fileField);
line = index;
} else if (!isTabConfig) {
line += -2;
this.setSampleLines(line, value, fileField);
line = index;
}
}
}
use of com.axelor.apps.base.db.FileField in project axelor-open-suite by axelor.
the class FileTabServiceImpl method updateFields.
@Override
public FileTab updateFields(FileTab fileTab) throws ClassNotFoundException {
MetaModel model = fileTab.getMetaModel();
if (model == null || CollectionUtils.isEmpty(fileTab.getFileFieldList())) {
return fileTab;
}
Beans.get(ValidatorService.class).sortFileFieldList(fileTab.getFileFieldList());
for (FileField fileField : fileTab.getFileFieldList()) {
MetaField importField = metaFieldRepo.all().filter("self.label = ?1 AND self.metaModel.id = ?2", fileField.getColumnTitle(), model.getId()).fetchOne();
if (importField != null) {
String relationship = importField.getRelationship();
if (!Strings.isNullOrEmpty(relationship) && relationship.equals("OneToMany")) {
continue;
}
fileField.setImportField(importField);
if (!Strings.isNullOrEmpty(relationship)) {
String subImportField = this.getSubImportField(importField);
fileField.setSubImportField(subImportField);
}
fileField = fileFieldService.fillType(fileField);
if (!Strings.isNullOrEmpty(relationship) && !fileField.getTargetType().equals("MetaFile")) {
fileField.setImportType(FileFieldRepository.IMPORT_TYPE_FIND);
} else {
if (!Strings.isNullOrEmpty(fileField.getTargetType()) && fileField.getTargetType().equals("MetaFile")) {
fileField.setImportType(FileFieldRepository.IMPORT_TYPE_NEW);
}
}
fileField.setFullName(fileFieldService.computeFullName(fileField));
} else {
fileField.setImportField(null);
fileField.setSubImportField(null);
}
}
return fileTab;
}
use of com.axelor.apps.base.db.FileField 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.FileField in project axelor-open-suite by axelor.
the class ValidatorService method validateObjectRequiredFields.
private void validateObjectRequiredFields(FileTab fileTab) throws ClassNotFoundException, IOException, AxelorException {
if (CollectionUtils.isEmpty(fileTab.getFileFieldList())) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.ADVANCED_IMPORT_NO_FIELDS), fileTab.getName()));
}
List<String> fieldList = new ArrayList<String>();
for (FileField fileField : fileTab.getFileFieldList()) {
if (fileField.getImportField() != null) {
fieldList.add(fileField.getImportField().getName());
} else {
logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_2, fileField.getColumnTitle(), null);
}
}
if (fileTab.getImportType() == FileFieldRepository.IMPORT_TYPE_FIND) {
return;
}
if (fileTab.getMetaModel() != null) {
Mapper mapper = advancedImportService.getMapper(fileTab.getMetaModel().getFullName());
Model obj = null;
try {
obj = (Model) Class.forName(fileTab.getMetaModel().getFullName()).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
TraceBackService.trace(e);
}
for (Property prop : mapper.getProperties()) {
if (prop.isRequired() && !fieldList.contains(prop.getName())) {
if (obj != null && mapper.get(obj, prop.getName()) != null) {
continue;
}
logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_3, prop.getName(), null);
}
}
}
}
Aggregations