Search in sources :

Example 21 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class RuleActionScheduleMessageImplementer method implement.

@Override
@Transactional
public void implement(RuleEffect ruleEffect, ProgramInstance programInstance) {
    NotificationValidationResult result = validate(ruleEffect, programInstance);
    if (!result.isValid()) {
        return;
    }
    ProgramNotificationTemplate template = result.getTemplate();
    String key = generateKey(template, programInstance);
    String date = StringUtils.unwrap(ruleEffect.data(), '\'');
    if (!isDateValid(date)) {
        return;
    }
    ProgramNotificationInstance notificationInstance = notificationTemplateService.createNotificationInstance(template, date);
    notificationInstance.setProgramStageInstance(null);
    notificationInstance.setProgramInstance(programInstance);
    programNotificationInstanceService.save(notificationInstance);
    log.info(String.format(LOG_MESSAGE, template.getUid()));
    if (result.getLogEntry() != null) {
        return;
    }
    ExternalNotificationLogEntry entry = createLogEntry(key, template.getUid());
    entry.setNotificationTriggeredBy(NotificationTriggerEvent.PROGRAM);
    entry.setAllowMultiple(template.isSendRepeatable());
    notificationLoggingService.save(entry);
}
Also used : NotificationValidationResult(org.hisp.dhis.notification.logging.NotificationValidationResult) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ExternalNotificationLogEntry(org.hisp.dhis.notification.logging.ExternalNotificationLogEntry) ProgramNotificationInstance(org.hisp.dhis.program.notification.ProgramNotificationInstance) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramRuleEngineTest method setUpNotificationForD2HasValue.

private void setUpNotificationForD2HasValue() {
    ProgramNotificationTemplate pnt = new ProgramNotificationTemplate();
    pnt.setName("Test-PNT");
    pnt.setMessageTemplate("message_template");
    pnt.setSubjectTemplate("subject_template");
    pnt.setNotificationTrigger(NotificationTrigger.PROGRAM_RULE);
    pnt.setRecipientProgramAttribute(attributeEmail);
    pnt.setNotificationRecipient(ProgramNotificationRecipient.PROGRAM_ATTRIBUTE);
    pnt.setAutoFields();
    pnt.setUid("PNT-2");
    programNotificationTemplateStore.save(pnt);
    ProgramRuleAction programRuleActionForSendMessage = createProgramRuleAction('C', programRuleE);
    programRuleActionForSendMessage.setProgramRuleActionType(ProgramRuleActionType.SENDMESSAGE);
    programRuleActionForSendMessage.setTemplateUid(pnt.getUid());
    programRuleActionForSendMessage.setContent("STATIC-TEXT");
    programRuleActionService.addProgramRuleAction(programRuleActionForSendMessage);
    programRuleE.setProgramRuleActions(Sets.newHashSet(programRuleActionForSendMessage));
    programRuleService.updateProgramRule(programRuleE);
}
Also used : ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate)

Example 23 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramRuleEngineTest method testSendMessageForEvent.

@Test
void testSendMessageForEvent() {
    setUpSendMessageForEnrollment();
    ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance("UID-PS1");
    List<RuleEffect> ruleEffects = programRuleEngine.evaluate(programStageInstance.getProgramInstance(), programStageInstance, Sets.newHashSet());
    assertEquals(1, ruleEffects.size());
    RuleAction ruleAction = ruleEffects.get(0).ruleAction();
    assertTrue(ruleAction instanceof RuleActionSendMessage);
    RuleActionSendMessage ruleActionSendMessage = (RuleActionSendMessage) ruleAction;
    assertEquals("PNT-1", ruleActionSendMessage.notification());
    ProgramNotificationTemplate template = programNotificationTemplateStore.getByUid("PNT-1");
    assertNotNull(template);
    assertEquals(NotificationTrigger.PROGRAM_RULE, template.getNotificationTrigger());
    assertEquals(ProgramNotificationRecipient.USER_GROUP, template.getNotificationRecipient());
    assertEquals("message_template", template.getMessageTemplate());
}
Also used : ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 24 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramRuleEngineTest method setUpSendMessageForEnrollment.

private void setUpSendMessageForEnrollment() {
    ProgramNotificationTemplate pnt = new ProgramNotificationTemplate();
    pnt.setName("Test-PNT");
    pnt.setMessageTemplate("message_template");
    pnt.setDeliveryChannels(Sets.newHashSet(DeliveryChannel.SMS));
    pnt.setSubjectTemplate("subject_template");
    pnt.setNotificationTrigger(NotificationTrigger.PROGRAM_RULE);
    pnt.setAutoFields();
    pnt.setUid("PNT-1");
    programNotificationTemplateStore.save(pnt);
    ProgramRuleAction programRuleActionForSendMessage = createProgramRuleAction('C', programRuleC);
    programRuleActionForSendMessage.setProgramRuleActionType(ProgramRuleActionType.SENDMESSAGE);
    programRuleActionForSendMessage.setTemplateUid(pnt.getUid());
    programRuleActionForSendMessage.setContent("STATIC-TEXT");
    programRuleActionService.addProgramRuleAction(programRuleActionForSendMessage);
    programRuleC.setProgramRuleActions(Sets.newHashSet(programRuleActionForSendMessage));
    programRuleService.updateProgramRule(programRuleC);
}
Also used : ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate)

Example 25 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramRuleEngineTest method testNotificationWhenUsingD2HasValueWithTEAForEnrollmentAndEvents.

@Test
void testNotificationWhenUsingD2HasValueWithTEAForEnrollmentAndEvents() {
    setUpNotificationForD2HasValue();
    ProgramInstance programInstance = programInstanceService.getProgramInstance("UID-P2");
    List<RuleEffects> ruleEffects = programRuleEngine.evaluateEnrollmentAndEvents(programInstance, Sets.newHashSet(), Lists.newArrayList());
    assertEquals(1, ruleEffects.size());
    RuleEffects enrollmentRuleEffects = ruleEffects.get(0);
    assertTrue(enrollmentRuleEffects.isEnrollment());
    assertEquals("UID-P2", enrollmentRuleEffects.getTrackerObjectUid());
    RuleAction ruleAction = enrollmentRuleEffects.getRuleEffects().get(0).ruleAction();
    assertTrue(ruleAction instanceof RuleActionSendMessage);
    RuleActionSendMessage ruleActionSendMessage = (RuleActionSendMessage) ruleAction;
    assertEquals("PNT-2", ruleActionSendMessage.notification());
    ProgramNotificationTemplate template = programNotificationTemplateStore.getByUid("PNT-2");
    assertNotNull(template);
    assertEquals(NotificationTrigger.PROGRAM_RULE, template.getNotificationTrigger());
    assertEquals(ProgramNotificationRecipient.PROGRAM_ATTRIBUTE, template.getNotificationRecipient());
    assertEquals("message_template", template.getMessageTemplate());
}
Also used : ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

ProgramNotificationTemplate (org.hisp.dhis.program.notification.ProgramNotificationTemplate)27 ProgramRuleAction (org.hisp.dhis.programrule.ProgramRuleAction)8 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 Test (org.junit.jupiter.api.Test)7 ProgramInstance (org.hisp.dhis.program.ProgramInstance)4 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 Date (java.util.Date)3 ExternalNotificationLogEntry (org.hisp.dhis.notification.logging.ExternalNotificationLogEntry)3 ProgramNotificationInstance (org.hisp.dhis.program.notification.ProgramNotificationInstance)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 ValueType (org.hisp.dhis.common.ValueType)2 NotificationValidationResult (org.hisp.dhis.notification.logging.NotificationValidationResult)2 Nonnull (javax.annotation.Nonnull)1 EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)1 ErrorReport (org.hisp.dhis.feedback.ErrorReport)1 BaseNotificationMessageRenderer.formatDate (org.hisp.dhis.notification.BaseNotificationMessageRenderer.formatDate)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 Program (org.hisp.dhis.program.Program)1