Skip to content

Commit

Permalink
Modify tables to enable for tracking of last sent data
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
ekigamba committed Mar 28, 2019
1 parent 1d27218 commit cf9a62b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 79 deletions.
60 changes: 23 additions & 37 deletions p2p-sync/schemas/org.smartregister.p2p.model.AppDatabase/1.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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\")"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,29 @@
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 = "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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<P2pReceivedHistory> getDeviceReceivedHistory(@NonNull String sendingDeviceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
});

Expand Down Expand Up @@ -313,7 +313,7 @@ public void onError(Exception e) {

private void registerSendingDevice(Map<String, Object> 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()
Expand All @@ -329,7 +329,7 @@ private Integer clearDeviceHistoryAndUpdateDeviceKey(SendingDevice sendingDevice
db.sendingDeviceDao().update(sendingDevice);

return db.p2pReceivedHistoryDao()
.clearDeviceRecords(sendingDevice.getId());
.clearDeviceRecords(sendingDevice.getDeviceId());
}

@Override
Expand Down
1 change: 1 addition & 0 deletions p2p-sync/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@
<string name="log_hash_key_was_sent_in_an_invalid_format">Hash key was sent in an invalid format</string>
<string name="reason_authorization_rejected_by_sender_details_invalid">Authorization details sent by receiver are invalid</string>
<string name="reason_authorization_rejected_by_receiver_details_invalid">Authorization details sent by receiver are invalid</string>
<string name="log_hash_key_sent_was_null">Hash key was sent was null</string>
</resources>

0 comments on commit cf9a62b

Please sign in to comment.