Search in sources :

Example 1 with NotificationMessage

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;
}
Also used : NotificationMessage(org.hisp.dhis.notification.NotificationMessage) DataSet(org.hisp.dhis.dataset.DataSet)

Example 2 with NotificationMessage

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;
}
Also used : NotificationMessage(org.hisp.dhis.notification.NotificationMessage) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage)

Example 3 with NotificationMessage

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));
}
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 4 with NotificationMessage

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;
}
Also used : Importance(org.hisp.dhis.validation.Importance) NotificationMessageRenderer(org.hisp.dhis.notification.NotificationMessageRenderer) SortedSet(java.util.SortedSet) Importance(org.hisp.dhis.validation.Importance) Date(java.util.Date) HashMap(java.util.HashMap) BooleanUtils(org.apache.commons.lang3.BooleanUtils) SendStrategy(org.hisp.dhis.notification.SendStrategy) MessageService(org.hisp.dhis.message.MessageService) Function(java.util.function.Function) ValidationResult(org.hisp.dhis.validation.ValidationResult) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) HIGH(org.hisp.dhis.validation.Importance.HIGH) Service(org.springframework.stereotype.Service) Map(java.util.Map) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) User(org.hisp.dhis.user.User) EqualsBuilder(org.apache.commons.lang3.builder.EqualsBuilder) LOW(org.hisp.dhis.validation.Importance.LOW) BiMap(com.google.common.collect.BiMap) LN(org.hisp.dhis.commons.util.TextUtils.LN) MessageConversationPriority(org.hisp.dhis.message.MessageConversationPriority) Predicate(java.util.function.Predicate) ValidationResultService(org.hisp.dhis.validation.ValidationResultService) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) CompareToBuilder(org.apache.commons.lang3.builder.CompareToBuilder) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Slf4j(lombok.extern.slf4j.Slf4j) Clock(org.hisp.dhis.system.util.Clock) HashBiMap(com.google.common.collect.HashBiMap) MEDIUM(org.hisp.dhis.validation.Importance.MEDIUM) NotificationMessage(org.hisp.dhis.notification.NotificationMessage) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) NotificationMessage(org.hisp.dhis.notification.NotificationMessage)

Example 5 with 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);
}
Also used : Program(org.hisp.dhis.program.Program) NotificationMessage(org.hisp.dhis.notification.NotificationMessage) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

NotificationMessage (org.hisp.dhis.notification.NotificationMessage)7 Date (java.util.Date)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 MessageConversationPriority (org.hisp.dhis.message.MessageConversationPriority)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 Sets (com.google.common.collect.Sets)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 NotNull (javax.validation.constraints.NotNull)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BooleanUtils (org.apache.commons.lang3.BooleanUtils)1