use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.
the class DataSetLoaderImpl method enumerateManyToManyReferences.
private Map<Ref, Set<Object>> enumerateManyToManyReferences(DataSet dataSet, Ref[] references) {
Map<Ref, Set<Object>> ret = new HashMap<>();
for (Ref ref : references) {
if (!ref.isManyToMany()) {
continue;
}
String entityTypeCode = ref.getFromEntity();
Set<Object> ids = EasyCrudDtoUtils.enumerateIds(dataSet.get(entityTypeCode).getRows().values());
if (ids.size() != 0) {
ret.put(ref, ids);
}
}
return ret;
}
use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.
the class DataSetLoaderImpl method enumerateOutgoingReferences.
/**
* @param scanForReferences
* dataSet to scan for Many2one & One2one referenced objects
* @param checkForExistence
* data set that contains already loaded objects so that we can
* skip loading these objects again
* @param references
* references to use
* @return map entityTypeCode to list of ids of these entities to be loaded
*/
private Map<String, Set<Object>> enumerateOutgoingReferences(DataSet scanForReferences, DataSet checkForExistence, Ref[] references) {
Map<String, Set<Object>> ret = new HashMap<>();
for (DataTable table : scanForReferences.getTables().values()) {
List<Ref> outgoingRefs = enumOutgoingRefsToTableOrNull(references, table.getName());
if (outgoingRefs == null) {
continue;
}
for (Object rowObj : table.getRows().values()) {
HasId row = (HasId) rowObj;
PropertyAccessor propertyAccessor = PropertyAccessorFactory.forBeanPropertyAccess(row);
for (Ref ref : outgoingRefs) {
Object referencedId = null;
try {
referencedId = propertyAccessor.getPropertyValue(ref.getFromField());
} catch (Throwable t) {
throw new RuntimeException("Failed to read property " + ref.getFromField() + " from " + row, t);
}
if (referencedId == null) {
continue;
}
if (checkForExistence.get(ref.getToEntity()).find(referencedId) != null) {
// that one is already loaded, skip
continue;
}
Set<Object> referencedIds = ret.get(ref.getToEntity());
if (referencedIds == null) {
ret.put(ref.getToEntity(), referencedIds = new HashSet<>());
}
referencedIds.add(referencedId);
}
}
}
return ret;
}
use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.
the class DataSetLoaderImpl method loadManyToManyReferences.
private ManyToManyRefToReferenceesMap loadManyToManyReferences(Map<Ref, Set<Object>> manyToManyReferences) {
// NOTE: Queries to same types of referencees are not grouped. Should we
// impl this like we did for one2many?
ManyToManyRefToReferenceesMap ret = new ManyToManyRefToReferenceesMap();
for (Entry<Ref, Set<Object>> refToReferencersEntry : manyToManyReferences.entrySet()) {
EasyCrudService m2mServiceTmp = easyCrudServiceResolver.resolveByEntityType(refToReferencersEntry.getKey().getM2mEntity());
Preconditions.checkState(m2mServiceTmp instanceof EasyCrudM2mService);
EasyCrudM2mService m2mService = (EasyCrudM2mService) m2mServiceTmp;
Map<Object, List<HasId>> referenceeMap = m2mService.findReferenceeByReferencers(refToReferencersEntry.getValue());
ret.put(refToReferencersEntry.getKey(), referenceeMap);
}
return ret;
}
use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.
the class DataSetLoaderImpl method populateBackReferencesOne2Many.
private void populateBackReferencesOne2Many(EntityTypeToObjectsMap rowsMap, Map<Ref, Set<Object>> refs, DataSet dataSet) {
for (Entry<String, List<HasId>> entry : rowsMap.entrySet()) {
for (HasId row : entry.getValue()) {
PropertyAccessor propertyAccessor = PropertyAccessorFactory.forBeanPropertyAccess(row);
for (Ref ref : refs.keySet()) {
if (!ref.getToEntity().equals(entry.getKey())) {
continue;
}
Object referencedId = null;
try {
referencedId = propertyAccessor.getPropertyValue(ref.getToField());
} catch (Throwable t) {
throw new RuntimeException("Failed to read property " + ref.getFromField() + " from " + row, t);
}
if (referencedId == null) {
continue;
}
RowIdToBackReferencesMap backRefs = dataSet.get(ref.getFromEntity()).getBackRefs();
if (backRefs.get(referencedId) == null) {
backRefs.put(referencedId, new RefToReferencedObjectsIdsMap());
}
RefToReferencedObjectsIdsMap refToObjsMap = backRefs.get(referencedId);
if (refToObjsMap.get(ref.getName()) == null) {
refToObjsMap.put(ref.getName(), new HashSet<>());
}
refToObjsMap.get(ref.getName()).add(row.getId());
}
}
}
}
use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.
the class DataSetLoaderImpl method enumerateOneToManyReferences.
private Map<Ref, Set<Object>> enumerateOneToManyReferences(DataSet dataSet, Ref[] references) {
Map<Ref, Set<Object>> ret = new HashMap<>();
for (Ref ref : references) {
if (!ref.isOneToMany()) {
continue;
}
String entityTypeCode = ref.getFromEntity();
Set<Object> ids = EasyCrudDtoUtils.enumerateIds(dataSet.get(entityTypeCode).getRows().values());
if (!ids.isEmpty()) {
ret.put(ref, ids);
}
}
return ret;
}
Aggregations