use of org.hisp.dhis.organisationunit.OrganisationUnitLevel in project dhis2-core by dhis2.
the class TranslationServiceTest method testPredictorTranslations.
@Test
void testPredictorTranslations() {
DataElement dataElementX = createDataElement('X', ValueType.NUMBER, AggregationType.NONE);
DataElement dataElementA = createDataElement('A');
DataElement dataElementB = createDataElement('B');
manager.save(dataElementA);
manager.save(dataElementB);
manager.save(dataElementX);
OrganisationUnitLevel orgUnitLevel1 = new OrganisationUnitLevel(1, "Level1");
manager.save(orgUnitLevel1);
CategoryOptionCombo defaultCombo = categoryService.getDefaultCategoryOptionCombo();
PeriodType periodTypeMonthly = PeriodType.getPeriodTypeByName("Monthly");
Expression expressionA = new Expression("AVG(#{" + dataElementA.getUid() + "})+1.5*STDDEV(#{" + dataElementA.getUid() + "})", "descriptionA");
expressionA.setTranslations(Sets.newHashSet(new Translation(locale.getLanguage(), "DESCRIPTION", "translated descriptionA")));
Expression expressionB = new Expression("AVG(#{" + dataElementB.getUid() + "." + defaultCombo.getUid() + "})", "descriptionB");
expressionB.setTranslations(Sets.newHashSet(new Translation(locale.getLanguage(), "DESCRIPTION", "translated descriptionB")));
Predictor predictor = createPredictor(dataElementX, defaultCombo, "A", expressionA, expressionB, periodTypeMonthly, orgUnitLevel1, 6, 1, 0);
manager.save(predictor);
manager.updateTranslations(predictor, Sets.newHashSet(new Translation(locale.getLanguage(), "NAME", "translated Predictor Name")));
predictor = manager.get(Predictor.class, predictor.getUid());
assertEquals("translated Predictor Name", predictor.getDisplayName());
assertEquals("translated descriptionA", predictor.getGenerator().getDisplayDescription());
assertEquals("translated descriptionB", predictor.getSampleSkipTest().getDisplayDescription());
}
use of org.hisp.dhis.organisationunit.OrganisationUnitLevel in project dhis2-core by dhis2.
the class ObjectBundleServiceTest method testCreateDataSetWithSectionsAndGreyedFields.
@Test
void testCreateDataSetWithSectionsAndGreyedFields() throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_sections_gf.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.COMMIT);
params.setImportStrategy(ImportStrategy.CREATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertFalse(validate.hasErrorReports());
objectBundleService.commit(bundle);
List<DataSet> dataSets = manager.getAll(DataSet.class);
List<Section> sections = manager.getAll(Section.class);
List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
List<DataElement> dataElements = manager.getAll(DataElement.class);
List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
List<User> users = manager.getAll(User.class);
List<DataElementOperand> dataElementOperands = manager.getAll(DataElementOperand.class);
List<TrackedEntityType> trackedEntityTypes = manager.getAll(TrackedEntityType.class);
List<OrganisationUnitLevel> organisationUnitLevels = manager.getAll(OrganisationUnitLevel.class);
assertFalse(organisationUnits.isEmpty());
assertEquals(1, organisationUnitLevels.size());
assertEquals(1, trackedEntityTypes.size());
assertFalse(dataElements.isEmpty());
assertFalse(users.isEmpty());
assertFalse(userRoles.isEmpty());
assertEquals(1, dataSets.size());
assertEquals(2, sections.size());
assertEquals(1, dataElementOperands.size());
DataSet dataSet = dataSets.get(0);
assertEquals(2, dataSet.getSections().size());
Section section1 = sections.get(0);
Section section2 = sections.get(1);
assertEquals(1, section1.getDataElements().size());
assertEquals(1, section2.getDataElements().size());
assertNotNull(section1.getDataSet());
assertNotNull(section2.getDataSet());
Section section = manager.get(Section.class, "C50M0WxaI7y");
assertNotNull(section.getDataSet());
assertEquals(1, section.getCategoryCombos().size());
assertEquals(1, section.getGreyedFields().size());
CategoryCombo categoryCombo = manager.get(CategoryCombo.class, "faV8QvLgIwB");
assertNotNull(categoryCombo);
Category category = manager.get(Category.class, "XJGLlMAMCcn");
assertNotNull(category);
CategoryOption categoryOption1 = manager.get(CategoryOption.class, "JYiFOMKa25J");
CategoryOption categoryOption2 = manager.get(CategoryOption.class, "tdaMRD34m8o");
assertNotNull(categoryOption1);
assertNotNull(categoryOption2);
}
use of org.hisp.dhis.organisationunit.OrganisationUnitLevel in project dhis2-core by dhis2.
the class DefaultPredictionService method predict.
@Override
public void predict(Predictor predictor, Date startDate, Date endDate, PredictionSummary predictionSummary) {
Expression generator = predictor.getGenerator();
Expression skipTest = predictor.getSampleSkipTest();
DataElement outputDataElement = predictor.getOutput();
DataType expressionDataType = DataType.fromValueType(outputDataElement.getValueType());
Map<DimensionalItemId, DimensionalItemObject> outputPeriodItemMap = new HashMap<>();
Map<DimensionalItemId, DimensionalItemObject> sampledItemMap = new HashMap<>();
expressionService.getExpressionDimensionalItemMaps(generator.getExpression(), PREDICTOR_EXPRESSION, expressionDataType, outputPeriodItemMap, sampledItemMap);
Set<String> orgUnitGroupIds = expressionService.getExpressionOrgUnitGroupIds(generator.getExpression(), PREDICTOR_EXPRESSION);
if (skipTest != null) {
expressionService.getExpressionDimensionalItemMaps(skipTest.getExpression(), PREDICTOR_SKIP_TEST, DataType.BOOLEAN, sampledItemMap, sampledItemMap);
orgUnitGroupIds.addAll(expressionService.getExpressionOrgUnitGroupIds(skipTest.getExpression(), PREDICTOR_SKIP_TEST));
}
Map<String, OrganisationUnitGroup> orgUnitGroupMap = orgUnitGroupIds.stream().map(organisationUnitGroupService::getOrganisationUnitGroup).filter(Objects::nonNull).collect(Collectors.toMap(OrganisationUnitGroup::getUid, g -> g));
Map<DimensionalItemId, DimensionalItemObject> itemMap = new HashMap<>(outputPeriodItemMap);
itemMap.putAll(sampledItemMap);
Set<DimensionalItemObject> outputPeriodItems = new HashSet<>(outputPeriodItemMap.values());
Set<DimensionalItemObject> sampledItems = new HashSet<>(sampledItemMap.values());
Set<DimensionalItemObject> items = new HashSet<>(itemMap.values());
List<Period> outputPeriods = getPeriodsBetweenDates(predictor.getPeriodType(), startDate, endDate);
Set<Period> existingOutputPeriods = getExistingPeriods(outputPeriods);
ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap(outputPeriods, predictor);
Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues();
Set<Period> analyticsQueryPeriods = getAnalyticsQueryPeriods(sampledItems, allSamplePeriods, outputPeriodItems, existingOutputPeriods);
Set<Period> dataValueQueryPeriods = getDataValueQueryPeriods(analyticsQueryPeriods, existingOutputPeriods);
outputPeriods = periodService.reloadPeriods(outputPeriods);
CategoryOptionCombo defaultCategoryOptionCombo = categoryService.getDefaultCategoryOptionCombo();
CategoryOptionCombo outputOptionCombo = predictor.getOutputCombo() == null ? defaultCategoryOptionCombo : predictor.getOutputCombo();
DataElementOperand outputDataElementOperand = new DataElementOperand(outputDataElement, outputOptionCombo);
Date now = new Date();
boolean requireData = generator.getMissingValueStrategy() != NEVER_SKIP && (!items.isEmpty());
DimensionalItemObject forwardReference = addOuputToItems(outputDataElementOperand, items);
Set<OrganisationUnit> currentUserOrgUnits = new HashSet<>();
String storedBy = "system-process";
User currentUser = currentUserService.getCurrentUser();
if (currentUser != null) {
currentUserOrgUnits = currentUser.getOrganisationUnits();
storedBy = currentUser.getUsername();
}
PredictionDataConsolidator consolidator = new PredictionDataConsolidator(items, predictor.getOrganisationUnitDescendants().equals(DESCENDANTS), new PredictionDataValueFetcher(dataValueService, categoryService), new PredictionAnalyticsDataFetcher(analyticsService, categoryService));
PredictionWriter predictionWriter = new PredictionWriter(dataValueService, batchHandlerFactory);
predictionWriter.init(existingOutputPeriods, predictionSummary);
predictionSummary.incrementPredictors();
for (OrganisationUnitLevel orgUnitLevel : predictor.getOrganisationUnitLevels()) {
List<OrganisationUnit> orgUnits = organisationUnitService.getOrganisationUnitsAtOrgUnitLevels(Lists.newArrayList(orgUnitLevel), currentUserOrgUnits);
consolidator.init(currentUserOrgUnits, orgUnitLevel.getLevel(), orgUnits, dataValueQueryPeriods, analyticsQueryPeriods, existingOutputPeriods, outputDataElementOperand);
PredictionData data;
while ((data = consolidator.getData()) != null) {
List<DataValue> predictions = new ArrayList<>();
List<PredictionContext> contexts = PredictionContextGenerator.getContexts(outputPeriods, data.getValues(), defaultCategoryOptionCombo);
for (PredictionContext c : contexts) {
List<Period> samplePeriods = new ArrayList<>(samplePeriodsMap.get(c.getOutputPeriod()));
samplePeriods.removeAll(getSkippedPeriods(allSamplePeriods, itemMap, c.getPeriodValueMap(), skipTest, orgUnitGroupMap, data.getOrgUnit()));
if (requireData && !dataIsPresent(outputPeriodItems, c.getValueMap(), sampledItems, samplePeriods, c.getPeriodValueMap())) {
continue;
}
Object value = expressionService.getExpressionValue(ExpressionParams.builder().expression(predictor.getGenerator().getExpression()).parseType(PREDICTOR_EXPRESSION).dataType(expressionDataType).itemMap(itemMap).valueMap(c.getValueMap()).orgUnitGroupMap(orgUnitGroupMap).days(c.getOutputPeriod().getDaysInPeriod()).missingValueStrategy(generator.getMissingValueStrategy()).orgUnit(data.getOrgUnit()).samplePeriods(samplePeriods).periodValueMap(c.getPeriodValueMap()).build());
if (value != null || generator.getMissingValueStrategy() == NEVER_SKIP) {
String valueString = formatPrediction(value, outputDataElement);
if (valueString != null) {
DataValue prediction = new DataValue(outputDataElement, c.getOutputPeriod(), data.getOrgUnit(), outputOptionCombo, c.getAttributeOptionCombo(), valueString, storedBy, now, null);
carryPredictionForward(prediction, contexts, forwardReference);
predictions.add(prediction);
}
}
}
predictionWriter.write(predictions, data.getOldPredictions());
}
}
predictionWriter.flush();
}
use of org.hisp.dhis.organisationunit.OrganisationUnitLevel in project dhis2-core by dhis2.
the class AnalyticsServiceMetadataTest method buildOrgUnitLevel.
private OrganisationUnitLevel buildOrgUnitLevel(int level, String uid, String name, String code) {
OrganisationUnitLevel oul = new OrganisationUnitLevel(level, name);
oul.setUid(uid);
oul.setCode(code);
return oul;
}
use of org.hisp.dhis.organisationunit.OrganisationUnitLevel in project dhis2-core by dhis2.
the class ConfigurationDeletionHandler method allowDeleteOrganisationUnitLevel.
private DeletionVeto allowDeleteOrganisationUnitLevel(OrganisationUnitLevel level) {
OrganisationUnitLevel offlineLevel = configService.getConfiguration().getOfflineOrganisationUnitLevel();
OrganisationUnitLevel defaultLevel = configService.getConfiguration().getFacilityOrgUnitLevel();
return (offlineLevel != null && offlineLevel.equals(level)) || (defaultLevel != null && defaultLevel.equals(level)) ? VETO : ACCEPT;
}
Aggregations