Search in sources :

Example 41 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class AbstractFullReadOnlyController method getObjectInternal.

@SuppressWarnings("unchecked")
private RootNode getObjectInternal(String uid, Map<String, String> parameters, List<String> filters, List<String> fields, User currentUser) throws Exception {
    WebOptions options = new WebOptions(parameters);
    List<T> entities = getEntity(uid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(notFound(getEntityClass(), uid));
    }
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>(), getPaginationData(options), options.getRootJunction());
    query.setUser(currentUser);
    query.setObjects(entities);
    query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
    entities = (List<T>) queryService.query(query);
    handleLinksAndAccess(entities, fields, true);
    handleAttributeValues(entities, fields);
    for (T entity : entities) {
        postProcessResponseEntity(entity, options, parameters);
    }
    CollectionNode collectionNode = fieldFilterService.toCollectionNode(getEntityClass(), new FieldFilterParams(entities, fields, Defaults.valueOf(options.get("defaults", DEFAULTS))).setUser(currentUser));
    if (options.isTrue("useWrapper") || entities.size() > 1) {
        RootNode rootNode = NodeUtils.createMetadata(collectionNode);
        rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
        return rootNode;
    } else {
        List<Node> children = collectionNode.getChildren();
        RootNode rootNode;
        if (!children.isEmpty()) {
            rootNode = NodeUtils.createRootNode(children.get(0));
        } else {
            rootNode = NodeUtils.createRootNode(new ComplexNode(getSchema().getSingular()));
        }
        rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
        return rootNode;
    }
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Query(org.hisp.dhis.query.Query) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ComplexNode(org.hisp.dhis.node.types.ComplexNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) CollectionNode(org.hisp.dhis.node.types.CollectionNode)

Example 42 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class ResponseHandlerTest method testAddPaginationToNodeWhenPagingIsFalse.

@Test
void testAddPaginationToNodeWhenPagingIsFalse() {
    // Given
    final RootNode anyRootNode = new RootNode("any");
    final Set<Class<? extends BaseIdentifiableObject>> anyTargetEntities = asSet(Indicator.class, DataSet.class);
    final Set<String> anyFilters = newHashSet("any");
    final User anyUser = new User();
    final WebOptions webOptionsNoPaging = mockWebOptionsNoPaging();
    // When
    responseHandler.addPaginationToNode(anyRootNode, anyTargetEntities, anyUser, webOptionsNoPaging, anyFilters);
    // Then
    assertThat(anyRootNode, is(notNullValue()));
    assertThat(anyRootNode.getName(), is(equalTo("any")));
    assertThat(anyRootNode.getChildren(), is(empty()));
    verify(linkService, never()).generatePagerLinks(any(Pager.class), anyString());
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) User(org.hisp.dhis.user.User) Pager(org.hisp.dhis.common.Pager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 43 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class ResponseHandlerTest method testAddPaginationToNodeWithSuccess.

@Test
void testAddPaginationToNodeWithSuccess() {
    // Given
    final RootNode anyRootNode = new RootNode("any");
    final Set<Class<? extends BaseIdentifiableObject>> anyTargetEntities = asSet(Indicator.class, DataSet.class);
    final Set<String> anyFilters = newHashSet("any");
    final User anyUser = new User();
    final WebOptions anyWebOptions = mockWebOptions(10, 1);
    // When
    responseHandler.addPaginationToNode(anyRootNode, anyTargetEntities, anyUser, anyWebOptions, anyFilters);
    // Then
    assertThat(anyRootNode, is(notNullValue()));
    assertThat(anyRootNode.getName(), is(equalTo("any")));
    assertThat(anyRootNode.getChildren(), hasSize(1));
    assertThat(anyRootNode.getChildren().get(0).isMetadata(), is(true));
    assertThat(anyRootNode.getChildren().get(0).isComplex(), is(true));
    verify(linkService, times(1)).generatePagerLinks(any(Pager.class), anyString());
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) User(org.hisp.dhis.user.User) Pager(org.hisp.dhis.common.Pager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 44 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class DimensionItemPageHandlerTest method testAddPaginationToNodeWhenPagingIsFalse.

@Test
void testAddPaginationToNodeWhenPagingIsFalse() {
    // Given
    final RootNode anyRootNode = new RootNode("any");
    final WebOptions webOptionsNoPaging = mockWebOptionsWithPagingFlagFalse();
    final String anyUid = "LFsZ8v5v7rq";
    final int anyTotals = 12;
    // When
    dimensionItemPageHandler.addPaginationToNodeIfEnabled(anyRootNode, webOptionsNoPaging, anyUid, anyTotals);
    // Then
    assertThat(anyRootNode, is(notNullValue()));
    assertThat(anyRootNode.getName(), is(equalTo("any")));
    assertThat(anyRootNode.getChildren(), is(empty()));
    verify(linkService, never()).generatePagerLinks(any(Pager.class), anyString());
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Pager(org.hisp.dhis.common.Pager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 45 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class DataItemQueryControllerTest method testGetWithSuccess.

@Test
void testGetWithSuccess() {
    // Given
    final Map<String, String> anyUrlParameters = new HashMap<>();
    final OrderParams anyOrderParams = new OrderParams();
    final User anyUser = new User();
    final Set<Class<? extends BaseIdentifiableObject>> targetEntities = new HashSet<>(singletonList(Indicator.class));
    final List<DataItem> itemsFound = singletonList(new DataItem());
    // When
    when(dataItemServiceFacade.extractTargetEntities(anySet())).thenReturn(targetEntities);
    when(aclService.canRead(anyUser, Indicator.class)).thenReturn(true);
    when(dataItemServiceFacade.retrieveDataItemEntities(anySet(), anySet(), any(WebOptions.class), any(OrderParams.class))).thenReturn(itemsFound);
    final ResponseEntity<RootNode> actualResponse = dataItemQueryController.getJson(anyUrlParameters, anyOrderParams, anyUser);
    // Then
    assertThat(actualResponse, is(not(nullValue())));
    assertThat(actualResponse.getStatusCode(), is(OK));
    verify(responseHandler, times(1)).addResultsToNode(any(RootNode.class), anyList(), anySet());
    verify(responseHandler, times(1)).addPaginationToNode(any(RootNode.class), anySet(), any(), any(), anySet());
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) DataItem(org.hisp.dhis.dataitem.DataItem) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Indicator(org.hisp.dhis.indicator.Indicator) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

RootNode (org.hisp.dhis.node.types.RootNode)112 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)59 CollectionNode (org.hisp.dhis.node.types.CollectionNode)50 SimpleNode (org.hisp.dhis.node.types.SimpleNode)40 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)30 GetMapping (org.springframework.web.bind.annotation.GetMapping)29 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)27 Pager (org.hisp.dhis.common.Pager)26 User (org.hisp.dhis.user.User)25 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)24 ComplexNode (org.hisp.dhis.node.types.ComplexNode)20 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)17 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)15 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 Node (org.hisp.dhis.node.Node)11 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)10 List (java.util.List)9 Query (org.hisp.dhis.query.Query)9 DataSet (org.hisp.dhis.dataset.DataSet)7