Search in sources :

Example 1 with DataSet

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet in project summerb by skarpushin.

the class DataSetLoaderTest method testResolveReferencedObjects_ExpectOneToManyTwoParentsOneChildLoadedOk.

@Test
public void testResolveReferencedObjects_ExpectOneToManyTwoParentsOneChildLoadedOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("required");
    d2i1.setLinkToFullDonwload("required");
    d2i1 = testDto2Service.create(d2i1);
    TestDto1 d1i1 = new TestDto1();
    d1i1.setEnv("required");
    d1i1.setLinkToFullDonwload("required");
    d1i1 = testDto1Service.create(d1i1);
    TestDto3 d3i1 = new TestDto3();
    d3i1.setLinkToDtoTwo(d2i1.getId());
    d3i1 = testDto3Service.create(d3i1);
    TestDto3 d3i2 = new TestDto3();
    d3i2.setLinkToDtoTwo(d2i1.getId());
    d3i2.setLinkToDtoOneOptional(d1i1.getId());
    d3i2 = testDto3Service.create(d3i2);
    DataSet result = new DataSet();
    result.get(testDto2Service.getEntityTypeMessageCode()).put(d2i1);
    result.get(testDto1Service.getEntityTypeMessageCode()).put(d1i1);
    loader.resolveReferencedObjects(result, Refs.ref3to2mand.reverse(), Refs.ref3to1.reverse());
    assertNotNull(result.get(testDto3Service.getEntityTypeMessageCode()).find(d3i1.getId()));
    assertNotNull(result.get(testDto3Service.getEntityTypeMessageCode()).find(d3i2.getId()));
}
Also used : DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) DataSetLoader(org.summerb.approaches.jdbccrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 2 with DataSet

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet in project summerb by skarpushin.

the class DataSetLoaderTest method testResolveReferencedObjects_ExpectM2mLoadedOkOk.

@Test
public void testResolveReferencedObjects_ExpectM2mLoadedOkOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto3 d3i1 = new TestDto3();
    // non-existing link
    d3i1.setLinkToDtoTwo(1L);
    d3i1 = testDto3Service.create(d3i1);
    TestDto1 d1i1 = new TestDto1();
    d1i1.setEnv(d3i1.getId());
    d1i1.setLinkToFullDonwload("required");
    d1i1 = testDto1Service.create(d1i1);
    TestDto1 d1i2 = new TestDto1();
    d1i2.setEnv(d3i1.getId());
    d1i2.setLinkToFullDonwload("required");
    d1i2 = testDto1Service.create(d1i2);
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("required");
    d2i1.setLinkToFullDonwload("required");
    d2i1 = testDto2Service.create(d2i1);
    m2mService.addReferencee(d2i1.getId(), d1i1.getId());
    m2mService.addReferencee(d2i1.getId(), d1i2.getId());
    // -----------------------------------
    String t1 = testDto1Service.getEntityTypeMessageCode();
    String t2 = testDto2Service.getEntityTypeMessageCode();
    String t3 = testDto3Service.getEntityTypeMessageCode();
    DataSet result = new DataSet();
    loader.loadObjectAndItsRefs(d2i1.getId(), t2, result, Refs.ref2to1m2m, Refs.ref1to3);
    assertNotNull(result.get(t2).find(d2i1.getId()));
    assertNotNull(result.get(t1).find(d1i1.getId()));
    assertNotNull(result.get(t1).find(d1i2.getId()));
    assertNotNull(result.get(t3).find(d3i1.getId()));
    Set<Object> d2refsTo1 = result.get(t2).getBackRefs().getForRow(d2i1).getForRef(Refs.ref2to1m2m);
    assertEquals(2, d2refsTo1.size());
    assertTrue(d2refsTo1.contains(d1i1.getId()));
    assertTrue(d2refsTo1.contains(d1i2.getId()));
}
Also used : DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) DataSetLoader(org.summerb.approaches.jdbccrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 3 with DataSet

use of org.summerb.approaches.jdbccrud.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.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 4 with DataSet

use of org.summerb.approaches.jdbccrud.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.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 5 with DataSet

use of org.summerb.approaches.jdbccrud.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.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

DataSet (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)15 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Test (org.junit.Test)9 DataSetLoader (org.summerb.approaches.jdbccrud.api.relations.DataSetLoader)9 HashMap (java.util.HashMap)8 Ref (org.summerb.approaches.jdbccrud.api.dto.relations.Ref)6 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)3 DataTable (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable)3 List (java.util.List)2 EasyCrudService (org.summerb.approaches.jdbccrud.api.EasyCrudService)2 PaginatedList (org.summerb.approaches.jdbccrud.api.dto.PaginatedList)2 Preconditions (com.google.common.base.Preconditions)1 TestDto1 (integr.org.summerb.jdbccrud.TestDto1)1 TestDto2 (integr.org.summerb.jdbccrud.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