Search in sources :

Example 11 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramNotificationTemplateController method getProgramNotificationTemplates.

// -------------------------------------------------------------------------
// GET
// -------------------------------------------------------------------------
@PreAuthorize("hasRole('ALL')")
@GetMapping(produces = { "application/json" }, value = "/filter")
@ResponseBody
public PagingWrapper<ProgramNotificationTemplate> getProgramNotificationTemplates(@RequestParam(required = false) String program, @RequestParam(required = false) String programStage, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false, defaultValue = "0") int page, @RequestParam(required = false, defaultValue = "50") int pageSize) {
    ProgramNotificationTemplateParam params = ProgramNotificationTemplateParam.builder().program(programService.getProgram(program)).programStage(programStageService.getProgramStage(programStage)).skipPaging(skipPaging).page(page).pageSize(pageSize).build();
    PagingWrapper<ProgramNotificationTemplate> templatePagingWrapper = new PagingWrapper<>();
    if (!skipPaging) {
        long total = programNotificationTemplateService.countProgramNotificationTemplates(params);
        templatePagingWrapper = templatePagingWrapper.withPager(PagingWrapper.Pager.builder().page(page).pageSize(pageSize).total(total).build());
    }
    List<ProgramNotificationTemplate> instances = programNotificationTemplateService.getProgramNotificationTemplates(params);
    return templatePagingWrapper.withInstances(instances);
}
Also used : PagingWrapper(org.hisp.dhis.webapi.controller.event.webrequest.PagingWrapper) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ProgramNotificationTemplateParam(org.hisp.dhis.program.notification.ProgramNotificationTemplateParam) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 12 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramNotificationTemplateObjectBundleHook method resolveTemplateRecipients.

private void resolveTemplateRecipients(ProgramNotificationTemplate pnt, ProgramNotificationRecipient pnr) {
    Function<ProgramNotificationTemplate, ValueType> resolver = RECIPIENT_RESOLVER.get(pnr);
    ValueType valueType = null;
    if (resolver != null && pnt.getRecipientProgramAttribute() != null) {
        valueType = resolver.apply(pnt);
    }
    pnt.setDeliveryChannels(CHANNEL_MAPPER.getOrDefault(valueType, Sets.newHashSet()));
}
Also used : ValueType(org.hisp.dhis.common.ValueType) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate)

Example 13 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramNotificationTemplateObjectBundleHook method preUpdate.

@Override
public <T extends IdentifiableObject> void preUpdate(T object, T persistedObject, ObjectBundle bundle) {
    if (!ProgramNotificationTemplate.class.isInstance(object))
        return;
    ProgramNotificationTemplate template = (ProgramNotificationTemplate) object;
    preProcess(template);
}
Also used : ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate)

Example 14 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramNotificationMessageRendererTest method setUpTest.

@Override
protected void setUpTest() throws Exception {
    DateTime testDate1 = DateTime.now();
    testDate1.withTimeAtStartOfDay();
    testDate1 = testDate1.minusDays(70);
    Date incidentDate = testDate1.toDate();
    DateTime testDate2 = DateTime.now();
    testDate2.withTimeAtStartOfDay();
    Date enrollmentDate = testDate2.toDate();
    dataElementA = createDataElement('A', ValueType.TEXT, AggregationType.NONE, DataElementDomain.TRACKER);
    dataElementA.setUid(dataElementUid);
    dataElementB = createDataElement('B', ValueType.TEXT, AggregationType.NONE, DataElementDomain.TRACKER);
    dataElementB.setUid("DEB-UID");
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    trackedEntityAttributeA = createTrackedEntityAttribute('A');
    trackedEntityAttributeA.setUid(trackedEntityAttributeUid);
    trackedEntityAttributeB = createTrackedEntityAttribute('B');
    attributeService.addTrackedEntityAttribute(trackedEntityAttributeA);
    attributeService.addTrackedEntityAttribute(trackedEntityAttributeB);
    organisationUnitA = createOrganisationUnit('A');
    organisationUnitA.setUid(orgUnitUid);
    organisationUnitService.addOrganisationUnit(organisationUnitA);
    programA = createProgram('A', new HashSet<>(), organisationUnitA);
    programA.setUid(programUid);
    programService.addProgram(programA);
    programTrackedEntityAttributeA = createProgramTrackedEntityAttribute(programA, trackedEntityAttributeA);
    programTrackedEntityAttributeB = createProgramTrackedEntityAttribute(programA, trackedEntityAttributeB);
    programTrackedEntityAttributeStore.save(programTrackedEntityAttributeA);
    programTrackedEntityAttributeStore.save(programTrackedEntityAttributeB);
    programA.setProgramAttributes(Arrays.asList(programTrackedEntityAttributeA, programTrackedEntityAttributeB));
    programService.updateProgram(programA);
    programStageA = createProgramStage('A', programA);
    programStageA.setUid(programStageUid);
    programStageService.saveProgramStage(programStageA);
    programStageDataElementA = createProgramStageDataElement(programStageA, dataElementA, 1);
    programStageDataElementB = createProgramStageDataElement(programStageA, dataElementB, 2);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementA);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementB);
    programStageA.setProgramStageDataElements(Sets.newHashSet(programStageDataElementA, programStageDataElementB));
    programStageService.updateProgramStage(programStageA);
    programA.setProgramStages(Sets.newHashSet(programStageA));
    programService.updateProgram(programA);
    trackedEntityInstanceA = createTrackedEntityInstance(organisationUnitA);
    trackedEntityInstanceA.setUid(trackedEntityUid);
    entityInstanceService.addTrackedEntityInstance(trackedEntityInstanceA);
    trackedEntityAttributeValueA = new TrackedEntityAttributeValue(trackedEntityAttributeA, trackedEntityInstanceA, "attribute-test");
    trackedEntityAttributeValueService.addTrackedEntityAttributeValue(trackedEntityAttributeValueA);
    trackedEntityInstanceA.setTrackedEntityAttributeValues(Sets.newHashSet(trackedEntityAttributeValueA));
    entityInstanceService.updateTrackedEntityInstance(trackedEntityInstanceA);
    // ProgramInstance to be provided in message renderer
    programInstanceA = programInstanceService.enrollTrackedEntityInstance(trackedEntityInstanceA, programA, enrollmentDate, incidentDate, organisationUnitA);
    programInstanceA.setUid(enrollmentUid);
    programInstanceService.updateProgramInstance(programInstanceA);
    // ProgramStageInstance to be provided in message renderer
    programStageInstanceA = new ProgramStageInstance(programInstanceA, programStageA);
    programStageInstanceA.setOrganisationUnit(organisationUnitA);
    programStageInstanceA.setDueDate(enrollmentDate);
    programStageInstanceA.setExecutionDate(new Date());
    programStageInstanceA.setUid("PSI-UID");
    eventDataValueA = new EventDataValue();
    eventDataValueA.setDataElement(dataElementA.getUid());
    eventDataValueA.setAutoFields();
    eventDataValueA.setValue("dataElementA-Text");
    eventDataValueB = new EventDataValue();
    eventDataValueB.setDataElement(dataElementB.getUid());
    eventDataValueB.setAutoFields();
    eventDataValueB.setValue("dataElementB-Text");
    programStageInstanceA.setEventDataValues(Sets.newHashSet(eventDataValueA, eventDataValueB));
    programStageInstanceService.addProgramStageInstance(programStageInstanceA);
    programInstanceA.getProgramStageInstances().add(programStageInstanceA);
    programInstanceService.updateProgramInstance(programInstanceA);
    programNotificationTemplate = new ProgramNotificationTemplate();
    programNotificationTemplate.setName("Test-PNT");
    programNotificationTemplate.setMessageTemplate("message_template");
    programNotificationTemplate.setDeliveryChannels(Sets.newHashSet(DeliveryChannel.SMS));
    programNotificationTemplate.setSubjectTemplate("subject_template");
    programNotificationTemplate.setNotificationTrigger(NotificationTrigger.PROGRAM_RULE);
    programNotificationTemplate.setAutoFields();
    programNotificationTemplate.setUid("PNT-1");
    programNotificationTemplateStore.save(programNotificationTemplate);
}
Also used : ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) DateTime(org.joda.time.DateTime) Date(java.util.Date) BaseNotificationMessageRenderer.formatDate(org.hisp.dhis.notification.BaseNotificationMessageRenderer.formatDate) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) HashSet(java.util.HashSet)

Example 15 with ProgramNotificationTemplate

use of org.hisp.dhis.program.notification.ProgramNotificationTemplate in project dhis2-core by dhis2.

the class ProgramInstanceStoreTest method testGetWithScheduledNotifications.

@Test
void testGetWithScheduledNotifications() {
    ProgramNotificationTemplate a1 = createProgramNotificationTemplate("a1", -1, SCHEDULED_DAYS_INCIDENT_DATE, ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE), a2 = createProgramNotificationTemplate("a2", 1, SCHEDULED_DAYS_INCIDENT_DATE, ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE), a3 = createProgramNotificationTemplate("a3", 7, SCHEDULED_DAYS_ENROLLMENT_DATE, ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE);
    programNotificationStore.save(a1);
    programNotificationStore.save(a2);
    programNotificationStore.save(a3);
    // TEI
    TrackedEntityInstance teiX = createTrackedEntityInstance(organisationUnitA);
    TrackedEntityInstance teiY = createTrackedEntityInstance(organisationUnitA);
    entityInstanceService.addTrackedEntityInstance(teiX);
    entityInstanceService.addTrackedEntityInstance(teiY);
    // Program
    programA.setNotificationTemplates(Sets.newHashSet(a1, a2, a3));
    programService.updateProgram(programA);
    // Dates
    Calendar cal = Calendar.getInstance();
    PeriodType.clearTimeOfDay(cal);
    Date today = cal.getTime();
    cal.add(Calendar.DATE, 1);
    Date tomorrow = cal.getTime();
    cal.add(Calendar.DATE, -2);
    Date yesterday = cal.getTime();
    cal.add(Calendar.DATE, -6);
    Date aWeekAgo = cal.getTime();
    // Enrollments
    ProgramInstance enrollmentA = new ProgramInstance(today, tomorrow, teiX, programA);
    programInstanceStore.save(enrollmentA);
    ProgramInstance enrollmentB = new ProgramInstance(aWeekAgo, yesterday, teiY, programA);
    programInstanceStore.save(enrollmentB);
    // Queries
    List<ProgramInstance> results;
    // A
    results = programInstanceStore.getWithScheduledNotifications(a1, today);
    assertEquals(1, results.size());
    assertEquals(enrollmentA, results.get(0));
    results = programInstanceStore.getWithScheduledNotifications(a2, today);
    assertEquals(1, results.size());
    assertEquals(enrollmentB, results.get(0));
    results = programInstanceStore.getWithScheduledNotifications(a3, today);
    assertEquals(1, results.size());
    assertEquals(enrollmentB, results.get(0));
    results = programInstanceStore.getWithScheduledNotifications(a3, yesterday);
    assertEquals(0, results.size());
}
Also used : ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) Calendar(java.util.Calendar) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

ProgramNotificationTemplate (org.hisp.dhis.program.notification.ProgramNotificationTemplate)27 ProgramRuleAction (org.hisp.dhis.programrule.ProgramRuleAction)8 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 Test (org.junit.jupiter.api.Test)7 ProgramInstance (org.hisp.dhis.program.ProgramInstance)4 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 Date (java.util.Date)3 ExternalNotificationLogEntry (org.hisp.dhis.notification.logging.ExternalNotificationLogEntry)3 ProgramNotificationInstance (org.hisp.dhis.program.notification.ProgramNotificationInstance)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 ValueType (org.hisp.dhis.common.ValueType)2 NotificationValidationResult (org.hisp.dhis.notification.logging.NotificationValidationResult)2 Nonnull (javax.annotation.Nonnull)1 EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)1 ErrorReport (org.hisp.dhis.feedback.ErrorReport)1 BaseNotificationMessageRenderer.formatDate (org.hisp.dhis.notification.BaseNotificationMessageRenderer.formatDate)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 Program (org.hisp.dhis.program.Program)1