Search in sources :

Example 6 with DataSet

use of org.summerb.easycrud.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.easycrud.api.dto.datapackage.DataSet) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 7 with DataSet

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

the class DataSetLoaderTest method testLoadObjectsByIds_ExpectCoupleOneLevelObjectsLoadedOK.

@Test
public void testLoadObjectsByIds_ExpectCoupleOneLevelObjectsLoadedOK() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("d2i1");
    d2i1.setLinkToFullDonwload("asdad");
    d2i1 = testDto2Service.create(d2i1);
    TestDto3 d3i1 = new TestDto3();
    d3i1.setLinkToDtoTwo(d2i1.getId());
    d3i1.setLinkToDtoTwo(0);
    d3i1 = testDto3Service.create(d3i1);
    DataSet result = new DataSet();
    Map<String, Set<Object>> idsToLoad = new HashMap<String, Set<Object>>();
    idsToLoad.put(testDto2Service.getEntityTypeMessageCode(), ids(d2i1.getId()));
    idsToLoad.put(testDto3Service.getEntityTypeMessageCode(), ids(d3i1.getId()));
    loader.loadObjectsByIds(idsToLoad, result);
    assertNotNull(result.get(testDto2Service.getEntityTypeMessageCode()).find(d2i1.getId()));
    assertNotNull(result.get(testDto3Service.getEntityTypeMessageCode()).find(d3i1.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)

Example 8 with DataSet

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

the class DataSetLoaderImpl method resolveReferencedObjects.

@Override
public void resolveReferencedObjects(DataSet dataSet, Ref... references) throws EntityNotFoundException, NotAuthorizedException {
    Preconditions.checkArgument(dataSet != null);
    Preconditions.checkArgument(references != null);
    DataSet nextDataSet = dataSet;
    while (nextDataSet != null) {
        DataSet curDataSet = nextDataSet;
        nextDataSet = null;
        DataSet newDataSet = new DataSet();
        Map<String, Set<Object>> idsToLoad = enumerateOutgoingReferences(curDataSet, dataSet, references);
        if (!idsToLoad.isEmpty()) {
            EntityTypeToObjectsMap loadObjects = loadObjectsByIds(idsToLoad);
            addAllObjects(loadObjects, dataSet);
            addAllObjects(loadObjects, newDataSet);
        }
        // Handle one2many
        Map<Ref, Set<Object>> oneToManyReferencesToLoad = enumerateOneToManyReferences(curDataSet, references);
        if (!oneToManyReferencesToLoad.isEmpty()) {
            EntityTypeToObjectsMap loadedOneToManyRefs = loadOneToManyReferences(oneToManyReferencesToLoad);
            addAllObjects(loadedOneToManyRefs, dataSet);
            addAllObjects(loadedOneToManyRefs, newDataSet);
            populateBackReferencesOne2Many(loadedOneToManyRefs, oneToManyReferencesToLoad, dataSet);
        }
        // Handle many2many
        Map<Ref, Set<Object>> manyToManyRefToReferencersIds = enumerateManyToManyReferences(curDataSet, references);
        if (!manyToManyRefToReferencersIds.isEmpty()) {
            ManyToManyRefToReferenceesMap loadedManyToManyRefs = loadManyToManyReferences(manyToManyRefToReferencersIds);
            addAllObjects(loadedManyToManyRefs, dataSet);
            addAllObjects(loadedManyToManyRefs, newDataSet);
            populateBackReferencesMany2Many(loadedManyToManyRefs, dataSet);
        }
        // next iteration
        if (!newDataSet.isEmpty()) {
            nextDataSet = newDataSet;
        }
    }
}
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) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet)

Example 9 with DataSet

use of org.summerb.easycrud.api.dto.datapackage.DataSet 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);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) EasyCrudQueryParams(org.summerb.easycrud.mvc.model.EasyCrudQueryParams) DataTable(org.summerb.easycrud.api.dto.datapackage.DataTable) Autowired(org.springframework.beans.factory.annotation.Autowired) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) PermissionsResolverStrategy(org.summerb.easycrud.rest.permissions.PermissionsResolverStrategy) Ref(org.summerb.easycrud.api.dto.relations.Ref) ReferencesRegistry(org.summerb.easycrud.api.relations.ReferencesRegistry) Function(java.util.function.Function) InitializingBean(org.springframework.beans.factory.InitializingBean) RequestBody(org.springframework.web.bind.annotation.RequestBody) OrderBy(org.summerb.easycrud.api.query.OrderBy) PutMapping(org.springframework.web.bind.annotation.PutMapping) EntityNotFoundException(org.summerb.easycrud.api.exceptions.EntityNotFoundException) GenericFilterBean(org.springframework.web.filter.GenericFilterBean) NotAuthorizedException(org.summerb.security.api.exceptions.NotAuthorizedException) GetMapping(org.springframework.web.bind.annotation.GetMapping) FilteringParamsToQueryConverterImpl(org.summerb.easycrud.mvc.filter.FilteringParamsToQueryConverterImpl) QueryNarrowerStrategy(org.summerb.easycrud.rest.querynarrower.QueryNarrowerStrategy) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Query(org.summerb.easycrud.api.query.Query) MultipleItemsResult(org.summerb.easycrud.rest.dto.MultipleItemsResult) PostMapping(org.springframework.web.bind.annotation.PostMapping) SingleItemResult(org.summerb.easycrud.rest.dto.SingleItemResult) MediaType(org.springframework.http.MediaType) BeansException(org.springframework.beans.BeansException) HasId(org.summerb.easycrud.api.dto.HasId) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) ApiIgnore(springfox.documentation.annotations.ApiIgnore) List(java.util.List) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) CollectionUtils(org.springframework.util.CollectionUtils) PathVariablesMap(org.summerb.easycrud.rest.commonpathvars.PathVariablesMap) PagerParams(org.summerb.easycrud.api.dto.PagerParams) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) CrudQueryResult(org.summerb.easycrud.rest.dto.CrudQueryResult) Preconditions(com.google.common.base.Preconditions) ApplicationContextAware(org.springframework.context.ApplicationContextAware) FilteringParamsToQueryConverter(org.summerb.easycrud.mvc.filter.FilteringParamsToQueryConverter) DataTable(org.summerb.easycrud.api.dto.datapackage.DataTable) Ref(org.summerb.easycrud.api.dto.relations.Ref) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet)

Example 10 with DataSet

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

the class DataSetLoaderTest method testResolveReferencedObjects_ExpectDirectReferencesLoadedOk.

@Test
public void testResolveReferencedObjects_ExpectDirectReferencesLoadedOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto1 d1i1 = new TestDto1();
    d1i1.setEnv("d1i1");
    d1i1.setLinkToFullDonwload("required");
    d1i1 = testDto1Service.create(d1i1);
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv(d1i1.getId());
    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.ref2to1);
    assertNotNull(result.get(testDto2Service.getEntityTypeMessageCode()).find(d2i1.getId()));
    assertNotNull(result.get(testDto1Service.getEntityTypeMessageCode()).find(d1i1.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