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;
13 import com.yuji.ef.utility.FolderUtil;
15 public class NodeDao implements IDao<Node> {
16 private static IDao<Node> instance = null;
17 public static final String DELM = "\t";
18 private SQLiteStatement insertStmt = null;
19 private SQLiteStatement updateChildrenStmt = null;
20 private SQLiteStatement updateStatusStmt = null;
21 private SQLiteStatement updateNameStmt = null;
22 private SQLiteStatement updateParentStmt = null;
23 private SQLiteStatement deleteStmt = null;
24 private SQLiteStatement deleteIdStmt = null;
26 public static IDao<Node> getInstance() {
27 if (instance == null) {
28 instance = new NodeDao();
37 public void onCreate(SQLiteDatabase db) {
38 db.execSQL("CREATE TABLE Node (" + android.provider.BaseColumns._ID
39 + " INTEGER PRIMARY KEY AUTOINCREMENT," + "TYPE INTEGER,"
40 + "GUID TEXT," + "PARENT INTEGER," + "NAME TEXT,"
41 + "CHILDREN TEXT," + "STATUS INTEGER" + ");");
44 public void init(SQLiteDatabase db) {
45 insertStmt = db.compileStatement("INSERT INTO Node (" + "TYPE,"
46 + "GUID," + "PARENT," + "NAME," + "CHILDREN," + "STATUS"
47 + ") VALUES (" + "?,?,?,?,?,?" + ");");
48 updateChildrenStmt = db
49 .compileStatement("UPDATE Node SET CHILDREN = ? WHERE "
50 + android.provider.BaseColumns._ID + " = ?");
52 .compileStatement("UPDATE Node SET STATUS = ? WHERE "
53 + android.provider.BaseColumns._ID + " = ?");
55 .compileStatement("UPDATE Node SET NAME = ? WHERE "
56 + android.provider.BaseColumns._ID + " = ?");
58 .compileStatement("UPDATE Node SET PARENT = ? WHERE "
59 + android.provider.BaseColumns._ID + " = ?");
60 deleteStmt = db.compileStatement("DELETE FROM Node");
61 deleteIdStmt = db.compileStatement("DELETE FROM Node WHERE "
62 + android.provider.BaseColumns._ID + " = ?");
65 public void start(SQLiteDatabase db) {
66 FolderUtil util = FolderUtil.getInstance();
70 public void start2(SQLiteDatabase db) {
71 // top = new DirNode(null, null);
72 // top.setStatus(Node.Status.OPEN);
75 // node = new DirNode("ディレクトリ", null);
77 // node.add(new FileNode("ファイルaaa", null));
78 // node.add(new FileNode("ファイルbb", null));
79 // node.add(new FileNode("ファイルc", null));
81 // node = new DirNode("ディレクトリAAA", null);
83 // node.add(new DirNode("directory qqqq", null));
84 // node.add(new FileNode("ファイルdddddddd", null));
85 // node.add(new FileNode("ファイルeee", null));
87 // node = new FileNode("ファイルzzz", null);
93 Node top = new RootNode("", null);
98 node = new DirNode("ディレクトリ", null);
99 node.setParent(top.getId());
102 addChildrenIdNT(top, node.getId());
103 // top.add(node.getId());
106 n = new FileNode("ファイルaaa", null);
107 n.setParent(node.getId());
109 addChildrenIdNT(node, id);
110 // node.add(n.getId());
111 n = new FileNode("ファイルbb", null);
112 n.setParent(node.getId());
114 addChildrenIdNT(node, id);
115 // node.add(n.getId());
116 n = new FileNode("ファイルc", null);
117 n.setParent(node.getId());
119 addChildrenIdNT(node, id);
120 // node.add(n.getId());
123 node = new DirNode("ディレクトリAAA", null);
124 node.setParent(top.getId());
126 addChildrenIdNT(top, id);
127 // top.add(node.getId());
130 n = new DirNode("ディレクトリ", null);
131 n.setParent(node.getId());
133 addChildrenIdNT(node, id);
134 // node.add(n.getId());
135 n = new FileNode("yyyファイル", null);
136 n.setParent(node.getId());
138 addChildrenIdNT(node, id);
139 // node.add(n.getId());
142 node = new FileNode("ファイルあいうえお", null);
143 node.setParent(top.getId());
145 addChildrenIdNT(top, id);
146 // top.add(node.getId());
149 public SQLiteDatabase getSQLiteDatabase() {
150 DatabaseHelper helper = DatabaseHelper.getInstance();
151 SQLiteDatabase db = helper.getWritableDatabase();
155 public List<Node> search() {
156 return search(getSQLiteDatabase(), null, null, null);
159 public Node searchRoot() {
160 return searchRoot(getSQLiteDatabase());
163 public Node searchRoot(SQLiteDatabase db) {
164 String selection = "TYPE = ?";
165 String[] selectionArgs = { "0" };
166 String orderBy = null;
167 List<Node> list = search(db, selection, selectionArgs, orderBy);
168 if (list.size() <= 0) {
174 public Node searchById(long id) {
175 return searchById(getSQLiteDatabase(), id);
178 public Node searchById(SQLiteDatabase db, long id) {
179 String selection = android.provider.BaseColumns._ID + " = ?";
180 String[] selectionArgs = { String.valueOf(id) };
181 String orderBy = null;
182 List<Node> list = search(db, selection, selectionArgs, orderBy);
183 if (list.size() <= 0) {
189 public Node searchByGuid(SQLiteDatabase db, String guid) {
190 String selection = "GUID = ?";
191 String[] selectionArgs = { guid };
192 String orderBy = null;
193 List<Node> list = search(db, selection, selectionArgs, orderBy);
194 if (list.size() <= 0) {
200 private List<Node> search(SQLiteDatabase db, String selection, String[] selectionArgs,
202 List<Node> list = new ArrayList<Node>();
203 Cursor cursor = null;
205 NodeFactory factory = NodeFactory.getInstance();
207 cursor = db.query("Node", new String[] {
208 android.provider.BaseColumns._ID, "TYPE", "GUID", "PARENT",
209 "NAME", "CHILDREN", "STATUS" }, selection, selectionArgs,
210 null, null, orderBy);
211 cursor.moveToFirst();
212 int size = cursor.getCount();
213 for (int i = 0; i < size; i++) {
215 // Node Node = new Node(
216 // cursor.getLong(0),
217 // cursor.getString(1),
218 // cursor.getString(2),
219 // cursor.getLong(3),
220 // cursor.getLong(4));
221 Node node = factory.create(cursor.getLong(0), cursor.getInt(1),
222 cursor.getString(2), cursor.getLong(3),
223 cursor.getString(4), cursor.getString(5),
228 } catch (SQLException e) {
229 Debug.d(this, null, e);
231 } catch (Exception e) {
232 Debug.d(this, null, e);
235 if (cursor != null) {
243 public boolean isEmpty() {
244 List<Node> list = search();
245 return list == null || list.size() <= 0;
248 public long add(Node node) {
249 DatabaseHelper helper = DatabaseHelper.getInstance();
250 SQLiteDatabase db = helper.getWritableDatabase();
251 return add(db, node);
254 private long add(SQLiteDatabase db, Node node) {
256 db.beginTransaction();
259 db.setTransactionSuccessful();
266 public long addNT(Node node) {
269 SQLiteStatement stmt = insertStmt;
270 stmt.bindLong(i++, node.getType());
271 stmt.bindString(i++, CommonUtil.nz(node.getGuid()));
272 stmt.bindLong(i++, node.getParent());
273 stmt.bindString(i++, node.getName());
274 stmt.bindString(i++, CommonUtil.nz(node.getChildrenString()));
275 stmt.bindLong(i++, Node.getStatusCode(node.getStatus()));
276 id = stmt.executeInsert();
280 public long addChildrenId(Node node, long id) {
281 DatabaseHelper helper = DatabaseHelper.getInstance();
282 SQLiteDatabase db = helper.getWritableDatabase();
283 return addChildrenId(db, node, id);
286 private long addChildrenId(SQLiteDatabase db, Node node, long aid) {
288 db.beginTransaction();
290 id = addChildrenIdNT(node, aid);
291 db.setTransactionSuccessful();
298 public long addChildrenIdNT(Node node, long id) {
299 List<Long> l = node.getChildren();
300 if (l.contains(id)) {
305 return updateChildrenNT(node, Node.concatChildren(l));
308 public long updateChildrenNT(Node node, String children) {
311 SQLiteStatement stmt = updateChildrenStmt;
312 stmt.bindString(i++, node.getChildrenString());
313 stmt.bindLong(i++, node.getId());
314 id = stmt.executeInsert();
318 public long remoteChildrenId(Node node, long id) {
319 DatabaseHelper helper = DatabaseHelper.getInstance();
320 SQLiteDatabase db = helper.getWritableDatabase();
321 return remoteChildrenId(db, node, id);
324 private long remoteChildrenId(SQLiteDatabase db, Node node, long aid) {
326 db.beginTransaction();
328 id = remoteChildrenIdNT(node, aid);
329 db.setTransactionSuccessful();
336 public long remoteChildrenIdNT(Node node, long id) {
337 List<Long> l = node.getChildren();
338 if (!l.contains(id)) {
343 return updateChildrenNT(node, Node.concatChildren(l));
346 public long updateStatus(Node node, Node.Status status) {
347 return updateStatus(getSQLiteDatabase(), node, status);
350 public long updateStatus(SQLiteDatabase db, Node node, Node.Status status) {
353 db.beginTransaction();
355 id = updateStatusNT(node, status);
356 db.setTransactionSuccessful();
363 public long updateStatusNT(Node node, Node.Status status) {
366 int code = Node.getStatusCode(status);
368 SQLiteStatement stmt = updateStatusStmt;
369 stmt.bindLong(i++, code);
370 stmt.bindLong(i++, node.getId());
371 id = stmt.executeInsert();
375 public long updateName(Node node, String name) {
376 return updateName(getSQLiteDatabase(), node, name);
379 public long updateName(SQLiteDatabase db, Node node, String name) {
382 db.beginTransaction();
384 id = updateNameNT(node, name);
385 db.setTransactionSuccessful();
392 public long updateNameNT(Node node, String name) {
396 SQLiteStatement stmt = updateNameStmt;
397 stmt.bindString(i++, name);
398 stmt.bindLong(i++, node.getId());
399 id = stmt.executeInsert();
403 public long updateParentNT(Node node, long parent) {
407 SQLiteStatement stmt = updateParentStmt;
408 stmt.bindLong(i++, parent);
409 stmt.bindLong(i++, node.getId());
410 id = stmt.executeInsert();
414 public long updateChildren(Node node, String children) {
415 DatabaseHelper helper = DatabaseHelper.getInstance();
416 SQLiteDatabase db = helper.getWritableDatabase();
417 return updateChildren(db, node, children);
420 private long updateChildren(SQLiteDatabase db, Node node, String children) {
422 db.beginTransaction();
424 id = updateChildrenNT(node, children);
425 db.setTransactionSuccessful();
432 public long delete(long id) {
433 DatabaseHelper helper = DatabaseHelper.getInstance();
434 SQLiteDatabase db = helper.getWritableDatabase();
435 return delete(db, id);
438 public long delete(SQLiteDatabase db, long did) {
441 db.beginTransaction();
444 db.setTransactionSuccessful();
451 public long deleteNT(long did) {
452 long id = -1; // TODO
455 SQLiteStatement stmt = deleteIdStmt;
456 stmt.bindLong(i++, did);
461 public long delete() {
462 DatabaseHelper helper = DatabaseHelper.getInstance();
463 SQLiteDatabase db = helper.getWritableDatabase();
467 public long delete(SQLiteDatabase db) {
470 db.beginTransaction();
473 db.setTransactionSuccessful();
480 public void deleteNT() {
481 SQLiteStatement stmt = deleteStmt;