DefaultVisualizationService extends
    GenericAnalyticalObjectService<Visualization> implements
    VisualizationService { @Override @Transactional( readOnly = true ) public Grid getVisualizationGridByUser( final String uid, final Date relativePeriodDate, final String organisationUnitUid, final User user ) { Visualization visualization = loadVisualization( uid ); final boolean hasPermission = visualization != null; if ( hasPermission ) { I18nFormat format = i18nManager.getI18nFormat(); OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitUid ); List<OrganisationUnit> atLevels = new ArrayList<>(); List<OrganisationUnit> inGroups = new ArrayList<>(); if ( visualization.hasOrganisationUnitLevels() ) { atLevels.addAll( organisationUnitService.getOrganisationUnitsAtLevels( visualization.getOrganisationUnitLevels(), visualization.getOrganisationUnits() ) ); } if ( visualization.hasItemOrganisationUnitGroups() ) { inGroups.addAll( organisationUnitService.getOrganisationUnits( visualization.getItemOrganisationUnitGroups(), visualization.getOrganisationUnits() ) ); } visualization.init( user, relativePeriodDate, organisationUnit, atLevels, inGroups, format ); Map<String, Object> valueMap = analyticsService.getAggregatedDataValueMapping( visualization ); Grid visualizationGrid = visualization.getGrid( new ListGrid(), valueMap, SHORTNAME, true ); visualization.clearTransientState(); return visualizationGrid; } else { return new ListGrid(); } } DefaultVisualizationService( final AnalyticsService analyticsService,
        @Qualifier( "org.hisp.dhis.visualization.generic.VisualizationStore" )
        final AnalyticalObjectStore<Visualization> visualizationStore,
        final OrganisationUnitService organisationUnitService, final CurrentUserService currentUserService,
        final I18nManager i18nManager ); @Override @Transactional long save( final Visualization visualization ); @Override @Transactional( readOnly = true ) Visualization loadVisualization( final long id ); @Override @Transactional( readOnly = true ) Visualization loadVisualization( final String uid ); @Override @Transactional void delete( final Visualization visualization ); @Override @Transactional( readOnly = true ) Grid getVisualizationGrid( final String uid, final Date relativePeriodDate,
        final String organisationUnitUid ); @Override @Transactional( readOnly = true ) Grid getVisualizationGridByUser( final String uid, final Date relativePeriodDate,
        final String organisationUnitUid, final User user ); @Override @Transactional( readOnly = true ) Visualization getVisualizationNoAcl( final String uid );  }
@Test public void getVisualizationGridByUserWhenItHasOrganisationUnitLevels() { final String anyVisualizationUid = "adbet5RTs"; final Date anyRelativePeriodDate = new Date(); final String anyOrganisationUnitUid = "ouiRzW5e"; final User userStub = userStub(); final List<Integer> orgUnitLevels = asList( 1, 2 ); final List<OrganisationUnit> orgUnits = asList( new OrganisationUnit() ); final Map<String, Object> valueMap = valueMapStub(); final Visualization visualizationStub = visualizationStub( "abc123xy" ); visualizationStub.setOrganisationUnitLevels( orgUnitLevels ); visualizationStub.setOrganisationUnits( orgUnits ); final Visualization visualizationSpy = spy( visualizationStub ); when( visualizationStore.getByUid( anyVisualizationUid ) ).thenReturn( visualizationSpy ); when( analyticsService.getAggregatedDataValueMapping( visualizationSpy ) ).thenReturn( valueMap ); final Grid expectedGrid = defaultVisualizationService.getVisualizationGridByUser( anyVisualizationUid, anyRelativePeriodDate, anyOrganisationUnitUid, userStub ); assertThat( expectedGrid.getRows(), hasSize( 1 ) ); assertThat( expectedGrid.getRows().get( 0 ), hasSize( 7 ) ); assertThat( expectedGrid.getRows().get( 0 ), hasItem( "abc123xy" ) ); assertThat( expectedGrid.getHeaders(), hasSize( 7 ) ); assertThat( expectedGrid.getMetaColumnIndexes(), hasSize( 7 ) ); assertThatHeadersAreTheExpectedOnes( expectedGrid ); verify( organisationUnitService, times( 1 ) ).getOrganisationUnitsAtLevels( orgUnitLevels, orgUnits ); verify( visualizationSpy, times( 1 ) ).clearTransientState(); }
@Test public void getVisualizationGridByUserWhenItHasItemOrganisationUnitGroups() { final String anyVisualizationUid = "adbet5RTs"; final Date anyRelativePeriodDate = new Date(); final String anyOrganisationUnitUid = "ouiRzW5e"; final User userStub = userStub(); final List<OrganisationUnit> orgUnits = asList( new OrganisationUnit() ); final List<OrganisationUnitGroup> orgUnitGroups = asList( new OrganisationUnitGroup() ); final Map<String, Object> valueMap = valueMapStub(); final Visualization visualizationStub = visualizationStub( "abc123xy" ); visualizationStub.setOrganisationUnits( orgUnits ); visualizationStub.setItemOrganisationUnitGroups( orgUnitGroups ); final Visualization visualizationSpy = spy( visualizationStub ); when( visualizationStore.getByUid( anyVisualizationUid ) ).thenReturn( visualizationSpy ); when( analyticsService.getAggregatedDataValueMapping( visualizationSpy ) ).thenReturn( valueMap ); final Grid expectedGrid = defaultVisualizationService.getVisualizationGridByUser( anyVisualizationUid, anyRelativePeriodDate, anyOrganisationUnitUid, userStub ); assertThat( expectedGrid.getRows(), hasSize( 1 ) ); assertThat( expectedGrid.getRows().get( 0 ), hasSize( 7 ) ); assertThat( expectedGrid.getRows().get( 0 ), hasItem( "abc123xy" ) ); assertThat( expectedGrid.getHeaders(), hasSize( 7 ) ); assertThat( expectedGrid.getMetaColumnIndexes(), hasSize( 7 ) ); assertThatHeadersAreTheExpectedOnes( expectedGrid ); verify( organisationUnitService, times( 1 ) ).getOrganisationUnits( orgUnitGroups, orgUnits ); verify( visualizationSpy, times( 1 ) ).clearTransientState(); }