From dfaed88a56f4a5f8cbc229b89b5d5308bcebf394 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 27 Mar 2022 18:06:30 +0200 Subject: [PATCH 1/6] New schema --- .../1.json | 82 ----------------- .../1.json | 88 ------------------- 2 files changed, 170 deletions(-) delete mode 100644 app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json delete mode 100644 app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json diff --git a/app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json b/app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json deleted file mode 100644 index 8c74d87..0000000 --- a/app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json +++ /dev/null @@ -1,82 +0,0 @@ -{ - "formatVersion": 1, - "database": { - "version": 1, - "identityHash": "96dc5c4ccb337047e323d613eb86bdd1", - "entities": [ - { - "tableName": "drugs", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER NOT NULL, `cis` TEXT, `cip13` TEXT, `cip7` TEXT, `administration_mode` TEXT, `name` TEXT, `presentation` TEXT, `label_group` TEXT, `generic_type` INTEGER, PRIMARY KEY(`_id`))", - "fields": [ - { - "fieldPath": "_id", - "columnName": "_id", - "affinity": "INTEGER", - "notNull": true - }, - { - "fieldPath": "cis", - "columnName": "cis", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "cip13", - "columnName": "cip13", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "cip7", - "columnName": "cip7", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "administration_mode", - "columnName": "administration_mode", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "name", - "columnName": "name", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "presentation", - "columnName": "presentation", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "label_group", - "columnName": "label_group", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "generic_type", - "columnName": "generic_type", - "affinity": "INTEGER", - "notNull": false - } - ], - "primaryKey": { - "columnNames": [ - "_id" - ], - "autoGenerate": false - }, - "indices": [], - "foreignKeys": [] - } - ], - "views": [], - "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, '96dc5c4ccb337047e323d613eb86bdd1')" - ] - } -} \ No newline at end of file diff --git a/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json b/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json deleted file mode 100644 index 64b56b9..0000000 --- a/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json +++ /dev/null @@ -1,88 +0,0 @@ -{ - "formatVersion": 1, - "database": { - "version": 1, - "identityHash": "46c4983931eaf7414e25dfd9e1a55e0f", - "entities": [ - { - "tableName": "prescriptions", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`cis` TEXT NOT NULL, `cip13` TEXT, `name` TEXT, `administration_mode` TEXT, `presentation` TEXT, `stock` REAL, `take` REAL, `warning` INTEGER, `alert` INTEGER, `last_update` INTEGER, PRIMARY KEY(`cis`))", - "fields": [ - { - "fieldPath": "cis", - "columnName": "cis", - "affinity": "TEXT", - "notNull": true - }, - { - "fieldPath": "cip13", - "columnName": "cip13", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "name", - "columnName": "name", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "administration_mode", - "columnName": "administration_mode", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "presentation", - "columnName": "presentation", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "stock", - "columnName": "stock", - "affinity": "REAL", - "notNull": false - }, - { - "fieldPath": "take", - "columnName": "take", - "affinity": "REAL", - "notNull": false - }, - { - "fieldPath": "warning", - "columnName": "warning", - "affinity": "INTEGER", - "notNull": false - }, - { - "fieldPath": "alert", - "columnName": "alert", - "affinity": "INTEGER", - "notNull": false - }, - { - "fieldPath": "last_update", - "columnName": "last_update", - "affinity": "INTEGER", - "notNull": false - } - ], - "primaryKey": { - "columnNames": [ - "cis" - ], - "autoGenerate": false - }, - "indices": [], - "foreignKeys": [] - } - ], - "views": [], - "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, '46c4983931eaf7414e25dfd9e1a55e0f')" - ] - } -} \ No newline at end of file From d6bd1590e836b0724db92a000613b833a21329b5 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 27 Mar 2022 18:08:41 +0200 Subject: [PATCH 2/6] change a method new; use singleton for the database access. --- .../java/net/foucry/pilldroid/DrugDetailActivity.java | 7 +++---- .../java/net/foucry/pilldroid/DrugListActivity.java | 11 ++--------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java index eea98e9..5e7cd07 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java @@ -55,7 +55,7 @@ public class DrugDetailActivity extends AppCompatActivity { public void onClick(View view) { Log.d(TAG, "Click on save icon"); - getMDrugChanges(); + getDrugChanges(); setResult(1); finish(); overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right); @@ -107,11 +107,10 @@ public class DrugDetailActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } - private void getMDrugChanges() { + private void getDrugChanges() { Log.d(TAG, "Time to save new values"); - PrescriptionDatabase prescriptions = null; - assert false; + PrescriptionDatabase prescriptions = PrescriptionDatabase.getInstanceDatabase(this); PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); Prescription newPrescription = prescriptionsDAO.getMedicByCIP13(aPrescription.getCip13()); diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index 6906407..628f7fb 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -93,17 +93,10 @@ public class DrugListActivity extends AppCompatActivity { } // Create medicines Room database from drugs.db files - medicines = Room - .databaseBuilder(getApplicationContext(), MedicineDatabase.class, "medicines") - .createFromAsset("drugs.db") - .allowMainThreadQueries() - .build(); + medicines = MedicineDatabase.getInstanceDatabase(this); // Create prescriptions Room database - prescriptions = Room - .databaseBuilder(getApplicationContext(), PrescriptionDatabase.class, "prescriptions") - .allowMainThreadQueries() - .build(); + prescriptions = PrescriptionDatabase.getInstanceDatabase(this); // Manually migrate old database to room PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); From 7f89c9b5aa158b2272bb0da0ae2a9e897fe53a5b Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 27 Mar 2022 18:09:19 +0200 Subject: [PATCH 3/6] Create singleton to access to database --- .../pilldroid/databases/MedicineDatabase.java | 19 ++++++++++++++++ .../databases/PrescriptionDatabase.java | 22 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java b/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java index 944fd9f..0f50517 100644 --- a/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java +++ b/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java @@ -1,6 +1,9 @@ package net.foucry.pilldroid.databases; +import android.content.Context; + import androidx.room.Database; +import androidx.room.Room; import androidx.room.RoomDatabase; import net.foucry.pilldroid.dao.MedicinesDAO; @@ -12,5 +15,21 @@ import net.foucry.pilldroid.models.Medicine; ) public abstract class MedicineDatabase extends RoomDatabase { + private static MedicineDatabase INSTANCE; public abstract MedicinesDAO getMedicinesDAO(); + public static MedicineDatabase getInstanceDatabase(Context context) { + if (INSTANCE == null) { + INSTANCE = + Room + .databaseBuilder(context.getApplicationContext(), MedicineDatabase.class, "medicines") + .createFromAsset("drugs.db") + .allowMainThreadQueries() + .build(); + } + return INSTANCE; + } + + public static void destroyInstance() { + INSTANCE = null; + } } \ No newline at end of file diff --git a/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java b/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java index 405d285..e8a3387 100644 --- a/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java +++ b/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java @@ -1,17 +1,39 @@ package net.foucry.pilldroid.databases; +import android.content.Context; + import androidx.room.AutoMigration; import androidx.room.Database; +import androidx.room.Room; import androidx.room.RoomDatabase; import net.foucry.pilldroid.dao.PrescriptionsDAO; import net.foucry.pilldroid.models.Prescription; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + @Database( version = 1, entities = {Prescription.class} ) public abstract class PrescriptionDatabase extends RoomDatabase { + private static PrescriptionDatabase INSTANCE; public abstract PrescriptionsDAO getPrescriptionsDAO(); + public static PrescriptionDatabase getInstanceDatabase(Context context) { + if (INSTANCE == null) { + INSTANCE = + Room + .databaseBuilder(context.getApplicationContext(), + PrescriptionDatabase.class, "prescriptions") + .allowMainThreadQueries() + .build(); + } + return INSTANCE; + } + + public static void destroyInstance() { + INSTANCE = null; + } } From 891368f5d7fa09b40e529169fa445f1f94b6aaa1 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 27 Mar 2022 18:09:52 +0200 Subject: [PATCH 4/6] add 2 columns --- .../foucry/pilldroid/models/Prescription.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/net/foucry/pilldroid/models/Prescription.java b/app/src/main/java/net/foucry/pilldroid/models/Prescription.java index 6d23413..6e3edfc 100644 --- a/app/src/main/java/net/foucry/pilldroid/models/Prescription.java +++ b/app/src/main/java/net/foucry/pilldroid/models/Prescription.java @@ -23,7 +23,8 @@ public class Prescription implements Serializable { private Integer warning; private Integer alert; private Long last_update; - + private String label_group; + private Integer genetic_type; public void setCis(@NonNull String cis) { @@ -113,7 +114,21 @@ public class Prescription implements Serializable { public int getWarnThreshold() { return this.warning; } + public String getLabel_group() { + return label_group; + } + public void setLabel_group(String label_group) { + this.label_group = label_group; + } + + public Integer getGenetic_type() { + return genetic_type; + } + + public void setGenetic_type(Integer genetic_type) { + this.genetic_type = genetic_type; + } public Date getDateEndOfStock() { int numberDayOfTake; if (this.getTake() > 0) { From a0aa8471c9fd6088384bc7ed73b33012962b11c8 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 27 Mar 2022 18:10:36 +0200 Subject: [PATCH 5/6] optimize import --- app/src/main/java/net/foucry/pilldroid/Utils.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/Utils.java b/app/src/main/java/net/foucry/pilldroid/Utils.java index 0d8c037..6e2f416 100644 --- a/app/src/main/java/net/foucry/pilldroid/Utils.java +++ b/app/src/main/java/net/foucry/pilldroid/Utils.java @@ -1,7 +1,5 @@ package net.foucry.pilldroid; -import androidx.room.Database; - import net.foucry.pilldroid.models.Medicine; import net.foucry.pilldroid.models.Prescription; From 2ae75d2f5ba57533fa14718b8aa74812f24e1002 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 27 Mar 2022 21:18:14 +0200 Subject: [PATCH 6/6] add json schemas --- .../1.json | 82 ++++++++++++++ .../1.json | 100 ++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json create mode 100644 app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json diff --git a/app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json b/app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json new file mode 100644 index 0000000..8c74d87 --- /dev/null +++ b/app/schemas/net.foucry.pilldroid.databases.MedicineDatabase/1.json @@ -0,0 +1,82 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "96dc5c4ccb337047e323d613eb86bdd1", + "entities": [ + { + "tableName": "drugs", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER NOT NULL, `cis` TEXT, `cip13` TEXT, `cip7` TEXT, `administration_mode` TEXT, `name` TEXT, `presentation` TEXT, `label_group` TEXT, `generic_type` INTEGER, PRIMARY KEY(`_id`))", + "fields": [ + { + "fieldPath": "_id", + "columnName": "_id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "cis", + "columnName": "cis", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "cip13", + "columnName": "cip13", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "cip7", + "columnName": "cip7", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "administration_mode", + "columnName": "administration_mode", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "presentation", + "columnName": "presentation", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "label_group", + "columnName": "label_group", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "generic_type", + "columnName": "generic_type", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "_id" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "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, '96dc5c4ccb337047e323d613eb86bdd1')" + ] + } +} \ No newline at end of file diff --git a/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json b/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json new file mode 100644 index 0000000..898897c --- /dev/null +++ b/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json @@ -0,0 +1,100 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "7d1384a7162a98602e19c3ee54b7aee5", + "entities": [ + { + "tableName": "prescriptions", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`cis` TEXT NOT NULL, `cip13` TEXT, `name` TEXT, `administration_mode` TEXT, `presentation` TEXT, `stock` REAL, `take` REAL, `warning` INTEGER, `alert` INTEGER, `last_update` INTEGER, `label_group` TEXT, `genetic_type` INTEGER, PRIMARY KEY(`cis`))", + "fields": [ + { + "fieldPath": "cis", + "columnName": "cis", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "cip13", + "columnName": "cip13", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "administration_mode", + "columnName": "administration_mode", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "presentation", + "columnName": "presentation", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "stock", + "columnName": "stock", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "take", + "columnName": "take", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "warning", + "columnName": "warning", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "alert", + "columnName": "alert", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "last_update", + "columnName": "last_update", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "label_group", + "columnName": "label_group", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "genetic_type", + "columnName": "genetic_type", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "cis" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "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, '7d1384a7162a98602e19c3ee54b7aee5')" + ] + } +} \ No newline at end of file