Search in sources :

Example 1 with DataSet

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

Example 2 with DataSet

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

Example 3 with DataSet

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

Example 4 with DataSet

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;
}
Also used : DataTable(org.summerb.easycrud.api.dto.datapackage.DataTable) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) PropertyAccessor(org.springframework.beans.PropertyAccessor) HashMap(java.util.HashMap) HasId(org.summerb.easycrud.api.dto.HasId) Ref(org.summerb.easycrud.api.dto.relations.Ref) HashSet(java.util.HashSet)

Example 5 with DataSet

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

Aggregations

DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)16 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Test (org.junit.Test)9 DataSetLoader (org.summerb.easycrud.api.relations.DataSetLoader)9 Ref (org.summerb.easycrud.api.dto.relations.Ref)7 HasId (org.summerb.easycrud.api.dto.HasId)4 DataTable (org.summerb.easycrud.api.dto.datapackage.DataTable)3 List (java.util.List)2 EasyCrudService (org.summerb.easycrud.api.EasyCrudService)2 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)2 Preconditions (com.google.common.base.Preconditions)1 TestDto1 (integr.org.summerb.easycrud.TestDto1)1 TestDto2 (integr.org.summerb.easycrud.TestDto2)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Function (java.util.function.Function)1