Search in sources :

Example 6 with ProgramMessageRecipients

use of org.hisp.dhis.program.message.ProgramMessageRecipients in project dhis2-core by dhis2.

the class DefaultProgramNotificationService method resolveRecipients.

private ProgramMessageRecipients resolveRecipients(ProgramNotificationTemplate template, OrganisationUnit ou, TrackedEntityInstance tei, ProgramInstance pi) {
    ProgramMessageRecipients recipients = new ProgramMessageRecipients();
    ProgramNotificationRecipient recipientType = template.getNotificationRecipient();
    if (recipientType == ProgramNotificationRecipient.ORGANISATION_UNIT_CONTACT) {
        recipients.setOrganisationUnit(ou);
    } else if (recipientType == ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE) {
        recipients.setTrackedEntityInstance(tei);
    } else if (recipientType == ProgramNotificationRecipient.PROGRAM_ATTRIBUTE && template.getRecipientProgramAttribute() != null) {
        List<String> recipientList = pi.getEntityInstance().getTrackedEntityAttributeValues().stream().filter(av -> template.getRecipientProgramAttribute().getUid().equals(av.getAttribute().getUid())).map(TrackedEntityAttributeValue::getPlainValue).collect(Collectors.toList());
        if (template.getDeliveryChannels().contains(DeliveryChannel.SMS)) {
            recipients.getPhoneNumbers().addAll(recipientList);
        } else if (template.getDeliveryChannels().contains(DeliveryChannel.EMAIL)) {
            recipients.getEmailAddresses().addAll(recipientList);
        }
    }
    return recipients;
}
Also used : ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients)

Example 7 with ProgramMessageRecipients

use of org.hisp.dhis.program.message.ProgramMessageRecipients in project dhis2-core by dhis2.

the class DefaultProgramNotificationService method resolveProgramMessageRecipients.

private ProgramMessageRecipients resolveProgramMessageRecipients(ProgramNotificationTemplate template, OrganisationUnit organisationUnit, ProgramInstance programInstance) {
    ProgramMessageRecipients recipients = new ProgramMessageRecipients();
    TrackedEntityInstance trackedEntityInstance = programInstance.getEntityInstance();
    ProgramNotificationRecipient recipientType = template.getNotificationRecipient();
    if (recipientType == ProgramNotificationRecipient.ORGANISATION_UNIT_CONTACT) {
        recipients.setOrganisationUnit(organisationUnit);
    } else if (recipientType == ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE) {
        recipients.setTrackedEntityInstance(trackedEntityInstance);
    } else if (recipientType == ProgramNotificationRecipient.PROGRAM_ATTRIBUTE && template.getRecipientProgramAttribute() != null) {
        List<String> recipientList = programInstance.getEntityInstance().getTrackedEntityAttributeValues().stream().filter(av -> template.getRecipientProgramAttribute().getUid().equals(av.getAttribute().getUid())).map(av -> av.getPlainValue()).collect(Collectors.toList());
        if (template.getDeliveryChannels().contains(DeliveryChannel.SMS)) {
            recipients.getPhoneNumbers().addAll(recipientList);
        } else if (template.getDeliveryChannels().contains(DeliveryChannel.EMAIL)) {
            recipients.getEmailAddresses().addAll(recipientList);
        }
    }
    return recipients;
}
Also used : ProgramMessageService(org.hisp.dhis.program.message.ProgramMessageService) NotificationMessageRenderer(org.hisp.dhis.notification.NotificationMessageRenderer) Date(java.util.Date) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) MessageService(org.hisp.dhis.message.MessageService) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ProgramInstance(org.hisp.dhis.program.ProgramInstance) User(org.hisp.dhis.user.User) Nullable(javax.annotation.Nullable) MessageType(org.hisp.dhis.message.MessageType) ProgramInstanceStore(org.hisp.dhis.program.ProgramInstanceStore) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Clock(org.hisp.dhis.system.util.Clock) List(java.util.List) DeliveryChannel(org.hisp.dhis.common.DeliveryChannel) ProgramStageInstanceStore(org.hisp.dhis.program.ProgramStageInstanceStore) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) NotificationMessage(org.hisp.dhis.notification.NotificationMessage) Transactional(org.springframework.transaction.annotation.Transactional) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 8 with ProgramMessageRecipients

use of org.hisp.dhis.program.message.ProgramMessageRecipients in project dhis2-core by dhis2.

the class ProgramMessageServiceTest method setUpTest.

// -------------------------------------------------------------------------
// Prerequisite
// -------------------------------------------------------------------------
@Override
public void setUpTest() {
    ouA = createOrganisationUnit('A');
    ouA.setPhoneNumber(msisdn);
    ouB = createOrganisationUnit('B');
    orgUnitService.addOrganisationUnit(ouA);
    orgUnitService.addOrganisationUnit(ouB);
    Program program = createProgram('A');
    program.setAutoFields();
    program.setOrganisationUnits(Sets.newHashSet(ouA, ouB));
    program.setName("programA");
    program.setShortName("programAshortname");
    program.setProgramType(ProgramType.WITHOUT_REGISTRATION);
    programService.addProgram(program);
    piA = new ProgramInstance();
    piA.setProgram(program);
    piA.setOrganisationUnit(ouA);
    piA.setName("programInstanceA");
    piA.setEnrollmentDate(new Date());
    piA.setAutoFields();
    programInstanceService.addProgramInstance(piA);
    Set<OrganisationUnit> ouSet = new HashSet<>();
    ouSet.add(ouA);
    Set<String> ouUids = new HashSet<>();
    ouUids.add(ouA.getUid());
    // ouSet.add( ouB );
    teiA = createTrackedEntityInstance(ouA);
    teiService.addTrackedEntityInstance(teiA);
    recipientsA = new ProgramMessageRecipients();
    recipientsA.setOrganisationUnit(ouA);
    recipientsA.setTrackedEntityInstance(teiA);
    recipientsB = new ProgramMessageRecipients();
    recipientsB.setOrganisationUnit(ouA);
    recipientsB.setTrackedEntityInstance(teiA);
    recipientsC = new ProgramMessageRecipients();
    recipientsC.setOrganisationUnit(ouA);
    recipientsC.setTrackedEntityInstance(teiA);
    recipientsD = new ProgramMessageRecipients();
    recipientsD.setOrganisationUnit(ouA);
    recipientsD.setTrackedEntityInstance(null);
    Set<String> phoneNumberListA = new HashSet<>();
    phoneNumberListA.add(msisdn);
    recipientsA.setPhoneNumbers(phoneNumberListA);
    Set<String> phoneNumberListB = new HashSet<>();
    phoneNumberListB.add(msisdn);
    recipientsB.setPhoneNumbers(phoneNumberListB);
    Set<String> phoneNumberListC = new HashSet<>();
    phoneNumberListC.add(msisdn);
    recipientsC.setPhoneNumbers(phoneNumberListC);
    channels.add(DeliveryChannel.SMS);
    pmsgA = createProgramMessage(text, subject, recipientsA, messageStatus, channels);
    pmsgA.setProgramInstance(piA);
    pmsgA.setStoreCopy(false);
    pmsgB = createProgramMessage(text, subject, recipientsB, messageStatus, channels);
    pmsgB.setProgramInstance(piA);
    pmsgC = createProgramMessage(text, subject, recipientsC, messageStatus, channels);
    pmsgD = createProgramMessage(text, subject, recipientsD, messageStatus, channels);
    pmsgD.setProgramInstance(piA);
    pmsgD.setStoreCopy(false);
    uidA = CodeGenerator.generateCode(10);
    uidB = CodeGenerator.generateCode(10);
    uidC = CodeGenerator.generateCode(10);
    pmsgA.setUid(uidA);
    pmsgB.setUid(uidB);
    pmsgC.setUid(uidC);
    params = new ProgramMessageQueryParams();
    params.setOrganisationUnit(ouUids);
    params.setProgramInstance(piA);
    params.setMessageStatus(messageStatus);
    bulkSmsConfig = new BulkSmsGatewayConfig();
    bulkSmsConfig.setDefault(true);
    bulkSmsConfig.setName("bulk");
    bulkSmsConfig.setUsername("user_uio");
    bulkSmsConfig.setPassword("5cKMMQTGNMkD");
    SmsConfiguration smsConfig = new SmsConfiguration();
    smsConfig.getGateways().add(bulkSmsConfig);
    smsConfigurationManager.updateSmsConfiguration(smsConfig);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramMessageQueryParams(org.hisp.dhis.program.message.ProgramMessageQueryParams) SmsConfiguration(org.hisp.dhis.sms.config.SmsConfiguration) BulkSmsGatewayConfig(org.hisp.dhis.sms.config.BulkSmsGatewayConfig) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) Date(java.util.Date) HashSet(java.util.HashSet)

Example 9 with ProgramMessageRecipients

use of org.hisp.dhis.program.message.ProgramMessageRecipients in project dhis2-core by dhis2.

the class MaintenanceServiceTest method testDeleteSoftDeletedTrackedEntityInstanceAProgramMessage.

@Test
void testDeleteSoftDeletedTrackedEntityInstanceAProgramMessage() {
    ProgramMessageRecipients programMessageRecipients = new ProgramMessageRecipients();
    programMessageRecipients.setEmailAddresses(Sets.newHashSet("testemail"));
    programMessageRecipients.setPhoneNumbers(Sets.newHashSet("testphone"));
    programMessageRecipients.setOrganisationUnit(organisationUnit);
    programMessageRecipients.setTrackedEntityInstance(entityInstanceB);
    ProgramMessage message = ProgramMessage.builder().subject("subject").text("text").recipients(programMessageRecipients).deliveryChannels(Sets.newHashSet(DeliveryChannel.EMAIL)).build();
    long idA = entityInstanceService.addTrackedEntityInstance(entityInstanceB);
    programMessageService.saveProgramMessage(message);
    assertNotNull(entityInstanceService.getTrackedEntityInstance(idA));
    entityInstanceService.deleteTrackedEntityInstance(entityInstanceB);
    assertNull(entityInstanceService.getTrackedEntityInstance(idA));
    assertTrue(entityInstanceService.trackedEntityInstanceExistsIncludingDeleted(entityInstanceB.getUid()));
    maintenanceService.deleteSoftDeletedTrackedEntityInstances();
    assertFalse(entityInstanceService.trackedEntityInstanceExistsIncludingDeleted(entityInstanceB.getUid()));
}
Also used : ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) Test(org.junit.jupiter.api.Test)

Example 10 with ProgramMessageRecipients

use of org.hisp.dhis.program.message.ProgramMessageRecipients in project dhis2-core by dhis2.

the class DefaultDataSetNotificationService method resolveExternalRecipients.

private ProgramMessageRecipients resolveExternalRecipients(DataSetNotificationTemplate template, CompleteDataSetRegistration registration) {
    ProgramMessageRecipients recipients = new ProgramMessageRecipients();
    OrganisationUnit ou = registration.getSource();
    for (DeliveryChannel channel : template.getDeliveryChannels()) {
        if (VALIDATOR.get(channel).test(ou)) {
            recipients = RECIPIENT_MAPPER.get(channel).apply(Sets.newHashSet(ou), recipients);
        } else {
            log.error(String.format("DataSet notification not sent due to invalid %s recipient", channel));
            throw new IllegalArgumentException(String.format("Invalid %s recipient", channel));
        }
    }
    return recipients;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DeliveryChannel(org.hisp.dhis.common.DeliveryChannel) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients)

Aggregations

ProgramMessageRecipients (org.hisp.dhis.program.message.ProgramMessageRecipients)10 ProgramMessage (org.hisp.dhis.program.message.ProgramMessage)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 DeliveryChannel (org.hisp.dhis.common.DeliveryChannel)3 NotificationMessage (org.hisp.dhis.notification.NotificationMessage)3 Test (org.junit.jupiter.api.Test)3 Sets (com.google.common.collect.Sets)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Collectors (java.util.stream.Collectors)2 MessageService (org.hisp.dhis.message.MessageService)2 MessageType (org.hisp.dhis.message.MessageType)2 NotificationMessageRenderer (org.hisp.dhis.notification.NotificationMessageRenderer)2 BatchResponseStatus (org.hisp.dhis.outboundmessage.BatchResponseStatus)2 ProgramMessageQueryParams (org.hisp.dhis.program.message.ProgramMessageQueryParams)2 ProgramMessageService (org.hisp.dhis.program.message.ProgramMessageService)2 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)2 User (org.hisp.dhis.user.User)2 Function (com.google.common.base.Function)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1