From e02bd3530912767e7d21166bf4d19cdea41fd07b Mon Sep 17 00:00:00 2001 From: Isaac_Chen Date: Thu, 7 May 2020 01:27:03 +0800 Subject: [PATCH] Final check about normalization --- .../notes/persistence/NotesDatabaseTest.java | 23 ++-- .../persistence/AbstractNotesDatabase.java | 13 --- .../notes/persistence/NotesDatabase.java | 110 ++++++------------ 3 files changed, 50 insertions(+), 96 deletions(-) diff --git a/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java b/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java index 7409ca75..8e5bbc2e 100644 --- a/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java +++ b/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java @@ -545,6 +545,10 @@ public class NotesDatabaseTest { "hello4", getCurDate() + " no person cooler than Mike.", true, "Peter's Diary", null)); + long noteID5 = db.addNote(account.getId(), new CloudNote(1, Calendar.getInstance(), + "hello5", getCurDate() + " helloworld", + true, "Diary/'s Diary", null)); + Method method_ids_by_title = NotesDatabase.class.getDeclaredMethod("getCategoryIdsByTitle", long.class, String.class); method_ids_by_title.setAccessible(true); @@ -554,7 +558,7 @@ public class NotesDatabaseTest { List categories = db.getCategories(account.getId()); Log.i("Test_14_getCategoryIdsByTitle", "size: " + categories.size()); // Log.i("Test_14_getCategoryIdsByTitle", "item: "+ categories.get(0).label); - String pattern = "'s Dia"; + String pattern = "Dia"; List matchList = new ArrayList<>(); for (NavigationAdapter.NavigationItem categoryItem : categories) { String catTitle = categoryItem.label; @@ -569,16 +573,14 @@ public class NotesDatabaseTest { } } if (contain) { - Log.e("###", catTitle); - // She will be matched, store ID - int catId = (int) method_id_by_title.invoke(db, account.getId(), catTitle, false); - matchList.add(catId); + if (pattern.length() == catTitle.length() || + (catTitle.charAt(pattern.length()) == '/')) { + Log.e("###", catTitle); + // She will be matched, store ID + int catId = (int) method_id_by_title.invoke(db, account.getId(), catTitle, false); + matchList.add(catId); + } } -// if (catTitle.contains(pattern)) { -// // She will be matched, store ID -// int catId = (int) method_id_by_title.invoke(db, account.getId(), catTitle, false); -// matchList.add(catId); -// } } // Now use our testing method @@ -594,6 +596,7 @@ public class NotesDatabaseTest { db.deleteNote(noteID2, DBStatus.VOID); db.deleteNote(noteID3, DBStatus.VOID); db.deleteNote(noteID4, DBStatus.VOID); + db.deleteNote(noteID5, DBStatus.VOID); assertEquals(matchList.toString(), testList.toString()); } catch (Exception e) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/AbstractNotesDatabase.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/AbstractNotesDatabase.java index 2651b89f..7745b899 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/AbstractNotesDatabase.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/AbstractNotesDatabase.java @@ -30,12 +30,6 @@ import it.niedermann.owncloud.notes.util.NoteUtil; // Protected APIs @SuppressWarnings("WeakerAccess") abstract class AbstractNotesDatabase extends SQLiteOpenHelper { - - // TODO : delete after testing - // ------------------- just for testing ------------------- - public static final boolean testFlag = true; - // ------------------ just for testing -------------------- - private static final String TAG = AbstractNotesDatabase.class.getSimpleName(); private static final int database_version = 13; @@ -133,13 +127,6 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper { @SuppressWarnings("deprecation") @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - // TODO : delete after testing - // ----------------- just for testing -------------------------- -// if (testFlag) { -// recreateDatabase(db); -// return; -// } - // ---------------- just for testing - end --------------------- if (oldVersion < 3) { recreateDatabase(db); return; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java index ce08dd95..14215e28 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java @@ -23,7 +23,6 @@ import com.nextcloud.android.sso.AccountImporter; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; import com.nextcloud.android.sso.model.SingleSignOnAccount; -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -38,7 +37,6 @@ import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.android.activity.EditNoteActivity; import it.niedermann.owncloud.notes.android.appwidget.NoteListWidget; import it.niedermann.owncloud.notes.android.appwidget.SingleNoteWidget; -import it.niedermann.owncloud.notes.model.Category; import it.niedermann.owncloud.notes.model.CloudNote; import it.niedermann.owncloud.notes.model.DBNote; import it.niedermann.owncloud.notes.model.DBStatus; @@ -99,7 +97,6 @@ public class NotesDatabase extends AbstractNotesDatabase { * * @param note Note to be added. Remotely created Notes must be of type CloudNote and locally created Notes must be of Type DBNote (with DBStatus.LOCAL_EDITED)! */ - // TODO: test long addNote(long accountId, CloudNote note) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); @@ -225,7 +222,6 @@ public class NotesDatabase extends AbstractNotesDatabase { * @param pruneContent whether or not the content should be pruned for performance reasons * @return DBNote */ - // TODO: try reflection testing @NonNull private DBNote getNoteFromCursor(long accountId, @NonNull Cursor cursor, boolean pruneContent) { validateAccountId(accountId); @@ -282,10 +278,13 @@ public class NotesDatabase extends AbstractNotesDatabase { /** * Returns a list of all Notes in the Database + * This method only supports to return the notes in the categories with the matched title or the notes in the categories whose ancestor category matches + * For example, three categories with the title "abc", "abc/aaa" and "abcd" + * If search with "abc", then only notes in "abc" and "abc/aaa" will be returned. + * If any other information about this is needed, please refer to {@link this.getCategoryIdsByTitle(long, String)} method * * @return List<Note> */ - // TODO: Test @NonNull @WorkerThread public List searchNotes(long accountId, @Nullable CharSequence query, @Nullable String category, @Nullable Boolean favorite) { @@ -376,8 +375,6 @@ public class NotesDatabase extends AbstractNotesDatabase { return favorites; } - // TODO: test - /** * This method return all of the categories with given accountId * The join operation is used because it is needed that the number of notes in each category @@ -426,7 +423,6 @@ public class NotesDatabase extends AbstractNotesDatabase { * @return The category list containing all of the categories matched */ // TODO merge with getCategories(long accountId) - // TODO: test @NonNull @WorkerThread public List searchCategories(long accountId, String search) { @@ -476,8 +472,6 @@ public class NotesDatabase extends AbstractNotesDatabase { serverSyncHelper.scheduleSync(ssoAccount, true); } - // TODO: test - /** * Set the category for a given note. * This method will search in the database to find out the category id in the db. @@ -526,7 +520,6 @@ public class NotesDatabase extends AbstractNotesDatabase { * @param callback When the synchronization is finished, this callback will be invoked (optional). * @return changed note if differs from database, otherwise the old note. */ - // TODO: test public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable ISyncCallback callback) { //debugPrintFullDB(); DBNote newNote; @@ -850,14 +843,12 @@ public class NotesDatabase extends AbstractNotesDatabase { } } - // TODO: Add a method to update the sorting method in category - // TODO: Not sure: Add more methods for category table. - - - // TODO: Test - /** * Get the category if with the given category title + * The method does not support fuzzy search. + * Because the category title in database is unique, there will not at most one result. + * If there is no such category, database will create it if create flag is set. + * Otherwise this method will return -1 as default value. * * @param accountId The user account Id * @param categoryTitle The category title which will be search in the db @@ -893,7 +884,35 @@ public class NotesDatabase extends AbstractNotesDatabase { return id; } - // TODO: test + /** + * Find out all of the matched Category id with the given accountId and category title + * This method only supports to return the categories with the matched title or the categories whose ancestor category matches + * For example, three categories with the title "abc", "abc/aaa" and "abcd" + * If search with "abc", then only "abc" and "abc/aaa" will be return. + * This is also why /% is needed hear + * + * @param accountId The user account Id + * @param categoryTitle The category title which will be searched. + * @return All matched category ids. + */ + private List getCategoryIdsByTitle(long accountId, @NonNull String categoryTitle) { + validateAccountId(accountId); + Cursor cursor = getReadableDatabase().query( + table_category, + new String[]{key_id}, + key_title + " = ? OR " + key_title + " LIKE ? AND " + key_account_id + " = ? ", + new String[]{categoryTitle, categoryTitle + "/%", String.valueOf(accountId)}, + key_id, + null, + key_id + ); + List ids = new ArrayList<>(); + while (cursor.moveToNext()) { + ids.add(cursor.getInt(0)); + } + cursor.close(); + return ids; + } /** * Find the category title with the category id @@ -921,59 +940,4 @@ public class NotesDatabase extends AbstractNotesDatabase { cursor.close(); return title; } - - private void traverse(@NonNull String table) { - Log.e("Traverse", "###################START TRAVERSE#####################"); - String query = "SELECT * FROM " + table; - Cursor cursor = getReadableDatabase().rawQuery(query, null); - Log.e(table + " HEAD", Arrays.toString(cursor.getColumnNames())); - while (cursor.moveToNext()) { - Log.e(table, outputCursor(cursor)); - } - cursor.close(); - Log.e("Traverse", "###################END TRAVERSE#####################"); - } - - private String outputCursor(@NonNull Cursor cursor) { - StringBuffer str = new StringBuffer(); - for (int i = 0; i < cursor.getColumnNames().length; i++) { - if (cursor.getString(i) == null || cursor.getString(i).equals("")) { - str.append("empty"); - } else - str.append(cursor.getString(i)); - str.append(" "); - } - return str.toString(); - } - - // TODO: test - // TODO: not fuzzy search by testing bug? - - /** - * Find out all of the matched Category id with the given accountId and category title - * Note that this method supports the fuzzy search. But only prefix match is supported. - * "abcd" will be returned when categoryTitle is "ab" while it will not be returned when the categoryTitle is "bc" - * - * @param accountId The user account Id - * @param categoryTitle The category title which will be searched. - * @return All matched category ids. - */ - private List getCategoryIdsByTitle(long accountId, @NonNull String categoryTitle) { - validateAccountId(accountId); - Cursor cursor = getReadableDatabase().query( - table_category, - new String[]{key_id}, - key_title + " = ? OR " + key_title + " LIKE ? AND " + key_account_id + " = ? ", - new String[]{categoryTitle, categoryTitle + "/%", String.valueOf(accountId)}, - key_id, - null, - key_id - ); - List ids = new ArrayList<>(); - while (cursor.moveToNext()) { - ids.add(cursor.getInt(0)); - } - cursor.close(); - return ids; - } }