1 package com.yuji.ef.dao;
3 import java.util.ArrayList;
6 import android.database.Cursor;
7 import android.database.SQLException;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.database.sqlite.SQLiteStatement;
11 import com.yuji.ef.common.CommonUtil;
12 import com.yuji.ef.utility.Debug;
14 public class NodeDaoImpl implements IDao<Node>, NodeDao {
15 private static IDao<Node> instance = null;
16 private SQLiteStatement insertStmt = null;
17 private SQLiteStatement updateChildrenStmt = null;
18 private SQLiteStatement updateStatusStmt = null;
19 private SQLiteStatement updateNameStmt = null;
20 private SQLiteStatement updateGuidStmt = null;
21 private SQLiteStatement updateSelectedStmt = null;
22 private SQLiteStatement updateNoteFlagStmt = null;
23 private SQLiteStatement updateFlagStmt = null;
24 private SQLiteStatement updateParentStmt = null;
25 private SQLiteStatement deleteStmt = null;
26 private SQLiteStatement deleteIdStmt = null;
28 public static IDao<Node> getInstance() {
29 if (instance == null) {
30 instance = new NodeDaoImpl();
35 protected NodeDaoImpl() {
43 * com.yuji.ef.dao.NodeDao#onCreate(android.database.sqlite.SQLiteDatabase)
46 public void onCreate(SQLiteDatabase db) {
47 db.execSQL("CREATE TABLE Node (" + android.provider.BaseColumns._ID
48 + " INTEGER PRIMARY KEY AUTOINCREMENT," + "TYPE INTEGER,"
49 + "GUID TEXT," + "PARENT INTEGER," + "NAME TEXT,"
50 + "CHILDREN TEXT," + "STATUS INTEGER," + "SELECTED INTEGER,"
58 * @see com.yuji.ef.dao.NodeDao#init(android.database.sqlite.SQLiteDatabase)
61 public void init(SQLiteDatabase db) {
62 insertStmt = db.compileStatement("INSERT INTO Node (" + "TYPE,"
63 + "GUID," + "PARENT," + "NAME," + "CHILDREN," + "STATUS, " + "SELECTED"
64 + ") VALUES (" + "?,?,?,?,?,?,?" + ");");
65 updateChildrenStmt = db
66 .compileStatement("UPDATE Node SET CHILDREN = ? WHERE "
67 + android.provider.BaseColumns._ID + " = ?");
69 .compileStatement("UPDATE Node SET STATUS = ? WHERE "
70 + android.provider.BaseColumns._ID + " = ?");
71 updateNameStmt = db.compileStatement("UPDATE Node SET NAME = ? WHERE "
72 + android.provider.BaseColumns._ID + " = ?");
74 .compileStatement("UPDATE Node SET GUID = ?, TYPE = ? WHERE "
75 + android.provider.BaseColumns._ID + " = ?");
76 updateSelectedStmt = db
77 .compileStatement("UPDATE Node SET SELECTED = ? WHERE "
78 + android.provider.BaseColumns._ID + " = ?");
79 updateNoteFlagStmt = db
80 .compileStatement("UPDATE Node SET FLAG = ? WHERE "
83 .compileStatement("UPDATE Node SET FLAG = ? WHERE "
84 + android.provider.BaseColumns._ID + " = ?");
86 .compileStatement("UPDATE Node SET PARENT = ? WHERE "
87 + android.provider.BaseColumns._ID + " = ?");
88 deleteStmt = db.compileStatement("DELETE FROM Node");
89 deleteIdStmt = db.compileStatement("DELETE FROM Node WHERE "
90 + android.provider.BaseColumns._ID + " = ?");
97 * com.yuji.ef.dao.NodeDao#start(android.database.sqlite.SQLiteDatabase)
100 public void start(SQLiteDatabase db) {
107 * @see com.yuji.ef.dao.NodeDao#search()
110 public List<Node> search() {
111 return search(DatabaseHelper.getInstance().getSQLiteDatabase(), null,
118 * @see com.yuji.ef.dao.NodeDao#searchRoot()
121 public Node searchRoot() {
122 return searchRoot(DatabaseHelper.getInstance().getSQLiteDatabase());
129 * com.yuji.ef.dao.NodeDao#searchRoot(android.database.sqlite.SQLiteDatabase
133 public Node searchRoot(SQLiteDatabase db) {
134 String selection = "TYPE = ?";
135 String[] selectionArgs = { String.valueOf(Node.TYPE_ROOT) };
136 String orderBy = null;
137 List<Node> list = search(db, selection, selectionArgs, orderBy);
138 if (list.size() <= 0) {
145 public List<Node> searchNote() {
146 return searchNote(DatabaseHelper.getInstance().getSQLiteDatabase());
150 public List<Node> searchNote(SQLiteDatabase db) {
151 String selection = "TYPE = ?";
152 String[] selectionArgs = { String.valueOf(Node.TYPE_NOTE) };
153 String orderBy = null;
154 List<Node> list = search(db, selection, selectionArgs, orderBy);
159 public List<Node> searchBook() {
160 return searchBook(DatabaseHelper.getInstance().getSQLiteDatabase());
164 public List<Node> searchBook(SQLiteDatabase db) {
165 String selection = "TYPE = ?";
166 String[] selectionArgs = { String.valueOf(Node.TYPE_BOOK) };
167 String orderBy = "NAME";;
168 List<Node> list = search(db, selection, selectionArgs, orderBy);
173 public List<Node> searchSelectedBook() {
174 return searchSelectedBook(DatabaseHelper.getInstance()
175 .getSQLiteDatabase());
179 public List<Node> searchSelectedBook(SQLiteDatabase db) {
180 String selection = "TYPE = ? AND SELECTED = ?";
181 String[] selectionArgs = { String.valueOf(Node.TYPE_BOOK), "1" };
182 String orderBy = null;
183 List<Node> list = search(db, selection, selectionArgs, orderBy);
188 public List<Node> searchNoteByFlag(int flag) {
189 return searchNoteByFlag(DatabaseHelper.getInstance()
190 .getSQLiteDatabase(), flag);
194 public List<Node> searchNoteByFlag(SQLiteDatabase db, int flag) {
195 String selection = "TYPE = ? AND FLAG = ?";
196 String[] selectionArgs = { String.valueOf(Node.TYPE_NOTE),
197 String.valueOf(flag) };
198 String orderBy = null;
199 List<Node> list = search(db, selection, selectionArgs, orderBy);
206 * @see com.yuji.ef.dao.NodeDao#searchById(long)
209 public Node searchById(long id) {
210 return searchById(DatabaseHelper.getInstance().getSQLiteDatabase(), id);
217 * com.yuji.ef.dao.NodeDao#searchById(android.database.sqlite.SQLiteDatabase
221 public Node searchById(SQLiteDatabase db, long id) {
222 String selection = android.provider.BaseColumns._ID + " = ?";
223 String[] selectionArgs = { String.valueOf(id) };
224 String orderBy = null;
225 List<Node> list = search(db, selection, selectionArgs, orderBy);
226 if (list.size() <= 0) {
236 * com.yuji.ef.dao.NodeDao#searchByGuid(android.database.sqlite.SQLiteDatabase
237 * , java.lang.String)
240 public Node searchByGuid(SQLiteDatabase db, String guid) {
241 String selection = "GUID = ?";
242 String[] selectionArgs = { guid };
243 String orderBy = null;
244 List<Node> list = search(db, selection, selectionArgs, orderBy);
245 if (list.size() <= 0) {
251 private List<Node> search(SQLiteDatabase db, String selection,
252 String[] selectionArgs, String orderBy) {
253 List<Node> list = new ArrayList<Node>();
254 Cursor cursor = null;
256 NodeFactory factory = NodeFactory.getInstance();
258 cursor = db.query("Node", new String[] {
259 android.provider.BaseColumns._ID, "TYPE", "GUID", "PARENT",
260 "NAME", "CHILDREN", "STATUS", "SELECTED" }, selection,
261 selectionArgs, null, null, orderBy);
262 cursor.moveToFirst();
263 int size = cursor.getCount();
264 for (int i = 0; i < size; i++) {
266 // Node Node = new Node(
267 // cursor.getLong(0),
268 // cursor.getString(1),
269 // cursor.getString(2),
270 // cursor.getLong(3),
271 // cursor.getLong(4));
272 Node node = factory.create(cursor.getLong(0), cursor.getInt(1),
273 cursor.getString(2), cursor.getLong(3),
274 cursor.getString(4), cursor.getString(5),
275 cursor.getInt(6), (cursor.getInt(7) != 0));
279 } catch (SQLException e) {
280 Debug.d(this, null, e);
282 } catch (Exception e) {
283 Debug.d(this, null, e);
286 if (cursor != null) {
297 * @see com.yuji.ef.dao.NodeDao#isEmpty()
300 public boolean isEmpty() {
301 // List<Node> list = search();
302 // return list == null || list.size() <= 0;
303 return this.searchRoot() == null;
309 * @see com.yuji.ef.dao.NodeDao#add(com.yuji.ef.dao.Node)
312 public long add(Node node) {
313 DatabaseHelper helper = DatabaseHelper.getInstance();
314 SQLiteDatabase db = helper.getWritableDatabase();
315 return add(db, node);
318 private long add(SQLiteDatabase db, Node node) {
320 db.beginTransaction();
323 db.setTransactionSuccessful();
333 * @see com.yuji.ef.dao.NodeDao#addNT(com.yuji.ef.dao.Node)
336 public long addNT(Node node) {
339 SQLiteStatement stmt = insertStmt;
340 stmt.bindLong(i++, node.getType());
341 stmt.bindString(i++, CommonUtil.nz(node.getGuid()));
342 stmt.bindLong(i++, node.getParent());
343 stmt.bindString(i++, node.getName());
344 stmt.bindString(i++, CommonUtil.nz(node.getChildrenString()));
345 stmt.bindLong(i++, Node.getStatusCode(node.getStatus()));
346 stmt.bindLong(i++, node.isSelected()? 1 : 0);
347 id = stmt.executeInsert();
354 * @see com.yuji.ef.dao.NodeDao#addChildrenId(com.yuji.ef.dao.Node, long)
357 public long addChildrenId(Node node, long id) {
358 DatabaseHelper helper = DatabaseHelper.getInstance();
359 SQLiteDatabase db = helper.getWritableDatabase();
360 return addChildrenId(db, node, id);
363 private long addChildrenId(SQLiteDatabase db, Node node, long aid) {
365 db.beginTransaction();
367 id = addChildrenIdNT(node, aid);
368 db.setTransactionSuccessful();
378 * @see com.yuji.ef.dao.NodeDao#addChildrenIdNT(com.yuji.ef.dao.Node, long)
381 public long addChildrenIdNT(Node node, long id) {
382 List<Long> l = node.getChildren();
383 if (l.contains(id)) {
388 return updateChildrenNT(node, Node.concatChildren(l));
394 * @see com.yuji.ef.dao.NodeDao#updateChildrenNT(com.yuji.ef.dao.Node,
398 public long updateChildrenNT(Node node, String children) {
401 SQLiteStatement stmt = updateChildrenStmt;
402 //stmt.bindString(i++, node.getChildrenString());
403 stmt.bindString(i++, children);
404 stmt.bindLong(i++, node.getId());
405 id = stmt.executeInsert();
412 * @see com.yuji.ef.dao.NodeDao#remoteChildrenId(com.yuji.ef.dao.Node, long)
415 public long removeChildrenId(Node node, long id) {
416 DatabaseHelper helper = DatabaseHelper.getInstance();
417 SQLiteDatabase db = helper.getWritableDatabase();
418 return remoteChildrenId(db, node, id);
421 private long remoteChildrenId(SQLiteDatabase db, Node node, long aid) {
423 db.beginTransaction();
425 id = removeChildrenIdNT(node, aid);
426 db.setTransactionSuccessful();
436 * @see com.yuji.ef.dao.NodeDao#remoteChildrenIdNT(com.yuji.ef.dao.Node,
440 public long removeChildrenIdNT(Node node, long id) {
441 List<Long> l = node.getChildren();
442 if (!l.contains(id)) {
447 return updateChildrenNT(node, Node.concatChildren(l));
453 * @see com.yuji.ef.dao.NodeDao#updateStatus(com.yuji.ef.dao.Node,
454 * com.yuji.ef.dao.Node.Status)
457 public long updateStatus(Node node, Node.Status status) {
458 return updateStatus(DatabaseHelper.getInstance().getSQLiteDatabase(),
466 * com.yuji.ef.dao.NodeDao#updateStatus(android.database.sqlite.SQLiteDatabase
467 * , com.yuji.ef.dao.Node, com.yuji.ef.dao.Node.Status)
470 public long updateStatus(SQLiteDatabase db, Node node, Node.Status status) {
473 db.beginTransaction();
475 id = updateStatusNT(node, status);
476 db.setTransactionSuccessful();
486 * @see com.yuji.ef.dao.NodeDao#updateStatusNT(com.yuji.ef.dao.Node,
487 * com.yuji.ef.dao.Node.Status)
490 public long updateStatusNT(Node node, Node.Status status) {
493 int code = Node.getStatusCode(status);
495 SQLiteStatement stmt = updateStatusStmt;
496 stmt.bindLong(i++, code);
497 stmt.bindLong(i++, node.getId());
498 id = stmt.executeInsert();
505 * @see com.yuji.ef.dao.NodeDao#updateName(com.yuji.ef.dao.Node,
509 public long updateName(Node node, String name) {
510 return updateName(DatabaseHelper.getInstance().getSQLiteDatabase(),
518 * com.yuji.ef.dao.NodeDao#updateName(android.database.sqlite.SQLiteDatabase
519 * , com.yuji.ef.dao.Node, java.lang.String)
522 public long updateName(SQLiteDatabase db, Node node, String name) {
525 db.beginTransaction();
527 id = updateNameNT(node, name);
528 db.setTransactionSuccessful();
538 * @see com.yuji.ef.dao.NodeDao#updateNameNT(com.yuji.ef.dao.Node,
542 public long updateNameNT(Node node, String name) {
546 SQLiteStatement stmt = updateNameStmt;
547 stmt.bindString(i++, name);
548 stmt.bindLong(i++, node.getId());
549 id = stmt.executeInsert();
554 public long updateGuid(Node node, String guid) {
555 return updateGuid(DatabaseHelper.getInstance().getSQLiteDatabase(),
560 public long updateGuid(SQLiteDatabase db, Node node, String guid) {
563 db.beginTransaction();
565 id = updateGuid(node, guid);
566 db.setTransactionSuccessful();
574 public long updateGuidNT(Node node, String guid) {
578 SQLiteStatement stmt = updateGuidStmt;
579 if (CommonUtil.isNull(guid)) {
581 stmt.bindLong(i++, (node instanceof FileNode) ? Node.TYPE_NOTE
584 stmt.bindString(i++, guid);
585 stmt.bindLong(i++, (node instanceof FileNode) ? Node.TYPE_NOTE
588 stmt.bindLong(i++, node.getId());
589 id = stmt.executeInsert();
594 public long updateSelected(Node node, boolean isSelected) {
595 return updateSelected(DatabaseHelper.getInstance().getSQLiteDatabase(),
600 public long updateSelected(SQLiteDatabase db, Node node, boolean isSelected) {
603 db.beginTransaction();
605 id = updateSelectedNT(node, isSelected);
606 db.setTransactionSuccessful();
614 public long updateSelectedNT(Node node, boolean isSelected) {
618 SQLiteStatement stmt = updateSelectedStmt;
619 stmt.bindLong(i++, isSelected ? 1 : 0);
620 stmt.bindLong(i++, node.getId());
621 id = stmt.executeInsert();
628 * @see com.yuji.ef.dao.NodeDao#updateParent(com.yuji.ef.dao.Node, long)
631 public long updateParent(Node node, long parent) {
632 return updateParent(DatabaseHelper.getInstance().getSQLiteDatabase(),
640 * com.yuji.ef.dao.NodeDao#updateParent(android.database.sqlite.SQLiteDatabase
641 * , com.yuji.ef.dao.Node, long)
644 public long updateParent(SQLiteDatabase db, Node node, long parent) {
647 db.beginTransaction();
649 id = updateParentNT(node, parent);
650 db.setTransactionSuccessful();
660 * @see com.yuji.ef.dao.NodeDao#updateParentNT(com.yuji.ef.dao.Node, long)
663 public long updateParentNT(Node node, long parent) {
667 SQLiteStatement stmt = updateParentStmt;
668 stmt.bindLong(i++, parent);
669 stmt.bindLong(i++, node.getId());
670 id = stmt.executeInsert();
675 public long updateNoteFlag(int flag) {
676 return updateNoteFlag(DatabaseHelper.getInstance().getSQLiteDatabase(),
681 public long updateNoteFlag(SQLiteDatabase db, int flag) {
684 db.beginTransaction();
686 id = updateNoteFlagNT(flag);
687 db.setTransactionSuccessful();
695 public long updateNoteFlagNT(int flag) {
699 SQLiteStatement stmt = updateNoteFlagStmt;
700 stmt.bindLong(i++, flag);
701 stmt.bindLong(i++, Node.TYPE_NOTE);
702 id = stmt.executeInsert();
707 public long updateFlag(Node node, int flag) {
708 return updateFlag(DatabaseHelper.getInstance().getSQLiteDatabase(),
713 public long updateFlag(SQLiteDatabase db, Node node, int flag) {
716 db.beginTransaction();
718 id = updateFlagNT(node, flag);
719 db.setTransactionSuccessful();
727 public long updateFlagNT(Node node, int flag) {
731 SQLiteStatement stmt = updateFlagStmt;
732 stmt.bindLong(i++, flag);
733 stmt.bindLong(i++, node.getId());
734 id = stmt.executeInsert();
741 * @see com.yuji.ef.dao.NodeDao#updateChildren(com.yuji.ef.dao.Node,
745 public long updateChildren(Node node, String children) {
746 DatabaseHelper helper = DatabaseHelper.getInstance();
747 SQLiteDatabase db = helper.getWritableDatabase();
748 return updateChildren(db, node, children);
751 private long updateChildren(SQLiteDatabase db, Node node, String children) {
753 db.beginTransaction();
755 id = updateChildrenNT(node, children);
756 db.setTransactionSuccessful();
766 * @see com.yuji.ef.dao.NodeDao#delete(long)
769 public long delete(long id) {
770 DatabaseHelper helper = DatabaseHelper.getInstance();
771 SQLiteDatabase db = helper.getWritableDatabase();
772 return delete(db, id);
779 * com.yuji.ef.dao.NodeDao#delete(android.database.sqlite.SQLiteDatabase,
783 public long delete(SQLiteDatabase db, long did) {
786 db.beginTransaction();
789 db.setTransactionSuccessful();
799 * @see com.yuji.ef.dao.NodeDao#deleteNT(long)
802 public long deleteNT(long did) {
803 long id = -1; // TODO
806 SQLiteStatement stmt = deleteIdStmt;
807 stmt.bindLong(i++, did);
815 * @see com.yuji.ef.dao.NodeDao#delete()
818 public long delete() {
819 DatabaseHelper helper = DatabaseHelper.getInstance();
820 SQLiteDatabase db = helper.getWritableDatabase();
828 * com.yuji.ef.dao.NodeDao#delete(android.database.sqlite.SQLiteDatabase)
831 public long delete(SQLiteDatabase db) {
834 db.beginTransaction();
837 db.setTransactionSuccessful();
847 * @see com.yuji.ef.dao.NodeDao#deleteNT()
850 public void deleteNT() {
851 SQLiteStatement stmt = deleteStmt;