Search in sources :

Example 1 with ReferencesRegistry

use of org.summerb.easycrud.api.relations.ReferencesRegistry 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 2 with ReferencesRegistry

use of org.summerb.easycrud.api.relations.ReferencesRegistry in project summerb by skarpushin.

the class DomLoaderDeviceGatewayTest method testMapDtoToDom_expectCorrectFieldTypeResolution.

@Test
public void testMapDtoToDom_expectCorrectFieldTypeResolution() throws Exception {
    // Deps and fixture
    ReferencesRegistry referencesRegistry = new Refs();
    EasyCrudServiceResolver easyCrudServiceResolver = mock(EasyCrudServiceResolver.class);
    DeviceService deviceService = mock(DeviceService.class);
    when(deviceService.getEntityTypeMessageCode()).thenReturn(DeviceService.ENTITY_TYPE_MESSAGE_CODE);
    when(easyCrudServiceResolver.resolveByDtoClass(DeviceRow.class)).thenReturn(deviceService);
    when(easyCrudServiceResolver.resolveByEntityType(DeviceService.ENTITY_TYPE_MESSAGE_CODE)).thenReturn(deviceService);
    EnvService envService = mock(EnvService.class);
    when(envService.getEntityTypeMessageCode()).thenReturn(EnvService.ENTITY_TYPE_MESSAGE_CODE);
    when(easyCrudServiceResolver.resolveByDtoClass(EnvironmentRow.class)).thenReturn(envService);
    when(easyCrudServiceResolver.resolveByEntityType(EnvService.ENTITY_TYPE_MESSAGE_CODE)).thenReturn(envService);
    DataSetLoaderImpl dataSetLoader = new DataSetLoaderImpl();
    dataSetLoader.setEasyCrudServiceResolver(easyCrudServiceResolver);
    dataSetLoader.setReferencesRegistry(referencesRegistry);
    DomLoaderImpl f = new DomLoaderImpl(dataSetLoader, easyCrudServiceResolver);
    // Setup DataSet
    // DataSet ds = new DataSet();
    EnvironmentRow envRow = new EnvironmentRow();
    envRow.setId(1L);
    envRow.setName("Hurray");
    when(envService.query(any(), any(), any())).thenReturn(new PaginatedList<>(PagerParams.ALL, Arrays.asList(envRow), 1));
    when(envService.findById(1L)).thenReturn(envRow);
    // ds.get(EnvService.ENTITY_TYPE_MESSAGE_CODE).put(envRow);
    DeviceRow deviceRow = new DeviceRow();
    deviceRow.setId(2L);
    deviceRow.setEnvId(1);
    deviceRow.setName("Yes it is");
    when(deviceService.query(any(), any())).thenReturn(new PaginatedList<>(PagerParams.ALL, Arrays.asList(deviceRow), 1));
    when(deviceService.findById(2L)).thenReturn(deviceRow);
    // ds.get(DeviceService.ENTITY_TYPE_MESSAGE_CODE).put(deviceRow);
    // when(dataSetLoader.loadObjectsByIds(any(),
    // anyString())).thenReturn(Arrays.asList(envRow));
    // Now let's invoke it
    Env env = f.load(Env.class, 1L, Refs.envDevices, Refs.deviceEnv);
    // verify(deviceService, times(1)).query(any(), any(), any());
    // verify(envService, times(1)).query(any(), any(), any());
    // Verify
    assertNotNull(env);
    assertEquals("Hurray", env.getName());
    assertNotNull(env.getDevices());
    assertEquals(1, env.getDevices().size());
    Device device = env.getDevices().get(0);
    assertEquals("Yes it is", device.getName());
    assertNotNull(device.getEnv());
    assertEquals(env, device.getEnv());
}
Also used : ReferencesRegistry(org.summerb.easycrud.api.relations.ReferencesRegistry) EnvService(org.summerb.easycrud.impl.relations.example.EnvService) EasyCrudServiceResolver(org.summerb.easycrud.api.EasyCrudServiceResolver) Refs(org.summerb.easycrud.impl.relations.example.Refs) DeviceRow(org.summerb.easycrud.impl.relations.example.DeviceRow) Device(org.summerb.easycrud.impl.relations.example.Device) DeviceService(org.summerb.easycrud.impl.relations.example.DeviceService) Env(org.summerb.easycrud.impl.relations.example.Env) EnvironmentRow(org.summerb.easycrud.impl.relations.example.EnvironmentRow) Test(org.junit.Test)

Example 3 with ReferencesRegistry

use of org.summerb.easycrud.api.relations.ReferencesRegistry in project summerb by skarpushin.

the class DataSetLoaderImplTest method buildMockedInstance.

private DataSetLoaderImpl buildMockedInstance() {
    DataSetLoaderImpl ret = new DataSetLoaderImpl();
    EasyCrudServiceResolver easyCrudServiceResolver = Mockito.mock(EasyCrudServiceResolver.class);
    ret.setEasyCrudServiceResolver(easyCrudServiceResolver);
    ReferencesRegistry referencesRegistry = Mockito.mock(ReferencesRegistry.class);
    ret.setReferencesRegistry(referencesRegistry);
    return ret;
}
Also used : ReferencesRegistry(org.summerb.easycrud.api.relations.ReferencesRegistry) EasyCrudServiceResolver(org.summerb.easycrud.api.EasyCrudServiceResolver)

Aggregations

ReferencesRegistry (org.summerb.easycrud.api.relations.ReferencesRegistry)3 EasyCrudServiceResolver (org.summerb.easycrud.api.EasyCrudServiceResolver)2 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 Test (org.junit.Test)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 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PathVariable (org.springframework.web.bind.annotation.PathVariable)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1