Search in sources :

Example 1 with RowIdToBackReferencesMap

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap 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());
            }
        }
    }
}
Also used : HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) PropertyAccessor(org.springframework.beans.PropertyAccessor) RefToReferencedObjectsIdsMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) PaginatedList(org.summerb.approaches.jdbccrud.api.dto.PaginatedList) List(java.util.List) RowIdToBackReferencesMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap)

Example 2 with RowIdToBackReferencesMap

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap 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);
        }
    }
}
Also used : DataTable(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) RefToReferencedObjectsIdsMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap) HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) PaginatedList(org.summerb.approaches.jdbccrud.api.dto.PaginatedList) List(java.util.List) HashMap(java.util.HashMap) RowIdToBackReferencesMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap) Map(java.util.Map) RefToReferencedObjectsIdsMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap) RowIdToBackReferencesMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)2 PaginatedList (org.summerb.approaches.jdbccrud.api.dto.PaginatedList)2 RefToReferencedObjectsIdsMap (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap)2 RowIdToBackReferencesMap (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap)2 Ref (org.summerb.approaches.jdbccrud.api.dto.relations.Ref)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 PropertyAccessor (org.springframework.beans.PropertyAccessor)1 DataSet (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)1 DataTable (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable)1