use of com.axelor.rpc.JsonContext 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.rpc.JsonContext 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.rpc.JsonContext 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