Skip to content

Commit

Permalink
Merge pull request #28 from OpenSRP/issue27
Browse files Browse the repository at this point in the history
Added created at field for vaccine and recurring services
  • Loading branch information
Ephraim (Keyman) Muhia committed Mar 21, 2018
2 parents a7859ae + e164e84 commit 953c5af
Show file tree
Hide file tree
Showing 32 changed files with 251 additions and 111 deletions.
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.2.1
VERSION_NAME=1.2.3-SNAPSHOT
VERSION_CODE=2
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Immunization
Expand Down
2 changes: 1 addition & 1 deletion opensrp-immunization/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ android {

dependencies {
compile 'junit:junit:4.12'
compile('org.smartregister:opensrp-client-core:1.2.0@aar') {
compile('org.smartregister:opensrp-client-core:1.2.5-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.github.bmelnychuk', module: 'atv'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public void updateWrapper(VaccineWrapper tag) {
tag.setName(array[1]);
}
}
tag.setCreatedAt(vaccine.getCreatedAt());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void updateWrapper(ServiceWrapper tag) {
}
tag.setDbKey(serviceRecord.getId());
tag.setSynced(serviceRecord.getSyncStatus() != null && serviceRecord.getSyncStatus().equals(VaccineRepository.TYPE_Synced));
tag.setCreatedAt(serviceRecord.getCreatedAt());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ServiceRecord {
private String eventId;
private String formSubmissionId;
private Long updatedAt;
private Date createdAt;

protected String type;
protected String name;
Expand All @@ -45,7 +46,7 @@ public ServiceRecord(Long id, String baseEntityId, Long recurringServiceId, Stri

public ServiceRecord(Long id, String baseEntityId, String programClientId, Long
recurringServiceId, String value, Date date, String anmId, String locationId, String
syncStatus, String eventId, String formSubmissionId, Long updatedAt) {
syncStatus, String eventId, String formSubmissionId, Long updatedAt, Date createdAt) {
this.id = id;
this.baseEntityId = baseEntityId;
this.programClientId = programClientId;
Expand All @@ -58,6 +59,7 @@ public ServiceRecord(Long id, String baseEntityId, String programClientId, Long
this.eventId = eventId;
this.formSubmissionId = formSubmissionId;
this.updatedAt = updatedAt;
this.createdAt = createdAt;
}

public Long getId() {
Expand Down Expand Up @@ -177,4 +179,12 @@ public HashMap<String, String> getIdentifiers() {
identifiers.put(ZEIR_ID, programClientId);
return identifiers;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getCreatedAt() {
return createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.smartregister.domain.Photo;

import java.io.Serializable;
import java.util.Date;

/**
* Created by keyman on 16/11/2016.
Expand Down Expand Up @@ -34,6 +35,8 @@ public class ServiceWrapper implements Serializable {
private ServiceType serviceType;
private String value;

private Date createdAt;

public String getId() {
return id;
}
Expand Down Expand Up @@ -211,4 +214,12 @@ public String getValue() {
public void setValue(String value) {
this.value = value;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getCreatedAt() {
return createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Vaccine {
private String eventId;
private String formSubmissionId;
private Integer outOfCatchment;
private Date createdAt;

public Vaccine() {
}
Expand Down Expand Up @@ -49,7 +50,7 @@ public Vaccine(Long id, String baseEntityId, String name, Integer calculation, D
public Vaccine(Long id, String baseEntityId, String programClientId, String name, Integer
calculation, Date date, String anmId, String locationId, String syncStatus, String
hia2Status, Long updatedAt, String eventId, String formSubmissionId, Integer
outOfCatchment) {
outOfCatchment, Date createdAt) {
this.id = id;
this.baseEntityId = baseEntityId;
this.programClientId = programClientId;
Expand All @@ -64,6 +65,7 @@ public Vaccine(Long id, String baseEntityId, String programClientId, String name
this.eventId = eventId;
this.formSubmissionId = formSubmissionId;
this.outOfCatchment = outOfCatchment;
this.createdAt = createdAt;
}

public Long getId() {
Expand Down Expand Up @@ -186,4 +188,12 @@ public Integer getOutOfCatchment() {
public void setOutOfCatchment(Integer outOfCatchment) {
this.outOfCatchment = outOfCatchment;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getCreatedAt() {
return createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.smartregister.immunization.db.VaccineRepo;

import java.io.Serializable;
import java.util.Date;

/**
* Created by keyman on 16/11/2016.
Expand Down Expand Up @@ -36,6 +37,8 @@ public class VaccineWrapper implements Serializable {
private boolean today;
private boolean synced;

private Date createdAt;

public String getId() {
return id;
}
Expand Down Expand Up @@ -200,4 +203,12 @@ public boolean isSynced() {
public void setSynced(boolean synced) {
this.synced = synced;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getCreatedAt() {
return createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.apache.commons.lang3.StringUtils;
import org.smartregister.immunization.domain.ServiceRecord;
import org.smartregister.repository.BaseRepository;
import org.smartregister.repository.EventClientRepository;
import org.smartregister.repository.Repository;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -32,16 +34,17 @@ public class RecurringServiceRecordRepository extends BaseRepository {
public static final String LOCATIONID = "location_id";
public static final String SYNC_STATUS = "sync_status";
public static final String UPDATED_AT_COLUMN = "updated_at";
public static final String[] TABLE_COLUMNS = {ID_COLUMN, BASE_ENTITY_ID, PROGRAM_CLIENT_ID, RECURRING_SERVICE_ID, VALUE, DATE, ANMID, LOCATIONID, SYNC_STATUS, EVENT_ID, FORMSUBMISSION_ID, UPDATED_AT_COLUMN};
public static final String CREATED_AT = "created_at";

public static final String[] TABLE_COLUMNS = {ID_COLUMN, BASE_ENTITY_ID, PROGRAM_CLIENT_ID, RECURRING_SERVICE_ID, VALUE, DATE, ANMID, LOCATIONID, SYNC_STATUS, EVENT_ID, FORMSUBMISSION_ID, UPDATED_AT_COLUMN, CREATED_AT};

private static final String BASE_ENTITY_ID_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + BASE_ENTITY_ID + "_index ON " + TABLE_NAME + "(" + BASE_ENTITY_ID + " COLLATE NOCASE);";
public static final String RECURRING_SERVICE_ID_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + RECURRING_SERVICE_ID + "_index ON " + TABLE_NAME + "(" + RECURRING_SERVICE_ID + ");";
public static final String EVENT_ID_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + EVENT_ID + "_index ON " + TABLE_NAME + "(" + EVENT_ID + " COLLATE NOCASE);";
public static final String FORMSUBMISSION_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + FORMSUBMISSION_ID + "_index ON " + TABLE_NAME + "(" + FORMSUBMISSION_ID + " COLLATE NOCASE);";
private static final String UPDATED_AT_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + UPDATED_AT_COLUMN + "_index ON " + TABLE_NAME + "(" + UPDATED_AT_COLUMN + ");";

public static String TYPE_Unsynced = "Unsynced";
public static String TYPE_Synced = "Synced";
public static final String ALTER_ADD_CREATED_AT_COLUMN = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + CREATED_AT + " DATETIME NULL ";

public RecurringServiceRecordRepository(Repository repository) {
super(repository);
Expand Down Expand Up @@ -81,6 +84,9 @@ public void add(ServiceRecord serviceRecord) {
serviceRecord.setId(sameServiceRecord.getId());
update(database, serviceRecord);
} else {
if (serviceRecord.getCreatedAt() == null) {
serviceRecord.setCreatedAt(new Date());
}
serviceRecord.setId(database.insert(TABLE_NAME, null, createValuesFor(serviceRecord)));
}
} else {
Expand Down Expand Up @@ -225,7 +231,15 @@ private List<ServiceRecord> readAllServiceRecords(Cursor cursor) {
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {


Date createdAt = null;
String dateCreatedString = cursor.getString(cursor.getColumnIndex(CREATED_AT));
if (StringUtils.isNotBlank(dateCreatedString)) {
try {
createdAt = EventClientRepository.dateFormat.parse(dateCreatedString);
} catch (ParseException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
ServiceRecord serviceRecord = new ServiceRecord(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)),
cursor.getString(cursor.getColumnIndex(BASE_ENTITY_ID)),
cursor.getString(cursor.getColumnIndex(PROGRAM_CLIENT_ID)),
Expand All @@ -237,7 +251,8 @@ private List<ServiceRecord> readAllServiceRecords(Cursor cursor) {
cursor.getString(cursor.getColumnIndex(SYNC_STATUS)),
cursor.getString(cursor.getColumnIndex(EVENT_ID)),
cursor.getString(cursor.getColumnIndex(FORMSUBMISSION_ID)),
cursor.getLong(cursor.getColumnIndex(UPDATED_AT_COLUMN)));
cursor.getLong(cursor.getColumnIndex(UPDATED_AT_COLUMN)),
createdAt);


if (cursor.getColumnIndex(RecurringServiceTypeRepository.TYPE) > -1) {
Expand Down Expand Up @@ -286,6 +301,7 @@ private ContentValues createValuesFor(ServiceRecord serviceRecord) {
values.put(EVENT_ID, serviceRecord.getEventId() != null ? serviceRecord.getEventId() : null);
values.put(FORMSUBMISSION_ID, serviceRecord.getFormSubmissionId() != null ? serviceRecord.getFormSubmissionId() : null);
values.put(UPDATED_AT_COLUMN, serviceRecord.getUpdatedAt() != null ? serviceRecord.getUpdatedAt() : null);
values.put(CREATED_AT, serviceRecord.getCreatedAt() != null ? EventClientRepository.dateFormat.format(serviceRecord.getCreatedAt()) : null);
return values;
}

Expand All @@ -295,4 +311,20 @@ public static String removeHyphen(String s) {
}
return s;
}

public static void migrateCreatedAt(SQLiteDatabase database) {
try {
String sql = "UPDATE " + TABLE_NAME +
" SET " + CREATED_AT + " = " +
" ( SELECT " + EventClientRepository.event_column.dateCreated.name() +
" FROM " + EventClientRepository.Table.event.name() +
" WHERE " + EventClientRepository.event_column.eventId.name() + " = " + TABLE_NAME + "." + EVENT_ID +
" OR " + EventClientRepository.event_column.formSubmissionId.name() + " = " + TABLE_NAME + "." + FORMSUBMISSION_ID +
" ) " +
" WHERE " + CREATED_AT + " is null ";
database.execSQL(sql);
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.domain.Vaccine;
import org.smartregister.repository.BaseRepository;
import org.smartregister.repository.EventClientRepository;
import org.smartregister.repository.Repository;
import org.smartregister.service.AlertService;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -39,8 +41,9 @@ public class VaccineRepository extends BaseRepository {
public static final String HIA2_STATUS = "hia2_status";
public static final String UPDATED_AT_COLUMN = "updated_at";
public static final String OUT_OF_AREA = "out_of_area";
public static final String CREATED_AT = "created_at";

public static final String[] VACCINE_TABLE_COLUMNS = {ID_COLUMN, BASE_ENTITY_ID, PROGRAM_CLIENT_ID, NAME, CALCULATION, DATE, ANMID, LOCATIONID, SYNC_STATUS, HIA2_STATUS, UPDATED_AT_COLUMN, EVENT_ID, FORMSUBMISSION_ID, OUT_OF_AREA};
public static final String[] VACCINE_TABLE_COLUMNS = {ID_COLUMN, BASE_ENTITY_ID, PROGRAM_CLIENT_ID, NAME, CALCULATION, DATE, ANMID, LOCATIONID, SYNC_STATUS, HIA2_STATUS, UPDATED_AT_COLUMN, EVENT_ID, FORMSUBMISSION_ID, OUT_OF_AREA, CREATED_AT};

private static final String BASE_ENTITY_ID_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + BASE_ENTITY_ID + "_index ON " + VACCINE_TABLE_NAME + "(" + BASE_ENTITY_ID + " COLLATE NOCASE);";
private static final String UPDATED_AT_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + UPDATED_AT_COLUMN + "_index ON " + VACCINE_TABLE_NAME + "(" + UPDATED_AT_COLUMN + ");";
Expand All @@ -54,6 +57,7 @@ public class VaccineRepository extends BaseRepository {
public static final String UPDATE_TABLE_ADD_OUT_OF_AREA_COL_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + OUT_OF_AREA + "_index ON " + VACCINE_TABLE_NAME + "(" + OUT_OF_AREA + " COLLATE NOCASE);";

public static final String UPDATE_TABLE_ADD_HIA2_STATUS_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + HIA2_STATUS + " VARCHAR;";
public static final String ALTER_ADD_CREATED_AT_COLUMN = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + CREATED_AT + " DATETIME NULL ";

public static String HIA2_Within = "Within";
public static String HIA2_Overdue = "Overdue";
Expand Down Expand Up @@ -101,6 +105,9 @@ public void add(Vaccine vaccine) {
vaccine.setId(sameVaccine.getId());
update(database, vaccine);
} else {
if(vaccine.getCreatedAt() == null){
vaccine.setCreatedAt(new Date());
}
vaccine.setId(database.insert(VACCINE_TABLE_NAME, null, createValuesFor(vaccine)));
}
} else {
Expand Down Expand Up @@ -271,6 +278,17 @@ private List<Vaccine> readAllVaccines(Cursor cursor) {
if (vaccineName != null) {
vaccineName = removeHyphen(vaccineName);
}

Date createdAt = null;
String dateCreatedString = cursor.getString(cursor.getColumnIndex(CREATED_AT));
if (StringUtils.isNotBlank(dateCreatedString)) {
try {
createdAt = EventClientRepository.dateFormat.parse(dateCreatedString);
} catch (ParseException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}

vaccines.add(
new Vaccine(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)),
cursor.getString(cursor.getColumnIndex(BASE_ENTITY_ID)),
Expand All @@ -282,7 +300,11 @@ private List<Vaccine> readAllVaccines(Cursor cursor) {
cursor.getString(cursor.getColumnIndex(LOCATIONID)),
cursor.getString(cursor.getColumnIndex(SYNC_STATUS)),
cursor.getString(cursor.getColumnIndex(HIA2_STATUS)),
cursor.getLong(cursor.getColumnIndex(UPDATED_AT_COLUMN)), cursor.getString(cursor.getColumnIndex(EVENT_ID)), cursor.getString(cursor.getColumnIndex(FORMSUBMISSION_ID)), cursor.getInt(cursor.getColumnIndex(OUT_OF_AREA))
cursor.getLong(cursor.getColumnIndex(UPDATED_AT_COLUMN)),
cursor.getString(cursor.getColumnIndex(EVENT_ID)),
cursor.getString(cursor.getColumnIndex(FORMSUBMISSION_ID)),
cursor.getInt(cursor.getColumnIndex(OUT_OF_AREA)),
createdAt
));

cursor.moveToNext();
Expand Down Expand Up @@ -313,6 +335,7 @@ private ContentValues createValuesFor(Vaccine vaccine) {
values.put(EVENT_ID, vaccine.getEventId() != null ? vaccine.getEventId() : null);
values.put(FORMSUBMISSION_ID, vaccine.getFormSubmissionId() != null ? vaccine.getFormSubmissionId() : null);
values.put(OUT_OF_AREA, vaccine.getOutOfCatchment() != null ? vaccine.getOutOfCatchment() : null);
values.put(CREATED_AT, vaccine.getCreatedAt() != null ? EventClientRepository.dateFormat.format(vaccine.getCreatedAt()) : null);
return values;
}

Expand Down Expand Up @@ -381,4 +404,20 @@ public static String removeHyphen(String s) {
}
return s;
}

public static void migrateCreatedAt(SQLiteDatabase database) {
try {
String sql = "UPDATE " + VACCINE_TABLE_NAME +
" SET " + CREATED_AT + " = " +
" ( SELECT " + EventClientRepository.event_column.dateCreated.name() +
" FROM " + EventClientRepository.Table.event.name() +
" WHERE " + EventClientRepository.event_column.eventId.name() + " = " + VACCINE_TABLE_NAME + "." + EVENT_ID +
" OR " + EventClientRepository.event_column.formSubmissionId.name() + " = " + VACCINE_TABLE_NAME + "." + FORMSUBMISSION_ID +
" ) " +
" WHERE " + CREATED_AT + " is null ";
database.execSQL(sql);
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.R;
import org.smartregister.immunization.db.VaccineRepo;
import org.smartregister.immunization.domain.ServiceRecord;
import org.smartregister.immunization.domain.ServiceType;
import org.smartregister.immunization.domain.VaccinateFormSubmissionWrapper;
import org.smartregister.immunization.domain.Vaccine;
Expand Down Expand Up @@ -568,4 +569,21 @@ public static void addBcg2SpecialVaccine(Context context, VaccineGroup vaccineGr
}
}

public static boolean moreThanThreeMonths(Date createdAt) {
return createdAt != null && org.smartregister.util.DateUtil.checkIfDateThreeMonthsOlder(createdAt);
}

public static boolean lessThanThreeMonths(Vaccine vaccine) {
////////////////////////check 3 months///////////////////////////////
return vaccine == null || vaccine.getCreatedAt() == null || !org.smartregister.util.DateUtil.checkIfDateThreeMonthsOlder(vaccine.getCreatedAt());
///////////////////////////////////////////////////////////////////////
}


public static boolean lessThanThreeMonths(ServiceRecord serviceRecord) {
////////////////////////check 3 months///////////////////////////////
return serviceRecord == null || serviceRecord.getCreatedAt() == null || !org.smartregister.util.DateUtil.checkIfDateThreeMonthsOlder(serviceRecord.getCreatedAt());
///////////////////////////////////////////////////////////////////////
}

}

0 comments on commit 953c5af

Please sign in to comment.