Search in sources :

Example 1 with ProgramRule

use of org.hisp.dhis.programrule.ProgramRule in project dhis2-core by dhis2.

the class ProgramRuleIntegrationTest method setUpTest.

@Override
public void setUpTest() throws Exception {
    renderService = _renderService;
    userService = _userService;
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("tracker/simple_metadata.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 validationReport = objectBundleValidationService.validate(bundle);
    assertFalse(validationReport.hasErrorReports());
    objectBundleService.commit(bundle);
    Program program = bundle.getPreheat().get(PreheatIdentifier.UID, Program.class, "BFcipDERJnf");
    Program programWithoutRegistration = bundle.getPreheat().get(PreheatIdentifier.UID, Program.class, "BFcipDERJne");
    DataElement dataElement1 = bundle.getPreheat().get(PreheatIdentifier.UID, DataElement.class, "DATAEL00001");
    DataElement dataElement2 = bundle.getPreheat().get(PreheatIdentifier.UID, DataElement.class, "DATAEL00002");
    ProgramStage programStage = bundle.getPreheat().get(PreheatIdentifier.UID, ProgramStage.class, "NpsdDv6kKSO");
    ProgramRuleVariable programRuleVariable = createProgramRuleVariableWithDataElement('A', program, dataElement2);
    programRuleVariableService.addProgramRuleVariable(programRuleVariable);
    ProgramRule programRuleA = createProgramRule('A', program);
    programRuleA.setUid("ProgramRule");
    programRuleService.addProgramRule(programRuleA);
    ProgramRule programRuleWithoutRegistration = createProgramRule('W', programWithoutRegistration);
    programRuleService.addProgramRule(programRuleWithoutRegistration);
    ProgramRule programRuleB = createProgramRule('B', program);
    programRuleB.setProgramStage(programStage);
    programRuleService.addProgramRule(programRuleB);
    ProgramRuleAction programRuleActionShowWarning = createProgramRuleAction('A', programRuleA);
    programRuleActionShowWarning.setProgramRuleActionType(ProgramRuleActionType.SHOWWARNING);
    programRuleActionShowWarning.setContent("WARNING");
    programRuleActionService.addProgramRuleAction(programRuleActionShowWarning);
    ProgramRuleAction programRuleActionAssign = createProgramRuleAction('C', programRuleA);
    programRuleActionAssign.setProgramRuleActionType(ProgramRuleActionType.ASSIGN);
    programRuleActionAssign.setData("#{ProgramRuleVariableA}");
    programRuleActionAssign.setDataElement(dataElement1);
    programRuleActionService.addProgramRuleAction(programRuleActionAssign);
    ProgramRuleAction programRuleActionShowWarningForProgramStage = createProgramRuleAction('B', programRuleB);
    programRuleActionShowWarningForProgramStage.setProgramRuleActionType(ProgramRuleActionType.SHOWWARNING);
    programRuleActionShowWarningForProgramStage.setContent("PROGRAM STAGE WARNING");
    programRuleActionService.addProgramRuleAction(programRuleActionShowWarningForProgramStage);
    programRuleA.getProgramRuleActions().add(programRuleActionShowWarning);
    programRuleA.getProgramRuleActions().add(programRuleActionAssign);
    programRuleWithoutRegistration.getProgramRuleActions().add(programRuleActionShowWarning);
    programRuleService.updateProgramRule(programRuleWithoutRegistration);
    programRuleB.getProgramRuleActions().add(programRuleActionShowWarningForProgramStage);
    programRuleService.updateProgramRule(programRuleB);
    userA = userService.getUser("M5zQapPyTZI");
    injectSecurityContext(userA);
}
Also used : ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) ObjectBundleParams(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams) Program(org.hisp.dhis.program.Program) ProgramRule(org.hisp.dhis.programrule.ProgramRule) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) List(java.util.List) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 2 with ProgramRule

use of org.hisp.dhis.programrule.ProgramRule in project dhis2-core by dhis2.

the class BaseProgramRuleActionValidator method validate.

@Override
public ProgramRuleActionValidationResult validate(ProgramRuleAction programRuleAction, ProgramRuleActionValidationContext validationContext) {
    ProgramRule rule = validationContext.getProgramRule();
    if (!programRuleAction.hasDataElement() && !programRuleAction.hasTrackedEntityAttribute()) {
        log.debug(String.format("DataElement or TrackedEntityAttribute cannot be null for program rule: %s ", rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(DataElement.class, ErrorCode.E4044, rule.getName())).build();
    }
    Program program = Optional.ofNullable(validationContext.getProgram()).orElse(validationContext.getProgramRuleActionValidationService().getProgramService().getProgram(rule.getProgram().getUid()));
    if (programRuleAction.hasDataElement()) {
        return handleDataElement(validationContext, programRuleAction, program);
    }
    if (programRuleAction.hasTrackedEntityAttribute()) {
        return handleTrackedEntityAttribute(validationContext, programRuleAction, program);
    }
    return ProgramRuleActionValidationResult.builder().valid(true).build();
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) Program(org.hisp.dhis.program.Program) ProgramRule(org.hisp.dhis.programrule.ProgramRule)

Example 3 with ProgramRule

use of org.hisp.dhis.programrule.ProgramRule in project dhis2-core by dhis2.

the class BaseProgramRuleActionValidator method handleTrackedEntityAttribute.

private ProgramRuleActionValidationResult handleTrackedEntityAttribute(ProgramRuleActionValidationContext validationContext, ProgramRuleAction programRuleAction, Program program) {
    ProgramRule rule = validationContext.getProgramRule();
    TrackedEntityAttribute attribute = validationContext.getTrackedEntityAttribute();
    if (attribute == null) {
        log.debug(String.format("TrackedEntityAttribute: %s associated with program rule: %s does not exist", programRuleAction.getAttribute().getUid(), rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(TrackedEntityAttribute.class, ErrorCode.E4046, programRuleAction.getAttribute().getUid(), rule.getName())).build();
    }
    List<String> trackedEntityAttributes = program.getProgramAttributes().stream().map(att -> att.getAttribute().getUid()).collect(Collectors.toList());
    if (!trackedEntityAttributes.contains(attribute.getUid())) {
        log.debug(String.format("TrackedEntityAttribute: %s is not linked to any ProgramTrackedEntityAttribute", attribute.getUid()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(TrackedEntityAttribute.class, ErrorCode.E4048, attribute.getUid(), rule.getName())).build();
    }
    return ProgramRuleActionValidationResult.builder().valid(true).build();
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ProgramRuleActionValidationResult(org.hisp.dhis.programrule.ProgramRuleActionValidationResult) Set(java.util.Set) Collectors(java.util.stream.Collectors) ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) Program(org.hisp.dhis.program.Program) ProgramStage(org.hisp.dhis.program.ProgramStage) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramRule(org.hisp.dhis.programrule.ProgramRule) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Optional(java.util.Optional) ErrorCode(org.hisp.dhis.feedback.ErrorCode) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramRule(org.hisp.dhis.programrule.ProgramRule) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute)

Example 4 with ProgramRule

use of org.hisp.dhis.programrule.ProgramRule in project dhis2-core by dhis2.

the class HideProgramStageProgramRuleActionValidator method validate.

@Override
public ProgramRuleActionValidationResult validate(ProgramRuleAction programRuleAction, ProgramRuleActionValidationContext validationContext) {
    ProgramRule rule = validationContext.getProgramRule();
    if (!programRuleAction.hasProgramStage()) {
        log.debug(String.format("ProgramStage cannot be null for program rule: %s ", rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(ProgramStage.class, ErrorCode.E4038, rule.getName())).build();
    }
    ProgramStage programStage = validationContext.getProgramStage();
    if (programStage == null) {
        programStage = validationContext.getProgramRuleActionValidationService().getProgramStageService().getProgramStage(programRuleAction.getProgramStage().getUid());
    }
    if (programStage == null) {
        log.debug(String.format("ProgramStage: %s associated with program rule: %s does not exist", programRuleAction.getProgramStage().getUid(), rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(ProgramStage.class, ErrorCode.E4039, programRuleAction.getProgramStage().getUid(), rule.getName())).build();
    }
    return ProgramRuleActionValidationResult.builder().valid(true).build();
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ProgramRule(org.hisp.dhis.programrule.ProgramRule) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 5 with ProgramRule

use of org.hisp.dhis.programrule.ProgramRule in project dhis2-core by dhis2.

the class NotificationProgramRuleActionValidator method validate.

@Override
public ProgramRuleActionValidationResult validate(ProgramRuleAction programRuleAction, ProgramRuleActionValidationContext validationContext) {
    ProgramRule rule = validationContext.getProgramRule();
    if (!programRuleAction.hasNotification()) {
        log.debug(String.format("ProgramNotificationTemplate cannot be null for program rule: %s ", rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(ProgramNotificationTemplate.class, ErrorCode.E4035, rule.getName())).build();
    }
    // fetch notification from preheat
    ProgramNotificationTemplate pnt = validationContext.getNotificationTemplate();
    if (pnt == null) {
        // fetch it from database
        pnt = validationContext.getProgramRuleActionValidationService().getNotificationTemplateService().getByUid(programRuleAction.getTemplateUid());
    }
    if (pnt == null) {
        log.debug(String.format("ProgramNotificationTemplate id: %s for program rule: %s does not exist", programRuleAction.getTemplateUid(), rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(ProgramNotificationTemplate.class, ErrorCode.E4034, programRuleAction.getTemplateUid(), rule.getName())).build();
    }
    return ProgramRuleActionValidationResult.builder().valid(true).build();
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ProgramRule(org.hisp.dhis.programrule.ProgramRule) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate)

Aggregations

ProgramRule (org.hisp.dhis.programrule.ProgramRule)14 ErrorReport (org.hisp.dhis.feedback.ErrorReport)8 Program (org.hisp.dhis.program.Program)5 ProgramStage (org.hisp.dhis.program.ProgramStage)5 List (java.util.List)3 DataElement (org.hisp.dhis.dataelement.DataElement)3 ProgramRuleAction (org.hisp.dhis.programrule.ProgramRuleAction)3 ProgramRuleActionValidationResult (org.hisp.dhis.programrule.ProgramRuleActionValidationResult)3 ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)2 Option (org.hisp.dhis.option.Option)2 ProgramStageSection (org.hisp.dhis.program.ProgramStageSection)2 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)2 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 ObjectBundleParams (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams)1