Search in sources :

Example 1 with ExternalNotificationLogEntry

use of org.hisp.dhis.notification.logging.ExternalNotificationLogEntry in project dhis2-core by dhis2.

the class NotificationRuleActionImplementer method createLogEntry.

protected ExternalNotificationLogEntry createLogEntry(String key, String templateUid) {
    ExternalNotificationLogEntry entry = new ExternalNotificationLogEntry();
    entry.setLastSentAt(new Date());
    entry.setKey(key);
    entry.setNotificationTemplateUid(templateUid);
    entry.setAllowMultiple(false);
    return entry;
}
Also used : ExternalNotificationLogEntry(org.hisp.dhis.notification.logging.ExternalNotificationLogEntry) Date(java.util.Date)

Example 2 with ExternalNotificationLogEntry

use of org.hisp.dhis.notification.logging.ExternalNotificationLogEntry in project dhis2-core by dhis2.

the class RuleActionSendMessageImplementer method implement.

@Override
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);
    publisher.publishEvent(new ProgramRuleEnrollmentEvent(this, template.getId(), programInstance));
    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) ProgramRuleEnrollmentEvent(org.hisp.dhis.program.notification.event.ProgramRuleEnrollmentEvent) ExternalNotificationLogEntry(org.hisp.dhis.notification.logging.ExternalNotificationLogEntry)

Example 3 with ExternalNotificationLogEntry

use of org.hisp.dhis.notification.logging.ExternalNotificationLogEntry in project dhis2-core by dhis2.

the class RuleActionSendMessageImplementer method implement.

@Override
public void implement(RuleEffect ruleEffect, ProgramStageInstance programStageInstance) {
    checkNotNull(programStageInstance, "ProgramStageInstance cannot be null");
    NotificationValidationResult result = validate(ruleEffect, programStageInstance.getProgramInstance());
    // For program without registration
    if (programStageInstance.getProgramStage().getProgram().isWithoutRegistration()) {
        handleSingleEvent(ruleEffect, programStageInstance);
        return;
    }
    if (!result.isValid()) {
        return;
    }
    ProgramInstance pi = programStageInstance.getProgramInstance();
    ProgramNotificationTemplate template = result.getTemplate();
    String key = generateKey(template, pi);
    publisher.publishEvent(new ProgramRuleStageEvent(this, template.getId(), programStageInstance));
    if (result.getLogEntry() != null) {
        return;
    }
    ExternalNotificationLogEntry entry = createLogEntry(key, template.getUid());
    entry.setNotificationTriggeredBy(NotificationTriggerEvent.PROGRAM_STAGE);
    entry.setAllowMultiple(template.isSendRepeatable());
    notificationLoggingService.save(entry);
}
Also used : NotificationValidationResult(org.hisp.dhis.notification.logging.NotificationValidationResult) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ExternalNotificationLogEntry(org.hisp.dhis.notification.logging.ExternalNotificationLogEntry) ProgramRuleStageEvent(org.hisp.dhis.program.notification.event.ProgramRuleStageEvent)

Example 4 with ExternalNotificationLogEntry

use of org.hisp.dhis.notification.logging.ExternalNotificationLogEntry in project dhis2-core by dhis2.

the class NotificationRuleActionImplementer method validate.

@Transactional(readOnly = true)
public NotificationValidationResult validate(RuleEffect ruleEffect, ProgramInstance programInstance) {
    checkNotNull(ruleEffect, "Rule Effect cannot be null");
    checkNotNull(programInstance, "ProgramInstance cannot be null");
    ProgramNotificationTemplate template = getNotificationTemplate(ruleEffect.ruleAction());
    if (template == null) {
        log.warn(String.format("No template found for Program: %s", programInstance.getProgram().getName()));
        return NotificationValidationResult.builder().valid(false).build();
    }
    ExternalNotificationLogEntry logEntry = notificationLoggingService.getByKey(generateKey(template, programInstance));
    // 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.builder().valid(true).template(template).logEntry(logEntry).build();
}
Also used : ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ExternalNotificationLogEntry(org.hisp.dhis.notification.logging.ExternalNotificationLogEntry) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ExternalNotificationLogEntry

use of org.hisp.dhis.notification.logging.ExternalNotificationLogEntry in project dhis2-core by dhis2.

the class RuleActionScheduleMessageImplementer method implement.

@Override
@Transactional
public void implement(RuleEffect ruleEffect, ProgramStageInstance programStageInstance) {
    checkNotNull(programStageInstance, "ProgramStageInstance cannot be null");
    NotificationValidationResult result = validate(ruleEffect, programStageInstance.getProgramInstance());
    // For program without registration
    if (programStageInstance.getProgramStage().getProgram().isWithoutRegistration()) {
        handleSingleEvent(ruleEffect, programStageInstance);
        return;
    }
    if (!result.isValid()) {
        return;
    }
    ProgramInstance pi = programStageInstance.getProgramInstance();
    ProgramNotificationTemplate template = result.getTemplate();
    String key = generateKey(template, pi);
    String date = StringUtils.unwrap(ruleEffect.data(), '\'');
    if (!isDateValid(date)) {
        return;
    }
    ProgramNotificationInstance notificationInstance = notificationTemplateService.createNotificationInstance(template, date);
    notificationInstance.setProgramStageInstance(programStageInstance);
    notificationInstance.setProgramInstance(null);
    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_STAGE);
    entry.setAllowMultiple(template.isSendRepeatable());
    notificationLoggingService.save(entry);
}
Also used : NotificationValidationResult(org.hisp.dhis.notification.logging.NotificationValidationResult) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ExternalNotificationLogEntry(org.hisp.dhis.notification.logging.ExternalNotificationLogEntry) ProgramNotificationInstance(org.hisp.dhis.program.notification.ProgramNotificationInstance) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ExternalNotificationLogEntry (org.hisp.dhis.notification.logging.ExternalNotificationLogEntry)7 NotificationValidationResult (org.hisp.dhis.notification.logging.NotificationValidationResult)4 ProgramInstance (org.hisp.dhis.program.ProgramInstance)3 ProgramNotificationInstance (org.hisp.dhis.program.notification.ProgramNotificationInstance)3 ProgramNotificationTemplate (org.hisp.dhis.program.notification.ProgramNotificationTemplate)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Date (java.util.Date)1 DhisSpringTest (org.hisp.dhis.DhisSpringTest)1 ProgramRuleEnrollmentEvent (org.hisp.dhis.program.notification.event.ProgramRuleEnrollmentEvent)1 ProgramRuleStageEvent (org.hisp.dhis.program.notification.event.ProgramRuleStageEvent)1 ProgramRuleAction (org.hisp.dhis.programrule.ProgramRuleAction)1 Test (org.junit.jupiter.api.Test)1