import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
-import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import com.yuji.ef.dao.LockDao;
import com.yuji.ef.dao.Node;
import com.yuji.ef.dao.Node.Status;
+import com.yuji.ef.dao.NodeCacheDao;
import com.yuji.ef.dao.NodeComparator;
import com.yuji.ef.dao.NodeDao;
+import com.yuji.ef.dao.NodeDaoImpl;
import com.yuji.ef.dao.RootNode;
import com.yuji.ef.utility.BaseActivity;
-import com.yuji.ef.utility.Debug;
import com.yuji.ef.utility.EvernoteIntentUtil;
import com.yuji.ef.utility.FolderUtil;
import com.yuji.ef.utility.ScaleUtil;
if (status == ScreenStatus.Normal) {
LabelIconView target = layout.getSelectedTarget();
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
long id = -1;
Node node = null;
if (target != null) {
}
private void dummy() {
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDaoImpl dao = (NodeDaoImpl) NodeCacheDao.getInstance();
List<Node> list = dao.search();
for (Node node : list) {
System.out.println(node.toString());
FolderUtil util = FolderUtil.getInstance();
LabelIconView target = layout.getSelectedTarget();
long id = target.getNodeId();
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
Node node = dao.searchById(id);
if (node != null) {
util.deleteNode(node);
}
long id = target.getNodeId();
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
Node node = dao.searchById(id);
if (node != null) {
String text = node.getName();
}
private void updateList() {
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
top = dao.searchRoot();
if (top == null) {
// TODO
}
List<Node> list = new ArrayList<Node>();
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
for (Long id : idList) {
Node n = dao.searchById(id);
list.add(n);
private void msgOkButtonOnClick() {
FolderUtil util = FolderUtil.getInstance();
LabelIconView target = layout.getSelectedTarget();
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
if (status == ScreenStatus.AddNode) {
long id = target.getNodeId();
try {
removeAllNodeView();
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
if (srcId < 0) {
// TODO
.getSQLiteDatabase();
db.beginTransaction();
try {
- dao.remoteChildrenIdNT(sp, src.getId());
+ dao.removeChildrenIdNT(sp, src.getId());
if (dst instanceof DirNode) {
// dst.add(src.getId());
dao.updateParentNT(src, dst.getId());
}
private boolean isMovable(long srcId, long dstId) {
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
while (dstId >= 0) {
if (srcId == dstId) {
}
public void execute(long id) {
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
Node node = dao.searchById(id);
if (node == null) {
}
public void executeView(long id) {
- NodeDao dao = (NodeDao) NodeDao.getInstance();
+ NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
Node node = dao.searchById(id);
String guid = node.getGuid();
return;
}
daoList = new ArrayList<IDao<?>>();
- daoList.add(NodeDao.getInstance());
+ daoList.add(NodeCacheDao.getInstance());
daoList.add(BookDao.getInstance());
daoList.add(LockDao.getInstance());
--- /dev/null
+package com.yuji.ef.dao;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import android.database.sqlite.SQLiteDatabase;
+
+import com.yuji.ef.dao.Node.Status;
+
+public class NodeCacheDao extends NodeDaoImpl implements IDao<Node>, NodeDao {
+ private static final int MAP_SIZE = 8;
+
+ private static IDao<Node> instance = null;
+ //private static NodeDao dao = (NodeDao) NodeDaoImpl.getInstance();
+ private static LinkedHashMap<Long, Node> map = new LinkedHashMap<Long, Node>(
+ MAP_SIZE, (float) 0.75, true) {
+ @Override
+ protected boolean removeEldestEntry(Map.Entry eldest) {
+ return size() > MAP_SIZE;
+ }
+ };
+
+ public static IDao<Node> getInstance() {
+ if (instance == null) {
+ instance = new NodeCacheDao();
+ }
+ return instance;
+ }
+
+ private NodeCacheDao() {
+
+ }
+
+// @Override
+// public void onCreate(SQLiteDatabase db) {
+// dao.onCreate(db);
+// }
+//
+// @Override
+// public void init(SQLiteDatabase db) {
+// dao.init(db);
+// }
+//
+// @Override
+// public void start(SQLiteDatabase db) {
+// dao.start(db);
+// }
+//
+// @Override
+// public void start2(SQLiteDatabase db) {
+// dao.start2(db);
+// }
+//
+// @Override
+// public List<Node> search() {
+// return dao.search();
+// }
+//
+// @Override
+// public Node searchRoot() {
+// return dao.searchRoot();
+// }
+//
+// @Override
+// public Node searchRoot(SQLiteDatabase db) {
+// return dao.searchRoot(db);
+// }
+//
+// @Override
+// public Node searchById(long id) {
+// return dao.searchById(id);
+// }
+
+ @Override
+ public Node searchById(SQLiteDatabase db, long id) {
+ Node n = map.get(id);
+ if (n != null) {
+ return n;
+ }
+ n = super.searchById(db, id);
+ map.put(id, n);
+ return n;
+ }
+
+// @Override
+// public Node searchByGuid(SQLiteDatabase db, String guid) {
+// return dao.searchByGuid(db, guid);
+// }
+//
+// @Override
+// public boolean isEmpty() {
+// return dao.isEmpty();
+// }
+//
+// @Override
+// public long add(Node node) {
+// return dao.add(node);
+// }
+//
+// @Override
+// public long addNT(Node node) {
+// return dao.addNT(node);
+// }
+//
+// @Override
+// public long addChildrenId(Node node, long id) {
+// return dao.addChildrenId(node, id);
+// }
+//
+// @Override
+// public long addChildrenIdNT(Node node, long id) {
+// return dao.addChildrenIdNT(node, id);
+// }
+
+ @Override
+ public long updateChildrenNT(Node node, String children) {
+ map.remove(node.getId());
+ return super.updateChildrenNT(node, children);
+ }
+
+// @Override
+// public long removeChildrenId(Node node, long id) {
+// return dao.removeChildrenId(node, id);
+// }
+
+ @Override
+ public long removeChildrenIdNT(Node node, long id) {
+ map.remove(node.getId());
+ return super.removeChildrenIdNT(node, id);
+ }
+
+// @Override
+// public long updateStatus(Node node, Status status) {
+// return dao.updateStatus(node, status);
+// }
+//
+// @Override
+// public long updateStatus(SQLiteDatabase db, Node node, Status status) {
+// return dao.updateStatus(db, node, status);
+// }
+
+ @Override
+ public long updateStatusNT(Node node, Status status) {
+ map.remove(node.getId());
+ return super.updateStatusNT(node, status);
+ }
+
+// @Override
+// public long updateName(Node node, String name) {
+// return dao.updateName(node, name);
+// }
+//
+// @Override
+// public long updateName(SQLiteDatabase db, Node node, String name) {
+// return dao.updateName(db, node, name);
+// }
+
+ @Override
+ public long updateNameNT(Node node, String name) {
+ map.remove(node.getId());
+ return super.updateNameNT(node, name);
+ }
+
+// @Override
+// public long updateParent(Node node, long parent) {
+// return dao.updateParent(node, parent);
+// }
+//
+// @Override
+// public long updateParent(SQLiteDatabase db, Node node, long parent) {
+// return dao.updateParent(db, node, parent);
+// }
+
+ @Override
+ public long updateParentNT(Node node, long parent) {
+ map.remove(node.getId());
+ return super.updateParentNT(node, parent);
+ }
+
+// @Override
+// public long updateChildren(Node node, String children) {
+// return dao.updateChildren(node, children);
+// }
+//
+// @Override
+// public long delete(long id) {
+// return dao.delete(id);
+// }
+//
+// @Override
+// public long delete(SQLiteDatabase db, long did) {
+// return dao.delete(db, did);
+// }
+
+ @Override
+ public long deleteNT(long did) {
+ map.remove(did);
+ return super.deleteNT(did);
+ }
+
+// @Override
+// public long delete() {
+// return dao.delete();
+// }
+//
+// @Override
+// public long delete(SQLiteDatabase db) {
+// return dao.delete(db);
+// }
+
+ @Override
+ public void deleteNT() {
+ map.clear();
+ super.deleteNT();
+ }
+}
package com.yuji.ef.dao;
-import java.util.ArrayList;
import java.util.List;
-import android.database.Cursor;
-import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
-import android.database.sqlite.SQLiteStatement;
-import com.yuji.ef.common.CommonUtil;
-import com.yuji.ef.utility.Debug;
-import com.yuji.ef.utility.FolderUtil;
+public interface NodeDao {
-public class NodeDao implements IDao<Node> {
- private static IDao<Node> instance = null;
public static final String DELM = "\t";
- private SQLiteStatement insertStmt = null;
- private SQLiteStatement updateChildrenStmt = null;
- private SQLiteStatement updateStatusStmt = null;
- private SQLiteStatement updateNameStmt = null;
- private SQLiteStatement updateParentStmt = null;
- private SQLiteStatement deleteStmt = null;
- private SQLiteStatement deleteIdStmt = null;
-
- public static IDao<Node> getInstance() {
- if (instance == null) {
- instance = new NodeDao();
- }
- return instance;
- }
-
- private NodeDao() {
-
- }
-
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE Node (" + android.provider.BaseColumns._ID
- + " INTEGER PRIMARY KEY AUTOINCREMENT," + "TYPE INTEGER,"
- + "GUID TEXT," + "PARENT INTEGER," + "NAME TEXT,"
- + "CHILDREN TEXT," + "STATUS INTEGER" + ");");
- }
-
- public void init(SQLiteDatabase db) {
- insertStmt = db.compileStatement("INSERT INTO Node (" + "TYPE,"
- + "GUID," + "PARENT," + "NAME," + "CHILDREN," + "STATUS"
- + ") VALUES (" + "?,?,?,?,?,?" + ");");
- updateChildrenStmt = db
- .compileStatement("UPDATE Node SET CHILDREN = ? WHERE "
- + android.provider.BaseColumns._ID + " = ?");
- updateStatusStmt = db
- .compileStatement("UPDATE Node SET STATUS = ? WHERE "
- + android.provider.BaseColumns._ID + " = ?");
- updateNameStmt = db.compileStatement("UPDATE Node SET NAME = ? WHERE "
- + android.provider.BaseColumns._ID + " = ?");
- updateParentStmt = db
- .compileStatement("UPDATE Node SET PARENT = ? WHERE "
- + android.provider.BaseColumns._ID + " = ?");
- deleteStmt = db.compileStatement("DELETE FROM Node");
- deleteIdStmt = db.compileStatement("DELETE FROM Node WHERE "
- + android.provider.BaseColumns._ID + " = ?");
- }
-
- public void start(SQLiteDatabase db) {
- FolderUtil util = FolderUtil.getInstance();
- util.init(db);
- }
-
- public void start2(SQLiteDatabase db) {
- // top = new DirNode(null, null);
- // top.setStatus(Node.Status.OPEN);
- //
- // Node node;
- // node = new DirNode("ディレクトリ", null);
- // top.add(node);
- // node.add(new FileNode("ファイルaaa", null));
- // node.add(new FileNode("ファイルbb", null));
- // node.add(new FileNode("ファイルc", null));
- //
- // node = new DirNode("ディレクトリAAA", null);
- // top.add(node);
- // node.add(new DirNode("directory qqqq", null));
- // node.add(new FileNode("ファイルdddddddd", null));
- // node.add(new FileNode("ファイルeee", null));
- //
- // node = new FileNode("ファイルzzz", null);
- // top.add(node);
-
- Node node;
- Node n;
- long id;
- Node top = new RootNode("", null);
- id = addNT(top);
- top.setId(id);
-
- // NODE
- node = new DirNode("ディレクトリ", null);
- node.setParent(top.getId());
- id = addNT(node);
- node.setId(id);
- addChildrenIdNT(top, node.getId());
- // top.add(node.getId());
-
- // SUB
- n = new FileNode("ファイルaaa", null);
- n.setParent(node.getId());
- id = addNT(n);
- addChildrenIdNT(node, id);
- // node.add(n.getId());
- n = new FileNode("ファイルbb", null);
- n.setParent(node.getId());
- id = addNT(n);
- addChildrenIdNT(node, id);
- // node.add(n.getId());
- n = new FileNode("ファイルc", null);
- n.setParent(node.getId());
- id = addNT(n);
- addChildrenIdNT(node, id);
- // node.add(n.getId());
-
- // NODE
- node = new DirNode("ディレクトリAAA", null);
- node.setParent(top.getId());
- id = addNT(node);
- addChildrenIdNT(top, id);
- // top.add(node.getId());
-
- // SUB
- n = new DirNode("ディレクトリ", null);
- n.setParent(node.getId());
- id = addNT(n);
- addChildrenIdNT(node, id);
- // node.add(n.getId());
- n = new FileNode("yyyファイル", null);
- n.setParent(node.getId());
- id = addNT(n);
- addChildrenIdNT(node, id);
- // node.add(n.getId());
-
- // NODE
- node = new FileNode("ファイルあいうえお", null);
- node.setParent(top.getId());
- id = addNT(node);
- addChildrenIdNT(top, id);
- // top.add(node.getId());
- }
-
- public List<Node> search() {
- return search(DatabaseHelper.getInstance().getSQLiteDatabase(), null,
- null, null);
- }
-
- public Node searchRoot() {
- return searchRoot(DatabaseHelper.getInstance().getSQLiteDatabase());
- }
-
- public Node searchRoot(SQLiteDatabase db) {
- String selection = "TYPE = ?";
- String[] selectionArgs = { "0" };
- String orderBy = null;
- List<Node> list = search(db, selection, selectionArgs, orderBy);
- if (list.size() <= 0) {
- return null;
- }
- return list.get(0);
- }
-
- public Node searchById(long id) {
- return searchById(DatabaseHelper.getInstance().getSQLiteDatabase(), id);
- }
-
- public Node searchById(SQLiteDatabase db, long id) {
- String selection = android.provider.BaseColumns._ID + " = ?";
- String[] selectionArgs = { String.valueOf(id) };
- String orderBy = null;
- List<Node> list = search(db, selection, selectionArgs, orderBy);
- if (list.size() <= 0) {
- return null;
- }
- return list.get(0);
- }
-
- public Node searchByGuid(SQLiteDatabase db, String guid) {
- String selection = "GUID = ?";
- String[] selectionArgs = { guid };
- String orderBy = null;
- List<Node> list = search(db, selection, selectionArgs, orderBy);
- if (list.size() <= 0) {
- return null;
- }
- return list.get(0);
- }
-
- private List<Node> search(SQLiteDatabase db, String selection,
- String[] selectionArgs, String orderBy) {
- List<Node> list = new ArrayList<Node>();
- Cursor cursor = null;
- try {
- NodeFactory factory = NodeFactory.getInstance();
-
- cursor = db.query("Node", new String[] {
- android.provider.BaseColumns._ID, "TYPE", "GUID", "PARENT",
- "NAME", "CHILDREN", "STATUS" }, selection, selectionArgs,
- null, null, orderBy);
- cursor.moveToFirst();
- int size = cursor.getCount();
- for (int i = 0; i < size; i++) {
- // TODO Factory
- // Node Node = new Node(
- // cursor.getLong(0),
- // cursor.getString(1),
- // cursor.getString(2),
- // cursor.getLong(3),
- // cursor.getLong(4));
- Node node = factory.create(cursor.getLong(0), cursor.getInt(1),
- cursor.getString(2), cursor.getLong(3),
- cursor.getString(4), cursor.getString(5),
- cursor.getInt(6));
- list.add(node);
- cursor.moveToNext();
- }
- } catch (SQLException e) {
- Debug.d(this, null, e);
- list = null;
- } catch (Exception e) {
- Debug.d(this, null, e);
- list = null;
- } finally {
- if (cursor != null) {
- cursor.close();
- cursor = null;
- }
- }
- return list;
- }
-
- public boolean isEmpty() {
- List<Node> list = search();
- return list == null || list.size() <= 0;
- }
-
- public long add(Node node) {
- DatabaseHelper helper = DatabaseHelper.getInstance();
- SQLiteDatabase db = helper.getWritableDatabase();
- return add(db, node);
- }
-
- private long add(SQLiteDatabase db, Node node) {
- long id = -1;
- db.beginTransaction();
- try {
- id = addNT(node);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long addNT(Node node) {
- long id = -1;
- int i = 1;
- SQLiteStatement stmt = insertStmt;
- stmt.bindLong(i++, node.getType());
- stmt.bindString(i++, CommonUtil.nz(node.getGuid()));
- stmt.bindLong(i++, node.getParent());
- stmt.bindString(i++, node.getName());
- stmt.bindString(i++, CommonUtil.nz(node.getChildrenString()));
- stmt.bindLong(i++, Node.getStatusCode(node.getStatus()));
- id = stmt.executeInsert();
- return id;
- }
-
- public long addChildrenId(Node node, long id) {
- DatabaseHelper helper = DatabaseHelper.getInstance();
- SQLiteDatabase db = helper.getWritableDatabase();
- return addChildrenId(db, node, id);
- }
-
- private long addChildrenId(SQLiteDatabase db, Node node, long aid) {
- long id = -1;
- db.beginTransaction();
- try {
- id = addChildrenIdNT(node, aid);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long addChildrenIdNT(Node node, long id) {
- List<Long> l = node.getChildren();
- if (l.contains(id)) {
- return -1;
- }
- l.add(id);
- // node.add(id);
- return updateChildrenNT(node, Node.concatChildren(l));
- }
-
- public long updateChildrenNT(Node node, String children) {
- long id = -1;
- int i = 1;
- SQLiteStatement stmt = updateChildrenStmt;
- stmt.bindString(i++, node.getChildrenString());
- stmt.bindLong(i++, node.getId());
- id = stmt.executeInsert();
- return id;
- }
-
- public long remoteChildrenId(Node node, long id) {
- DatabaseHelper helper = DatabaseHelper.getInstance();
- SQLiteDatabase db = helper.getWritableDatabase();
- return remoteChildrenId(db, node, id);
- }
-
- private long remoteChildrenId(SQLiteDatabase db, Node node, long aid) {
- long id = -1;
- db.beginTransaction();
- try {
- id = remoteChildrenIdNT(node, aid);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long remoteChildrenIdNT(Node node, long id) {
- List<Long> l = node.getChildren();
- if (!l.contains(id)) {
- return -1;
- }
- l.remove(id);
- // node.add(id);
- return updateChildrenNT(node, Node.concatChildren(l));
- }
-
- public long updateStatus(Node node, Node.Status status) {
- return updateStatus(DatabaseHelper.getInstance().getSQLiteDatabase(),
- node, status);
- }
-
- public long updateStatus(SQLiteDatabase db, Node node, Node.Status status) {
- long id = -1;
-
- db.beginTransaction();
- try {
- id = updateStatusNT(node, status);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long updateStatusNT(Node node, Node.Status status) {
- long id = -1;
- int i = 1;
- int code = Node.getStatusCode(status);
-
- SQLiteStatement stmt = updateStatusStmt;
- stmt.bindLong(i++, code);
- stmt.bindLong(i++, node.getId());
- id = stmt.executeInsert();
- return id;
- }
-
- public long updateName(Node node, String name) {
- return updateName(DatabaseHelper.getInstance().getSQLiteDatabase(),
- node, name);
- }
-
- public long updateName(SQLiteDatabase db, Node node, String name) {
- long id = -1;
-
- db.beginTransaction();
- try {
- id = updateNameNT(node, name);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long updateNameNT(Node node, String name) {
- long id = -1;
- int i = 1;
-
- SQLiteStatement stmt = updateNameStmt;
- stmt.bindString(i++, name);
- stmt.bindLong(i++, node.getId());
- id = stmt.executeInsert();
- return id;
- }
-
- public long updateParent(Node node, long parent) {
- return updateParent(DatabaseHelper.getInstance().getSQLiteDatabase(),
- node, parent);
- }
-
- public long updateParent(SQLiteDatabase db, Node node, long parent) {
- long id = -1;
-
- db.beginTransaction();
- try {
- id = updateParentNT(node, parent);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long updateParentNT(Node node, long parent) {
- long id = -1;
- int i = 1;
-
- SQLiteStatement stmt = updateParentStmt;
- stmt.bindLong(i++, parent);
- stmt.bindLong(i++, node.getId());
- id = stmt.executeInsert();
- return id;
- }
-
- public long updateChildren(Node node, String children) {
- DatabaseHelper helper = DatabaseHelper.getInstance();
- SQLiteDatabase db = helper.getWritableDatabase();
- return updateChildren(db, node, children);
- }
-
- private long updateChildren(SQLiteDatabase db, Node node, String children) {
- long id = -1;
- db.beginTransaction();
- try {
- id = updateChildrenNT(node, children);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long delete(long id) {
- DatabaseHelper helper = DatabaseHelper.getInstance();
- SQLiteDatabase db = helper.getWritableDatabase();
- return delete(db, id);
- }
-
- public long delete(SQLiteDatabase db, long did) {
- long id = -1;
-
- db.beginTransaction();
- try {
- id = deleteNT(did);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public long deleteNT(long did) {
- long id = -1; // TODO
- int i = 1;
-
- SQLiteStatement stmt = deleteIdStmt;
- stmt.bindLong(i++, did);
- stmt.execute();
- return id;
- }
-
- public long delete() {
- DatabaseHelper helper = DatabaseHelper.getInstance();
- SQLiteDatabase db = helper.getWritableDatabase();
- return delete(db);
- }
-
- public long delete(SQLiteDatabase db) {
- long id = -1;
-
- db.beginTransaction();
- try {
- deleteNT();
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- return id;
- }
-
- public void deleteNT() {
- SQLiteStatement stmt = deleteStmt;
- stmt.execute();
- }
-}
+
+ public abstract void onCreate(SQLiteDatabase db);
+
+ public abstract void init(SQLiteDatabase db);
+
+ public abstract void start(SQLiteDatabase db);
+
+ public abstract void start2(SQLiteDatabase db);
+
+ public abstract List<Node> search();
+
+ public abstract Node searchRoot();
+
+ public abstract Node searchRoot(SQLiteDatabase db);
+
+ public abstract Node searchById(long id);
+
+ public abstract Node searchById(SQLiteDatabase db, long id);
+
+ public abstract Node searchByGuid(SQLiteDatabase db, String guid);
+
+ public abstract boolean isEmpty();
+
+ public abstract long add(Node node);
+
+ public abstract long addNT(Node node);
+
+ public abstract long addChildrenId(Node node, long id);
+
+ public abstract long addChildrenIdNT(Node node, long id);
+
+ public abstract long updateChildrenNT(Node node, String children);
+
+ public abstract long removeChildrenId(Node node, long id);
+
+ public abstract long removeChildrenIdNT(Node node, long id);
+
+ public abstract long updateStatus(Node node, Node.Status status);
+
+ public abstract long updateStatus(SQLiteDatabase db, Node node,
+ Node.Status status);
+
+ public abstract long updateStatusNT(Node node, Node.Status status);
+
+ public abstract long updateName(Node node, String name);
+
+ public abstract long updateName(SQLiteDatabase db, Node node, String name);
+
+ public abstract long updateNameNT(Node node, String name);
+
+ public abstract long updateParent(Node node, long parent);
+
+ public abstract long updateParent(SQLiteDatabase db, Node node, long parent);
+
+ public abstract long updateParentNT(Node node, long parent);
+
+ public abstract long updateChildren(Node node, String children);
+
+ public abstract long delete(long id);
+
+ public abstract long delete(SQLiteDatabase db, long did);
+
+ public abstract long deleteNT(long did);
+
+ public abstract long delete();
+
+ public abstract long delete(SQLiteDatabase db);
+
+ public abstract void deleteNT();
+
+}
\ No newline at end of file
--- /dev/null
+package com.yuji.ef.dao;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import android.database.Cursor;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteStatement;
+
+import com.yuji.ef.common.CommonUtil;
+import com.yuji.ef.utility.Debug;
+import com.yuji.ef.utility.FolderUtil;
+
+public class NodeDaoImpl implements IDao<Node>, NodeDao {
+ private static IDao<Node> instance = null;
+ private SQLiteStatement insertStmt = null;
+ private SQLiteStatement updateChildrenStmt = null;
+ private SQLiteStatement updateStatusStmt = null;
+ private SQLiteStatement updateNameStmt = null;
+ private SQLiteStatement updateParentStmt = null;
+ private SQLiteStatement deleteStmt = null;
+ private SQLiteStatement deleteIdStmt = null;
+
+ public static IDao<Node> getInstance() {
+ if (instance == null) {
+ instance = new NodeDaoImpl();
+ }
+ return instance;
+ }
+
+ protected NodeDaoImpl() {
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#onCreate(android.database.sqlite.SQLiteDatabase)
+ */
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ db.execSQL("CREATE TABLE Node (" + android.provider.BaseColumns._ID
+ + " INTEGER PRIMARY KEY AUTOINCREMENT," + "TYPE INTEGER,"
+ + "GUID TEXT," + "PARENT INTEGER," + "NAME TEXT,"
+ + "CHILDREN TEXT," + "STATUS INTEGER" + ");");
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#init(android.database.sqlite.SQLiteDatabase)
+ */
+ @Override
+ public void init(SQLiteDatabase db) {
+ insertStmt = db.compileStatement("INSERT INTO Node (" + "TYPE,"
+ + "GUID," + "PARENT," + "NAME," + "CHILDREN," + "STATUS"
+ + ") VALUES (" + "?,?,?,?,?,?" + ");");
+ updateChildrenStmt = db
+ .compileStatement("UPDATE Node SET CHILDREN = ? WHERE "
+ + android.provider.BaseColumns._ID + " = ?");
+ updateStatusStmt = db
+ .compileStatement("UPDATE Node SET STATUS = ? WHERE "
+ + android.provider.BaseColumns._ID + " = ?");
+ updateNameStmt = db.compileStatement("UPDATE Node SET NAME = ? WHERE "
+ + android.provider.BaseColumns._ID + " = ?");
+ updateParentStmt = db
+ .compileStatement("UPDATE Node SET PARENT = ? WHERE "
+ + android.provider.BaseColumns._ID + " = ?");
+ deleteStmt = db.compileStatement("DELETE FROM Node");
+ deleteIdStmt = db.compileStatement("DELETE FROM Node WHERE "
+ + android.provider.BaseColumns._ID + " = ?");
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#start(android.database.sqlite.SQLiteDatabase)
+ */
+ @Override
+ public void start(SQLiteDatabase db) {
+ FolderUtil util = FolderUtil.getInstance();
+ util.init(db);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#start2(android.database.sqlite.SQLiteDatabase)
+ */
+ @Override
+ public void start2(SQLiteDatabase db) {
+ // top = new DirNode(null, null);
+ // top.setStatus(Node.Status.OPEN);
+ //
+ // Node node;
+ // node = new DirNode("ディレクトリ", null);
+ // top.add(node);
+ // node.add(new FileNode("ファイルaaa", null));
+ // node.add(new FileNode("ファイルbb", null));
+ // node.add(new FileNode("ファイルc", null));
+ //
+ // node = new DirNode("ディレクトリAAA", null);
+ // top.add(node);
+ // node.add(new DirNode("directory qqqq", null));
+ // node.add(new FileNode("ファイルdddddddd", null));
+ // node.add(new FileNode("ファイルeee", null));
+ //
+ // node = new FileNode("ファイルzzz", null);
+ // top.add(node);
+
+ Node node;
+ Node n;
+ long id;
+ Node top = new RootNode("", null);
+ id = addNT(top);
+ top.setId(id);
+
+ // NODE
+ node = new DirNode("ディレクトリ", null);
+ node.setParent(top.getId());
+ id = addNT(node);
+ node.setId(id);
+ addChildrenIdNT(top, node.getId());
+ // top.add(node.getId());
+
+ // SUB
+ n = new FileNode("ファイルaaa", null);
+ n.setParent(node.getId());
+ id = addNT(n);
+ addChildrenIdNT(node, id);
+ // node.add(n.getId());
+ n = new FileNode("ファイルbb", null);
+ n.setParent(node.getId());
+ id = addNT(n);
+ addChildrenIdNT(node, id);
+ // node.add(n.getId());
+ n = new FileNode("ファイルc", null);
+ n.setParent(node.getId());
+ id = addNT(n);
+ addChildrenIdNT(node, id);
+ // node.add(n.getId());
+
+ // NODE
+ node = new DirNode("ディレクトリAAA", null);
+ node.setParent(top.getId());
+ id = addNT(node);
+ addChildrenIdNT(top, id);
+ // top.add(node.getId());
+
+ // SUB
+ n = new DirNode("ディレクトリ", null);
+ n.setParent(node.getId());
+ id = addNT(n);
+ addChildrenIdNT(node, id);
+ // node.add(n.getId());
+ n = new FileNode("yyyファイル", null);
+ n.setParent(node.getId());
+ id = addNT(n);
+ addChildrenIdNT(node, id);
+ // node.add(n.getId());
+
+ // NODE
+ node = new FileNode("ファイルあいうえお", null);
+ node.setParent(top.getId());
+ id = addNT(node);
+ addChildrenIdNT(top, id);
+ // top.add(node.getId());
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#search()
+ */
+ @Override
+ public List<Node> search() {
+ return search(DatabaseHelper.getInstance().getSQLiteDatabase(), null,
+ null, null);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#searchRoot()
+ */
+ @Override
+ public Node searchRoot() {
+ return searchRoot(DatabaseHelper.getInstance().getSQLiteDatabase());
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#searchRoot(android.database.sqlite.SQLiteDatabase)
+ */
+ @Override
+ public Node searchRoot(SQLiteDatabase db) {
+ String selection = "TYPE = ?";
+ String[] selectionArgs = { "0" };
+ String orderBy = null;
+ List<Node> list = search(db, selection, selectionArgs, orderBy);
+ if (list.size() <= 0) {
+ return null;
+ }
+ return list.get(0);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#searchById(long)
+ */
+ @Override
+ public Node searchById(long id) {
+ return searchById(DatabaseHelper.getInstance().getSQLiteDatabase(), id);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#searchById(android.database.sqlite.SQLiteDatabase, long)
+ */
+ @Override
+ public Node searchById(SQLiteDatabase db, long id) {
+ String selection = android.provider.BaseColumns._ID + " = ?";
+ String[] selectionArgs = { String.valueOf(id) };
+ String orderBy = null;
+ List<Node> list = search(db, selection, selectionArgs, orderBy);
+ if (list.size() <= 0) {
+ return null;
+ }
+ return list.get(0);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#searchByGuid(android.database.sqlite.SQLiteDatabase, java.lang.String)
+ */
+ @Override
+ public Node searchByGuid(SQLiteDatabase db, String guid) {
+ String selection = "GUID = ?";
+ String[] selectionArgs = { guid };
+ String orderBy = null;
+ List<Node> list = search(db, selection, selectionArgs, orderBy);
+ if (list.size() <= 0) {
+ return null;
+ }
+ return list.get(0);
+ }
+
+ private List<Node> search(SQLiteDatabase db, String selection,
+ String[] selectionArgs, String orderBy) {
+ List<Node> list = new ArrayList<Node>();
+ Cursor cursor = null;
+ try {
+ NodeFactory factory = NodeFactory.getInstance();
+
+ cursor = db.query("Node", new String[] {
+ android.provider.BaseColumns._ID, "TYPE", "GUID", "PARENT",
+ "NAME", "CHILDREN", "STATUS" }, selection, selectionArgs,
+ null, null, orderBy);
+ cursor.moveToFirst();
+ int size = cursor.getCount();
+ for (int i = 0; i < size; i++) {
+ // TODO Factory
+ // Node Node = new Node(
+ // cursor.getLong(0),
+ // cursor.getString(1),
+ // cursor.getString(2),
+ // cursor.getLong(3),
+ // cursor.getLong(4));
+ Node node = factory.create(cursor.getLong(0), cursor.getInt(1),
+ cursor.getString(2), cursor.getLong(3),
+ cursor.getString(4), cursor.getString(5),
+ cursor.getInt(6));
+ list.add(node);
+ cursor.moveToNext();
+ }
+ } catch (SQLException e) {
+ Debug.d(this, null, e);
+ list = null;
+ } catch (Exception e) {
+ Debug.d(this, null, e);
+ list = null;
+ } finally {
+ if (cursor != null) {
+ cursor.close();
+ cursor = null;
+ }
+ }
+ return list;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#isEmpty()
+ */
+ @Override
+ public boolean isEmpty() {
+ List<Node> list = search();
+ return list == null || list.size() <= 0;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#add(com.yuji.ef.dao.Node)
+ */
+ @Override
+ public long add(Node node) {
+ DatabaseHelper helper = DatabaseHelper.getInstance();
+ SQLiteDatabase db = helper.getWritableDatabase();
+ return add(db, node);
+ }
+
+ private long add(SQLiteDatabase db, Node node) {
+ long id = -1;
+ db.beginTransaction();
+ try {
+ id = addNT(node);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#addNT(com.yuji.ef.dao.Node)
+ */
+ @Override
+ public long addNT(Node node) {
+ long id = -1;
+ int i = 1;
+ SQLiteStatement stmt = insertStmt;
+ stmt.bindLong(i++, node.getType());
+ stmt.bindString(i++, CommonUtil.nz(node.getGuid()));
+ stmt.bindLong(i++, node.getParent());
+ stmt.bindString(i++, node.getName());
+ stmt.bindString(i++, CommonUtil.nz(node.getChildrenString()));
+ stmt.bindLong(i++, Node.getStatusCode(node.getStatus()));
+ id = stmt.executeInsert();
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#addChildrenId(com.yuji.ef.dao.Node, long)
+ */
+ @Override
+ public long addChildrenId(Node node, long id) {
+ DatabaseHelper helper = DatabaseHelper.getInstance();
+ SQLiteDatabase db = helper.getWritableDatabase();
+ return addChildrenId(db, node, id);
+ }
+
+ private long addChildrenId(SQLiteDatabase db, Node node, long aid) {
+ long id = -1;
+ db.beginTransaction();
+ try {
+ id = addChildrenIdNT(node, aid);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#addChildrenIdNT(com.yuji.ef.dao.Node, long)
+ */
+ @Override
+ public long addChildrenIdNT(Node node, long id) {
+ List<Long> l = node.getChildren();
+ if (l.contains(id)) {
+ return -1;
+ }
+ l.add(id);
+ // node.add(id);
+ return updateChildrenNT(node, Node.concatChildren(l));
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateChildrenNT(com.yuji.ef.dao.Node, java.lang.String)
+ */
+ @Override
+ public long updateChildrenNT(Node node, String children) {
+ long id = -1;
+ int i = 1;
+ SQLiteStatement stmt = updateChildrenStmt;
+ stmt.bindString(i++, node.getChildrenString());
+ stmt.bindLong(i++, node.getId());
+ id = stmt.executeInsert();
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#remoteChildrenId(com.yuji.ef.dao.Node, long)
+ */
+ @Override
+ public long removeChildrenId(Node node, long id) {
+ DatabaseHelper helper = DatabaseHelper.getInstance();
+ SQLiteDatabase db = helper.getWritableDatabase();
+ return remoteChildrenId(db, node, id);
+ }
+
+ private long remoteChildrenId(SQLiteDatabase db, Node node, long aid) {
+ long id = -1;
+ db.beginTransaction();
+ try {
+ id = removeChildrenIdNT(node, aid);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#remoteChildrenIdNT(com.yuji.ef.dao.Node, long)
+ */
+ @Override
+ public long removeChildrenIdNT(Node node, long id) {
+ List<Long> l = node.getChildren();
+ if (!l.contains(id)) {
+ return -1;
+ }
+ l.remove(id);
+ // node.add(id);
+ return updateChildrenNT(node, Node.concatChildren(l));
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateStatus(com.yuji.ef.dao.Node, com.yuji.ef.dao.Node.Status)
+ */
+ @Override
+ public long updateStatus(Node node, Node.Status status) {
+ return updateStatus(DatabaseHelper.getInstance().getSQLiteDatabase(),
+ node, status);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateStatus(android.database.sqlite.SQLiteDatabase, com.yuji.ef.dao.Node, com.yuji.ef.dao.Node.Status)
+ */
+ @Override
+ public long updateStatus(SQLiteDatabase db, Node node, Node.Status status) {
+ long id = -1;
+
+ db.beginTransaction();
+ try {
+ id = updateStatusNT(node, status);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateStatusNT(com.yuji.ef.dao.Node, com.yuji.ef.dao.Node.Status)
+ */
+ @Override
+ public long updateStatusNT(Node node, Node.Status status) {
+ long id = -1;
+ int i = 1;
+ int code = Node.getStatusCode(status);
+
+ SQLiteStatement stmt = updateStatusStmt;
+ stmt.bindLong(i++, code);
+ stmt.bindLong(i++, node.getId());
+ id = stmt.executeInsert();
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateName(com.yuji.ef.dao.Node, java.lang.String)
+ */
+ @Override
+ public long updateName(Node node, String name) {
+ return updateName(DatabaseHelper.getInstance().getSQLiteDatabase(),
+ node, name);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateName(android.database.sqlite.SQLiteDatabase, com.yuji.ef.dao.Node, java.lang.String)
+ */
+ @Override
+ public long updateName(SQLiteDatabase db, Node node, String name) {
+ long id = -1;
+
+ db.beginTransaction();
+ try {
+ id = updateNameNT(node, name);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateNameNT(com.yuji.ef.dao.Node, java.lang.String)
+ */
+ @Override
+ public long updateNameNT(Node node, String name) {
+ long id = -1;
+ int i = 1;
+
+ SQLiteStatement stmt = updateNameStmt;
+ stmt.bindString(i++, name);
+ stmt.bindLong(i++, node.getId());
+ id = stmt.executeInsert();
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateParent(com.yuji.ef.dao.Node, long)
+ */
+ @Override
+ public long updateParent(Node node, long parent) {
+ return updateParent(DatabaseHelper.getInstance().getSQLiteDatabase(),
+ node, parent);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateParent(android.database.sqlite.SQLiteDatabase, com.yuji.ef.dao.Node, long)
+ */
+ @Override
+ public long updateParent(SQLiteDatabase db, Node node, long parent) {
+ long id = -1;
+
+ db.beginTransaction();
+ try {
+ id = updateParentNT(node, parent);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateParentNT(com.yuji.ef.dao.Node, long)
+ */
+ @Override
+ public long updateParentNT(Node node, long parent) {
+ long id = -1;
+ int i = 1;
+
+ SQLiteStatement stmt = updateParentStmt;
+ stmt.bindLong(i++, parent);
+ stmt.bindLong(i++, node.getId());
+ id = stmt.executeInsert();
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#updateChildren(com.yuji.ef.dao.Node, java.lang.String)
+ */
+ @Override
+ public long updateChildren(Node node, String children) {
+ DatabaseHelper helper = DatabaseHelper.getInstance();
+ SQLiteDatabase db = helper.getWritableDatabase();
+ return updateChildren(db, node, children);
+ }
+
+ private long updateChildren(SQLiteDatabase db, Node node, String children) {
+ long id = -1;
+ db.beginTransaction();
+ try {
+ id = updateChildrenNT(node, children);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#delete(long)
+ */
+ @Override
+ public long delete(long id) {
+ DatabaseHelper helper = DatabaseHelper.getInstance();
+ SQLiteDatabase db = helper.getWritableDatabase();
+ return delete(db, id);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#delete(android.database.sqlite.SQLiteDatabase, long)
+ */
+ @Override
+ public long delete(SQLiteDatabase db, long did) {
+ long id = -1;
+
+ db.beginTransaction();
+ try {
+ id = deleteNT(did);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#deleteNT(long)
+ */
+ @Override
+ public long deleteNT(long did) {
+ long id = -1; // TODO
+ int i = 1;
+
+ SQLiteStatement stmt = deleteIdStmt;
+ stmt.bindLong(i++, did);
+ stmt.execute();
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#delete()
+ */
+ @Override
+ public long delete() {
+ DatabaseHelper helper = DatabaseHelper.getInstance();
+ SQLiteDatabase db = helper.getWritableDatabase();
+ return delete(db);
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#delete(android.database.sqlite.SQLiteDatabase)
+ */
+ @Override
+ public long delete(SQLiteDatabase db) {
+ long id = -1;
+
+ db.beginTransaction();
+ try {
+ deleteNT();
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see com.yuji.ef.dao.NodeDao#deleteNT()
+ */
+ @Override
+ public void deleteNT() {
+ SQLiteStatement stmt = deleteStmt;
+ stmt.execute();
+ }
+}
import com.yuji.ef.dao.DirNode;
import com.yuji.ef.dao.FileNode;
import com.yuji.ef.dao.Node;
+import com.yuji.ef.dao.NodeCacheDao;
import com.yuji.ef.dao.NodeDao;
import com.yuji.ef.dao.RootNode;
public class FolderUtil {
private EvernoteUtil util = EvernoteUtil.getInstance();
- private NodeDao nodeDao = (NodeDao) NodeDao.getInstance();
+ private NodeDao nodeDao = (NodeDao) NodeCacheDao.getInstance();
private BookDao bookDao = (BookDao) BookDao.getInstance();
public static FolderUtil getInstance() {
}
long id;
- id = nodeDao.remoteChildrenIdNT(oldParent, oldNote.getId());
+ id = nodeDao.removeChildrenIdNT(oldParent, oldNote.getId());
if (id < 0){
// TODO
}
if (id >= 0) {
Node p = nodeDao.searchById(id);
if (p != null) {
- nodeDao.remoteChildrenIdNT(p, node.getId());
+ nodeDao.removeChildrenIdNT(p, node.getId());
}
}
nodeDao.deleteNT(node.getId());