DefaultDataQueryService implements DataQueryService { @Override public DataQueryParams getFromRequest( DataQueryRequest request ) { I18nFormat format = i18nManager.getI18nFormat(); DataQueryParams.Builder params = DataQueryParams.newBuilder(); IdScheme inputIdScheme = ObjectUtils.firstNonNull( request.getInputIdScheme(), IdScheme.UID ); if ( request.getDimension() != null && !request.getDimension().isEmpty() ) { params.addDimensions( getDimensionalObjects( request.getDimension(), request.getRelativePeriodDate(), request.getUserOrgUnit(), format, request.isAllowAllPeriods(), inputIdScheme ) ); } if ( request.getFilter() != null && !request.getFilter().isEmpty() ) { params.addFilters( getDimensionalObjects( request.getFilter(), request.getRelativePeriodDate(), request.getUserOrgUnit(), format, request.isAllowAllPeriods(), inputIdScheme ) ); } if ( request.getMeasureCriteria() != null && !request.getMeasureCriteria().isEmpty() ) { params.withMeasureCriteria( getMeasureCriteriaFromParam( request.getMeasureCriteria() ) ); } if ( request.getPreAggregationMeasureCriteria() != null && !request.getPreAggregationMeasureCriteria().isEmpty() ) { params.withPreAggregationMeasureCriteria( getMeasureCriteriaFromParam( request.getPreAggregationMeasureCriteria()) ); } if ( request.getAggregationType() != null ) { params.withAggregationType( AnalyticsAggregationType.fromAggregationType( request.getAggregationType() ) ); } return params .withStartDate( request.getStartDate() ) .withEndDate( request.getEndDate() ) .withOrder( request.getOrder() ) .withTimeField( request.getTimeField() ) .withOrgUnitField( request.getOrgUnitField() ) .withSkipMeta( request.isSkipMeta() ) .withSkipData( request.isSkipData() ) .withSkipRounding( request.isSkipRounding() ) .withCompletedOnly( request.isCompletedOnly() ) .withIgnoreLimit( request.isIgnoreLimit() ) .withHierarchyMeta( request.isHierarchyMeta() ) .withHideEmptyRows( request.isHideEmptyRows() ) .withHideEmptyColumns( request.isHideEmptyColumns() ) .withShowHierarchy( request.isShowHierarchy() ) .withIncludeNumDen( request.isIncludeNumDen() ) .withIncludeMetadataDetails( request.isIncludeMetadataDetails() ) .withDisplayProperty( request.getDisplayProperty() ) .withOutputIdScheme( request.getOutputIdScheme() ) .withOutputFormat( OutputFormat.ANALYTICS ) .withDuplicatesOnly( request.isDuplicatesOnly() ) .withApprovalLevel( request.getApprovalLevel() ) .withApiVersion( request.getApiVersion() ) .withUserOrgUnitType( request.getUserOrgUnitType() ) .build(); } DefaultDataQueryService( IdentifiableObjectManager idObjectManager,
        OrganisationUnitService organisationUnitService, DimensionService dimensionService,
        AnalyticsSecurityManager securityManager, SystemSettingManager systemSettingManager, AclService aclService,
        CurrentUserService currentUserService, I18nManager i18nManager ); @Override DataQueryParams getFromRequest( DataQueryRequest request ); @Override DataQueryParams getFromAnalyticalObject( AnalyticalObject object ); @Override List<DimensionalObject> getDimensionalObjects( Set<String> dimensionParams, Date relativePeriodDate, String userOrgUnit,
        I18nFormat format, boolean allowAllPeriods, IdScheme inputIdScheme ); @Override DimensionalObject getDimension( String dimension, List<String> items, Date relativePeriodDate,
        List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme ); @Override List<OrganisationUnit> getUserOrgUnits( DataQueryParams params, String userOrgUnit );  }
@Test public void convertAnalyticsRequestWithOuLevelToDataQueryParam() { mockDimensionService(); when( organisationUnitService.getOrganisationUnitLevelByLevel( 2 ) ) .thenReturn( buildOrgUnitLevel( 2, "level2UID", "District", null ) ); when( organisationUnitService.getOrganisationUnitLevelByLevelOrUid( "2" ) ).thenReturn( 2 ); when( organisationUnitService.getOrganisationUnitsAtLevels( Mockito.anyList(), Mockito.anyList() ) ) .thenReturn( Lists.newArrayList( new OrganisationUnit(), new OrganisationUnit() ) ); rb.addOuFilter( "LEVEL-2;ImspTQPwCqd" ); rb.addDimension( concatenateUuid( DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3 ) ); rb.addPeDimension( PERIOD_DIMENSION ); DataQueryRequest request = DataQueryRequest.newBuilder().filter( rb.getFilterParams() ) .dimension( rb.getDimensionParams() ).build(); DataQueryParams params = target.getFromRequest( request ); DimensionalObject filter = params.getFilters().get( 0 ); assertThat( filter.getDimensionalKeywords().getGroupBy(), hasSize( 1 ) ); assertThat( filter.getDimensionalKeywords().getGroupBy().get( 0 ), allOf( hasProperty( "name", is( "District" ) ), hasProperty( "uid", is( "level2UID" ) ), hasProperty( "code", is( nullValue() ) ) ) ); }
@Test public void convertAnalyticsRequestWithMultipleOuLevelToDataQueryParam() { mockDimensionService(); when( organisationUnitService.getOrganisationUnitLevelByLevel( 2 ) ) .thenReturn( buildOrgUnitLevel( 2, "level2UID", "District", null ) ); when( organisationUnitService.getOrganisationUnitLevelByLevel( 3 ) ) .thenReturn( buildOrgUnitLevel( 3, "level3UID", "Chiefdom", null ) ); when( organisationUnitService.getOrganisationUnitLevelByLevelOrUid( "3" ) ) .thenReturn( 3 ); when( organisationUnitService.getOrganisationUnitLevelByLevelOrUid( "2" ) ) .thenReturn( 2 ); when( organisationUnitService.getOrganisationUnitsAtLevels( Mockito.anyList(), Mockito.anyList() ) ) .thenReturn( Lists.newArrayList( new OrganisationUnit(), new OrganisationUnit() ) ); rb.addOuFilter( "LEVEL-2;LEVEL-3;ImspTQPwCqd" ); rb.addDimension( concatenateUuid( DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3 ) ); rb.addPeDimension( PERIOD_DIMENSION ); DataQueryRequest request = DataQueryRequest.newBuilder() .filter( rb.getFilterParams() ) .dimension( rb.getDimensionParams() ).build(); DataQueryParams params = target.getFromRequest( request ); DimensionalObject filter = params.getFilters().get( 0 ); assertThat( filter.getDimensionalKeywords().getGroupBy(), hasSize( 2 ) ); assertThat( filter.getDimensionalKeywords().getGroupBy(), IsIterableContainingInAnyOrder.containsInAnyOrder( allOf( hasProperty( "name", is( "District" ) ), hasProperty( "uid", is( "level2UID" ) ), hasProperty( "code", is( nullValue() ) ) ), allOf( hasProperty( "name", is( "Chiefdom" ) ), hasProperty( "uid", is( "level3UID" ) ), hasProperty( "code", is( nullValue() ) ) ) ) ); }
@Test public void convertAnalyticsRequestWithOrgUnitGroupAsFilter() { mockDimensionService(); final String ouGroupUID = "gzcv65VyaGq"; initOrgUnitGroup( ouGroupUID ); rb.addPeDimension( PERIOD_DIMENSION ); rb.addDimension( concatenateUuid( DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3 ) ); rb.addOuFilter( "OU_GROUP-" + ouGroupUID + ";" + rootOu.getUid() ); DataQueryRequest request = DataQueryRequest.newBuilder().dimension( rb.getDimensionParams() ) .filter( rb.getFilterParams() ).build(); DataQueryParams params = target.getFromRequest( request ); assertOrgUnitGroup( ouGroupUID, params.getFilter( "ou" ) ); }
@Test public void convertAnalyticsRequestWithOrgUnitLevelAsFilter() { OrganisationUnit level2OuA = new OrganisationUnit( "Bo" ); OrganisationUnit level2OuB = new OrganisationUnit( "Bombali" ); mockDimensionService(); when( organisationUnitService.getOrganisationUnitLevelByLevelOrUid( "wjP19dkFeIk" ) ).thenReturn( 2 ); when( idObjectManager.getObject( OrganisationUnit.class, UID, "ImspTQPwCqd" ) ).thenReturn( rootOu ); when( organisationUnitService.getOrganisationUnitsAtLevels( Mockito.anyList(), Mockito.anyList() ) ).thenReturn( Lists.newArrayList( level2OuA, level2OuB ) ); when( organisationUnitService.getOrganisationUnitLevelByLevel( 2 ) ) .thenReturn( buildOrgUnitLevel( 2, "level2UID", "District", null ) ); rb.addOuFilter( "LEVEL-wjP19dkFeIk;ImspTQPwCqd" ); rb.addDimension( concatenateUuid( DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3 ) ); rb.addPeDimension( PERIOD_DIMENSION ); DataQueryRequest request = DataQueryRequest.newBuilder().filter( rb.getFilterParams() ) .dimension( rb.getDimensionParams() ).build(); DataQueryParams params = target.getFromRequest( request ); DimensionalObject filter = params.getFilters().get( 0 ); assertThat( filter.getDimensionalKeywords().getGroupBy(), hasSize( 2 ) ); assertThat( filter.getDimensionalKeywords().getGroupBy(), IsIterableContainingInAnyOrder.containsInAnyOrder( allOf( hasProperty( "name", is( "District" ) ), hasProperty( "uid", is( "level2UID" ) ), hasProperty( "code", is( nullValue() ) ) ), allOf( hasProperty( "name", is( "Sierra Leone" ) ), hasProperty( "uid", is( rootOu.getUid() ) ), hasProperty( "code", is( rootOu.getCode() ) ) ) ) ); }
@Test public void convertAnalyticsRequestWithOrgUnitLevelAndOrgUnitGroupAsFilter() { OrganisationUnit level2OuA = new OrganisationUnit( "Bo" ); OrganisationUnit level2OuB = new OrganisationUnit( "Bombali" ); OrganisationUnit ou1Group = new OrganisationUnit( "ou1-group" ); OrganisationUnit ou2Group = new OrganisationUnit( "ou2-group" ); OrganisationUnitGroup groupOu = beanRandomizer.randomObject(OrganisationUnitGroup.class, "geometry"); mockDimensionService(); when( organisationUnitService.getOrganisationUnitLevelByLevelOrUid( "wjP19dkFeIk" ) ).thenReturn( 2 ); when( idObjectManager.getObject( OrganisationUnit.class, UID, "ImspTQPwCqd" ) ).thenReturn( rootOu ); when( idObjectManager.getObject( OrganisationUnitGroup.class, UID, "tDZVQ1WtwpA" ) ).thenReturn( groupOu ); when( organisationUnitService.getOrganisationUnitsAtLevels( Mockito.anyList(), Mockito.anyList() ) ).thenReturn( Lists.newArrayList( level2OuA, level2OuB ) ); when( organisationUnitService.getOrganisationUnitLevelByLevel( 2 ) ) .thenReturn( buildOrgUnitLevel( 2, "level2UID", "District", null ) ); when( organisationUnitService.getOrganisationUnits( Lists.newArrayList( groupOu ), Lists.newArrayList( rootOu ) ) ) .thenReturn( Lists.newArrayList( ou1Group, ou2Group ) ); rb.addOuFilter( "LEVEL-wjP19dkFeIk;OU_GROUP-tDZVQ1WtwpA;ImspTQPwCqd" ); rb.addDimension( concatenateUuid( DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3 ) ); rb.addPeDimension( PERIOD_DIMENSION ); DataQueryRequest request = DataQueryRequest.newBuilder().filter( rb.getFilterParams() ) .dimension( rb.getDimensionParams() ).build(); DataQueryParams params = target.getFromRequest( request ); DimensionalObject filter = params.getFilters().get( 0 ); assertThat( filter.getDimensionalKeywords().getGroupBy(), hasSize( 3 ) ); assertThat( filter.getDimensionalKeywords().getGroupBy(), IsIterableContainingInAnyOrder.containsInAnyOrder( allOf( hasProperty( "name", is( "District" ) ), hasProperty( "uid", is( "level2UID" ) ), hasProperty( "code", is( nullValue() ) ) ), allOf( hasProperty( "name", is( groupOu.getName() ) ), hasProperty( "uid", is( groupOu.getUid() ) ), hasProperty( "code", is( groupOu.getCode() ) ) ), allOf( hasProperty( "name", is( "Sierra Leone" ) ), hasProperty( "uid", is( rootOu.getUid() ) ), hasProperty( "code", is( rootOu.getCode() ) ) ) ) ); }