Search in sources :

Example 1 with Ref

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;
}
Also used : Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) HashMap(java.util.HashMap)

Example 2 with Ref

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;
}
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) PropertyAccessor(org.springframework.beans.PropertyAccessor) HashMap(java.util.HashMap) HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) HashSet(java.util.HashSet)

Example 3 with Ref

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;
}
Also used : Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) EasyCrudM2mService(org.summerb.approaches.jdbccrud.api.relations.EasyCrudM2mService) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) PaginatedList(org.summerb.approaches.jdbccrud.api.dto.PaginatedList) List(java.util.List)

Example 4 with Ref

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());
            }
        }
    }
}
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 5 with Ref

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;
}
Also used : Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) HashMap(java.util.HashMap)

Aggregations

Ref (org.summerb.approaches.jdbccrud.api.dto.relations.Ref)11 DataSet (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)8 HashSet (java.util.HashSet)7 Set (java.util.Set)7 HashMap (java.util.HashMap)5 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 PaginatedList (org.summerb.approaches.jdbccrud.api.dto.PaginatedList)4 LinkedList (java.util.LinkedList)3 EasyCrudService (org.summerb.approaches.jdbccrud.api.EasyCrudService)3 DataTable (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable)3 PropertyAccessor (org.springframework.beans.PropertyAccessor)2 RefToReferencedObjectsIdsMap (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap)2 RowIdToBackReferencesMap (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap)2 Query (org.summerb.approaches.jdbccrud.api.query.Query)2 Preconditions (com.google.common.base.Preconditions)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1