use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class JdbcRawAnalyticsManager method getStatement.
private String getStatement(DataQueryParams params, String partition) {
List<String> dimensionColumns = params.getDimensions().stream().map(d -> statementBuilder.columnQuote(d.getDimensionName())).collect(Collectors.toList());
setOrgUnitSelect(params, dimensionColumns);
SqlHelper sqlHelper = new SqlHelper();
String sql = "select " + StringUtils.join(dimensionColumns, ", ") + ", " + DIM_NAME_OU + ", value " + "from " + partition + " ax " + "inner join organisationunit ou on ax.ou = ou.uid " + "inner join _periodstructure ps on ax.pe = ps.iso ";
for (DimensionalObject dim : params.getDimensions()) {
if (!dim.getItems().isEmpty() && !dim.isFixed()) {
String col = statementBuilder.columnQuote(dim.getDimensionName());
if (DimensionalObject.ORGUNIT_DIM_ID.equals(dim.getDimension())) {
sql += sqlHelper.whereAnd() + " (";
for (DimensionalItemObject item : dim.getItems()) {
OrganisationUnit unit = (OrganisationUnit) item;
sql += DIM_NAME_OU + " like '" + unit.getPath() + "%' or ";
}
sql = TextUtils.removeLastOr(sql) + ") ";
} else {
sql += sqlHelper.whereAnd() + " " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
}
}
}
if (params.hasStartEndDate()) {
sql += sqlHelper.whereAnd() + " " + "ps.startdate >= '" + DateUtils.getMediumDateString(params.getStartDate()) + "' and " + "ps.enddate <= '" + DateUtils.getMediumDateString(params.getEndDate()) + "' ";
}
return sql;
}
use of org.hisp.dhis.common.DimensionalObject 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.common.DimensionalObject in project dhis2-core by dhis2.
the class BaseAnalyticalObject method getDimensionalObject.
/**
* Assembles a DimensionalObject based on the persisted properties of this
* AnalyticalObject. Collapses indicators, data elements, data element
* operands and data sets into the dx dimension.
* <p>
* Collapses fixed and relative periods into the pe dimension. Collapses
* fixed and user organisation units into the ou dimension.
*
* @param dimension the dimension identifier.
* @param date the date used for generating relative periods.
* @param user the current user.
* @param dynamicNames whether to use dynamic or static names.
* @param format the I18nFormat.
* @return a DimensionalObject.
*/
protected DimensionalObject getDimensionalObject(String dimension, Date date, User user, boolean dynamicNames, List<OrganisationUnit> organisationUnitsAtLevel, List<OrganisationUnit> organisationUnitsInGroups, I18nFormat format) {
List<DimensionalItemObject> items = new ArrayList<>();
DimensionType type = null;
if (DATA_X_DIM_ID.equals(dimension)) {
items.addAll(getDataDimensionNameableObjects());
type = DimensionType.DATA_X;
} else if (PERIOD_DIM_ID.equals(dimension)) {
setPeriodNames(periods, dynamicNames, format);
items.addAll(periods);
if (hasRelativePeriods()) {
items.addAll(relatives.getRelativePeriods(date, format, dynamicNames));
}
type = DimensionType.PERIOD;
} else if (ORGUNIT_DIM_ID.equals(dimension)) {
items.addAll(organisationUnits);
items.addAll(transientOrganisationUnits);
if (userOrganisationUnit && user != null && user.hasOrganisationUnit()) {
items.addAll(user.getOrganisationUnits());
}
if (userOrganisationUnitChildren && user != null && user.hasOrganisationUnit()) {
user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedChildren()));
}
if (userOrganisationUnitGrandChildren && user != null && user.hasOrganisationUnit()) {
user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedGrandChildren()));
}
if (organisationUnitLevels != null && !organisationUnitLevels.isEmpty() && organisationUnitsAtLevel != null) {
// Must be set externally
items.addAll(organisationUnitsAtLevel);
}
if (itemOrganisationUnitGroups != null && !itemOrganisationUnitGroups.isEmpty() && organisationUnitsInGroups != null) {
// Must be set externally
items.addAll(organisationUnitsInGroups);
}
type = DimensionType.ORGANISATION_UNIT;
} else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
items.addAll(transientCategoryOptionCombos);
type = DimensionType.CATEGORY_OPTION_COMBO;
} else if (STATIC_DIMS.contains(dimension)) {
type = DimensionType.STATIC;
} else {
// Embedded dimensions
Optional<DimensionalObject> object = Optional.empty();
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.DATA_ELEMENT_GROUP_SET, dataElementGroupSetDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.DATA_ELEMENT_GROUP_SET;
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.ORGANISATION_UNIT_GROUP_SET, organisationUnitGroupSetDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.ORGANISATION_UNIT_GROUP_SET;
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY, categoryDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.CATEGORY;
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY_OPTION_GROUP_SET, categoryOptionGroupSetDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.CATEGORY_OPTION_GROUP_SET;
}
// Tracked entity attribute
Map<String, TrackedEntityAttributeDimension> attributes = Maps.uniqueIndex(attributeDimensions, TrackedEntityAttributeDimension::getUid);
if (attributes.containsKey(dimension)) {
TrackedEntityAttributeDimension tead = attributes.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_ATTRIBUTE, null, tead.getDisplayName(), tead.getLegendSet(), tead.getFilter());
}
// Tracked entity data element
Map<String, TrackedEntityDataElementDimension> dataElements = Maps.uniqueIndex(dataElementDimensions, TrackedEntityDataElementDimension::getUid);
if (dataElements.containsKey(dimension)) {
TrackedEntityDataElementDimension tedd = dataElements.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_DATA_ELEMENT, null, tedd.getDisplayName(), tedd.getLegendSet(), tedd.getFilter());
}
// Tracked entity program indicator
Map<String, TrackedEntityProgramIndicatorDimension> programIndicators = Maps.uniqueIndex(programIndicatorDimensions, TrackedEntityProgramIndicatorDimension::getUid);
if (programIndicators.containsKey(dimension)) {
TrackedEntityProgramIndicatorDimension teid = programIndicators.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_INDICATOR, null, teid.getDisplayName(), teid.getLegendSet(), teid.getFilter());
}
}
IdentifiableObjectUtils.removeDuplicates(items);
return new BaseDimensionalObject(dimension, type, items);
}
use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class UserCredentials method getDimensionConstraints.
/**
* Returns the dimensions to use as constrains (filters) in data analytics
* aggregation.
*/
public Set<DimensionalObject> getDimensionConstraints() {
Set<DimensionalObject> constraints = new HashSet<>();
for (CategoryOptionGroupSet cogs : cogsDimensionConstraints) {
cogs.setDimensionType(DimensionType.CATEGORY_OPTION_GROUP_SET);
constraints.add(cogs);
}
for (DataElementCategory cat : catDimensionConstraints) {
cat.setDimensionType(DimensionType.CATEGORY);
constraints.add(cat);
}
return constraints;
}
use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class AnalyticsUtils method getDimensionItemNameMap.
/**
* Returns a mapping between identifiers and names for the given query.
*
* @param params the data query parameters.
* @return a mapping between identifiers and names.
*/
public static Map<String, String> getDimensionItemNameMap(DataQueryParams params) {
List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
Map<String, String> map = new HashMap<>();
Calendar calendar = PeriodType.getCalendar();
for (DimensionalObject dimension : dimensions) {
for (DimensionalItemObject item : dimension.getItems()) {
if (DimensionType.PERIOD.equals(dimension.getDimensionType()) && !calendar.isIso8601()) {
Period period = (Period) item;
DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
map.put(period.getPeriodType().getIsoDate(dateTimeUnit), period.getDisplayName());
} else {
map.put(item.getDimensionItem(), item.getDisplayProperty(params.getDisplayProperty()));
}
if (DimensionType.ORGANISATION_UNIT.equals(dimension.getDimensionType()) && params.isHierarchyMeta()) {
OrganisationUnit unit = (OrganisationUnit) item;
map.putAll(NameableObjectUtils.getUidDisplayPropertyMap(unit.getAncestors(), params.getDisplayProperty()));
}
}
map.put(dimension.getDimension(), dimension.getDisplayProperty(params.getDisplayProperty()));
}
return map;
}
Aggregations