Skip to content

Commit

Permalink
Merge pull request #691 from OpenSRP/task-data-model-changes
Browse files Browse the repository at this point in the history
Task data model changes
  • Loading branch information
githengi authored Nov 20, 2020
2 parents 11a2a05 + 66e6f5a commit 3a1554f
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 89 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=3.2.1-SNAPSHOT
VERSION_NAME=3.3.0-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
2 changes: 1 addition & 1 deletion opensrp-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ dependencies {

implementation 'org.smartregister:opensrp-client-utils:0.0.2-SNAPSHOT'

implementation 'org.smartregister:opensrp-plan-evaluator:0.2.0-SNAPSHOT'
implementation 'org.smartregister:opensrp-plan-evaluator:1.0.0-SNAPSHOT'

implementation 'xerces:xercesImpl:2.12.0'

Expand Down
4 changes: 4 additions & 0 deletions opensrp-app/src/main/java/org/smartregister/AllConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,8 @@ public static class DATA_CAPTURE_STRATEGY {
public static String ADVANCED = "Advanced";
public static String NORMAL = "Normal";
}

public static class DataTypes {
public static final String INTEGER = "INTEGER";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Campaign {

private TaskStatus status;

private ExecutionPeriod executionPeriod;
private Period executionPeriod;

private DateTime authoredOn;

Expand Down Expand Up @@ -57,11 +57,11 @@ public void setStatus(TaskStatus status) {
this.status = status;
}

public ExecutionPeriod getExecutionPeriod() {
public Period getExecutionPeriod() {
return executionPeriod;
}

public void setExecutionPeriod(ExecutionPeriod executionPeriod) {
public void setExecutionPeriod(Period executionPeriod) {
this.executionPeriod = executionPeriod;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import org.apache.commons.lang3.StringUtils;
import org.smartregister.domain.Campaign;
import org.smartregister.domain.ExecutionPeriod;
import org.smartregister.domain.Period;
import org.smartregister.util.DateUtil;

import java.util.ArrayList;
Expand Down Expand Up @@ -124,9 +124,9 @@ private Campaign readCursor(Cursor cursor) {
if (cursor.getString(cursor.getColumnIndex(STATUS)) != null) {
campaign.setStatus(TaskStatus.valueOf(cursor.getString(cursor.getColumnIndex(STATUS))));
}
ExecutionPeriod executionPeriod = new ExecutionPeriod();
executionPeriod.setStart(DateUtil.getDateFromMillis(cursor.getLong(cursor.getColumnIndex(START))));
executionPeriod.setEnd(DateUtil.getDateFromMillis(cursor.getLong(cursor.getColumnIndex(END))));
Period executionPeriod = new Period();
executionPeriod.setStart(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(START))));
executionPeriod.setEnd(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(END))));
campaign.setExecutionPeriod(executionPeriod);

campaign.setAuthoredOn(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(AUTHORED_ON))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class EventClientRepository extends BaseRepository {

private static final String _ID = "_id";

private static final String VARCHAR = "VARCHAR";
public static final String VARCHAR = "VARCHAR";

protected Table clientTable;
protected Table eventTable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.smartregister.domain.Client;
import org.smartregister.domain.Location;
import org.smartregister.domain.Note;
import org.smartregister.domain.Period;
import org.smartregister.domain.Task;
import org.smartregister.domain.Task.TaskStatus;
import org.smartregister.domain.TaskUpdate;
import org.smartregister.p2p.sync.data.JsonData;
import org.smartregister.sync.helper.TaskServiceHelper;
import org.smartregister.util.DatabaseMigrationUtils;
import org.smartregister.util.DateUtil;
import org.smartregister.util.P2PUtil;

Expand All @@ -37,6 +39,7 @@

import timber.log.Timber;

import static org.smartregister.AllConstants.DataTypes.INTEGER;
import static org.smartregister.AllConstants.ROWID;
import static org.smartregister.domain.Task.INACTIVE_TASK_STATUS;

Expand Down Expand Up @@ -69,10 +72,13 @@ public class TaskRepository extends BaseRepository {
private static final String REASON_REFERENCE = "reason_reference";
private static final String LOCATION = "location";
private static final String REQUESTER = "requester";
private static final String RESTRICTION_REPEAT = "restriction_repeat";
private static final String RESTRICTION_START = "restriction_start";
private static final String RESTRICTION_END = "restriction_end";

private TaskNotesRepository taskNotesRepository;
private final TaskNotesRepository taskNotesRepository;

protected static final String[] COLUMNS = {ID, PLAN_ID, GROUP_ID, STATUS, BUSINESS_STATUS, PRIORITY, CODE, DESCRIPTION, FOCUS, FOR, START, END, AUTHORED_ON, LAST_MODIFIED, OWNER, SYNC_STATUS, SERVER_VERSION, STRUCTURE_ID, REASON_REFERENCE, LOCATION, REQUESTER};
protected static final String[] COLUMNS = {ROWID, ID, PLAN_ID, GROUP_ID, STATUS, BUSINESS_STATUS, PRIORITY, CODE, DESCRIPTION, FOCUS, FOR, START, END, AUTHORED_ON, LAST_MODIFIED, OWNER, SYNC_STATUS, SERVER_VERSION, STRUCTURE_ID, REASON_REFERENCE, LOCATION, REQUESTER, RESTRICTION_REPEAT, RESTRICTION_START, RESTRICTION_END};

protected static final String TASK_TABLE = "task";

Expand All @@ -83,7 +89,7 @@ public class TaskRepository extends BaseRepository {
GROUP_ID + " VARCHAR NOT NULL, " +
STATUS + " VARCHAR NOT NULL, " +
BUSINESS_STATUS + " VARCHAR, " +
PRIORITY + " INTEGER, " +
PRIORITY + " VARCHAR, " +
CODE + " VARCHAR , " +
DESCRIPTION + " VARCHAR , " +
FOCUS + " VARCHAR , " +
Expand All @@ -98,7 +104,10 @@ public class TaskRepository extends BaseRepository {
STRUCTURE_ID + " VARCHAR, " +
REASON_REFERENCE + " VARCHAR, " +
LOCATION + " VARCHAR, " +
REQUESTER + " VARCHAR )";
REQUESTER + " VARCHAR, " +
RESTRICTION_REPEAT + " INTEGER, " +
RESTRICTION_START + " INTEGER, " +
RESTRICTION_END + " INTEGER )";

private static final String CREATE_TASK_PLAN_GROUP_INDEX = "CREATE INDEX "
+ TASK_TABLE + "_plan_group_ind ON " + TASK_TABLE + "(" + PLAN_ID + "," + GROUP_ID + "," + SYNC_STATUS + ")";
Expand All @@ -112,6 +121,18 @@ public static void createTable(SQLiteDatabase database) {
database.execSQL(CREATE_TASK_PLAN_GROUP_INDEX);
}

/**
* Migrate the older database by add restriction columns and changing of priority to enum
*
* @param database the database being upgraded
*/
public static void updatePriorityToEnumAndAddRestrictions(@NonNull SQLiteDatabase database) {
DatabaseMigrationUtils.addColumnIfNotExists(database, TASK_TABLE, RESTRICTION_REPEAT, INTEGER);
DatabaseMigrationUtils.addColumnIfNotExists(database, TASK_TABLE, RESTRICTION_START, INTEGER);
DatabaseMigrationUtils.addColumnIfNotExists(database, TASK_TABLE, RESTRICTION_END, INTEGER);
DatabaseMigrationUtils.recreateSyncTableWithExistingColumnsOnly(database, TASK_TABLE, COLUMNS, CREATE_TASK_TABLE);
database.execSQL(String.format("UPDATE %s SET %s=?", TASK_TABLE, PRIORITY), new Object[]{Task.TaskPriority.ROUTINE.name()});
}

public void addOrUpdate(Task task) {
addOrUpdate(task, false);
Expand Down Expand Up @@ -139,13 +160,15 @@ public Task addOrUpdate(Task task, boolean updateOnly) {
contentValues.put(STATUS, task.getStatus().name());
}
contentValues.put(BUSINESS_STATUS, task.getBusinessStatus());
contentValues.put(PRIORITY, task.getPriority());
contentValues.put(PRIORITY, task.getPriority().name());
contentValues.put(CODE, task.getCode());
contentValues.put(DESCRIPTION, task.getDescription());
contentValues.put(FOCUS, task.getFocus());
contentValues.put(FOR, task.getForEntity());
contentValues.put(START, DateUtil.getMillis(task.getExecutionStartDate()));
contentValues.put(END, DateUtil.getMillis(task.getExecutionEndDate()));
if (task.getExecutionPeriod() != null) {
contentValues.put(START, DateUtil.getMillis(task.getExecutionPeriod().getStart()));
contentValues.put(END, DateUtil.getMillis(task.getExecutionPeriod().getEnd()));
}
contentValues.put(AUTHORED_ON, DateUtil.getMillis(task.getAuthoredOn()));
contentValues.put(LAST_MODIFIED, DateUtil.getMillis(task.getLastModified()));
contentValues.put(OWNER, task.getOwner());
Expand All @@ -155,6 +178,13 @@ public Task addOrUpdate(Task task, boolean updateOnly) {
contentValues.put(REASON_REFERENCE, task.getReasonReference());
contentValues.put(LOCATION, task.getLocation());
contentValues.put(REQUESTER, task.getRequester());
if (task.getRestriction() != null) {
contentValues.put(RESTRICTION_REPEAT, task.getRestriction().getRepetitions());
if (task.getRestriction().getPeriod() != null) {
contentValues.put(RESTRICTION_START, DateUtil.getMillis(task.getRestriction().getPeriod().getStart()));
contentValues.put(RESTRICTION_END, DateUtil.getMillis(task.getRestriction().getPeriod().getEnd()));
}
}

if (updateOnly) {
getWritableDatabase().update(TASK_TABLE, contentValues, ID + " =?", new String[]{task.getIdentifier()});
Expand Down Expand Up @@ -274,13 +304,15 @@ public Task readCursor(Cursor cursor) {
task.setStatus(TaskStatus.valueOf(cursor.getString(cursor.getColumnIndex(STATUS))));
}
task.setBusinessStatus(cursor.getString(cursor.getColumnIndex(BUSINESS_STATUS)));
task.setPriority(cursor.getInt(cursor.getColumnIndex(PRIORITY)));
task.setPriority(Task.TaskPriority.valueOf(cursor.getString(cursor.getColumnIndex(PRIORITY))));
task.setCode(cursor.getString(cursor.getColumnIndex(CODE)));
task.setDescription(cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
task.setFocus(cursor.getString(cursor.getColumnIndex(FOCUS)));
task.setForEntity(cursor.getString(cursor.getColumnIndex(FOR)));
task.setExecutionStartDate(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(START))));
task.setExecutionEndDate(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(END))));
Period period = new Period();
period.setStart(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(START))));
period.setEnd(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(END))));
task.setExecutionPeriod(period);
task.setAuthoredOn(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(AUTHORED_ON))));
task.setLastModified(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(LAST_MODIFIED))));
task.setOwner(cursor.getString(cursor.getColumnIndex(OWNER)));
Expand All @@ -290,7 +322,13 @@ public Task readCursor(Cursor cursor) {
task.setReasonReference(cursor.getString(cursor.getColumnIndex(REASON_REFERENCE)));
task.setLocation(cursor.getString(cursor.getColumnIndex(LOCATION)));
task.setRequester(cursor.getString(cursor.getColumnIndex(REQUESTER)));

Period restrictionPeriod = new Period();
restrictionPeriod.setStart(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(RESTRICTION_START))));
restrictionPeriod.setEnd(DateUtil.getDateTimeFromMillis(cursor.getLong(cursor.getColumnIndex(RESTRICTION_END))));
Task.Restriction restriction = new Task.Restriction(cursor.getInt(cursor.getColumnIndex(RESTRICTION_REPEAT)), restrictionPeriod);
if (restriction.getRepetitions() != 0 || restrictionPeriod.getStart() != null && restrictionPeriod.getEnd() != null) {
task.setRestriction(restriction);
}
return task;
}

Expand Down Expand Up @@ -580,7 +618,7 @@ public void cancelTasksForEntity(@NonNull String entityId) {

/**
* Cancels a particular task
*
* <p>
* Cancels the task if it has a status ready @{@link TaskStatus}
*
* @param identifier id of the task to be cancelled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -22,6 +23,7 @@
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.PlanDefinitionRepository;
import org.smartregister.service.HTTPAgent;
import org.smartregister.util.DateTimeTypeConverter;
import org.smartregister.util.DateTypeConverter;
import org.smartregister.util.Utils;

Expand All @@ -38,7 +40,10 @@ public class PlanIntentServiceHelper extends BaseHelper {

private final PlanDefinitionRepository planDefinitionRepository;
private final AllSharedPreferences allSharedPreferences;
protected static final Gson gson = new GsonBuilder().registerTypeAdapter(LocalDate.class, new DateTypeConverter()).create();
protected static Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new DateTimeTypeConverter("yyyy-MM-dd"))
.registerTypeAdapter(LocalDate.class, new DateTypeConverter())
.disableHtmlEscaping()
.create();

protected final Context context;
protected static PlanIntentServiceHelper instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public class TaskServiceHelper extends BaseHelper {
protected final Context context;
private TaskRepository taskRepository;
public static final String TASK_LAST_SYNC_DATE = "TASK_LAST_SYNC_DATE";
public static final String UPDATE_STATUS_URL = "/rest/task/update_status";
public static final String ADD_TASK_URL = "/rest/task/add";
public static final String SYNC_TASK_URL = "/rest/task/sync";
public static final String UPDATE_STATUS_URL = "/rest/v2/task/update_status";
public static final String ADD_TASK_URL = "/rest/v2/task/add";
public static final String SYNC_TASK_URL = "/rest/v2/task/sync";

private static final String TASKS_NOT_PROCESSED = "Tasks with identifiers not processed: ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@ public static void recreateSyncTableWithExistingColumnsOnly(SQLiteDatabase datab
}


public static void recreateSyncTableWithExistingColumnsOnly(SQLiteDatabase database, String table ,String[] columns, String createTableDDL) {
database.beginTransaction();
//rename original table
database.execSQL("ALTER TABLE " + table + " RENAME TO " + TABLE_PREFIX + table);
//recreate table
database.execSQL(createTableDDL);
//
String insertQuery = "INSERT INTO "
+ table
+ " (" + StringUtils.join(columns, ", ") + ")"
+ " SELECT " + StringUtils.join(columns, ", ") + " FROM "
+ TABLE_PREFIX + table;

Log.d(TAG, "Insert query is\n---------------------------\n" + insertQuery);

database.execSQL(insertQuery);

database.execSQL("DROP TABLE " + TABLE_PREFIX + table);

database.setTransactionSuccessful();

database.endTransaction();

}


private static boolean tableExists(SQLiteDatabase database, String tableName) {
Cursor cursor = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name= ?", new String[]{tableName});
boolean exists = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.reflect.Whitebox;
import org.smartregister.BaseUnitTest;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseRobolectricUnitTest;

import java.io.IOException;

/**
* Created by ndegwamartin on 26/05/2020.
*/

public class AccountHelperTest extends BaseUnitTest {
public class AccountHelperTest extends BaseRobolectricUnitTest {
private static final String CORE_ACCOUNT_NAME = "demo";
private static final String CORE_ACCOUNT_TYPE = "org.smartregister.core";
private static final String TEST_KEY = "testKey";
Expand All @@ -41,7 +41,7 @@ public void setUp() throws Exception {
Account[] accounts = {new Account(CORE_ACCOUNT_NAME, CORE_ACCOUNT_TYPE)};
Mockito.doReturn(accounts).when(accountManager).getAccountsByType(CORE_ACCOUNT_TYPE);

Whitebox.setInternalState(AccountHelper.class, "accountManager", accountManager);
ReflectionHelpers.setStaticField(AccountHelper.class, "accountManager", accountManager);
}

@Test
Expand All @@ -55,8 +55,6 @@ public void testGetOauthAccountByType() {
@Test
public void testGetAccountManagerValue() {

Whitebox.setInternalState(AccountHelper.class, "accountManager", accountManager);

Mockito.doReturn(TEST_VALUE).when(accountManager).getUserData(ArgumentMatchers.any(Account.class), ArgumentMatchers.eq(TEST_KEY));

String value = AccountHelper.getAccountManagerValue(TEST_KEY, CORE_ACCOUNT_NAME, CORE_ACCOUNT_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CampaignTest {

private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HHmm");

private String campaignJson = "{\"identifier\":\"IRS_2018_S1\",\"title\":\"2019 IRS Season 1\",\"description\":\"This is the 2010 IRS Spray Campaign for Zambia for the first spray season dated 1 Jan 2019 - 31 Mar 2019.\",\"status\":\"In Progress\",\"executionPeriod\":{\"start\":\"2019-01-01\",\"end\":\"2019-03-31\"},\"authoredOn\":\"2018-10-01T0900\",\"lastModified\":\"2018-10-01T0900\",\"owner\":\"jdoe\",\"serverVersion\":0}";
private String campaignJson = "{\"identifier\":\"IRS_2018_S1\",\"title\":\"2019 IRS Season 1\",\"description\":\"This is the 2010 IRS Spray Campaign for Zambia for the first spray season dated 1 Jan 2019 - 31 Mar 2019.\",\"status\":\"In Progress\",\"executionPeriod\":{\"start\":\"2019-01-01T0000\",\"end\":\"2019-03-31T0000\"},\"authoredOn\":\"2018-10-01T0900\",\"lastModified\":\"2018-10-01T0900\",\"owner\":\"jdoe\",\"serverVersion\":0}";

@Test
public void testDeserialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public class PlanDefinitionTest {

public static Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new DateTimeTypeConverter("yyyy-MM-dd HH:mm:ss.SSSZ"))
public static Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new DateTimeTypeConverter("yyyy-MM-dd"))
.registerTypeAdapter(LocalDate.class, new DateTypeConverter())
.disableHtmlEscaping()
.create();
Expand Down
Loading

0 comments on commit 3a1554f

Please sign in to comment.