@Test @SkipBaseSetup public void testSingleNotify() { try { Context.openSession(); Context.authenticate("admin", "test"); RegistrarBean registrarBean = Context.getService(MotechService.class) .getRegistrarBean(); Date date = new Date(); Integer motherMotechId = 1234649; Facility facility = registrarBean.getFacilityById(11117); registrarBean.registerPatient(RegistrationMode.USE_PREPRINTED_ID, motherMotechId, RegistrantType.PREGNANT_MOTHER, "motherfirstName", "mothermiddleName", "motherlastName", "motherprefName", date, false, Gender.FEMALE, true, "mothernhis", date, null, null, facility, "Address", "1111111111", date, true, true, true, ContactNumberType.PERSONAL, MediaType.TEXT, "language", DayOfWeek.MONDAY, date, InterestReason.CURRENTLY_PREGNANT, HowLearned.FRIEND, null); Integer childMotechId = 1234654; registrarBean.registerPatient(RegistrationMode.USE_PREPRINTED_ID, childMotechId, RegistrantType.CHILD_UNDER_FIVE, "childfirstName", "childmiddleName", "childlastName", "childprefName", date, false, Gender.FEMALE, true, "childnhis", date, null, null, facility, "Address", "1111111111", null, null, false, false, ContactNumberType.PERSONAL, MediaType.TEXT, "language", DayOfWeek.MONDAY, date, InterestReason.FAMILY_FRIEND_PREGNANT, HowLearned.FRIEND, null); assertEquals(4, Context.getPatientService().getAllPatients().size()); ArrayList<PatientIdentifierType> patientIdTypeList = new ArrayList<PatientIdentifierType>(); patientIdTypeList.add(Context.getPatientService() .getPatientIdentifierTypeByName( MotechConstants.PATIENT_IDENTIFIER_MOTECH_ID)); List<Patient> motherMatchingPatients = Context.getPatientService() .getPatients("motherfirstName motherlastName", motherMotechId.toString(), patientIdTypeList, true); assertEquals(1, motherMatchingPatients.size()); Patient mother = motherMatchingPatients.get(0); Obs pregnancyObs = registrarBean.getActivePregnancy(mother .getPatientId()); assertNotNull("Pregnancy Obs does not exist", pregnancyObs); List<Patient> childMatchingPatients = Context.getPatientService() .getPatients("childfirstName childlastName", childMotechId.toString(), patientIdTypeList, true); assertEquals(1, childMatchingPatients.size()); Patient child = childMatchingPatients.get(0); String messageKey = "Test Definition"; String messageKeyA = "Test DefinitionA"; String messageKeyB = "Test DefinitionB"; String messageKeyC = "Test DefinitionC"; MessageDefinition messageDefinition = new MessageDefinition( messageKey, 2L, MessageType.INFORMATIONAL); MessageDefinition messageDefinitionA = new MessageDefinition( messageKeyA, 3L, MessageType.INFORMATIONAL); MessageDefinition messageDefinitionB = new MessageDefinition( messageKeyB, 4L, MessageType.INFORMATIONAL); MessageDefinition messageDefinitionC = new MessageDefinition( messageKeyC, 5L, MessageType.INFORMATIONAL); Context.getService(MotechService.class).saveMessageDefinition( messageDefinition); Context.getService(MotechService.class).saveMessageDefinition( messageDefinitionA); Context.getService(MotechService.class).saveMessageDefinition( messageDefinitionB); Context.getService(MotechService.class).saveMessageDefinition( messageDefinitionC); MessageProgramEnrollment enrollment = new MessageProgramEnrollment(); enrollment.setStartDate(new Date()); enrollment.setProgram("Fake Program Name"); enrollment.setPersonId(child.getPatientId()); Context.getService(MotechService.class) .saveMessageProgramEnrollment(enrollment); Date scheduledMessageDate = new Date( System.currentTimeMillis() + 5 * 1000); MessageSchedulerImpl messageScheduler = new MessageSchedulerImpl(); messageScheduler.setRegistrarBean(registrarBean); messageScheduler.scheduleMessages(messageKey, messageKeyA, messageKeyB, messageKeyC, enrollment, scheduledMessageDate, date); } finally { Context.closeSession(); } NotificationTask task = new NotificationTask(); TaskDefinition taskDef = new TaskDefinition(); taskDef.setRepeatInterval(30L); task.initialize(taskDef); task.execute(); try { Context.openSession(); List<ScheduledMessage> scheduledMessages = Context.getService( MotechService.class).getAllScheduledMessages(); assertEquals(3, scheduledMessages.size()); for (int i = 0; i < 3; i++) { List<Message> messageAttempts = scheduledMessages.get(i) .getMessageAttempts(); assertEquals(1, messageAttempts.size()); Message message = messageAttempts.get(0); assertNotNull("Message attempt date is null", message .getAttemptDate()); if (i == 0) { assertEquals(MessageStatus.ATTEMPT_PENDING, message .getAttemptStatus()); } else { assertEquals(MessageStatus.SHOULD_ATTEMPT, message .getAttemptStatus()); } } } finally { Context.closeSession(); } }
@Override public void execute() { long start = System.currentTimeMillis(); log.info("executing task"); String timeOffsetString = this.taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_TIME_OFFSET); Boolean sendImmediate = Boolean.valueOf(taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_SEND_IMMEDIATE)); Long timeOffset = 0L; if (timeOffsetString != null) { timeOffset = Long.valueOf(timeOffsetString); } Date startDate = new Date(System.currentTimeMillis() + (timeOffset * 1000)); Date endDate = new Date(System.currentTimeMillis() + (this.taskDefinition.getRepeatInterval() * 1000) + (timeOffset * 1000)); try { contextService.openSession(); contextService.getRegistrarBean().sendMessages(startDate, endDate, sendImmediate); } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); }
NotificationTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("executing task"); String timeOffsetString = this.taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_TIME_OFFSET); Boolean sendImmediate = Boolean.valueOf(taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_SEND_IMMEDIATE)); Long timeOffset = 0L; if (timeOffsetString != null) { timeOffset = Long.valueOf(timeOffsetString); } Date startDate = new Date(System.currentTimeMillis() + (timeOffset * 1000)); Date endDate = new Date(System.currentTimeMillis() + (this.taskDefinition.getRepeatInterval() * 1000) + (timeOffset * 1000)); try { contextService.openSession(); contextService.getRegistrarBean().sendMessages(startDate, endDate, sendImmediate); } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } }
NotificationTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("executing task"); String timeOffsetString = this.taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_TIME_OFFSET); Boolean sendImmediate = Boolean.valueOf(taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_SEND_IMMEDIATE)); Long timeOffset = 0L; if (timeOffsetString != null) { timeOffset = Long.valueOf(timeOffsetString); } Date startDate = new Date(System.currentTimeMillis() + (timeOffset * 1000)); Date endDate = new Date(System.currentTimeMillis() + (this.taskDefinition.getRepeatInterval() * 1000) + (timeOffset * 1000)); try { contextService.openSession(); contextService.getRegistrarBean().sendMessages(startDate, endDate, sendImmediate); } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } NotificationTask(); }
NotificationTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("executing task"); String timeOffsetString = this.taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_TIME_OFFSET); Boolean sendImmediate = Boolean.valueOf(taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_SEND_IMMEDIATE)); Long timeOffset = 0L; if (timeOffsetString != null) { timeOffset = Long.valueOf(timeOffsetString); } Date startDate = new Date(System.currentTimeMillis() + (timeOffset * 1000)); Date endDate = new Date(System.currentTimeMillis() + (this.taskDefinition.getRepeatInterval() * 1000) + (timeOffset * 1000)); try { contextService.openSession(); contextService.getRegistrarBean().sendMessages(startDate, endDate, sendImmediate); } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } NotificationTask(); void setContextService(ContextService contextService); @Override void execute(); }
NotificationTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("executing task"); String timeOffsetString = this.taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_TIME_OFFSET); Boolean sendImmediate = Boolean.valueOf(taskDefinition .getProperty(MotechConstants.TASK_PROPERTY_SEND_IMMEDIATE)); Long timeOffset = 0L; if (timeOffsetString != null) { timeOffset = Long.valueOf(timeOffsetString); } Date startDate = new Date(System.currentTimeMillis() + (timeOffset * 1000)); Date endDate = new Date(System.currentTimeMillis() + (this.taskDefinition.getRepeatInterval() * 1000) + (timeOffset * 1000)); try { contextService.openSession(); contextService.getRegistrarBean().sendMessages(startDate, endDate, sendImmediate); } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } NotificationTask(); void setContextService(ContextService contextService); @Override void execute();  }