Search in sources :

Example 1 with DataSetLoader

use of org.summerb.approaches.jdbccrud.api.relations.DataSetLoader in project summerb by skarpushin.

the class DataSetLoaderTest method testLoadObjectsByIds_ExpectTwoObjsOfSameTypeLoadedOk.

@Test
public void testLoadObjectsByIds_ExpectTwoObjsOfSameTypeLoadedOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("d2i1");
    d2i1.setLinkToFullDonwload("asdad");
    d2i1 = testDto2Service.create(d2i1);
    TestDto2 d2i2 = new TestDto2();
    d2i2.setEnv("d2i2");
    d2i2.setLinkToFullDonwload("asdad");
    d2i2 = testDto2Service.create(d2i2);
    List<HasId> result = loader.loadObjectsByIds(ids(d2i1, d2i2), testDto2Service.getEntityTypeMessageCode());
    assertNotNull(result);
    assertEquals(2, result.size());
}
Also used : HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) DataSetLoader(org.summerb.approaches.jdbccrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 2 with DataSetLoader

use of org.summerb.approaches.jdbccrud.api.relations.DataSetLoader 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 3 with DataSetLoader

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

use of org.summerb.approaches.jdbccrud.api.relations.DataSetLoader in project summerb by skarpushin.

the class EasyCrudRestControllerBase method resolveReferences.

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

Example 5 with DataSetLoader

use of org.summerb.approaches.jdbccrud.api.relations.DataSetLoader 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.approaches.jdbccrud.api.dto.datapackage.DataSet) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) HashMap(java.util.HashMap) DataSetLoader(org.summerb.approaches.jdbccrud.api.relations.DataSetLoader) Test(org.junit.Test)

Aggregations

DataSetLoader (org.summerb.approaches.jdbccrud.api.relations.DataSetLoader)11 Test (org.junit.Test)10 DataSet (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)9 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)3 Preconditions (com.google.common.base.Preconditions)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 BeansException (org.springframework.beans.BeansException)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 MediaType (org.springframework.http.MediaType)1 CollectionUtils (org.springframework.util.CollectionUtils)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1