use of org.summerb.easycrud.api.dto.datapackage.DataTable 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.DataTable in project summerb by skarpushin.
the class DataSetLoaderImpl method addAllObjects.
private void addAllObjects(EntityTypeToObjectsMap loadedObjects, DataSet dataSet) {
for (Entry<String, List<HasId>> entry : loadedObjects.entrySet()) {
DataTable table = dataSet.get(entry.getKey());
table.putAll(entry.getValue());
}
}
use of org.summerb.easycrud.api.dto.datapackage.DataTable 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.DataTable in project summerb by skarpushin.
the class DataSetUpdaterOnEntityChangedEventImpl method updateDataSet.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void updateDataSet(DataSet dataSet, EntityChangedEvent<?> e) {
if (!e.isTypeOf(HasId.class)) {
return;
}
EasyCrudService service = easyCrudServiceResolver.resolveByDtoClass(e.getValue().getClass());
if (!dataSet.getTables().containsKey(service.getEntityTypeMessageCode())) {
return;
}
DataTable table = dataSet.getTables().get(service.getEntityTypeMessageCode());
HasId dto = (HasId) e.getValue();
if (e.getChangeType() == ChangeType.REMOVED) {
table.getRows().remove(dto.getId());
} else {
table.put(dto);
}
}
use of org.summerb.easycrud.api.dto.datapackage.DataTable in project summerb by skarpushin.
the class EasyCrudRestControllerBase method resolveReferences.
protected 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);
}
Aggregations