use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.
the class StructureTest method testPasteUnderFolded.
@Test
public void testPasteUnderFolded() throws IOException {
Book book = shelfTestUtils.setupBook("notebook", "" + "description\n" + "\n" + "* Note 1\n" + "** Note 1.1\n" + "*** Note 1.1.1\n" + "*** Note 1.1.2\n" + "** Note 1.2\n" + "* Note 2\n");
/* Cut & paste 2 under folded 1.1. */
shelf.cut(book.getId(), shelf.getNote("Note 2").getId());
shelf.toggleFoldedState(shelf.getNote("Note 1.1").getId());
Note n = shelf.getNote("Note 1.1");
shelf.paste(n.getPosition().getBookId(), n.getId(), Place.UNDER);
assertEquals("description\n" + "\n" + "* Note 1\n" + "** Note 1.1\n" + "*** Note 1.1.1\n" + "*** Note 1.1.2\n" + "*** Note 2\n" + "** Note 1.2\n", shelf.getBookContent("notebook", BookName.Format.ORG));
NotePosition n1 = shelf.getNote("Note 1").getPosition();
NotePosition n11 = shelf.getNote("Note 1.1").getPosition();
NotePosition n111 = shelf.getNote("Note 1.1.1").getPosition();
NotePosition n112 = shelf.getNote("Note 1.1.2").getPosition();
NotePosition n2 = shelf.getNote("Note 2").getPosition();
NotePosition n12 = shelf.getNote("Note 1.2").getPosition();
assertEquals(shelf.getNote("Note 1.1").getId(), n2.getParentId());
assertEquals(1, n1.getLevel());
assertEquals(2, n11.getLevel());
assertEquals(3, n111.getLevel());
assertEquals(3, n112.getLevel());
assertEquals(3, n2.getLevel());
assertEquals(2, n12.getLevel());
assertTrue(n1.getLft() < n11.getLft());
assertTrue(n11.getLft() < n111.getLft());
assertTrue(n111.getLft() < n111.getRgt());
assertTrue(n111.getLft() < n111.getRgt());
assertTrue(n111.getRgt() < n112.getLft());
assertTrue(n112.getLft() < n112.getRgt());
assertTrue(n112.getRgt() < n2.getLft());
assertTrue(n2.getLft() < n2.getRgt());
assertTrue(n2.getRgt() < n11.getRgt());
assertTrue(n11.getRgt() < n12.getLft());
assertTrue(n12.getLft() < n12.getRgt());
assertTrue(n12.getRgt() < n1.getRgt());
assertEquals(5, n1.getDescendantsCount());
assertEquals(3, n11.getDescendantsCount());
assertEquals(0, n111.getDescendantsCount());
assertEquals(0, n112.getDescendantsCount());
assertEquals(0, n2.getDescendantsCount());
assertEquals(0, n12.getDescendantsCount());
assertEquals(0, n1.getFoldedUnderId());
assertEquals(0, n11.getFoldedUnderId());
assertEquals(shelf.getNote("Note 1.1").getId(), n111.getFoldedUnderId());
assertEquals(shelf.getNote("Note 1.1").getId(), n112.getFoldedUnderId());
assertEquals(shelf.getNote("Note 1.1").getId(), n2.getFoldedUnderId());
assertEquals(0, n12.getFoldedUnderId());
}
use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.
the class StructureTest method testNewNoteUnder.
@Test
public void testNewNoteUnder() throws IOException {
Book book = shelfTestUtils.setupBook("notebook", "description\n" + "* Note 1\n" + "** Note 1.1\n" + "*** Note 1.1.1\n" + "** Note 1.2\n");
NotePosition note1, note11, note111, note12, note112;
note1 = shelf.getNote("Note 1").getPosition();
note11 = shelf.getNote("Note 1.1").getPosition();
note111 = shelf.getNote("Note 1.1.1").getPosition();
note12 = shelf.getNote("Note 1.2").getPosition();
assertTrue(note1.getLft() < note11.getLft());
assertTrue(note11.getLft() < note111.getLft());
assertTrue(note111.getLft() < note111.getRgt());
assertTrue(note111.getRgt() < note11.getRgt());
assertTrue(note11.getRgt() < note12.getLft());
assertTrue(note12.getLft() < note12.getRgt());
assertTrue(note12.getRgt() < note1.getRgt());
assertEquals(3, note1.getDescendantsCount());
assertEquals(1, note11.getDescendantsCount());
assertEquals(0, note111.getDescendantsCount());
assertEquals(0, note12.getDescendantsCount());
/* Create new note under Note 1.1. */
Note n = new Note();
n.getPosition().setBookId(book.getId());
n.getHead().setTitle("Note 1.1.2");
NotePlace target = new NotePlace(book.getId(), shelf.getNote("Note 1.1").getId(), Place.UNDER);
shelf.createNote(n, target);
note1 = shelf.getNote("Note 1").getPosition();
note11 = shelf.getNote("Note 1.1").getPosition();
note111 = shelf.getNote("Note 1.1.1").getPosition();
note112 = shelf.getNote("Note 1.1.2").getPosition();
note12 = shelf.getNote("Note 1.2").getPosition();
assertTrue(note1.getLft() < note11.getLft());
assertTrue(note11.getLft() < note111.getLft());
assertTrue(note111.getLft() < note111.getRgt());
assertTrue(note111.getRgt() < note112.getLft());
assertTrue(note112.getLft() < note112.getRgt());
assertTrue(note112.getRgt() < note11.getRgt());
assertTrue(note11.getRgt() < note12.getLft());
assertTrue(note12.getLft() < note12.getRgt());
assertTrue(note12.getRgt() < note1.getRgt());
assertEquals(4, note1.getDescendantsCount());
assertEquals(2, note11.getDescendantsCount());
assertEquals(0, note111.getDescendantsCount());
assertEquals(0, note112.getDescendantsCount());
assertEquals(0, note12.getDescendantsCount());
}
use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.
the class PasteNotesAction method getLastHighestLevelDescendant.
private NotePosition getLastHighestLevelDescendant(SQLiteDatabase db, NotePosition note) {
NotePosition position = null;
Cursor cursor = db.query(DbNote.TABLE, DbNote.POSITION_PROJECTION, DatabaseUtils.whereDescendants(note.getBookId(), note.getLft(), note.getRgt()), null, null, null, DbNote.LEVEL + ", " + DbNote.LFT + " DESC");
try {
if (cursor.moveToFirst()) {
position = DbNote.positionFromCursor(cursor);
}
} finally {
cursor.close();
}
return position;
}
use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.
the class PromoteNotesAction method promote.
private int promote(SQLiteDatabase db) {
Cursor cursor;
NotePosition note;
long parent;
ContentValues values;
/* Get note info. */
cursor = db.query(DbNote.TABLE, null, DbNote._ID + " IN (" + ids + ")", null, null, null, null);
try {
if (cursor.moveToFirst()) {
note = DbNote.positionFromCursor(cursor);
} else {
return 0;
}
} finally {
cursor.close();
}
/* Can't promote top level note. */
if (note.getLevel() <= 1 || note.getParentId() <= 0) {
return 0;
}
/* Delete affected notes from ancestors table. */
String w = "(SELECT " + DbNote._ID + " FROM " + DbNote.TABLE + " WHERE " + DatabaseUtils.whereDescendantsAndNotes(bookId, ids) + ")";
String sql = "DELETE FROM " + DbNoteAncestor.TABLE + " WHERE " + DbNoteAncestor.NOTE_ID + " IN " + w;
if (BuildConfig.LOG_DEBUG)
LogUtils.d("SQL", sql);
db.execSQL(sql);
/* Cut note and all its descendants. */
long batchId = System.currentTimeMillis();
values = new ContentValues();
values.put(DbNote.IS_CUT, batchId);
db.update(DbNote.TABLE, values, DatabaseUtils.whereDescendantsAndNote(bookId, note.getLft(), note.getRgt()), null);
/* Paste below parent. */
values = new ContentValues();
values.put(ProviderContract.Paste.Param.BATCH_ID, batchId);
values.put(ProviderContract.Paste.Param.NOTE_ID, note.getParentId());
values.put(ProviderContract.Paste.Param.SPOT, Place.BELOW.toString());
new PasteNotesAction(values).run(db);
DatabaseUtils.updateBookMtime(db, bookId);
return 1;
}
use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.
the class ProviderTest method testCreatingNotes.
@Test
public void testCreatingNotes() throws IOException {
Book book = shelfTestUtils.setupBook("notebook", "* Note");
NotePosition root, note1, note2;
note1 = NotesClient.getNote(context, 1).getPosition();
root = NotesClient.getNote(context, 2).getPosition();
assertEquals(1, NotesClient.getCount(context, book.getId()));
assertNotNull(root);
assertEquals(1, root.getLft());
assertEquals(6, note1.getLft());
assertEquals(11, note1.getRgt());
assertEquals(16, root.getRgt());
Note note = new Note();
note.getPosition().setBookId(book.getId());
NotesClient.create(context, note, null, System.currentTimeMillis());
note1 = NotesClient.getNote(context, 1).getPosition();
root = NotesClient.getNote(context, 2).getPosition();
note2 = NotesClient.getNote(context, 3).getPosition();
assertEquals(2, NotesClient.getCount(context, book.getId()));
assertNotNull(note2);
assertTrue(root.getLft() < note1.getLft());
assertTrue(note1.getLft() < note1.getRgt());
assertTrue(note1.getRgt() < note2.getLft());
assertTrue(note2.getLft() < note2.getRgt());
assertTrue(note2.getRgt() < root.getRgt());
}
Aggregations