use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method removeSubRecords.
@SuppressWarnings("unchecked")
@Transactional
public void removeSubRecords(Class<? extends Model> klass, JsonContext jsonContext) throws ClassNotFoundException {
for (Property prop : Mapper.of(klass).getProperties()) {
if (prop.getTarget() == null || prop.isCollection()) {
continue;
}
String simpleModelName = StringUtils.substringAfterLast(prop.getTarget().getName(), ".");
String field = inflector.camelize(simpleModelName, true) + "Set";
if (!jsonContext.containsKey(field)) {
continue;
}
List<Object> recList = (List<Object>) jsonContext.get(field);
String ids = recList.stream().map(obj -> {
Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
return recordMap.get("id").toString();
}).collect(Collectors.joining(","));
JpaRepository<? extends Model> modelRepo = JpaRepository.of((Class<? extends Model>) Class.forName(prop.getTarget().getName()));
modelRepo.all().filter("self.id IN (" + ids + ")").delete();
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class AdvancedImportServiceImpl method resetSubPropertyValue.
@SuppressWarnings("unchecked")
private void resetSubPropertyValue(Class<? extends Model> klass, JsonContext jsonContext) throws ClassNotFoundException {
for (Property prop : Mapper.of(klass).getProperties()) {
if (prop.getTarget() == null || prop.isRequired()) {
continue;
}
String simpleModelName = StringUtils.substringAfterLast(prop.getTarget().getName(), ".");
String field = inflector.camelize(simpleModelName, true) + "Set";
if (!jsonContext.containsKey(field)) {
continue;
}
List<Object> recordList = (List<Object>) jsonContext.get(field);
Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(prop.getTarget().getName());
this.resetPropertyValue(modelKlass, recordList);
}
}
use of com.axelor.db.Model 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.db.Model 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);
}
}
}
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class DataImportServiceImpl method createJsonContext.
@Override
public Map<String, Object> createJsonContext(FileTab fileTab) {
Class<? extends Model> klass = (Class<? extends Model>) fileTab.getClass();
Context context = new Context(klass);
JsonContext jsonContext = new JsonContext(context, Mapper.of(klass).getProperty("attrs"), fileTab.getAttrs());
Map<String, Object> _map = new HashMap<String, Object>();
_map.put("context", context);
_map.put("jsonContext", jsonContext);
return _map;
}
Aggregations