finish the backend API and corresponding test case for category sorting method

This commit is contained in:
Hui-Ouyang16 2020-05-23 00:20:10 +08:00
parent a6580bcdbc
commit ab89852b22
2 changed files with 74 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import it.niedermann.owncloud.notes.model.DBStatus;
import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.util.CategorySortingMethod;
import it.niedermann.owncloud.notes.util.NoteUtil;
import android.content.Context;
@ -25,6 +26,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
@ -555,6 +557,34 @@ public class NotesDatabaseTest {
// assertEquals(0, sorting_method);
// }
@Test
public void test_15_getAndModifyCategoryOrderById() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// add a note to database
CloudNote cloudNote = new CloudNote(1, Calendar.getInstance(),
"A Coding Day", "This is a day which is very suitable to code.",
true, "CodingDiary", null);
long noteID = db.addNote(account.getId(), cloudNote);
// reflection to get categoryID by title
Method method = NotesDatabase.class.getDeclaredMethod("getCategoryIdByTitle",
long.class,
String.class);
method.setAccessible(true);
int categoryID = (int) method.invoke(db, account.getId(), "CodingDiary");
// check the default value of ordering_method
CategorySortingMethod defaultMethod = db.getCategoryOrderById(account.getId(), categoryID);
assertEquals(defaultMethod, CategorySortingMethod.getCSM(0));
// modify the value of ordering_method and check
db.modifyCategoryOrderById(account.getId(), categoryID, CategorySortingMethod.getCSM(1));
CategorySortingMethod methodAfterModify = db.getCategoryOrderById(account.getId(), categoryID);
assertEquals(methodAfterModify, CategorySortingMethod.getCSM(1));
// delete the Node
db.deleteNote(noteID, DBStatus.VOID);
}
public static String getCurDate() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

View file

@ -49,6 +49,7 @@ import it.niedermann.owncloud.notes.model.DBStatus;
import it.niedermann.owncloud.notes.model.ISyncCallback;
import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.util.CategorySortingMethod;
import it.niedermann.owncloud.notes.util.NoteUtil;
import static it.niedermann.owncloud.notes.android.activity.EditNoteActivity.ACTION_SHORTCUT;
@ -957,4 +958,47 @@ public class NotesDatabase extends AbstractNotesDatabase {
null);
}
/**
* This function is used to get the sorting method of a category.
* The sorting method of the category can be used to decide
* to use which sorting method to show the notes for each categories.
*
* @param accountId The user accountID
* @param categoryId The category ID
* @return The sorting method in CategorySortingMethod enum format
*/
public CategorySortingMethod getCategoryOrderById(long accountId, long categoryId) {
validateAccountId(accountId);
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(table_category, new String[]{key_category_sorting_method},
key_category_id + " = ?", new String[]{String.valueOf(categoryId)},
null, null, null);
int orderIndex = 0;
while (cursor.moveToNext()) {
orderIndex = cursor.getInt(0);
}
return CategorySortingMethod.getCSM(orderIndex);
}
/**
* This method is used to modify the sorting method for one category.
* The user can determine use which sorting method to show the notes for a category.
* When the user changes the sorting method, this method should be called.
*
* @param accountId The user accountID
* @param categoryId The category ID
* @param sortingMethod The sorting method in CategorySortingMethod enum format
*/
public void modifyCategoryOrderById(
long accountId, long categoryId, CategorySortingMethod sortingMethod) {
validateAccountId(accountId);
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(key_category_sorting_method, sortingMethod.getCSMID());
db.update(table_category, values,
key_category_id + " = ?", new String[]{String.valueOf(categoryId)});
}
}