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();
}
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;
}
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"));
}
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());
}
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);
}
Aggregations