use of org.hisp.dhis.notification.NotificationMessage in project dhis2-core by dhis2.
the class DefaultDataSetNotificationService method createBatchForSummaryNotifications.
private MessageBatch createBatchForSummaryNotifications(Set<DataSetNotificationTemplate> templates) {
MessageBatch batch = new MessageBatch();
String messageText = "";
boolean summaryCreated = false;
Long pendingOus;
for (DataSetNotificationTemplate template : templates) {
DhisMessage dhisMessage = new DhisMessage();
for (DataSet dataSet : template.getDataSets()) {
if (isValidForSending(getDataSetPeriod(dataSet), template)) {
summaryCreated = true;
pendingOus = dataSet.getSources().stream().filter(ou -> !isCompleted(createRespectiveRegistrationObject(dataSet, ou))).count();
messageText += String.format(SUMMARY_TEXT, pendingOus, getPeriodString(dataSet.getPeriodType().createPeriod()), dataSet.getName()) + TEXT_SEPARATOR;
}
}
if (summaryCreated) {
dhisMessage.message = new NotificationMessage(createSubjectString(template), messageText);
dhisMessage.recipients = resolveInternalRecipients(template);
batch.dhisMessages.add(dhisMessage);
messageText = "";
}
}
log.info(String.format("%d summary dataset notifications created.", batch.dhisMessages.size()));
return batch;
}
use of org.hisp.dhis.notification.NotificationMessage in project dhis2-core by dhis2.
the class DefaultDataSetNotificationService method createProgramMessage.
private ProgramMessage createProgramMessage(DataSetNotificationTemplate template, CompleteDataSetRegistration registration) {
NotificationMessage message = renderer.render(registration, template);
ProgramMessageRecipients recipients;
if (template.getDataSetNotificationTrigger().isScheduled()) {
recipients = resolveExternalRecipientsForSchedule(template, registration);
} else {
recipients = resolveExternalRecipients(template, registration);
}
ProgramMessage programMessage = ProgramMessage.builder().subject(message.getSubject()).text(message.getMessage()).recipients(recipients).build();
programMessage.setDeliveryChannels(template.getDeliveryChannels());
return programMessage;
}
use of org.hisp.dhis.notification.NotificationMessage 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));
}
use of org.hisp.dhis.notification.NotificationMessage in project dhis2-core by dhis2.
the class DefaultValidationNotificationService method createSummarizedMessage.
/**
* Creates a summarized message from the given MessagePairs and pre-rendered
* map of NotificationMessages. The messages generated by each distinct
* MessagePair are concatenated in their given order.
*/
private static NotificationMessage createSummarizedMessage(SortedSet<MessagePair> pairs, final Map<MessagePair, NotificationMessage> renderedNotificationsMap, final Date validationDate) {
Map<Importance, Long> counts = pairs.stream().map(m -> m.result.getValidationRule().getImportance()).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
String subject = String.format("Validation violations as of %s", DateUtils.getLongDateString(validationDate));
String message = String.format("Violations: High %d, medium %d, low %d", counts.getOrDefault(HIGH, 0L), counts.getOrDefault(Importance.MEDIUM, 0L), counts.getOrDefault(LOW, 0L));
// Concatenate the notifications in sorted order, divide by double
// linebreak
message = message + pairs.stream().sorted().map(renderedNotificationsMap::get).map(n -> String.format("%s%s%s", n.getSubject(), LN, n.getMessage())).reduce("", (initStr, newStr) -> String.format("%s%s%s", initStr, LN + LN, newStr));
NotificationMessage notificationMessage = new NotificationMessage(subject, message);
notificationMessage.setPriority(getPriority(counts.getOrDefault(HIGH, 0L) > 0 ? HIGH : counts.getOrDefault(MEDIUM, 0L) > 0 ? MEDIUM : LOW));
return notificationMessage;
}
use of org.hisp.dhis.notification.NotificationMessage in project dhis2-core by dhis2.
the class ProgramNotificationServiceTest method setUpInstances.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private void setUpInstances() {
programNotificationTemplate = createProgramNotificationTemplate(TEMPLATE_NAME, 0, NotificationTrigger.ENROLLMENT, ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE);
programNotificationTemplate.setUid(notificationTemplate);
java.util.Calendar cal = java.util.Calendar.getInstance();
Date today = cal.getTime();
cal.add(java.util.Calendar.DATE, -1);
programNotificationTemplateForToday = createProgramNotificationTemplate(TEMPLATE_NAME, 0, NotificationTrigger.PROGRAM_RULE, ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE, today);
programNotificationInstaceForToday = new ProgramNotificationInstance();
programNotificationInstaceForToday.setProgramNotificationTemplateSnapshot(notificationTemplateMapper.toProgramNotificationTemplateSnapshot(programNotificationTemplateForToday));
programNotificationInstaceForToday.setName(programNotificationTemplateForToday.getName());
programNotificationInstaceForToday.setAutoFields();
programNotificationInstaceForToday.setScheduledAt(today);
root = createOrganisationUnit('R');
lvlOneLeft = createOrganisationUnit('1');
lvlOneRight = createOrganisationUnit('2');
lvlTwoLeftLeft = createOrganisationUnit('3');
lvlTwoLeftLeft.setPhoneNumber(OU_PHONE_NUMBER);
lvlTwoLeftRight = createOrganisationUnit('4');
configureHierarchy(root, lvlOneLeft, lvlOneRight, lvlTwoLeftLeft, lvlTwoLeftRight);
// User and UserGroup
userA = createUser('U');
userA.setPhoneNumber(USERA_PHONE_NUMBER);
userA.getOrganisationUnits().add(lvlTwoLeftLeft);
userB = createUser('V');
userB.setPhoneNumber(USERB_PHONE_NUMBER);
userB.getOrganisationUnits().add(lvlTwoLeftLeft);
userGroup = createUserGroup('G', Sets.newHashSet(userA, userB));
// User based on hierarchy
userLvlTwoLeftLeft = createUser('K');
userLvlTwoLeftLeft.getOrganisationUnits().add(lvlTwoLeftLeft);
lvlTwoLeftLeft.getUsers().add(userLvlTwoLeftLeft);
userLvlTwoLeftRight = createUser('L');
userLvlTwoLeftRight.getOrganisationUnits().add(lvlTwoLeftRight);
lvlTwoLeftRight.getUsers().add(userLvlTwoLeftRight);
userLvlOneLeft = createUser('M');
userLvlOneLeft.getOrganisationUnits().add(lvlOneLeft);
lvlOneLeft.getUsers().add(userLvlOneLeft);
userLvlOneRight = createUser('N');
userLvlOneRight.getOrganisationUnits().add(lvlOneRight);
lvlOneRight.getUsers().add(userLvlOneLeft);
userRoot = createUser('R');
userRoot.getOrganisationUnits().add(root);
root.getUsers().add(userRoot);
userGroupBasedOnHierarchy = createUserGroup('H', Sets.newHashSet(userLvlOneLeft, userLvlOneRight, userLvlTwoLeftLeft, userLvlTwoLeftRight, userRoot));
userGroupBasedOnParent = createUserGroup('H', Sets.newHashSet(userLvlTwoLeftLeft, userLvlTwoLeftRight));
// Program
Program programA = createProgram('A');
programA.setAutoFields();
programA.setOrganisationUnits(Sets.newHashSet(lvlTwoLeftLeft, lvlTwoLeftRight));
programA.setNotificationTemplates(Sets.newHashSet(programNotificationTemplate, programNotificationTemplateForToday));
programA.getProgramAttributes().add(programTrackedEntityAttribute);
trackedEntityAttribute = createTrackedEntityAttribute('T');
trackedEntityAttributeEmail = createTrackedEntityAttribute('E');
trackedEntityAttribute.setValueType(ValueType.PHONE_NUMBER);
trackedEntityAttribute.setValueType(ValueType.EMAIL);
programTrackedEntityAttribute = createProgramTrackedEntityAttribute(programA, trackedEntityAttribute);
programTrackedEntityAttributeEmail = createProgramTrackedEntityAttribute(programA, trackedEntityAttributeEmail);
programTrackedEntityAttribute.setAttribute(trackedEntityAttribute);
programTrackedEntityAttributeEmail.setAttribute(trackedEntityAttributeEmail);
// ProgramStage
ProgramStage programStage = createProgramStage('S', programA);
dataElement = createDataElement('D');
dataElementEmail = createDataElement('E');
dataElement.setValueType(ValueType.PHONE_NUMBER);
dataElementEmail.setValueType(ValueType.EMAIL);
// ProgramInstance & TEI
tei = new TrackedEntityInstance();
tei.setAutoFields();
tei.setOrganisationUnit(lvlTwoLeftLeft);
attributeValue = createTrackedEntityAttributeValue('P', tei, trackedEntityAttribute);
attributeValueEmail = createTrackedEntityAttributeValue('E', tei, trackedEntityAttribute);
attributeValue.setValue(ATT_PHONE_NUMBER);
attributeValueEmail.setValue(ATT_EMAIL);
tei.getTrackedEntityAttributeValues().add(attributeValue);
tei.getTrackedEntityAttributeValues().add(attributeValueEmail);
ProgramInstance programInstance = new ProgramInstance();
programInstance.setAutoFields();
programInstance.setProgram(programA);
programInstance.setOrganisationUnit(lvlTwoLeftLeft);
programInstance.setEntityInstance(tei);
// ProgramStageInstance
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setAutoFields();
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setOrganisationUnit(lvlTwoLeftLeft);
programStageInstance.setProgramStage(programStage);
// lists returned by stubs
programStageInstances.add(programStageInstance);
programInstances.add(programInstance);
programNotificationInstaceForToday.setProgramInstance(programInstance);
notificationMessage = new NotificationMessage(SUBJECT, MESSAGE);
}
Aggregations