@Test @SkipBaseSetup public void testMessageProgramUpdate() throws InterruptedException { MessageProgramUpdateTask task = new MessageProgramUpdateTask(); TaskDefinition definition = new TaskDefinition(); definition.setProperty(MotechConstants.TASK_PROPERTY_BATCH_SIZE, "10"); task.initialize(definition); Integer patientId = null; try { Context.openSession(); Context.authenticate("admin", "test"); RegistrarBean regService = Context.getService(MotechService.class) .getRegistrarBean(); Date date = new Date(); Integer motechId = 1234665; Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.YEAR, -30); Date birthdate = calendar.getTime(); Facility facility = regService.getFacilityById(11117); regService.registerPatient(RegistrationMode.USE_PREPRINTED_ID, motechId, RegistrantType.OTHER, "firstName", "middleName", "lastName", "prefName", birthdate, false, Gender.MALE, true, "nhis", null, null, null, facility, "Address", "1111111111", null, null, true, true, ContactNumberType.PERSONAL, MediaType.VOICE, "language", DayOfWeek.MONDAY, date, InterestReason.KNOW_MORE_PREGNANCY_CHILDBIRTH, HowLearned.FRIEND, 5); List<Patient> matchingPatients = regService.getPatients( "firstName", "lastName", "prefName", birthdate, facility.getFacilityId(), "1111111111", "nhis", null, motechId.toString()); assertEquals(1, matchingPatients.size()); Patient patient = matchingPatients.get(0); patientId = patient.getPatientId(); List<MessageProgramEnrollment> enrollments = Context.getService( MotechService.class).getActiveMessageProgramEnrollments( patientId, null, null, null, null); assertEquals(2, enrollments.size()); assertEquals("Weekly Info Pregnancy Message Program", enrollments .get(0).getProgram()); assertNotNull("Obs is not set on enrollment", enrollments.get(0) .getObsId()); Context.getService(MotechService.class).saveMessageDefinition( new MessageDefinition("pregnancy.week.5", 18L, MessageType.INFORMATIONAL)); Context.getService(MotechService.class).saveMessageDefinition( new MessageDefinition("pregnancy.week.6", 19L, MessageType.INFORMATIONAL)); Context.getService(MotechService.class).saveMessageDefinition( new MessageDefinition("pregnancy.week.7", 20L, MessageType.INFORMATIONAL)); Context.getService(MotechService.class).saveMessageDefinition( new MessageDefinition("pregnancy.week.8", 21L, MessageType.INFORMATIONAL)); } finally { Context.closeSession(); } task.execute(); try { Context.openSession(); Context.authenticate("admin", "test"); List<ScheduledMessage> scheduledMessages = Context.getService( MotechService.class).getAllScheduledMessages(); assertEquals(1, scheduledMessages.size()); ScheduledMessage scheduledMessage = scheduledMessages.get(0); assertEquals("pregnancy.week.5", scheduledMessage.getMessage() .getMessageKey()); Concept refDate = Context.getConceptService().getConcept( MotechConstants.CONCEPT_ENROLLMENT_REFERENCE_DATE); Patient patient = Context.getPatientService().getPatient(patientId); assertNotNull("Patient does not exist with id", patient); List<Obs> matchingObs = Context.getObsService() .getObservationsByPersonAndConcept(patient, refDate); assertEquals(1, matchingObs.size()); Obs refDateObs = matchingObs.get(0); Integer originalObsId = refDateObs.getObsId(); Date date = refDateObs.getValueDatetime(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int slush = 4; calendar.add(Calendar.DATE, -(2 * 7 + slush)); refDateObs.setValueDatetime(calendar.getTime()); refDateObs = Context.getObsService().saveObs(refDateObs, "New date in future"); assertTrue("New reference date obs id is same as previous", !originalObsId.equals(refDateObs.getObsId())); List<MessageProgramEnrollment> enrollments = Context.getService( MotechService.class).getActiveMessageProgramEnrollments( patient.getPatientId(), null, null, null, null); assertEquals(2, enrollments.size()); MessageProgramEnrollment infoEnrollment = enrollments.get(0); infoEnrollment.setObsId(refDateObs.getObsId()); Context.getService(MotechService.class) .saveMessageProgramEnrollment(infoEnrollment); } finally { Context.closeSession(); } task.execute(); try { Context.openSession(); List<ScheduledMessage> scheduledMessages = Context.getService( MotechService.class).getAllScheduledMessages(); assertEquals(2, scheduledMessages.size()); for (ScheduledMessage scheduledMessage : scheduledMessages) { assertEquals(1, scheduledMessage.getMessageAttempts().size()); Message message = scheduledMessage.getMessageAttempts().get(0); if (scheduledMessage.getMessage().getMessageKey().equals( "pregnancy.week.5")) { assertEquals(MessageStatus.CANCELLED, message .getAttemptStatus()); } else if (scheduledMessage.getMessage().getMessageKey() .equals("pregnancy.week.7") || scheduledMessage.getMessage().getMessageKey() .equals("pregnancy.week.8")) { assertEquals(MessageStatus.SHOULD_ATTEMPT, message .getAttemptStatus()); } else { fail("Scheduled message has wrong message key"); } } } finally { Context.closeSession(); } }
@Override public void execute() { long start = System.currentTimeMillis(); log.info("Message Program Task - Update Enrolled Programs for all Patients"); Integer batchSize = null; String batchSizeProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_SIZE); if (batchSizeProperty != null) { try { batchSize = Integer.valueOf(batchSizeProperty); } catch (NumberFormatException e) { log.error("Invalid Integer batch size value", e); } } Long batchPreviousId = null; String batchPreviousProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_PREVIOUS_ID); if (batchPreviousProperty != null) { try { batchPreviousId = Long.valueOf(batchPreviousProperty); } catch (NumberFormatException e) { log.error("Invalid Long batch previous id value", e); } } try { contextService.openSession(); TaskDefinition updatedTask = contextService.getRegistrarBean() .updateAllMessageProgramsState(batchSize, batchPreviousId); if (updatedTask != null) { this.initialize(updatedTask); } } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); }
MessageProgramUpdateTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("Message Program Task - Update Enrolled Programs for all Patients"); Integer batchSize = null; String batchSizeProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_SIZE); if (batchSizeProperty != null) { try { batchSize = Integer.valueOf(batchSizeProperty); } catch (NumberFormatException e) { log.error("Invalid Integer batch size value", e); } } Long batchPreviousId = null; String batchPreviousProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_PREVIOUS_ID); if (batchPreviousProperty != null) { try { batchPreviousId = Long.valueOf(batchPreviousProperty); } catch (NumberFormatException e) { log.error("Invalid Long batch previous id value", e); } } try { contextService.openSession(); TaskDefinition updatedTask = contextService.getRegistrarBean() .updateAllMessageProgramsState(batchSize, batchPreviousId); if (updatedTask != null) { this.initialize(updatedTask); } } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } }
MessageProgramUpdateTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("Message Program Task - Update Enrolled Programs for all Patients"); Integer batchSize = null; String batchSizeProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_SIZE); if (batchSizeProperty != null) { try { batchSize = Integer.valueOf(batchSizeProperty); } catch (NumberFormatException e) { log.error("Invalid Integer batch size value", e); } } Long batchPreviousId = null; String batchPreviousProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_PREVIOUS_ID); if (batchPreviousProperty != null) { try { batchPreviousId = Long.valueOf(batchPreviousProperty); } catch (NumberFormatException e) { log.error("Invalid Long batch previous id value", e); } } try { contextService.openSession(); TaskDefinition updatedTask = contextService.getRegistrarBean() .updateAllMessageProgramsState(batchSize, batchPreviousId); if (updatedTask != null) { this.initialize(updatedTask); } } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } MessageProgramUpdateTask(); }
MessageProgramUpdateTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("Message Program Task - Update Enrolled Programs for all Patients"); Integer batchSize = null; String batchSizeProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_SIZE); if (batchSizeProperty != null) { try { batchSize = Integer.valueOf(batchSizeProperty); } catch (NumberFormatException e) { log.error("Invalid Integer batch size value", e); } } Long batchPreviousId = null; String batchPreviousProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_PREVIOUS_ID); if (batchPreviousProperty != null) { try { batchPreviousId = Long.valueOf(batchPreviousProperty); } catch (NumberFormatException e) { log.error("Invalid Long batch previous id value", e); } } try { contextService.openSession(); TaskDefinition updatedTask = contextService.getRegistrarBean() .updateAllMessageProgramsState(batchSize, batchPreviousId); if (updatedTask != null) { this.initialize(updatedTask); } } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } MessageProgramUpdateTask(); void setContextService(ContextService contextService); @Override void execute(); }
MessageProgramUpdateTask extends AbstractTask { @Override public void execute() { long start = System.currentTimeMillis(); log.info("Message Program Task - Update Enrolled Programs for all Patients"); Integer batchSize = null; String batchSizeProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_SIZE); if (batchSizeProperty != null) { try { batchSize = Integer.valueOf(batchSizeProperty); } catch (NumberFormatException e) { log.error("Invalid Integer batch size value", e); } } Long batchPreviousId = null; String batchPreviousProperty = taskDefinition.getProperty(MotechConstants.TASK_PROPERTY_BATCH_PREVIOUS_ID); if (batchPreviousProperty != null) { try { batchPreviousId = Long.valueOf(batchPreviousProperty); } catch (NumberFormatException e) { log.error("Invalid Long batch previous id value", e); } } try { contextService.openSession(); TaskDefinition updatedTask = contextService.getRegistrarBean() .updateAllMessageProgramsState(batchSize, batchPreviousId); if (updatedTask != null) { this.initialize(updatedTask); } } finally { contextService.closeSession(); } long end = System.currentTimeMillis(); long runtime = (end - start) / 1000; log.info("executed for " + runtime + " seconds"); } MessageProgramUpdateTask(); void setContextService(ContextService contextService); @Override void execute();  }