Skip to content

Commit

Permalink
- Add support for relaxing vaccination schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Jul 23, 2019
1 parent 8a8bb5b commit 3fa8351
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 24 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,10 @@ Example: `6 Weeks` group name has an id `Six_Wks` thus the key in _strings.xml_
English <string name="six_wks">6 weeks</string>
French <string name="six_wks">6 semaines</string>
```
## Vaccine Relaxation
You can relax your vaccine schedules and specifies how many days prior to the actual due date of the vaccine one can allow its administration
This can be done via the setting below in your implementation's _app.properties_ file

```
vaccine.relaxation.days=2
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.4.17-SNAPSHOT
VERSION_NAME=1.4.18-TUE-SNAPSHOT
VERSION_CODE=2
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Immunization
Expand Down
4 changes: 2 additions & 2 deletions opensrp-immunization/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ tasks.withType(Test) {
}

dependencies {
implementation('org.smartregister:opensrp-client-core:1.6.43-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-core:1.7.1-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.github.bmelnychuk', module: 'atv'
}

implementation('org.smartregister:opensrp-client-native-form:1.6.21-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-native-form:1.6.25-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.android.support', module: 'recyclerview-v7'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.smartregister.immunization.domain;

import android.util.Log;

import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.db.VaccineRepo;
import org.smartregister.immunization.domain.jsonmapping.Due;
import org.smartregister.immunization.domain.jsonmapping.Expiry;
import org.smartregister.immunization.util.IMConstants;
import org.smartregister.immunization.util.Utils;

import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -45,20 +42,9 @@ public static VaccineTrigger init(String vaccineCategory, Due data) {
} else if (data.reference.equalsIgnoreCase(Reference.PREREQUISITE.name())) {
VaccineRepo.Vaccine prerequisite = VaccineRepo.getVaccine(data.prerequisite, vaccineCategory);
if (prerequisite != null) {
try {
//Vaccine Relaxation Logic
String relaxationsDays = ImmunizationLibrary.getInstance().getProperties().getProperty(IMConstants.APP_PROPERTIES.VACCINE_RELAXATION_DAYS);
if (relaxationsDays != null && data.offset.charAt(data.offset.length() - 1) == 'd') {

String prefix = data.offset.substring(0, 1);
String suffix = data.offset.substring(data.offset.length() - 1);
String offset = data.offset.substring(1, data.offset.length() - 1);
data.offset = prefix + (Integer.valueOf(offset) - Integer.valueOf(relaxationsDays)) + suffix;
}
} catch (Exception e) {
Log.e(VaccineTrigger.class.getCanonicalName(), e.getMessage());
}

//Vaccine Relaxation Logic
data.offset = Utils.updateRelaxationDays(data.offset);
return new VaccineTrigger(data.offset, data.window, prerequisite);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import android.app.Activity;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.util.Log;

import org.apache.commons.lang3.StringUtils;
import org.smartregister.immunization.ImmunizationLibrary;

/**
* Created by vkaruri on 29/03/2018.
Expand All @@ -16,4 +21,45 @@ public static double calculateDialogWidthFactor(Activity activity) {
}
return widthFactor;
}

/**
* Returns an offset with the relaxation value appended. Offsets can look like: "+5y,3m,2d" : Plus 5 years, 3 months, and 2 days "-2d" : Minus 2 days
*
* @param offset original offset value
* @return the offset with the relaxation appended e.g. +5y with a relaxation of 3days gives +5y,-3d
*/
public static String updateRelaxationDays(String offset) {
String newOffset = offset;
try {
//Vaccine Relaxation Logic
String relaxationsDays = ImmunizationLibrary.getInstance().getProperties().getProperty(IMConstants.APP_PROPERTIES.VACCINE_RELAXATION_DAYS);
if (relaxationsDays != null) {

String[] tokens = offset.split(",");
boolean foundDay = false;
for (int i = 0; i < tokens.length; i++) {

char lastCharacter = tokens[i].trim().charAt(tokens[i].length() - 1);
if (lastCharacter == 'd') {
String suffix = tokens[i].substring(tokens[i].length() - 1);
String mid = tokens[i].substring(0, tokens[i].length() - 1);
tokens[i] = (Integer.valueOf(mid) - Integer.valueOf(relaxationsDays)) + suffix;
foundDay = true;
}
}

char lastCharacter = offset.trim().charAt(offset.length() - 1);
if (foundDay) {
newOffset = StringUtils.join(tokens,",");
} else if (lastCharacter == 'm' || lastCharacter == 'y') {
newOffset = offset + ",-" + relaxationsDays + "d";
}

newOffset = (newOffset.charAt(0) != '-' && newOffset.charAt(0) != '+') ? '+' + newOffset : newOffset;
}
} catch (Exception e) {
Log.e(Utils.class.getCanonicalName(), e.getMessage());
}
return newOffset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.joda.time.DateTime;
import org.smartregister.domain.Alert;
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.R;
import org.smartregister.immunization.domain.State;
import org.smartregister.immunization.domain.VaccineWrapper;
Expand Down Expand Up @@ -68,7 +69,7 @@ public VaccineCard(Context context, AttributeSet attrs, int defStyleAttr) {
init(context);
}

@TargetApi (Build.VERSION_CODES.LOLLIPOP)
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public VaccineCard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.smartregister.immunization.utils;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.util.IMConstants;
import org.smartregister.immunization.util.Utils;

import java.util.Properties;

/**
* Created by ndegwamartin on 2019-07-23.
*/
@PrepareForTest({ImmunizationLibrary.class})
public class UtilsTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Mock
private ImmunizationLibrary immunizationLibrary;
@Mock
private Properties properties;

private static String OFFSET_days = "+2d";
private static String OFFSET_days_negative = "-2d";
private static String OFFSET_month_days = "+4m,3d";
private static String OFFSET_months = "+6m";
private static String OFFSET_years = "-5y";
private static String RELAXATION_DAYS = "2";

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testUpdateRelaxationDaysGivesCorrectOffsetForDaysDefault() {
String result = Utils.updateRelaxationDays(OFFSET_days);
Assert.assertEquals(OFFSET_days, result);
}

@Test
public void testUpdateRelaxationDaysGivesCorrectOffsetForDays() {

PowerMockito.mockStatic(ImmunizationLibrary.class);
PowerMockito.when(ImmunizationLibrary.getInstance()).thenReturn(immunizationLibrary);
PowerMockito.when(immunizationLibrary.getProperties()).thenReturn(properties);
PowerMockito.when(properties.getProperty(IMConstants.APP_PROPERTIES.VACCINE_RELAXATION_DAYS)).thenReturn(RELAXATION_DAYS);

String result = Utils.updateRelaxationDays(OFFSET_days);
Assert.assertEquals("+0d", result);
}

@Test
public void testUpdateRelaxationDaysGivesCorrectOffsetForDaysNegative() {

PowerMockito.mockStatic(ImmunizationLibrary.class);
PowerMockito.when(ImmunizationLibrary.getInstance()).thenReturn(immunizationLibrary);
PowerMockito.when(immunizationLibrary.getProperties()).thenReturn(properties);
PowerMockito.when(properties.getProperty(IMConstants.APP_PROPERTIES.VACCINE_RELAXATION_DAYS)).thenReturn(RELAXATION_DAYS);

String result = Utils.updateRelaxationDays(OFFSET_days_negative);
Assert.assertEquals("-4d", result);
}

@Test
public void testUpdateRelaxationDaysGivesCorrectOffsetForMonths() {

PowerMockito.mockStatic(ImmunizationLibrary.class);
PowerMockito.when(ImmunizationLibrary.getInstance()).thenReturn(immunizationLibrary);
PowerMockito.when(immunizationLibrary.getProperties()).thenReturn(properties);
PowerMockito.when(properties.getProperty(IMConstants.APP_PROPERTIES.VACCINE_RELAXATION_DAYS)).thenReturn(RELAXATION_DAYS);

String result = Utils.updateRelaxationDays(OFFSET_months);
Assert.assertEquals("+6m,-2d", result);
}

@Test
public void testUpdateRelaxationDaysGivesCorrectOffsetForYears() {

PowerMockito.mockStatic(ImmunizationLibrary.class);
PowerMockito.when(ImmunizationLibrary.getInstance()).thenReturn(immunizationLibrary);
PowerMockito.when(immunizationLibrary.getProperties()).thenReturn(properties);
PowerMockito.when(properties.getProperty(IMConstants.APP_PROPERTIES.VACCINE_RELAXATION_DAYS)).thenReturn(RELAXATION_DAYS);

String result = Utils.updateRelaxationDays(OFFSET_years);
Assert.assertEquals("-5y,-2d", result);
}

@Test
public void testUpdateRelaxationDaysGivesCorrectOffsetForMonthsDays() {

PowerMockito.mockStatic(ImmunizationLibrary.class);
PowerMockito.when(ImmunizationLibrary.getInstance()).thenReturn(immunizationLibrary);
PowerMockito.when(immunizationLibrary.getProperties()).thenReturn(properties);
PowerMockito.when(properties.getProperty(IMConstants.APP_PROPERTIES.VACCINE_RELAXATION_DAYS)).thenReturn(RELAXATION_DAYS);

String result = Utils.updateRelaxationDays(OFFSET_month_days);
Assert.assertEquals("+4m,1d", result);
}
}
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ android {

dependencies {

implementation('org.smartregister:opensrp-client-core:1.6.43-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-core:1.7.1-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.github.bmelnychuk', module: 'atv'
}

implementation('org.smartregister:opensrp-client-native-form:1.6.21-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-native-form:1.6.25-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.android.support', module: 'recyclerview-v7'
}
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/assets/app.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ DRISHTI_BASE_URL=http://202.141.249.106:6806/opensrp
PORT=-1
SHOULD_VERIFY_CERTIFICATE=false
SYNC_DOWNLOAD_BATCH_SIZE=100
vaccine.relaxation.days=2
vaccine.relaxation.days=0

0 comments on commit 3fa8351

Please sign in to comment.