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);
}
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);
}
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());
}
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);
}
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());
}
Aggregations