use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.
the class DataSetOrganisationUnitCategoryResourceTable method getPopulateTempTableContent.
/**
* Iterate over data sets and associated organisation units. If data set
* has a category combination and the organisation unit has category options,
* find the intersection of the category option combinations linked to the
* organisation unit through its category options, and the category option
* combinations linked to the data set through its category combination. If
* not, use the default category option combo.
*/
@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
List<Object[]> batchArgs = new ArrayList<>();
for (DataSet dataSet : objects) {
DataElementCategoryCombo categoryCombo = dataSet.getCategoryCombo();
for (OrganisationUnit orgUnit : dataSet.getSources()) {
if (!categoryCombo.isDefault()) {
if (orgUnit.hasCategoryOptions()) {
Set<DataElementCategoryOption> orgUnitOptions = orgUnit.getCategoryOptions();
for (DataElementCategoryOptionCombo optionCombo : categoryCombo.getOptionCombos()) {
Set<DataElementCategoryOption> optionComboOptions = optionCombo.getCategoryOptions();
if (orgUnitOptions.containsAll(optionComboOptions)) {
Date startDate = DateUtils.min(optionComboOptions.stream().map(co -> co.getStartDate()).collect(Collectors.toSet()));
Date endDate = DateUtils.max(optionComboOptions.stream().map(co -> co.getEndDate()).collect(Collectors.toSet()));
List<Object> values = Lists.newArrayList(dataSet.getId(), orgUnit.getId(), optionCombo.getId(), startDate, endDate);
batchArgs.add(values.toArray());
}
}
}
} else {
List<Object> values = Lists.newArrayList(dataSet.getId(), orgUnit.getId(), defaultOptionCombo.getId(), null, null);
batchArgs.add(values.toArray());
}
}
}
return Optional.of(batchArgs);
}
use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.
the class ResourceTableServiceTest method setUpTest.
@Override
public void setUpTest() {
PeriodType pt = new MonthlyPeriodType();
DataElement deA = createDataElement('A');
DataElement deB = createDataElement('B');
idObjectManager.save(deA);
idObjectManager.save(deB);
DataElementGroup degA = createDataElementGroup('A');
DataElementGroup degB = createDataElementGroup('B');
degA.addDataElement(deA);
degB.addDataElement(deB);
idObjectManager.save(degA);
idObjectManager.save(degB);
DataElementGroupSet degsA = createDataElementGroupSet('A');
degsA.addDataElementGroup(degA);
degsA.addDataElementGroup(degB);
idObjectManager.save(degsA);
OrganisationUnit ouA = createOrganisationUnit('A');
OrganisationUnit ouB = createOrganisationUnit('B');
OrganisationUnit ouC = createOrganisationUnit('C');
ouB.setParent(ouA);
ouC.setParent(ouA);
ouA.getChildren().add(ouB);
ouA.getChildren().add(ouC);
idObjectManager.save(ouA);
idObjectManager.save(ouB);
idObjectManager.save(ouC);
DataSet dsA = createDataSet('A', pt);
DataSet dsB = createDataSet('B', pt);
dsA.addDataSetElement(deA);
dsB.addDataSetElement(deA);
dsA.addOrganisationUnit(ouA);
dsB.addOrganisationUnit(ouA);
dataSetService.addDataSet(dsA);
dataSetService.addDataSet(dsB);
}
use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.
the class DefaultPredictorService method predict.
// -------------------------------------------------------------------------
// Predictor run
// -------------------------------------------------------------------------
@Override
public int predict(Predictor predictor, Date startDate, Date endDate) {
log.info("Predicting for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
Expression generator = predictor.getGenerator();
Expression skipTest = predictor.getSampleSkipTest();
DataElement outputDataElement = predictor.getOutput();
Set<String> aggregates = expressionService.getAggregatesInExpression(generator.getExpression());
Map<String, Double> constantMap = constantService.getConstantMap();
List<Period> outputPeriods = getPeriodsBetweenDates(predictor.getPeriodType(), startDate, endDate);
ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap(outputPeriods, predictor);
Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues();
Set<DataElementOperand> dataElementOperands = getDataElementOperands(aggregates, skipTest);
User currentUser = currentUserService.getCurrentUser();
DataElementCategoryOptionCombo outputOptionCombo = predictor.getOutputCombo() == null ? categoryService.getDefaultDataElementCategoryOptionCombo() : predictor.getOutputCombo();
List<OrganisationUnit> orgUnits = organisationUnitService.getOrganisationUnitsAtOrgUnitLevels(predictor.getOrganisationUnitLevels(), currentUser.getOrganisationUnits());
int predictionCount = 0;
for (OrganisationUnit orgUnit : orgUnits) {
MapMapMap<Period, String, DimensionalItemObject, Double> dataMap = dataElementOperands.isEmpty() ? null : dataValueService.getDataElementOperandValues(dataElementOperands, allSamplePeriods, orgUnit);
applySkipTest(dataMap, skipTest, constantMap);
for (Period period : outputPeriods) {
ListMapMap<String, String, Double> aggregateSampleMap = getAggregateSamples(dataMap, aggregates, samplePeriodsMap.get(period), constantMap);
for (String aoc : aggregateSampleMap.keySet()) {
ListMap<String, Double> aggregateValueMap = aggregateSampleMap.get(aoc);
Double value = expressionService.getExpressionValue(generator, new HashMap<>(), constantMap, null, period.getDaysInPeriod(), aggregateValueMap);
if (value != null && !value.isNaN() && !value.isInfinite()) {
writeDataValue(outputDataElement, period, orgUnit, outputOptionCombo, categoryService.getDataElementCategoryOptionCombo(aoc), value.toString(), currentUser.getUsername());
predictionCount++;
}
}
}
}
log.info("Generated " + predictionCount + " predictions for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
return predictionCount;
}
use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.
the class HibernateProgramInstanceStore method buildProgramInstanceHql.
private String buildProgramInstanceHql(ProgramInstanceQueryParams params) {
String hql = "from ProgramInstance pi";
SqlHelper hlp = new SqlHelper(true);
if (params.hasLastUpdated()) {
hql += hlp.whereAnd() + "pi.lastUpdated >= '" + getMediumDateString(params.getLastUpdated()) + "'";
}
if (params.hasTrackedEntityInstance()) {
hql += hlp.whereAnd() + "pi.entityInstance.uid = '" + params.getTrackedEntityInstance().getUid() + "'";
}
if (params.hasTrackedEntity()) {
hql += hlp.whereAnd() + "pi.entityInstance.trackedEntity.uid = '" + params.getTrackedEntity().getUid() + "'";
}
if (params.hasOrganisationUnits()) {
if (params.isOrganisationUnitMode(OrganisationUnitSelectionMode.DESCENDANTS)) {
String ouClause = "(";
SqlHelper orHlp = new SqlHelper(true);
for (OrganisationUnit organisationUnit : params.getOrganisationUnits()) {
ouClause += orHlp.or() + "pi.organisationUnit.path LIKE '" + organisationUnit.getPath() + "%'";
}
ouClause += ")";
hql += hlp.whereAnd() + ouClause;
} else {
hql += hlp.whereAnd() + "pi.organisationUnit.uid in (" + getQuotedCommaDelimitedString(getUids(params.getOrganisationUnits())) + ")";
}
}
if (params.hasProgram()) {
hql += hlp.whereAnd() + "pi.program.uid = '" + params.getProgram().getUid() + "'";
}
if (params.hasProgramStatus()) {
hql += hlp.whereAnd() + "pi.status = '" + params.getProgramStatus() + "'";
}
if (params.hasFollowUp()) {
hql += hlp.whereAnd() + "pi.followup = " + params.getFollowUp();
}
if (params.hasProgramStartDate()) {
hql += hlp.whereAnd() + "pi.enrollmentDate >= '" + getMediumDateString(params.getProgramStartDate()) + "'";
}
if (params.hasProgramEndDate()) {
hql += hlp.whereAnd() + "pi.enrollmentDate <= '" + getMediumDateString(params.getProgramEndDate()) + "'";
}
return hql;
}
use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.
the class DeliveryChannelStrategy method getOrganisationUnit.
protected OrganisationUnit getOrganisationUnit(ProgramMessage message) {
if (message.getRecipients().getOrganisationUnit() == null) {
return null;
}
String uid = message.getRecipients().getOrganisationUnit().getUid();
OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(uid);
message.getRecipients().setOrganisationUnit(orgUnit);
return orgUnit;
}
Aggregations