use of org.summerb.approaches.jdbccrud.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());
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.approaches.jdbccrud.api.dto.datapackage.DataSet in project summerb by skarpushin.
the class EasyCrudRestControllerBase method resolveReferences.
private void resolveReferences(List<String> referencesToResolve, CrudQueryResult<TId, TDto> ret, List<TDto> items) throws EntityNotFoundException, NotAuthorizedException {
Preconditions.checkState(dataSetLoader != null, "DataSetLoader is required to resolve references");
Preconditions.checkState(referencesRegistry != null, "referencesRegistry is required to resolve references");
DataSet ds = new DataSet();
DataTable<TId, TDto> table = new DataTable<>(service.getEntityTypeMessageCode());
table.putAll(items);
ds.getTables().put(table.getName(), table);
List<Ref> references = referencesToResolve.stream().map(name -> referencesRegistry.getRefByName(name)).collect(Collectors.toList());
Ref[] refsArr = (Ref[]) references.toArray(new Ref[references.size()]);
dataSetLoader.resolveReferencedObjects(ds, refsArr);
// now remove initial table from dataset because we don't want to
// duplicate this. It's already populated to rows
ds.getTables().remove(table.getName());
// x. ret
ret.setRefsResolved(references.stream().collect(Collectors.toMap(Ref::getName, Function.identity())));
ret.setRefs(ds);
}
use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet in project summerb by skarpushin.
the class DataSetLoaderImpl method resolveReferencedObjects.
@Override
public void resolveReferencedObjects(DataSet dataSet, Ref... references) throws EntityNotFoundException, NotAuthorizedException {
Preconditions.checkArgument(dataSet != null);
Preconditions.checkArgument(references != null);
DataSet nextDataSet = dataSet;
while (nextDataSet != null) {
DataSet curDataSet = nextDataSet;
nextDataSet = null;
DataSet newDataSet = new DataSet();
Map<String, Set<Object>> idsToLoad = enumerateOutgoingReferences(curDataSet, dataSet, references);
if (!idsToLoad.isEmpty()) {
EntityTypeToObjectsMap loadObjects = loadObjectsByIds(idsToLoad);
addAllObjects(loadObjects, dataSet);
addAllObjects(loadObjects, newDataSet);
}
// Handle one2many
Map<Ref, Set<Object>> oneToManyReferencesToLoad = enumerateOneToManyReferences(curDataSet, references);
if (!oneToManyReferencesToLoad.isEmpty()) {
EntityTypeToObjectsMap loadedOneToManyRefs = loadOneToManyReferences(oneToManyReferencesToLoad);
addAllObjects(loadedOneToManyRefs, dataSet);
addAllObjects(loadedOneToManyRefs, newDataSet);
populateBackReferencesOne2Many(loadedOneToManyRefs, oneToManyReferencesToLoad, dataSet);
}
// Handle many2many
Map<Ref, Set<Object>> manyToManyRefToReferencersIds = enumerateManyToManyReferences(curDataSet, references);
if (!manyToManyRefToReferencersIds.isEmpty()) {
ManyToManyRefToReferenceesMap loadedManyToManyRefs = loadManyToManyReferences(manyToManyRefToReferencersIds);
addAllObjects(loadedManyToManyRefs, dataSet);
addAllObjects(loadedManyToManyRefs, newDataSet);
populateBackReferencesMany2Many(loadedManyToManyRefs, dataSet);
}
// next iteration
if (!newDataSet.isEmpty()) {
nextDataSet = newDataSet;
}
}
}
use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet in project summerb by skarpushin.
the class DataSetLoaderImplTest method testLoadObjectsByIds_ExpectTwoDifferentObjectsLoadedOk.
@Test
public void testLoadObjectsByIds_ExpectTwoDifferentObjectsLoadedOk() throws Exception {
DataSetLoaderImpl fixture = buildMockedInstance();
EasyCrudService service1 = Mockito.mock(EasyCrudService.class);
when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto1")).thenReturn(service1);
TestDto1 dto1 = new TestDto1();
dto1.setId("d1");
when(service1.findById("d1")).thenReturn(dto1);
EasyCrudService service2 = Mockito.mock(EasyCrudService.class);
when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto2")).thenReturn(service2);
TestDto2 dto2 = new TestDto2();
dto2.setId(2L);
when(service2.findById(2L)).thenReturn(dto2);
DataSet ret = new DataSet();
Map<String, Set<Object>> ids = new HashMap<>();
ids.put("dto1", ids("d1"));
ids.put("dto2", ids(2L));
fixture.loadObjectsByIds(ids, ret);
assertNotNull(ret.get("dto1"));
assertNotNull(ret.get("dto1").find("d1"));
assertNotNull(ret.get("dto2"));
assertNotNull(ret.get("dto2").find(2L));
}
use of org.summerb.approaches.jdbccrud.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