From 2aca35bf5bd901e171781cbd5f1201cbef7654b9 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Wed, 27 Mar 2019 18:50:16 +0300 Subject: [PATCH] Modify tables to enable for tracking of last sent data Fixes #6 --- .../1.json | 60 +++++++------------ .../p2p/model/P2pReceivedHistory.java | 41 +++++++------ .../p2p/model/SendingDevice.java | 27 ++++----- .../p2p/model/dao/P2pReceivedHistoryDao.java | 18 +++++- .../p2p/model/dao/SendingDeviceDao.java | 2 +- .../p2p/presenter/P2PReceiverPresenter.java | 8 +-- p2p-sync/src/main/res/values/strings.xml | 1 + 7 files changed, 78 insertions(+), 79 deletions(-) diff --git a/p2p-sync/schemas/org.smartregister.p2p.model.AppDatabase/1.json b/p2p-sync/schemas/org.smartregister.p2p.model.AppDatabase/1.json index 98c758b..3672238 100644 --- a/p2p-sync/schemas/org.smartregister.p2p.model.AppDatabase/1.json +++ b/p2p-sync/schemas/org.smartregister.p2p.model.AppDatabase/1.json @@ -2,75 +2,61 @@ "formatVersion": 1, "database": { "version": 1, - "identityHash": "29c5cef9b2b7110b713f68e04b744cc7", + "identityHash": "8a6115b536954578a0df0ca107873202", "entities": [ { "tableName": "sending_devices", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `device_unique_id` TEXT, `app_lifetime_key` TEXT)", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`device_id` TEXT NOT NULL, `app_lifetime_key` TEXT NOT NULL, PRIMARY KEY(`device_id`))", "fields": [ { - "fieldPath": "id", - "columnName": "id", - "affinity": "INTEGER", - "notNull": true - }, - { - "fieldPath": "deviceUniqueId", - "columnName": "device_unique_id", + "fieldPath": "deviceId", + "columnName": "device_id", "affinity": "TEXT", - "notNull": false + "notNull": true }, { "fieldPath": "appLifetimeKey", "columnName": "app_lifetime_key", "affinity": "TEXT", - "notNull": false + "notNull": true } ], "primaryKey": { "columnNames": [ - "id" + "device_id" ], - "autoGenerate": true + "autoGenerate": false }, - "indices": [ - { - "name": "index_sending_devices_device_unique_id", - "unique": false, - "columnNames": [ - "device_unique_id" - ], - "createSql": "CREATE INDEX `index_sending_devices_device_unique_id` ON `${TABLE_NAME}` (`device_unique_id`)" - } - ], + "indices": [], "foreignKeys": [] }, { "tableName": "p2p_received_history", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `sending_device_id` INTEGER NOT NULL, `entity_name` TEXT, PRIMARY KEY(`id`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`sending_device_id` TEXT NOT NULL, `entity_type` TEXT NOT NULL, `last_record_id` INTEGER NOT NULL, PRIMARY KEY(`entity_type`, `sending_device_id`))", "fields": [ - { - "fieldPath": "id", - "columnName": "id", - "affinity": "INTEGER", - "notNull": true - }, { "fieldPath": "sendingDeviceId", "columnName": "sending_device_id", - "affinity": "INTEGER", + "affinity": "TEXT", "notNull": true }, { - "fieldPath": "entityName", - "columnName": "entity_name", + "fieldPath": "entityType", + "columnName": "entity_type", "affinity": "TEXT", - "notNull": false + "notNull": true + }, + { + "fieldPath": "lastRecordId", + "columnName": "last_record_id", + "affinity": "INTEGER", + "notNull": true } ], "primaryKey": { "columnNames": [ - "id" + "entity_type", + "sending_device_id" ], "autoGenerate": false }, @@ -80,7 +66,7 @@ ], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"29c5cef9b2b7110b713f68e04b744cc7\")" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"8a6115b536954578a0df0ca107873202\")" ] } } \ No newline at end of file diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java b/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java index 4fdc167..fa95587 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/model/P2pReceivedHistory.java @@ -3,44 +3,47 @@ import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; +import android.support.annotation.NonNull; /** * Created by Ephraim Kigamba - ekigamba@ona.io on 26/03/2019 */ -@Entity(tableName = "p2p_received_history") +@Entity(tableName = "p2p_received_history", primaryKeys = {"entity_type", "sending_device_id"}) public class P2pReceivedHistory { - @PrimaryKey - private int id; - + @NonNull @ColumnInfo(name = "sending_device_id") - private int sendingDeviceId; + private String sendingDeviceId; + + @NonNull + @ColumnInfo(name = "entity_type") + private String entityType; - @ColumnInfo(name = "entity_name") - private String entityName; + @ColumnInfo(name = "last_record_id") + private long lastRecordId; - public int getId() { - return id; + public String getSendingDeviceId() { + return sendingDeviceId; } - public void setId(int id) { - this.id = id; + public void setSendingDeviceId(String sendingDeviceId) { + this.sendingDeviceId = sendingDeviceId; } - public int getSendingDeviceId() { - return sendingDeviceId; + public String getEntityType() { + return entityType; } - public void setSendingDeviceId(int sendingDeviceId) { - this.sendingDeviceId = sendingDeviceId; + public void setEntityType(String entityType) { + this.entityType = entityType; } - public String getEntityName() { - return entityName; + public long getLastRecordId() { + return lastRecordId; } - public void setEntityName(String entityName) { - this.entityName = entityName; + public void setLastRecordId(long lastRecordId) { + this.lastRecordId = lastRecordId; } } diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/model/SendingDevice.java b/p2p-sync/src/main/java/org/smartregister/p2p/model/SendingDevice.java index 766308b..acf3b04 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/model/SendingDevice.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/model/SendingDevice.java @@ -3,6 +3,7 @@ import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; +import android.support.annotation.NonNull; /** * Created by Ephraim Kigamba - ekigamba@ona.io on 26/03/2019 @@ -10,29 +11,21 @@ @Entity(tableName = "sending_devices") public class SendingDevice { - @PrimaryKey(autoGenerate = true) - private int id; - - @ColumnInfo(name = "device_unique_id", index = true) - private String deviceUniqueId; + @NonNull + @PrimaryKey + @ColumnInfo(name = "device_id") + private String deviceId; + @NonNull @ColumnInfo(name = "app_lifetime_key") private String appLifetimeKey; - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getDeviceUniqueId() { - return deviceUniqueId; + public String getDeviceId() { + return deviceId; } - public void setDeviceUniqueId(String deviceUniqueId) { - this.deviceUniqueId = deviceUniqueId; + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; } public String getAppLifetimeKey() { diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/P2pReceivedHistoryDao.java b/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/P2pReceivedHistoryDao.java index 9676d72..e5fa5bf 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/P2pReceivedHistoryDao.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/P2pReceivedHistoryDao.java @@ -1,7 +1,14 @@ package org.smartregister.p2p.model.dao; import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Insert; import android.arch.persistence.room.Query; +import android.arch.persistence.room.Update; +import android.support.annotation.NonNull; + +import org.smartregister.p2p.model.P2pReceivedHistory; + +import java.util.List; /** * Created by Ephraim Kigamba - ekigamba@ona.io on 26/03/2019 @@ -10,6 +17,15 @@ @Dao public interface P2pReceivedHistoryDao { + @Insert + void addReceivedHistory(@NonNull P2pReceivedHistory receivedHistory); + + @Update + void updateReceivedHistory(@NonNull P2pReceivedHistory receivedHistory); + @Query("DELETE FROM p2p_received_history WHERE sending_device_id = :sendingDeviceId") - int clearDeviceRecords(int sendingDeviceId); + int clearDeviceRecords(@NonNull String sendingDeviceId); + + @Query("SELECT * FROM p2p_received_history WHERE sending_device_id = :sendingDeviceId") + List getDeviceReceivedHistory(@NonNull String sendingDeviceId); } diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/SendingDeviceDao.java b/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/SendingDeviceDao.java index d3d36ce..3636830 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/SendingDeviceDao.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/model/dao/SendingDeviceDao.java @@ -21,6 +21,6 @@ public interface SendingDeviceDao { @Update void update(@NonNull SendingDevice sendingDevice); - @Query("SELECT * FROM sending_devices WHERE device_unique_id = :deviceUniqueId LIMIT 1") + @Query("SELECT * FROM sending_devices WHERE device_id = :deviceUniqueId LIMIT 1") SendingDevice getSendingDevice(@NonNull String deviceUniqueId); } diff --git a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java index 91cd28c..54bb518 100644 --- a/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java +++ b/p2p-sync/src/main/java/org/smartregister/p2p/presenter/P2PReceiverPresenter.java @@ -226,7 +226,7 @@ public void processHashKey(@NonNull final String endpointId, @NonNull Payload pa } if (basicDeviceDetails == null || basicDeviceDetails.get(Constants.BasicDeviceDetails.KEY_DEVICE_ID) == null) { - Timber.e("Hash key was sent was null"); + Timber.e(view.getString(R.string.log_hash_key_sent_was_null)); disconnectAndReset(endpointId); } else { connectionLevel = ConnectionLevel.RECEIVED_HASH_KEY; @@ -266,7 +266,7 @@ public void onSuccess(@Nullable Integer result) { @Override public void onError(Exception e) { Timber.e(view.getString(R.string.log_error_occurred_trying_to_delete_p2p_received_history_on_device) - , sendingDevice.getDeviceUniqueId()); + , sendingDevice.getDeviceId()); } }); @@ -313,7 +313,7 @@ public void onError(Exception e) { private void registerSendingDevice(Map basicDeviceDetails) { SendingDevice sendingDevice = new SendingDevice(); - sendingDevice.setDeviceUniqueId((String) basicDeviceDetails.get(Constants.BasicDeviceDetails.KEY_DEVICE_ID)); + sendingDevice.setDeviceId((String) basicDeviceDetails.get(Constants.BasicDeviceDetails.KEY_DEVICE_ID)); sendingDevice.setAppLifetimeKey((String) basicDeviceDetails.get(Constants.BasicDeviceDetails.KEY_APP_LIFETIME_KEY)); P2PLibrary.getInstance().getDb() @@ -329,7 +329,7 @@ private Integer clearDeviceHistoryAndUpdateDeviceKey(SendingDevice sendingDevice db.sendingDeviceDao().update(sendingDevice); return db.p2pReceivedHistoryDao() - .clearDeviceRecords(sendingDevice.getId()); + .clearDeviceRecords(sendingDevice.getDeviceId()); } @Override diff --git a/p2p-sync/src/main/res/values/strings.xml b/p2p-sync/src/main/res/values/strings.xml index 1dd0e2b..a32517e 100644 --- a/p2p-sync/src/main/res/values/strings.xml +++ b/p2p-sync/src/main/res/values/strings.xml @@ -61,4 +61,5 @@ Hash key was sent in an invalid format Authorization details sent by receiver are invalid Authorization details sent by receiver are invalid + Hash key was sent was null