use of com.axelor.db.JpaRepository 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.JpaRepository 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();
}
}
Aggregations