use of org.hisp.dhis.organisationunit.OrganisationUnitGroup in project dhis2-core by dhis2.
the class OrgUnitDistributionServiceTest method testGetOrganisationUnitsByNameAndGroups.
@Test
public void testGetOrganisationUnitsByNameAndGroups() {
//TODO make hierarchy
OrganisationUnit unitA = createOrganisationUnit('A');
OrganisationUnit unitB = createOrganisationUnit('B');
organisationUnitService.addOrganisationUnit(unitA);
organisationUnitService.addOrganisationUnit(unitB);
OrganisationUnitGroup groupA = createOrganisationUnitGroup('A');
OrganisationUnitGroup groupB = createOrganisationUnitGroup('B');
groupA.getMembers().add(unitA);
groupB.getMembers().add(unitB);
organisationUnitGroupService.addOrganisationUnitGroup(groupA);
organisationUnitGroupService.addOrganisationUnitGroup(groupB);
OrganisationUnitGroupSet groupSet = createOrganisationUnitGroupSet('A');
groupSet.getOrganisationUnitGroups().add(groupA);
groupSet.getOrganisationUnitGroups().add(groupB);
organisationUnitGroupService.addOrganisationUnitGroupSet(groupSet);
Grid grid = distributionService.getOrganisationUnitDistribution(groupSet, unitA, false);
assertNotNull(grid);
// Including total
assertEquals(4, grid.getWidth());
// Including total
assertEquals(1, grid.getHeight());
}
use of org.hisp.dhis.organisationunit.OrganisationUnitGroup in project dhis2-core by dhis2.
the class DefaultDataQueryService method getDimension.
// TODO optimize so that org unit levels + boundary are used in query instead of fetching all org units one by one
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
final boolean allItems = items.isEmpty();
if (DATA_X_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
for (String uid : items) {
if (uid.startsWith(KEY_DE_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
}
} else if (uid.startsWith(KEY_IN_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
}
} else {
DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
if (dimItemObject != null) {
dataDimensionItems.add(dimItemObject);
}
}
}
if (dataDimensionItems.isEmpty()) {
throw new IllegalQueryException("Dimension dx is present in query without any valid dimension options");
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems);
return object;
} else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> cocs = new ArrayList<>();
for (String uid : items) {
DataElementCategoryOptionCombo coc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
if (coc != null) {
cocs.add(coc);
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, cocs);
return object;
} else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> aocs = new ArrayList<>();
for (String uid : items) {
DataElementCategoryOptionCombo aoc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
if (aoc != null) {
aocs.add(aoc);
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, aocs);
return object;
} else if (PERIOD_DIM_ID.equals(dimension)) {
Calendar calendar = PeriodType.getCalendar();
List<Period> periods = new ArrayList<>();
for (String isoPeriod : items) {
if (RelativePeriodEnum.contains(isoPeriod)) {
RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriod);
List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true);
periods.addAll(relativePeriods);
} else {
Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
if (period != null) {
periods.add(period);
}
}
}
// Remove duplicates
periods = periods.stream().distinct().collect(Collectors.toList());
if (periods.isEmpty() && !allowAllPeriodItems) {
throw new IllegalQueryException("Dimension pe is present in query without any valid dimension options");
}
for (Period period : periods) {
String name = format != null ? format.formatPeriod(period) : null;
period.setName(name);
period.setShortName(name);
if (!calendar.isIso8601()) {
period.setUid(getLocalPeriodIdentifier(period, calendar));
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods));
return object;
} else if (ORGUNIT_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> ous = new ArrayList<>();
List<Integer> levels = new ArrayList<>();
List<OrganisationUnitGroup> groups = new ArrayList<>();
for (String ou : items) {
if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(userOrgUnits);
} else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
} else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
} else if (ou != null && ou.startsWith(KEY_LEVEL)) {
int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
if (level > 0) {
levels.add(level);
}
} else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
if (group != null) {
groups.add(group);
}
} else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
if (unit != null) {
ous.add(unit);
}
}
}
// Remove duplicates
ous = ous.stream().distinct().collect(Collectors.toList());
List<DimensionalItemObject> orgUnits = new ArrayList<>();
List<OrganisationUnit> ousList = asTypedList(ous);
if (!levels.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
}
if (!groups.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
}
if (levels.isEmpty() && groups.isEmpty()) {
orgUnits.addAll(ous);
}
if (orgUnits.isEmpty()) {
throw new IllegalQueryException("Dimension ou is present in query without any valid dimension options");
}
// Remove duplicates
orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits);
return object;
} else if (LONGITUDE_DIM_ID.contains(dimension)) {
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
return object;
} else if (LATITUDE_DIM_ID.contains(dimension)) {
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
return object;
} else {
DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
if (dimObject != null && dimObject.isDataDimension()) {
Class<?> dimClass = ReflectionUtils.getRealClass(dimObject.getClass());
Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getByUidOrdered(itemClass, items)) : dimObject.getItems();
DimensionalObject object = new BaseDimensionalObject(dimension, dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
return object;
}
}
if (allowNull) {
return null;
}
throw new IllegalQueryException("Dimension identifier does not reference any dimension: " + dimension);
}
use of org.hisp.dhis.organisationunit.OrganisationUnitGroup 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.organisationunit.OrganisationUnitGroup in project dhis2-core by dhis2.
the class DefaultDimensionService method mergeDimensionalObjects.
/**
* Sets persistent objects for dimensional associations on the given
* BaseAnalyticalObject based on the given list of transient DimensionalObjects.
* <p>
* Relative periods represented by enums are converted into a RelativePeriods
* object. User organisation units represented by enums are converted and
* represented by the user organisation unit persisted properties on the
* BaseAnalyticalObject.
*
* @param object the BaseAnalyticalObject to merge.
* @param dimensions the list of dimensions.
*/
private void mergeDimensionalObjects(BaseAnalyticalObject object, List<DimensionalObject> dimensions) {
if (object == null || dimensions == null) {
return;
}
for (DimensionalObject dimension : dimensions) {
DimensionType type = getDimensionType(dimension.getDimension());
String dimensionId = dimension.getDimension();
List<DimensionalItemObject> items = dimension.getItems();
if (items != null) {
List<String> uids = getUids(items);
if (DATA_X.equals(type)) {
for (String uid : uids) {
DimensionalItemObject dimItemObject = getDataDimensionalItemObject(IdScheme.UID, uid);
if (dimItemObject != null) {
DataDimensionItem item = DataDimensionItem.create(dimItemObject);
object.getDataDimensionItems().add(item);
}
}
} else if (PERIOD.equals(type)) {
List<RelativePeriodEnum> enums = new ArrayList<>();
List<Period> periods = new UniqueArrayList<>();
for (String isoPeriod : uids) {
if (RelativePeriodEnum.contains(isoPeriod)) {
enums.add(RelativePeriodEnum.valueOf(isoPeriod));
} else {
Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
if (period != null) {
periods.add(period);
}
}
}
object.setRelatives(new RelativePeriods().setRelativePeriodsFromEnums(enums));
object.setPeriods(periodService.reloadPeriods(new ArrayList<>(periods)));
} else if (ORGANISATION_UNIT.equals(type)) {
for (String ou : uids) {
if (KEY_USER_ORGUNIT.equals(ou)) {
object.setUserOrganisationUnit(true);
} else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou)) {
object.setUserOrganisationUnitChildren(true);
} else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou)) {
object.setUserOrganisationUnitGrandChildren(true);
} else if (ou != null && ou.startsWith(KEY_LEVEL)) {
int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
if (level > 0) {
object.getOrganisationUnitLevels().add(level);
}
} else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
OrganisationUnitGroup group = idObjectManager.get(OrganisationUnitGroup.class, uid);
if (group != null) {
object.getItemOrganisationUnitGroups().add(group);
}
} else {
OrganisationUnit unit = idObjectManager.get(OrganisationUnit.class, ou);
if (unit != null) {
object.getOrganisationUnits().add(unit);
}
}
}
} else if (DATA_ELEMENT_GROUP_SET.equals(type)) {
DataElementGroupSetDimension groupSetDimension = new DataElementGroupSetDimension();
groupSetDimension.setDimension(idObjectManager.get(DataElementGroupSet.class, dimensionId));
groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(DataElementGroup.class, uids));
object.getDataElementGroupSetDimensions().add(groupSetDimension);
} else if (ORGANISATION_UNIT_GROUP_SET.equals(type)) {
OrganisationUnitGroupSetDimension groupSetDimension = new OrganisationUnitGroupSetDimension();
groupSetDimension.setDimension(idObjectManager.get(OrganisationUnitGroupSet.class, dimensionId));
groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(OrganisationUnitGroup.class, uids));
object.getOrganisationUnitGroupSetDimensions().add(groupSetDimension);
} else if (CATEGORY.equals(type)) {
CategoryDimension categoryDimension = new CategoryDimension();
categoryDimension.setDimension(idObjectManager.get(DataElementCategory.class, dimensionId));
categoryDimension.getItems().addAll(idObjectManager.getByUidOrdered(DataElementCategoryOption.class, uids));
object.getCategoryDimensions().add(categoryDimension);
} else if (CATEGORY_OPTION_GROUP_SET.equals(type)) {
CategoryOptionGroupSetDimension groupSetDimension = new CategoryOptionGroupSetDimension();
groupSetDimension.setDimension(idObjectManager.get(CategoryOptionGroupSet.class, dimensionId));
groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(CategoryOptionGroup.class, uids));
object.getCategoryOptionGroupSetDimensions().add(groupSetDimension);
} else if (PROGRAM_ATTRIBUTE.equals(type)) {
TrackedEntityAttributeDimension attributeDimension = new TrackedEntityAttributeDimension();
attributeDimension.setAttribute(idObjectManager.get(TrackedEntityAttribute.class, dimensionId));
attributeDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
attributeDimension.setFilter(dimension.getFilter());
object.getAttributeDimensions().add(attributeDimension);
} else if (PROGRAM_DATA_ELEMENT.equals(type)) {
TrackedEntityDataElementDimension dataElementDimension = new TrackedEntityDataElementDimension();
dataElementDimension.setDataElement(idObjectManager.get(DataElement.class, dimensionId));
dataElementDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
dataElementDimension.setFilter(dimension.getFilter());
object.getDataElementDimensions().add(dataElementDimension);
} else if (PROGRAM_INDICATOR.equals(type)) {
TrackedEntityProgramIndicatorDimension programIndicatorDimension = new TrackedEntityProgramIndicatorDimension();
programIndicatorDimension.setProgramIndicator(idObjectManager.get(ProgramIndicator.class, dimensionId));
programIndicatorDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
programIndicatorDimension.setFilter(dimension.getFilter());
object.getProgramIndicatorDimensions().add(programIndicatorDimension);
}
}
}
}
use of org.hisp.dhis.organisationunit.OrganisationUnitGroup in project dhis2-core by dhis2.
the class OrganisationUnitController method writeFeature.
public void writeFeature(JsonGenerator generator, OrganisationUnit organisationUnit, boolean includeProperties, User user) throws IOException {
if (organisationUnit.getFeatureType() == null || organisationUnit.getCoordinates() == null) {
return;
}
FeatureType featureType = organisationUnit.getFeatureType();
if (!(featureType == FeatureType.POINT)) {
featureType = FeatureType.MULTI_POLYGON;
}
generator.writeStartObject();
generator.writeStringField("type", "Feature");
generator.writeStringField("id", organisationUnit.getUid());
generator.writeObjectFieldStart("geometry");
generator.writeObjectField("type", featureType.value());
generator.writeFieldName("coordinates");
generator.writeRawValue(organisationUnit.getCoordinates());
generator.writeEndObject();
generator.writeObjectFieldStart("properties");
if (includeProperties) {
Set<OrganisationUnit> roots = user.getDataViewOrganisationUnitsWithFallback();
generator.writeStringField("code", organisationUnit.getCode());
generator.writeStringField("name", organisationUnit.getName());
generator.writeStringField("level", String.valueOf(organisationUnit.getLevel()));
if (organisationUnit.getParent() != null) {
generator.writeStringField("parent", organisationUnit.getParent().getUid());
}
generator.writeStringField("parentGraph", organisationUnit.getParentGraph(roots));
generator.writeArrayFieldStart("groups");
for (OrganisationUnitGroup group : organisationUnit.getGroups()) {
generator.writeString(group.getUid());
}
generator.writeEndArray();
}
generator.writeEndObject();
generator.writeEndObject();
}
Aggregations