Search in sources :

Example 1 with BatchResponseStatus

use of org.hisp.dhis.outboundmessage.BatchResponseStatus in project dhis2-core by dhis2.

the class ProgramMessageController method saveMessages.

// -------------------------------------------------------------------------
// POST
// -------------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" })
public void saveMessages(HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
    ProgramMessageBatch batch = renderService.fromJson(request.getInputStream(), ProgramMessageBatch.class);
    for (ProgramMessage programMessage : batch.getProgramMessages()) {
        programMessageService.validatePayload(programMessage);
    }
    BatchResponseStatus status = programMessageService.sendMessages(batch.getProgramMessages());
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), status);
}
Also used : ProgramMessageBatch(org.hisp.dhis.program.message.ProgramMessageBatch) BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with BatchResponseStatus

use of org.hisp.dhis.outboundmessage.BatchResponseStatus in project dhis2-core by dhis2.

the class DataSetNotificationServiceTest method setUpConfigurations.

private void setUpConfigurations() {
    categoryOptionCombo = new CategoryOptionCombo();
    organisationUnitA = createOrganisationUnit('A');
    organisationUnitB = createOrganisationUnit('B');
    organisationUnitA.setPhoneNumber(PHONE_NUMBER);
    organisationUnitB.setPhoneNumber(PHONE_NUMBER);
    periodA = createPeriod(new MonthlyPeriodType(), getDate(2000, 1, 1), getDate(2000, 1, 31));
    dataElementA = createDataElement('A');
    dataElementB = createDataElement('B');
    dataElementA.setUid(DATA_ELEMENT_A_UID);
    dataElementB.setUid(DATA_ELEMENT_B_UID);
    dataSetA = createDataSet('A', new MonthlyPeriodType());
    dataSetA.addDataSetElement(dataElementA);
    dataSetA.addDataSetElement(dataElementB);
    dataSetA.getSources().add(organisationUnitA);
    dataSetA.getSources().add(organisationUnitB);
    smsTemplateA = new DataSetNotificationTemplate();
    smsTemplateA.setUid(TEMPALTE_A_UID);
    smsTemplateA.setDataSetNotificationTrigger(DataSetNotificationTrigger.DATA_SET_COMPLETION);
    smsTemplateA.setDeliveryChannels(Sets.newHashSet(DeliveryChannel.SMS));
    smsTemplateA.setNotificationRecipient(DataSetNotificationRecipient.ORGANISATION_UNIT_CONTACT);
    smsTemplateA.getDataSets().add(dataSetA);
    emailTemplateB = new DataSetNotificationTemplate();
    emailTemplateB.setUid(TEMPALTE_B_UID);
    emailTemplateB.setDataSetNotificationTrigger(DataSetNotificationTrigger.DATA_SET_COMPLETION);
    emailTemplateB.setDeliveryChannels(Sets.newHashSet(DeliveryChannel.EMAIL));
    emailTemplateB.setNotificationRecipient(DataSetNotificationRecipient.ORGANISATION_UNIT_CONTACT);
    emailTemplateB.getDataSets().add(dataSetA);
    templates.add(smsTemplateA);
    registrationA = new CompleteDataSetRegistration(dataSetA, periodA, organisationUnitA, categoryOptionCombo, new Date(), "", new Date(), "", true);
    notificationMessage = new NotificationMessage("subject", "message");
    summary = new OutboundMessageResponseSummary();
    summary.setBatchStatus(OutboundMessageBatchStatus.COMPLETED);
    summary.setChannel(DeliveryChannel.SMS);
    successStatus = new BatchResponseStatus(Arrays.asList(summary));
}
Also used : BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus) NotificationMessage(org.hisp.dhis.notification.NotificationMessage) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) OutboundMessageResponseSummary(org.hisp.dhis.outboundmessage.OutboundMessageResponseSummary) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Date(java.util.Date)

Example 3 with BatchResponseStatus

use of org.hisp.dhis.outboundmessage.BatchResponseStatus in project dhis2-core by dhis2.

the class DefaultProgramMessageService method sendMessages.

@Override
@Transactional
public BatchResponseStatus sendMessages(List<ProgramMessage> programMessages) {
    List<ProgramMessage> populatedProgramMessages = programMessages.stream().filter(this::hasDataWriteAccess).map(this::setAttributesBasedOnStrategy).collect(Collectors.toList());
    List<OutboundMessageBatch> batches = createBatches(populatedProgramMessages);
    BatchResponseStatus status = new BatchResponseStatus(messageBatchService.sendBatches(batches));
    saveProgramMessages(programMessages, status);
    return status;
}
Also used : BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus) OutboundMessageBatch(org.hisp.dhis.outboundmessage.OutboundMessageBatch) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with BatchResponseStatus

use of org.hisp.dhis.outboundmessage.BatchResponseStatus in project dhis2-core by dhis2.

the class DefaultProgramNotificationService method sendProgramMessages.

private void sendProgramMessages(Set<ProgramMessage> messages) {
    if (messages.isEmpty()) {
        return;
    }
    log.debug(String.format("Dispatching %d ProgramMessages", messages.size()));
    BatchResponseStatus status = programMessageService.sendMessages(Lists.newArrayList(messages));
    log.debug(String.format("Resulting status from ProgramMessageService:\n %s", status.toString()));
}
Also used : BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus)

Example 5 with BatchResponseStatus

use of org.hisp.dhis.outboundmessage.BatchResponseStatus in project dhis2-core by dhis2.

the class ProgramNotificationServiceTest method testSendEnrollmentNotification.

@Test
void testSendEnrollmentNotification() {
    when(programInstanceStore.get(anyLong())).thenReturn(programInstances.iterator().next());
    when(programMessageService.sendMessages(anyList())).thenAnswer(invocation -> {
        sentProgramMessages.addAll((List<ProgramMessage>) invocation.getArguments()[0]);
        return new BatchResponseStatus(Collections.emptyList());
    });
    when(programNotificationRenderer.render(any(ProgramInstance.class), any(NotificationTemplate.class))).thenReturn(notificationMessage);
    programNotificationTemplate.setNotificationTrigger(NotificationTrigger.ENROLLMENT);
    programNotificationService.sendEnrollmentNotifications(programInstances.iterator().next().getId());
    assertEquals(1, sentProgramMessages.size());
    ProgramMessage programMessage = sentProgramMessages.iterator().next();
    assertEquals(TrackedEntityInstance.class, programMessage.getRecipients().getTrackedEntityInstance().getClass());
    assertEquals(tei, programMessage.getRecipients().getTrackedEntityInstance());
}
Also used : BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) NotificationTemplate(org.hisp.dhis.notification.NotificationTemplate) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Aggregations

BatchResponseStatus (org.hisp.dhis.outboundmessage.BatchResponseStatus)12 ProgramMessage (org.hisp.dhis.program.message.ProgramMessage)8 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)7 NotificationTemplate (org.hisp.dhis.notification.NotificationTemplate)7 Test (org.junit.jupiter.api.Test)7 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)2 Date (java.util.Date)1 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)1 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)1 NotificationMessage (org.hisp.dhis.notification.NotificationMessage)1 OutboundMessageBatch (org.hisp.dhis.outboundmessage.OutboundMessageBatch)1 OutboundMessageResponseSummary (org.hisp.dhis.outboundmessage.OutboundMessageResponseSummary)1 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)1 ProgramMessageBatch (org.hisp.dhis.program.message.ProgramMessageBatch)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1