use of org.summerb.easycrud.api.dto.datapackage.DataSet 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.easycrud.api.dto.datapackage.DataSet 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;
}
use of org.summerb.easycrud.api.dto.datapackage.DataSet in project summerb by skarpushin.
the class DataSetLoaderImpl method populateBackReferencesMany2Many.
private void populateBackReferencesMany2Many(ManyToManyRefToReferenceesMap manyToManyRefs, DataSet dataSet) {
for (Entry<Ref, Map<Object, List<HasId>>> refToReferenceeListPair : manyToManyRefs.entrySet()) {
DataTable sourceTable = dataSet.get(refToReferenceeListPair.getKey().getFromEntity());
@SuppressWarnings("deprecation") RowIdToBackReferencesMap backRefs = sourceTable.getBackRefs();
for (Entry<Object, List<HasId>> referencerToReferencesListPair : refToReferenceeListPair.getValue().entrySet()) {
Object referencerId = referencerToReferencesListPair.getKey();
RefToReferencedObjectsIdsMap refToObjsMap = backRefs.get(referencerId);
if (refToObjsMap == null) {
backRefs.put(referencerId, refToObjsMap = new RefToReferencedObjectsIdsMap());
}
String refName = refToReferenceeListPair.getKey().getName();
Set<Object> referenceeIdsList = refToObjsMap.get(refName);
if (referenceeIdsList == null) {
refToObjsMap.put(refName, referenceeIdsList = new HashSet<>());
}
Set referenceeIds = EasyCrudDtoUtils.enumerateIds(referencerToReferencesListPair.getValue());
referenceeIdsList.addAll(referenceeIds);
}
}
}
use of org.summerb.easycrud.api.dto.datapackage.DataSet 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.easycrud.api.dto.datapackage.DataSet in project summerb by skarpushin.
the class DataSetLoaderTest method testResolveReferencedObjects_ExpectWillNotTryToLoadNullReferences.
@Test
public void testResolveReferencedObjects_ExpectWillNotTryToLoadNullReferences() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
DataSetLoader loader = buildLoaderCase1();
TestDto2 d2i1 = new TestDto2();
d2i1.setEnv("required");
d2i1.setLinkToFullDonwload("required");
d2i1 = testDto2Service.create(d2i1);
TestDto3 d3i1 = new TestDto3();
d3i1.setLinkToDtoTwo(d2i1.getId());
d3i1 = testDto3Service.create(d3i1);
DataSet result = new DataSet();
Map<String, Set<Object>> idsToLoad = new HashMap<String, Set<Object>>();
idsToLoad.put(testDto3Service.getEntityTypeMessageCode(), ids(d3i1.getId()));
loader.loadObjectsByIds(idsToLoad, result);
loader.resolveReferencedObjects(result, Refs.ref3to2mand, Refs.ref3to1);
assertNotNull(result.get(testDto2Service.getEntityTypeMessageCode()).find(d2i1.getId()));
}
Aggregations