use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method resetPropertyValue.
@Transactional
private void resetPropertyValue(Class<? extends Model> klass, List<Object> recordList) throws ClassNotFoundException {
JpaRepository<? extends Model> modelRepo = JpaRepository.of(klass);
for (Property prop : Mapper.of(klass).getProperties()) {
if (prop.getTarget() == null || prop.isRequired()) {
continue;
}
for (Object obj : recordList) {
Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
Long id = Long.valueOf(recordMap.get("id").toString());
Object bean = modelRepo.find(id);
if (bean != null) {
prop.set(bean, null);
}
}
}
}
use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method removeRecord.
@SuppressWarnings("unchecked")
@Transactional
public void removeRecord(FileTab fileTab, Class<? extends Model> modelKlass, List<Object> recordList, List<FileTab> fileTabList) throws ClassNotFoundException {
JpaRepository<? extends Model> modelRepo = JpaRepository.of(modelKlass);
for (FileTab tab : fileTabList) {
Map<String, Object> jsonContextMap = dataImportService.createJsonContext(tab);
JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
String fieldName = inflector.camelize(tab.getMetaModel().getName(), true) + "Set";
List<Object> recList = (List<Object>) jsonContext.get(fieldName);
if (CollectionUtils.isEmpty(recList)) {
continue;
}
Class<? extends Model> klass = (Class<? extends Model>) Class.forName(tab.getMetaModel().getFullName());
Property[] props = Mapper.of(klass).getProperties();
for (Property prop : props) {
if (prop.getTarget() != null && prop.getTarget() == modelKlass && prop.isRequired()) {
removeRecord(tab, klass, recList, fileTabList);
}
}
}
String ids = recordList.stream().map(obj -> {
Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
return recordMap.get("id").toString();
}).collect(Collectors.joining(","));
modelRepo.all().filter("self.id IN (" + ids + ")").delete();
fileTab.setAttrs(null);
LOG.debug("Reset imported data : {}", modelKlass.getSimpleName());
}
use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.
the class ValidatorService method validateImportRequiredField.
private void validateImportRequiredField(int line, Class<?> model, String fieldName, FileField fileField, List<String> relationalFieldList) throws IOException, ClassNotFoundException {
Mapper mapper = advancedImportService.getMapper(model.getName());
String field = getField(fileField);
Integer rowNum = fileField.getIsMatchWithFile() ? line : null;
int importType = fileField.getImportType();
for (Property prop : mapper.getProperties()) {
if (prop.isRequired()) {
if (prop.getName().equals(fieldName) && importType == FileFieldRepository.IMPORT_TYPE_IGNORE_EMPTY) {
logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_5, field, rowNum);
} else if ((importType == FileFieldRepository.IMPORT_TYPE_FIND_NEW || importType == FileFieldRepository.IMPORT_TYPE_NEW) && field.contains(".") && !fileField.getTargetType().equals("MetaFile")) {
String newField = StringUtils.substringBeforeLast(field, ".");
newField = newField + "." + prop.getName();
if (!relationalFieldList.contains(newField)) {
logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_3, newField, null);
}
}
}
}
}
use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.
the class ValidatorService method validateData.
private void validateData(String[] dataRow, int line, boolean isConfig, FileTab fileTab) throws IOException, ClassNotFoundException {
Map<String, Object> map = isConfig ? fieldMap : titleMap;
for (int fieldIndex = 0; fieldIndex < fileTab.getFileFieldList().size(); fieldIndex++) {
FileField fileField = fileTab.getFileFieldList().get(fieldIndex);
if (!fileField.getIsMatchWithFile() || !Strings.isNullOrEmpty(fileField.getExpression())) {
continue;
}
Mapper mapper = null;
Property property = null;
String key = null;
if (isConfig) {
key = this.getField(fileField);
} else {
key = fileField.getColumnTitle();
}
int cellIndex = 0;
if (map.containsKey(key)) {
cellIndex = (int) map.get(key);
}
cellIndex = (!isConfig && !fileTab.getAdvancedImport().getIsHeader()) ? fieldIndex : cellIndex;
if (fileField.getImportField() != null) {
mapper = advancedImportService.getMapper(fileField.getImportField().getMetaModel().getFullName());
property = mapper.getProperty(fileField.getImportField().getName());
}
if (property != null && Strings.isNullOrEmpty(fileField.getSubImportField())) {
if (this.validateDataRequiredField(dataRow, cellIndex, line, Class.forName(fileTab.getMetaModel().getFullName()), property.getName(), fileField)) {
continue;
}
if (!Strings.isNullOrEmpty(property.getSelection()) && fileField.getForSelectUse() != FileFieldRepository.SELECT_USE_VALUES) {
continue;
}
this.validateDataType(dataRow, cellIndex, line, property.getJavaType().getSimpleName(), fileField);
} else if (!Strings.isNullOrEmpty(fileField.getSubImportField())) {
Property subProperty = this.getAndValidateSubField(line, property, fileField, true);
if (subProperty != null) {
if (this.validateDataRequiredField(dataRow, cellIndex, line, subProperty.getEntity(), subProperty.getName(), fileField)) {
continue;
}
if (!Strings.isNullOrEmpty(subProperty.getSelection()) && fileField.getForSelectUse() != FileFieldRepository.SELECT_USE_VALUES) {
continue;
}
this.validateDataType(dataRow, cellIndex, line, subProperty.getJavaType().getSimpleName(), fileField);
}
}
}
}
use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.
the class DuplicateObjectsService method concatFields.
/*
* get all records for duplicate records
*/
private String concatFields(Class<?> modelClass, Set<String> fieldSet) throws AxelorException {
StringBuilder fields = new StringBuilder("LOWER(concat(");
Mapper mapper = Mapper.of(modelClass);
int count = 0;
for (String field : fieldSet) {
Property property = mapper.getProperty(field);
if (property == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.GENERAL_8), field, modelClass.getSimpleName());
}
if (property.isCollection()) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.GENERAL_9), field);
}
if (count != 0) {
fields.append(",");
}
count++;
fields.append("cast(self" + "." + field);
if (property.getTarget() != null) {
fields.append(".id");
}
fields.append(" as string)");
}
fields.append("))");
return fields.toString();
}
Aggregations