From d508dae272532e093ed7dd646cf2fc7b3196f281 Mon Sep 17 00:00:00 2001 From: Adam Guidarini <45023561+AdamGuidarini@users.noreply.github.com> Date: Wed, 23 Oct 2024 22:29:05 -0400 Subject: [PATCH 1/4] Update build.gradle --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a995fd4..06c3b82 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,8 +16,8 @@ android { targetSdkVersion 34 compileSdk 34 - versionCode 29 - versionName "0.13.0" + versionCode 30 + versionName "0.13.1" resourceConfigurations += ['en', 'de', 'es', 'it', 'tr'] ndk { From bf4d0c0b2ed0b8748ed73707206d252bda6c36b5 Mon Sep 17 00:00:00 2001 From: Adam Guidarini <45023561+AdamGuidarini@users.noreply.github.com> Date: Fri, 25 Oct 2024 19:05:05 -0400 Subject: [PATCH 2/4] Repairing broken data --- .../DatabaseController/DatabaseController.cpp | 112 ++++++++++++++++++ .../DatabaseController/DatabaseController.h | 9 +- .../Sqlite3Database/DbManager/DbManager.cpp | 2 +- .../medicationtracker/Helpers/DBHelper.java | 19 ++- .../medicationtracker/Models/Dose.java | 18 --- .../medicationtracker/Models/Medication.java | 5 - 6 files changed, 135 insertions(+), 30 deletions(-) diff --git a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp index 91f631b..cbc2772 100644 --- a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp +++ b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp @@ -227,6 +227,10 @@ void DatabaseController::upgrade(int currentVersion) { ); } + if (currentVersion < 16) { + repairImportErrors(); + } + manager.execSql("PRAGMA schema_version = " + to_string(DB_VERSION)); } @@ -551,3 +555,111 @@ vector DatabaseController::getStashedNotifications() { void DatabaseController::deleteNotification(long id) { manager.deleteRecord(NOTIFICATIONS, {pair(DOSE_ID, to_string(id))}); } + +void DatabaseController::repairImportErrors() { + auto doses = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TRACKER_TABLE); + auto meds = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TABLE); + auto times = manager.execSqlWithReturn("SELECT * FROM " + MEDICATION_TIMES); + auto notes = manager.execSqlWithReturn("SELECT * FROM " + NOTES_TABLE); + regex dateRegex("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$"); + + while (!doses->isAfterLast() && doses->getCount() > 0) { + auto doseTime = doses->getItem(DOSE_TIME); + auto timeTaken = doses->getItem(TIME_TAKEN); + bool updateRequired = false; + + // scheduled datetime is wrong + if (!regex_match(doseTime, dateRegex)) { + if (doseTime.length() == DateFormats::DB_DATE_FORMAT.length() - 1) { + doseTime += "0"; + + updateRequired = true; + } + } + + // take datetime is wrong + if (!regex_match(timeTaken, dateRegex)) { + if (timeTaken.length() == DateFormats::DB_DATE_FORMAT.length() - 1) { + doseTime += "0"; + + updateRequired = true; + } + } + + if (updateRequired) { + map vals = { + pair(DOSE_TIME, doseTime), + pair(TIME_TAKEN, timeTaken) + }; + + manager.update( + MEDICATION_TRACKER_TABLE, + vals, + {pair(DOSE_ID, doses->getItem(DOSE_ID))} + ); + } + + doses->moveToNext(); + } + + while (!meds->isAfterLast() && meds->getCount() > 0) { + auto start = meds->getItem(START_DATE); + + // scheduled datetime is wrong + if (!regex_match(start, dateRegex)) { + manager.update( + MEDICATION_TABLE, + {pair(START_DATE, start + "0")}, + {pair(MED_ID, meds->getItem(MED_ID))} + ); + } + + meds->moveToNext(); + } + + while (!times->isAfterLast() && times->getCount() > 0) { + auto schedTime = times->getItem(DRUG_TIME); + + bool match = !regex_match(schedTime, dateRegex); + + // scheduled datetime is wrong + if (!match && schedTime.length() == DateFormats::DB_DATE_FORMAT.length() - 1) { + manager.update( + MEDICATION_TIMES, + {pair(DRUG_TIME, schedTime + "0")}, + {pair(TIME_ID, times->getItem(TIME_ID))} + ); + } + + times->moveToNext(); + } + + while (!notes->isAfterLast() && notes->getCount() > 0) { + string editTime = notes->getItem(TIME_EDITED); + string timeEdited = TIME_EDITED; + bool match = !regex_match(editTime, dateRegex); + + timeEdited.pop_back(); + + if (timeEdited == editTime) { + manager.update( + NOTES_TABLE, + {pair(TIME_EDITED, "")}, + {pair(NOTE_ID, notes->getItem(NOTE_ID))} + ); + } else if (!match && timeEdited.length() == DateFormats::DB_DATE_FORMAT.length() - 1) { + manager.update( + NOTES_TABLE, + {pair(TIME_EDITED, timeEdited += "0")}, + {pair(NOTE_ID, notes->getItem(NOTE_ID))} + ); + } + + notes->moveToNext(); + } + + delete doses; + delete meds; + delete times;; + delete notes; +} diff --git a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h index 0a6a63c..4019bb9 100644 --- a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h +++ b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "DbManager.h" #include "../Medication/Medication.h" #include "../Dose/Dose.h" @@ -23,11 +24,12 @@ namespace TimeFormats { namespace DateFormats { const string MM_DD_YYYY = "MM/dd/yyyy"; const string DD_MM_YYYY = "dd/MM/yyyy"; + const string DB_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; } class DatabaseController { private: - const int DB_VERSION = 14; + const int DB_VERSION = 16; const string DATABASE_NAME = "Medications.db"; vector tablesToIgnore; DbManager manager; @@ -247,6 +249,11 @@ class DatabaseController { * @param id ID of notification to delete */ void deleteNotification(long id); + + /** + * Resolves issues in caused by imports where the last character of the last record was removed + */ + void repairImportErrors(); }; #endif //MEDICATIONTRACKER_DATABASECONTROLLER_H diff --git a/app/src/main/cpp/Sqlite3Database/DbManager/DbManager.cpp b/app/src/main/cpp/Sqlite3Database/DbManager/DbManager.cpp index 53c8da6..bec11a5 100644 --- a/app/src/main/cpp/Sqlite3Database/DbManager/DbManager.cpp +++ b/app/src/main/cpp/Sqlite3Database/DbManager/DbManager.cpp @@ -524,7 +524,7 @@ void DbManager::importData(string &inData, const vector &ignoreTables) { while ((pos = tblStr.find(',')) != string::npos || tblStr.length() > 0) { unsigned int end = - tblStr.find(',') != string::npos ? tblStr.find(',') : tblStr.length() - 1; + tblStr.find(',') != string::npos ? tblStr.find(',') : tblStr.length(); string token = tblStr.substr(0, end); bool incrementInd = false; diff --git a/app/src/main/java/projects/medicationtracker/Helpers/DBHelper.java b/app/src/main/java/projects/medicationtracker/Helpers/DBHelper.java index b295ae6..b470951 100644 --- a/app/src/main/java/projects/medicationtracker/Helpers/DBHelper.java +++ b/app/src/main/java/projects/medicationtracker/Helpers/DBHelper.java @@ -8,6 +8,7 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; +import android.util.Log; import android.util.Pair; import androidx.annotation.Nullable; @@ -882,11 +883,19 @@ public ArrayList getNotes(long medId) { Note n = new Note(noteId, medId, note, entryTime); - if (cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED)) != null && !cursor.getString((cursor.getColumnIndexOrThrow(TIME_EDITED))).isEmpty()) { - LocalDateTime editTime = TimeFormatting.stringToLocalDateTime( - cursor.getString((cursor.getColumnIndexOrThrow(TIME_EDITED))) - ); - n.setModifiedTime(editTime); + if (cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED)) != null && !cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED)).isEmpty()) { + + try { + LocalDateTime editTime = TimeFormatting.stringToLocalDateTime( + cursor.getString(cursor.getColumnIndexOrThrow(TIME_EDITED)) + ); + n.setModifiedTime(editTime); + } catch (Exception e) { + Log.e( + "Notes", + e.getMessage() + ); + } } notes.add(n); diff --git a/app/src/main/java/projects/medicationtracker/Models/Dose.java b/app/src/main/java/projects/medicationtracker/Models/Dose.java index e8fd573..848f757 100644 --- a/app/src/main/java/projects/medicationtracker/Models/Dose.java +++ b/app/src/main/java/projects/medicationtracker/Models/Dose.java @@ -42,15 +42,6 @@ public Dose(long id, long medicationId, boolean isTaken, @Nullable String timeTa final String dateFormat = DBHelper.DateFormats.DB_DATE_FORMAT; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat, Locale.getDefault()); - // Some times seem to be 1 character short, this protects against that - if (timeTaken.length() < dateFormat.length()) { - timeTaken += "0"; - } - - if (doseTime.length() < dateFormat.length()) { - doseTime += "0"; - } - doseId = id; medId = medicationId; taken = isTaken; @@ -72,15 +63,6 @@ public Dose(long id, long medicationId, boolean isTaken, String timeTaken, Strin final String dateFormat = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat, Locale.getDefault()); - // Some times seem to be 1 character short, this protects against that - if (timeTaken.length() < dateFormat.length()) { - timeTaken += "0"; - } - - if (doseTime.length() < dateFormat.length()) { - doseTime += "0"; - } - doseId = id; medId = medicationId; taken = isTaken; diff --git a/app/src/main/java/projects/medicationtracker/Models/Medication.java b/app/src/main/java/projects/medicationtracker/Models/Medication.java index 701acee..f5eb658 100644 --- a/app/src/main/java/projects/medicationtracker/Models/Medication.java +++ b/app/src/main/java/projects/medicationtracker/Models/Medication.java @@ -67,11 +67,6 @@ public Medication(String thisMed, String patient, String units, String[] times, DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat, Locale.getDefault()); LocalDateTime[] medTimes = new LocalDateTime[times.length]; - // Some times seem to be 1 character short, this protects against that - if (firstDate.length() < dateFormat.length()) { - firstDate += "0"; - } - for (int i = 0; i < times.length; i++) { // Some times seem to be 1 character short, this protects against that if (times[i].length() < dateFormat.length()) { From 92efd52bb99631a7cb6c5f104617a845e9c58132 Mon Sep 17 00:00:00 2001 From: Adam Guidarini <45023561+AdamGuidarini@users.noreply.github.com> Date: Fri, 25 Oct 2024 19:18:37 -0400 Subject: [PATCH 3/4] Fixed dose time & time taken, removed += "0" uses --- .../DatabaseController/DatabaseController.cpp | 8 ++++---- .../MediTrakCore/DatabaseController/DatabaseController.h | 2 +- .../projects/medicationtracker/Models/Medication.java | 5 ----- .../projects/medicationtracker/Models/Notification.java | 4 ---- 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp index cbc2772..0d823ce 100644 --- a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp +++ b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp @@ -227,7 +227,7 @@ void DatabaseController::upgrade(int currentVersion) { ); } - if (currentVersion < 16) { + if (currentVersion < 15) { repairImportErrors(); } @@ -580,7 +580,7 @@ void DatabaseController::repairImportErrors() { // take datetime is wrong if (!regex_match(timeTaken, dateRegex)) { if (timeTaken.length() == DateFormats::DB_DATE_FORMAT.length() - 1) { - doseTime += "0"; + timeTaken += "0"; updateRequired = true; } @@ -620,7 +620,7 @@ void DatabaseController::repairImportErrors() { while (!times->isAfterLast() && times->getCount() > 0) { auto schedTime = times->getItem(DRUG_TIME); - bool match = !regex_match(schedTime, dateRegex); + bool match = regex_match(schedTime, dateRegex); // scheduled datetime is wrong if (!match && schedTime.length() == DateFormats::DB_DATE_FORMAT.length() - 1) { @@ -637,7 +637,7 @@ void DatabaseController::repairImportErrors() { while (!notes->isAfterLast() && notes->getCount() > 0) { string editTime = notes->getItem(TIME_EDITED); string timeEdited = TIME_EDITED; - bool match = !regex_match(editTime, dateRegex); + bool match = regex_match(editTime, dateRegex); timeEdited.pop_back(); diff --git a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h index 4019bb9..775be00 100644 --- a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h +++ b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h @@ -29,7 +29,7 @@ namespace DateFormats { class DatabaseController { private: - const int DB_VERSION = 16; + const int DB_VERSION = 15; const string DATABASE_NAME = "Medications.db"; vector tablesToIgnore; DbManager manager; diff --git a/app/src/main/java/projects/medicationtracker/Models/Medication.java b/app/src/main/java/projects/medicationtracker/Models/Medication.java index f5eb658..6128c1b 100644 --- a/app/src/main/java/projects/medicationtracker/Models/Medication.java +++ b/app/src/main/java/projects/medicationtracker/Models/Medication.java @@ -68,11 +68,6 @@ public Medication(String thisMed, String patient, String units, String[] times, LocalDateTime[] medTimes = new LocalDateTime[times.length]; for (int i = 0; i < times.length; i++) { - // Some times seem to be 1 character short, this protects against that - if (times[i].length() < dateFormat.length()) { - times[i] += "0"; - } - medTimes[i] = LocalDateTime.parse(times[i], formatter); } diff --git a/app/src/main/java/projects/medicationtracker/Models/Notification.java b/app/src/main/java/projects/medicationtracker/Models/Notification.java index 2bafa8a..a1710fd 100644 --- a/app/src/main/java/projects/medicationtracker/Models/Notification.java +++ b/app/src/main/java/projects/medicationtracker/Models/Notification.java @@ -23,10 +23,6 @@ public Notification(long rowId, long medicationId, long notificationId, LocalDat public Notification(long rowId, long medicationId, long notificationId, String dosageTime) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DBHelper.DateFormats.DB_DATE_FORMAT, Locale.getDefault()); - if (dosageTime.length() < DBHelper.DateFormats.DB_DATE_FORMAT.length()) { - dosageTime += "0"; - } - id = rowId; medId = medicationId; this.notificationId = notificationId; From 39fa7debe243af1c252814b1d742f95de4a1daa9 Mon Sep 17 00:00:00 2001 From: Adam Guidarini <45023561+AdamGuidarini@users.noreply.github.com> Date: Fri, 25 Oct 2024 19:28:24 -0400 Subject: [PATCH 4/4] Made repair function private --- .../MediTrakCore/DatabaseController/DatabaseController.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h index 775be00..8043068 100644 --- a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h +++ b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.h @@ -104,6 +104,10 @@ class DatabaseController { */ Dose* setDose(Table* table); + /** + * Resolves issues in caused by imports where the last character of the last record was removed + */ + void repairImportErrors(); public: const string NOTES_TABLE = "Notes"; const string SETTINGS_TABLE = "Settings"; @@ -249,11 +253,6 @@ class DatabaseController { * @param id ID of notification to delete */ void deleteNotification(long id); - - /** - * Resolves issues in caused by imports where the last character of the last record was removed - */ - void repairImportErrors(); }; #endif //MEDICATIONTRACKER_DATABASECONTROLLER_H