Search in sources :

Example 41 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.

the class DefaultAnalyticsSecurityManager method withDataApprovalConstraints.

@Override
public DataQueryParams withDataApprovalConstraints(DataQueryParams params) {
    DataQueryParams.Builder paramsBuilder = DataQueryParams.newBuilder(params);
    User user = currentUserService.getCurrentUser();
    boolean hideUnapprovedData = systemSettingManager.hideUnapprovedDataInAnalytics();
    boolean canViewUnapprovedData = user != null ? user.getUserCredentials().isAuthorized(DataApproval.AUTH_VIEW_UNAPPROVED_DATA) : true;
    if (hideUnapprovedData && user != null) {
        Map<OrganisationUnit, Integer> approvalLevels = null;
        if (params.hasApprovalLevel()) {
            // Set approval level from query
            DataApprovalLevel approvalLevel = approvalLevelService.getDataApprovalLevel(params.getApprovalLevel());
            if (approvalLevel == null) {
                throw new IllegalQueryException(String.format("Approval level does not exist: %s", params.getApprovalLevel()));
            }
            approvalLevels = approvalLevelService.getUserReadApprovalLevels(approvalLevel);
        } else if (!canViewUnapprovedData) {
            // Set approval level from user level
            approvalLevels = approvalLevelService.getUserReadApprovalLevels();
        }
        if (approvalLevels != null && !approvalLevels.isEmpty()) {
            paramsBuilder.withDataApprovalLevels(approvalLevels);
            log.debug(String.format("User: %s constrained by data approval levels: %s", user.getUsername(), approvalLevels.values()));
        }
    }
    return paramsBuilder.build();
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataApprovalLevel(org.hisp.dhis.dataapproval.DataApprovalLevel) User(org.hisp.dhis.user.User)

Example 42 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.

the class GeoFeatureController method getGeoFeatures.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Returns list of geo features. Returns null if not modified based on the
     * request.
     *
     * @param ou                 the organisation unit parameter.
     * @param displayProperty    the display property.
     * @param relativePeriodDate the date to use as basis for relative periods.
     * @param userOrgUnit        the user organisation unit parameter.
     * @param request            the HTTP request.
     * @param response           the HTTP response.
     * @param includeGroupSets   whether to include organisation unit group sets.
     * @return a list of geo features or null.
     */
private List<GeoFeature> getGeoFeatures(String ou, DisplayProperty displayProperty, Date relativePeriodDate, String userOrgUnit, HttpServletRequest request, HttpServletResponse response, boolean includeGroupSets, DhisApiVersion apiVersion) {
    Set<String> set = new HashSet<>();
    set.add(ou);
    DataQueryParams params = dataQueryService.getFromUrl(set, null, AggregationType.SUM, null, null, null, null, false, false, false, false, false, false, false, false, false, false, displayProperty, null, null, false, null, relativePeriodDate, userOrgUnit, false, apiVersion);
    DimensionalObject dim = params.getDimension(DimensionalObject.ORGUNIT_DIM_ID);
    List<OrganisationUnit> organisationUnits = DimensionalObjectUtils.asTypedList(dim.getItems());
    FilterUtils.filter(organisationUnits, new OrganisationUnitWithValidCoordinatesFilter());
    boolean modified = !ContextUtils.clearIfNotModified(request, response, organisationUnits);
    if (!modified) {
        return null;
    }
    List<OrganisationUnitGroupSet> groupSets = includeGroupSets ? organisationUnitGroupService.getAllOrganisationUnitGroupSets() : null;
    List<GeoFeature> features = new ArrayList<>();
    Set<OrganisationUnit> roots = currentUserService.getCurrentUser().getDataViewOrganisationUnitsWithFallback();
    for (OrganisationUnit unit : organisationUnits) {
        GeoFeature feature = new GeoFeature();
        Integer ty = unit.getFeatureType() != null ? FEATURE_TYPE_MAP.get(unit.getFeatureType()) : null;
        feature.setId(unit.getUid());
        feature.setCode(unit.getCode());
        feature.setHcd(unit.hasChildrenWithCoordinates());
        feature.setHcu(unit.hasCoordinatesUp());
        feature.setLe(unit.getLevel());
        feature.setPg(unit.getParentGraph(roots));
        feature.setPi(unit.getParent() != null ? unit.getParent().getUid() : null);
        feature.setPn(unit.getParent() != null ? unit.getParent().getDisplayName() : null);
        feature.setTy(ObjectUtils.firstNonNull(ty, 0));
        feature.setCo(unit.getCoordinates());
        feature.setNa(unit.getDisplayProperty(params.getDisplayProperty()));
        if (includeGroupSets) {
            for (OrganisationUnitGroupSet groupSet : groupSets) {
                OrganisationUnitGroup group = unit.getGroupInGroupSet(groupSet);
                if (group != null) {
                    feature.getDimensions().put(groupSet.getUid(), group.getUid());
                }
            }
        }
        features.add(feature);
    }
    Collections.sort(features, (o1, o2) -> Integer.valueOf(o1.getTy()).compareTo(Integer.valueOf(o2.getTy())));
    return features;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ArrayList(java.util.ArrayList) DimensionalObject(org.hisp.dhis.common.DimensionalObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) OrganisationUnitWithValidCoordinatesFilter(org.hisp.dhis.system.filter.OrganisationUnitWithValidCoordinatesFilter) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature) HashSet(java.util.HashSet)

Example 43 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.

the class ContextUtilsTest method testConfigureAnalyticsResponseReturnsCorrectCacheHeaders.

@Test
public void testConfigureAnalyticsResponseReturnsCorrectCacheHeaders() {
    Calendar thisYear = Calendar.getInstance();
    Calendar fiveYearBack = Calendar.getInstance();
    thisYear.set(2017, 01, 01);
    fiveYearBack.set(2012, 01, 01);
    DataQueryParams withinThreshold = DataQueryParams.newBuilder().withEndDate(thisYear.getTime()).build();
    DataQueryParams outsideThreshold = DataQueryParams.newBuilder().withEndDate(fiveYearBack.getTime()).build();
    systemSettingManager.saveSystemSetting(SettingKey.CACHE_ANALYTICS_DATA_YEAR_THRESHOLD, 3);
    response.reset();
    contextUtils.configureAnalyticsResponse(response, null, CacheStrategy.CACHE_1_HOUR, null, false, withinThreshold.getLatestEndDate());
    assertEquals("no-cache", response.getHeader("Cache-Control"));
    response.reset();
    contextUtils.configureAnalyticsResponse(response, null, CacheStrategy.CACHE_1_HOUR, null, false, outsideThreshold.getLatestEndDate());
    assertEquals("max-age=3600, public", response.getHeader("Cache-Control"));
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Calendar(java.util.Calendar) Test(org.junit.Test) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest)

Example 44 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.

the class QueryPlannerTest method testSetGetCopy.

// -------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------
@Test
public void testSetGetCopy() {
    List<DimensionalItemObject> desA = getList(deA, deB);
    List<DimensionalItemObject> ousA = getList(ouA, ouB);
    List<DimensionalItemObject> ousB = getList(ouC, ouD);
    List<DimensionalItemObject> pesA = getList(createPeriod("2000Q1"), createPeriod("2000Q2"));
    List<DimensionalItemObject> pesB = getList(createPeriod("200001"), createPeriod("200002"));
    DataQueryParams paramsA = DataQueryParams.newBuilder().withDataElements(desA).withOrganisationUnits(ousA).withPeriods(pesA).build();
    DataQueryParams paramsB = DataQueryParams.newBuilder(paramsA).withOrganisationUnits(ousB).withPeriods(pesB).build();
    assertEquals(desA, paramsA.getDataElements());
    assertEquals(ousA, paramsA.getOrganisationUnits());
    assertEquals(pesA, paramsA.getPeriods());
    assertEquals(desA, paramsB.getDataElements());
    assertEquals(ousB, paramsB.getOrganisationUnits());
    assertEquals(pesB, paramsB.getPeriods());
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 45 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.

the class QueryPlannerTest method validateFailureOptionCombosWithIndicators.

@Test(expected = IllegalQueryException.class)
public void validateFailureOptionCombosWithIndicators() {
    DataQueryParams params = DataQueryParams.newBuilder().addDimension(new BaseDimensionalObject(DATA_X_DIM_ID, DimensionType.DATA_X, getList(deA, inA))).addDimension(new BaseDimensionalObject(CATEGORYOPTIONCOMBO_DIM_ID, DimensionType.DATA_X, getList())).addDimension(new BaseDimensionalObject(ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, getList(ouA, ouB))).addDimension(new BaseDimensionalObject(PERIOD_DIM_ID, DimensionType.PERIOD, getList(peA, peB))).build();
    queryPlanner.validate(params);
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)81 Test (org.junit.Test)52 DhisSpringTest (org.hisp.dhis.DhisSpringTest)51 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)24 QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)22 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)19 DataQueryGroups (org.hisp.dhis.analytics.DataQueryGroups)17 ArrayList (java.util.ArrayList)16 DimensionalObject (org.hisp.dhis.common.DimensionalObject)15 HashSet (java.util.HashSet)14 LinkedHashSet (java.util.LinkedHashSet)13 HashMap (java.util.HashMap)7 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 List (java.util.List)6 Map (java.util.Map)6 OrganisationUnit.getParentGraphMap (org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap)6 OrganisationUnit.getParentNameGraphMap (org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap)6 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)5 Grid (org.hisp.dhis.common.Grid)5