OSDN Git Service

e987701d8e0b176692c316c335e654906612afb5
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / dao / NodeDao.java
1 package com.yuji.ef.dao;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.database.Cursor;
7 import android.database.SQLException;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.database.sqlite.SQLiteStatement;
10
11 import com.yuji.ef.common.CommonUtil;
12 import com.yuji.ef.utility.Debug;
13 import com.yuji.ef.utility.FolderUtil;
14
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;
25
26         public static IDao<Node> getInstance() {
27                 if (instance == null) {
28                         instance = new NodeDao();
29                 }
30                 return instance;
31         }
32
33         private NodeDao() {
34
35         }
36
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" + ");");
42         }
43
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 + " = ?");
51                 updateStatusStmt = db
52                                 .compileStatement("UPDATE Node SET STATUS = ? WHERE "
53                                                 + android.provider.BaseColumns._ID + " = ?");
54                 updateNameStmt = db
55                                 .compileStatement("UPDATE Node SET NAME = ? WHERE "
56                                                 + android.provider.BaseColumns._ID + " = ?");
57                 updateParentStmt = db
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 + " = ?");
63         }
64
65         public void start(SQLiteDatabase db) {
66                 FolderUtil util = FolderUtil.getInstance();
67                 util.init(db);
68         }
69
70         public void start2(SQLiteDatabase db) {
71                 // top = new DirNode(null, null);
72                 // top.setStatus(Node.Status.OPEN);
73                 //
74                 // Node node;
75                 // node = new DirNode("ディレクトリ", null);
76                 // top.add(node);
77                 // node.add(new FileNode("ファイルaaa", null));
78                 // node.add(new FileNode("ファイルbb", null));
79                 // node.add(new FileNode("ファイルc", null));
80                 //
81                 // node = new DirNode("ディレクトリAAA", null);
82                 // top.add(node);
83                 // node.add(new DirNode("directory qqqq", null));
84                 // node.add(new FileNode("ファイルdddddddd", null));
85                 // node.add(new FileNode("ファイルeee", null));
86                 //
87                 // node = new FileNode("ファイルzzz", null);
88                 // top.add(node);
89
90                 Node node;
91                 Node n;
92                 long id;
93                 Node top = new RootNode("", null);
94                 id = addNT(top);
95                 top.setId(id);
96
97                 // NODE
98                 node = new DirNode("ディレクトリ", null);
99                 node.setParent(top.getId());
100                 id = addNT(node);
101                 node.setId(id);
102                 addChildrenIdNT(top, node.getId());
103                 // top.add(node.getId());
104
105                 // SUB
106                 n = new FileNode("ファイルaaa", null);
107                 n.setParent(node.getId());
108                 id = addNT(n);
109                 addChildrenIdNT(node, id);
110                 // node.add(n.getId());
111                 n = new FileNode("ファイルbb", null);
112                 n.setParent(node.getId());
113                 id = addNT(n);
114                 addChildrenIdNT(node, id);
115                 // node.add(n.getId());
116                 n = new FileNode("ファイルc", null);
117                 n.setParent(node.getId());
118                 id = addNT(n);
119                 addChildrenIdNT(node, id);
120                 // node.add(n.getId());
121
122                 // NODE
123                 node = new DirNode("ディレクトリAAA", null);
124                 node.setParent(top.getId());
125                 id = addNT(node);
126                 addChildrenIdNT(top, id);
127                 // top.add(node.getId());
128
129                 // SUB
130                 n = new DirNode("ディレクトリ", null);
131                 n.setParent(node.getId());
132                 id = addNT(n);
133                 addChildrenIdNT(node, id);
134                 // node.add(n.getId());
135                 n = new FileNode("yyyファイル", null);
136                 n.setParent(node.getId());
137                 id = addNT(n);
138                 addChildrenIdNT(node, id);
139                 // node.add(n.getId());
140
141                 // NODE
142                 node = new FileNode("ファイルあいうえお", null);
143                 node.setParent(top.getId());
144                 id = addNT(node);
145                 addChildrenIdNT(top, id);
146                 // top.add(node.getId());
147         }
148
149         public SQLiteDatabase getSQLiteDatabase() {
150                 DatabaseHelper helper = DatabaseHelper.getInstance();
151                 SQLiteDatabase db = helper.getWritableDatabase();
152                 return db;
153         }
154
155         public List<Node> search() {
156                 return search(getSQLiteDatabase(), null, null, null);
157         }
158
159         public Node searchRoot() {
160                 return searchRoot(getSQLiteDatabase());
161         }
162         
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) {
169                         return null;
170                 }
171                 return list.get(0);
172         }
173
174         public Node searchById(long id) {
175                 return searchById(getSQLiteDatabase(), id);
176         }
177         
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) {
184                         return null;
185                 }
186                 return list.get(0);
187         }
188
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) {
195                         return null;
196                 }
197                 return list.get(0);
198         }
199
200         private List<Node> search(SQLiteDatabase db, String selection, String[] selectionArgs,
201                         String orderBy) {
202                 List<Node> list = new ArrayList<Node>();
203                 Cursor cursor = null;
204                 try {
205                         NodeFactory factory = NodeFactory.getInstance();
206
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++) {
214                                 // TODO Factory
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),
224                                                 cursor.getInt(6));
225                                 list.add(node);
226                                 cursor.moveToNext();
227                         }
228                 } catch (SQLException e) {
229                         Debug.d(this, null, e);
230                         list = null;
231                 } catch (Exception e) {
232                         Debug.d(this, null, e);
233                         list = null;
234                 } finally {
235                         if (cursor != null) {
236                                 cursor.close();
237                                 cursor = null;
238                         }
239                 }
240                 return list;
241         }
242
243         public boolean isEmpty() {
244                 List<Node> list = search();
245                 return list == null || list.size() <= 0;
246         }
247
248         public long add(Node node) {
249                 DatabaseHelper helper = DatabaseHelper.getInstance();
250                 SQLiteDatabase db = helper.getWritableDatabase();
251                 return add(db, node);
252         }
253
254         private long add(SQLiteDatabase db, Node node) {
255                 long id = -1;
256                 db.beginTransaction();
257                 try {
258                         id = addNT(node);
259                         db.setTransactionSuccessful();
260                 } finally {
261                         db.endTransaction();
262                 }
263                 return id;
264         }
265
266         public long addNT(Node node) {
267                 long id = -1;
268                 int i = 1;
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();
277                 return id;
278         }
279
280         public long addChildrenId(Node node, long id) {
281                 DatabaseHelper helper = DatabaseHelper.getInstance();
282                 SQLiteDatabase db = helper.getWritableDatabase();
283                 return addChildrenId(db, node, id);
284         }
285
286         private long addChildrenId(SQLiteDatabase db, Node node, long aid) {
287                 long id = -1;
288                 db.beginTransaction();
289                 try {
290                         id = addChildrenIdNT(node, aid);
291                         db.setTransactionSuccessful();
292                 } finally {
293                         db.endTransaction();
294                 }
295                 return id;
296         }
297
298         public long addChildrenIdNT(Node node, long id) {
299                 List<Long> l = node.getChildren();
300                 if (l.contains(id)) {
301                         return -1;
302                 }
303                 l.add(id);
304                 // node.add(id);
305                 return updateChildrenNT(node, Node.concatChildren(l));
306         }
307
308         public long updateChildrenNT(Node node, String children) {
309                 long id = -1;
310                 int i = 1;
311                 SQLiteStatement stmt = updateChildrenStmt;
312                 stmt.bindString(i++, node.getChildrenString());
313                 stmt.bindLong(i++, node.getId());
314                 id = stmt.executeInsert();
315                 return id;
316         }
317
318         public long remoteChildrenId(Node node, long id) {
319                 DatabaseHelper helper = DatabaseHelper.getInstance();
320                 SQLiteDatabase db = helper.getWritableDatabase();
321                 return remoteChildrenId(db, node, id);
322         }
323
324         private long remoteChildrenId(SQLiteDatabase db, Node node, long aid) {
325                 long id = -1;
326                 db.beginTransaction();
327                 try {
328                         id = remoteChildrenIdNT(node, aid);
329                         db.setTransactionSuccessful();
330                 } finally {
331                         db.endTransaction();
332                 }
333                 return id;
334         }
335
336         public long remoteChildrenIdNT(Node node, long id) {
337                 List<Long> l = node.getChildren();
338                 if (!l.contains(id)) {
339                         return -1;
340                 }
341                 l.remove(id);
342                 // node.add(id);
343                 return updateChildrenNT(node, Node.concatChildren(l));
344         }
345
346         public long updateStatus(Node node, Node.Status status) {
347                 return updateStatus(getSQLiteDatabase(), node, status);
348         }
349
350         public long updateStatus(SQLiteDatabase db, Node node, Node.Status status) {
351                 long id = -1;
352
353                 db.beginTransaction();
354                 try {
355                         id = updateStatusNT(node, status);
356                         db.setTransactionSuccessful();
357                 } finally {
358                         db.endTransaction();
359                 }
360                 return id;
361         }
362
363         public long updateStatusNT(Node node, Node.Status status) {
364                 long id = -1;
365                 int i = 1;
366                 int code = Node.getStatusCode(status);
367
368                 SQLiteStatement stmt = updateStatusStmt;
369                 stmt.bindLong(i++, code);
370                 stmt.bindLong(i++, node.getId());
371                 id = stmt.executeInsert();
372                 return id;
373         }
374
375         public long updateName(Node node, String name) {
376                 return updateName(getSQLiteDatabase(), node, name);
377         }
378
379         public long updateName(SQLiteDatabase db, Node node, String name) {
380                 long id = -1;
381
382                 db.beginTransaction();
383                 try {
384                         id = updateNameNT(node, name);
385                         db.setTransactionSuccessful();
386                 } finally {
387                         db.endTransaction();
388                 }
389                 return id;
390         }
391
392         public long updateNameNT(Node node, String name) {
393                 long id = -1;
394                 int i = 1;
395
396                 SQLiteStatement stmt = updateNameStmt;
397                 stmt.bindString(i++, name);
398                 stmt.bindLong(i++, node.getId());
399                 id = stmt.executeInsert();
400                 return id;
401         }
402
403         public long updateParentNT(Node node, long parent) {
404                 long id = -1;
405                 int i = 1;
406
407                 SQLiteStatement stmt = updateParentStmt;
408                 stmt.bindLong(i++, parent);
409                 stmt.bindLong(i++, node.getId());
410                 id = stmt.executeInsert();
411                 return id;
412         }
413
414         public long updateChildren(Node node, String children) {
415                 DatabaseHelper helper = DatabaseHelper.getInstance();
416                 SQLiteDatabase db = helper.getWritableDatabase();
417                 return updateChildren(db, node, children);
418         }
419
420         private long updateChildren(SQLiteDatabase db, Node node, String children) {
421                 long id = -1;
422                 db.beginTransaction();
423                 try {
424                         id = updateChildrenNT(node, children);
425                         db.setTransactionSuccessful();
426                 } finally {
427                         db.endTransaction();
428                 }
429                 return id;
430         }
431
432         public long delete(long id) {
433                 DatabaseHelper helper = DatabaseHelper.getInstance();
434                 SQLiteDatabase db = helper.getWritableDatabase();
435                 return delete(db, id);
436         }
437
438         public long delete(SQLiteDatabase db, long did) {
439                 long id = -1;
440
441                 db.beginTransaction();
442                 try {
443                         id = deleteNT(did);
444                         db.setTransactionSuccessful();
445                 } finally {
446                         db.endTransaction();
447                 }
448                 return id;
449         }
450
451         public long deleteNT(long did) {
452                 long id = -1; // TODO
453                 int i = 1;
454
455                 SQLiteStatement stmt = deleteIdStmt;
456                 stmt.bindLong(i++, did);
457                 stmt.execute();
458                 return id;
459         }
460
461         public long delete() {
462                 DatabaseHelper helper = DatabaseHelper.getInstance();
463                 SQLiteDatabase db = helper.getWritableDatabase();
464                 return delete(db);
465         }
466
467         public long delete(SQLiteDatabase db) {
468                 long id = -1;
469
470                 db.beginTransaction();
471                 try {
472                         deleteNT();
473                         db.setTransactionSuccessful();
474                 } finally {
475                         db.endTransaction();
476                 }
477                 return id;
478         }
479
480         public void deleteNT() {
481                 SQLiteStatement stmt = deleteStmt;
482                 stmt.execute();
483         }
484 }