Skip to content

Commit

Permalink
fix: Remove publish application events for rule engine notifications …
Browse files Browse the repository at this point in the history
…[DHIS2-17243] (#17770)

* fix: Remove publish application events for rule engine notifications [DHIS2-17243]

* More clean up

* Add tests

* Format

* Delete unused application events

* fix: Run most preprocessors only for CREATE and UPDATE [DHIS2-17402]

* Code review comments

* Fix tests
  • Loading branch information
enricocolasante authored Jun 19, 2024
1 parent e1ef2a6 commit 0524427
Show file tree
Hide file tree
Showing 12 changed files with 676 additions and 725 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,30 @@
*/
package org.hisp.dhis.notification.logging;

import lombok.Builder;
import lombok.Data;
import org.hisp.dhis.program.notification.ProgramNotificationTemplate;
import lombok.Getter;

/**
* @author Zubair Asghar
*/
@Data
@Builder
public class NotificationValidationResult {
ProgramNotificationTemplate template;
private final boolean needsToCreateLogEntry;
@Getter private final boolean valid;

private NotificationValidationResult(boolean needsToCreatelogEntry, boolean valid) {
this.needsToCreateLogEntry = needsToCreatelogEntry;
this.valid = valid;
}

public static NotificationValidationResult invalid() {
return new NotificationValidationResult(false, false);
}

public static NotificationValidationResult validAndNoNeedForLogEntries() {
return new NotificationValidationResult(false, true);
}

ExternalNotificationLogEntry logEntry;
public static NotificationValidationResult validAndNeedsLogEntries() {
return new NotificationValidationResult(true, true);
}

boolean valid;
public boolean needsToCreateLogEntry() {
return needsToCreateLogEntry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,21 @@ public interface ProgramNotificationService {
* Send completion notifications for the Enrollment triggered by ProgramRule evaluation. {@link
* ProgramNotificationTemplate templates}, nothing will happen.
*
* @param pnt ProgramNotificationTemplate id to send
* @param enrollment the Enrollment id.
* @param template ProgramNotificationTemplate to send
* @param enrollment the enrollment.
*/
void sendProgramRuleTriggeredNotifications(long pnt, long enrollment);

void sendProgramRuleTriggeredNotifications(long pnt, Enrollment enrollment);
void sendProgramRuleTriggeredNotifications(
ProgramNotificationTemplate template, Enrollment enrollment);

/**
* Send completion notifications for the Event triggered by ProgramRule evaluation. {@link
* ProgramNotificationTemplate templates}, nothing will happen.
*
* @param pnt ProgramNotificationTemplate id to send
* @param eventId the event id.
* @param template ProgramNotificationTemplate to send
* @param event the event.
*/
void sendProgramRuleTriggeredEventNotifications(long pnt, long eventId);

void sendProgramRuleTriggeredEventNotifications(long pnt, Event event);
void sendProgramRuleTriggeredEventNotifications(
ProgramNotificationTemplate template, Event event);

/**
* Send completion notifications for the Enrollment. If the Program is not configured with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,40 +282,18 @@ public void sendEnrollmentNotifications(long enrollment) {

@Override
@Transactional
public void sendProgramRuleTriggeredNotifications(long pnt, long enrollment) {
public void sendProgramRuleTriggeredNotifications(
ProgramNotificationTemplate template, Enrollment enrollment) {
MessageBatch messageBatch =
createEnrollmentMessageBatch(
notificationTemplateService.get(pnt),
Collections.singletonList(enrollmentStore.get(enrollment)));
createEnrollmentMessageBatch(template, Collections.singletonList(enrollment));
sendAll(messageBatch);
}

@Override
@Transactional
public void sendProgramRuleTriggeredNotifications(long pnt, Enrollment enrollment) {
MessageBatch messageBatch =
createEnrollmentMessageBatch(
notificationTemplateService.get(pnt), Collections.singletonList(enrollment));
sendAll(messageBatch);
}

@Override
@Transactional
public void sendProgramRuleTriggeredEventNotifications(long pnt, long eventId) {
MessageBatch messageBatch =
createEventMessageBatch(
notificationTemplateService.get(pnt),
Collections.singletonList(eventStore.get(eventId)));
sendAll(messageBatch);
}

@Override
@Transactional
public void sendProgramRuleTriggeredEventNotifications(long pnt, Event event) {
MessageBatch messageBatch =
createEventMessageBatch(
notificationTemplateService.get(pnt),
Collections.singletonList(eventStore.getByUid(event.getUid())));
public void sendProgramRuleTriggeredEventNotifications(
ProgramNotificationTemplate template, Event event) {
MessageBatch messageBatch = createEventMessageBatch(template, Collections.singletonList(event));
sendAll(messageBatch);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.program.notification.event.ProgramEnrollmentCompletionNotificationEvent;
import org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent;
import org.hisp.dhis.program.notification.event.ProgramRuleEnrollmentEvent;
import org.hisp.dhis.program.notification.event.ProgramRuleStageEvent;
import org.hisp.dhis.program.notification.event.ProgramStageCompletionNotificationEvent;
import org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent;
import org.hisp.dhis.programrule.engine.TrackerEventWebHookEvent;
Expand Down Expand Up @@ -63,19 +61,6 @@ public void onEvent(ProgramStageCompletionNotificationEvent event) {
programNotificationService.sendEventCompletionNotifications(event.getEvent());
}

// Published by rule engine
@TransactionalEventListener(fallbackExecution = true)
public void onProgramRuleEnrollment(ProgramRuleEnrollmentEvent event) {
programNotificationService.sendProgramRuleTriggeredNotifications(
event.getTemplate(), event.getEnrollment());
}

@TransactionalEventListener(fallbackExecution = true)
public void onProgramRuleEvent(ProgramRuleStageEvent event) {
programNotificationService.sendProgramRuleTriggeredEventNotifications(
event.getTemplate(), event.getEvent());
}

@TransactionalEventListener(fallbackExecution = true)
public void onTrackerEventWebHook(TrackerEventWebHookEvent event) {
trackerNotificationWebHookService.handleEvent(event.getEvent());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,91 +27,69 @@
*/
package org.hisp.dhis.programrule.engine;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hisp.dhis.programrule.engine.RuleActionKey.NOTIFICATION;

import java.util.Date;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.notification.logging.ExternalNotificationLogEntry;
import org.hisp.dhis.notification.logging.NotificationLoggingService;
import org.hisp.dhis.notification.logging.NotificationTriggerEvent;
import org.hisp.dhis.notification.logging.NotificationValidationResult;
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.program.Event;
import org.hisp.dhis.program.notification.ProgramNotificationTemplate;
import org.hisp.dhis.program.notification.ProgramNotificationTemplateService;
import org.hisp.dhis.rules.models.RuleAction;
import org.hisp.dhis.rules.models.RuleEffect;
import org.springframework.stereotype.Component;

/**
* @author Zubair Asghar
*/
@Slf4j
@RequiredArgsConstructor
@Component("org.hisp.dhis.programrule.engine.NotificationRuleActionImplementer")
abstract class NotificationRuleActionImplementer implements RuleActionImplementer {
// -------------------------------------------------------------------------
// Dependencies
// -------------------------------------------------------------------------

protected final ProgramNotificationTemplateService programNotificationTemplateService;

protected final NotificationLoggingService notificationLoggingService;
@Component
class NotificationHelper {
private final ProgramNotificationTemplateService programNotificationTemplateService;
private final NotificationLoggingService notificationLoggingService;

protected ExternalNotificationLogEntry createLogEntry(String key, String templateUid) {
public void createLogEntry(ProgramNotificationTemplate template, Enrollment enrollment) {
String key = generateKey(template, enrollment);
ExternalNotificationLogEntry entry = new ExternalNotificationLogEntry();
entry.setLastSentAt(new Date());
entry.setKey(key);
entry.setNotificationTemplateUid(templateUid);
entry.setAllowMultiple(false);
entry.setNotificationTemplateUid(template.getUid());
entry.setNotificationTriggeredBy(NotificationTriggerEvent.PROGRAM);
entry.setAllowMultiple(template.isSendRepeatable());

return entry;
notificationLoggingService.save(entry);
}

public ProgramNotificationTemplate getNotificationTemplate(RuleAction action) {
String uid = action.getValues().get(NOTIFICATION);
return programNotificationTemplateService.getByUid(uid);
}

protected String generateKey(ProgramNotificationTemplate template, Enrollment enrollment) {
return template.getUid() + enrollment.getUid();
}

public NotificationValidationResult validate(RuleEffect ruleEffect, Enrollment enrollment) {
checkNotNull(ruleEffect, "Rule Effect cannot be null");
checkNotNull(enrollment, "Enrollment cannot be null");

ProgramNotificationTemplate template = getNotificationTemplate(ruleEffect.getRuleAction());

public NotificationValidationResult validate(
ProgramNotificationTemplate template, Enrollment enrollment) {
if (template == null) {
log.warn(
String.format("No template found for Program: %s", enrollment.getProgram().getName()));
return NotificationValidationResult.invalid();
}

return NotificationValidationResult.builder().valid(false).build();
if (enrollment == null || enrollment.getProgram().isWithoutRegistration()) {
return NotificationValidationResult.validAndNoNeedForLogEntries();
}

ExternalNotificationLogEntry logEntry =
notificationLoggingService.getByKey(generateKey(template, enrollment));

// template has already been delivered and repeated delivery not allowed
if (logEntry != null && !logEntry.isAllowMultiple()) {
return NotificationValidationResult.builder()
.valid(false)
.template(template)
.logEntry(logEntry)
.build();
return NotificationValidationResult.invalid();
}

return NotificationValidationResult.builder()
.valid(true)
.template(template)
.logEntry(logEntry)
.build();
return logEntry == null
? NotificationValidationResult.validAndNeedsLogEntries()
: NotificationValidationResult.validAndNoNeedForLogEntries();
}

protected void checkNulls(RuleEffect ruleEffect, Event event) {
checkNotNull(ruleEffect, "Rule Effect cannot be null");
checkNotNull(event, "Event cannot be null");
private String generateKey(ProgramNotificationTemplate template, Enrollment enrollment) {
return template.getUid() + enrollment.getUid();
}
}
Loading

0 comments on commit 0524427

Please sign in to comment.