diff --git a/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp b/app/src/main/cpp/MediTrakCore/DatabaseController/DatabaseController.cpp index 91f631b5..0d823ce4 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 < 15) { + 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) { + timeTaken += "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 0a6a63ca..80430680 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 = 15; const string DATABASE_NAME = "Medications.db"; vector tablesToIgnore; DbManager manager; @@ -102,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"; diff --git a/app/src/main/cpp/Sqlite3Database/DbManager/DbManager.cpp b/app/src/main/cpp/Sqlite3Database/DbManager/DbManager.cpp index 53c8da68..bec11a51 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 b295ae6d..b470951d 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 e8fd5735..848f757a 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 701aceed..6128c1b5 100644 --- a/app/src/main/java/projects/medicationtracker/Models/Medication.java +++ b/app/src/main/java/projects/medicationtracker/Models/Medication.java @@ -67,17 +67,7 @@ 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()) { - 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 2bafa8af..a1710fdd 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;