OSDN Git Service

Add logic to display stacks in notebook tree
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / WatchFolderTable.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 \r
21 package cx.fbn.nevernote.sql;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.List;\r
25 \r
26 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
27 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
28 import cx.fbn.nevernote.utilities.ListManager;\r
29 \r
30 public class WatchFolderTable {\r
31         ListManager parent;\r
32         private final ApplicationLogger                 logger;\r
33         private final DatabaseConnection                db;\r
34 \r
35         \r
36         // Constructor\r
37         public WatchFolderTable(ApplicationLogger l, DatabaseConnection d) {\r
38                 logger = l;\r
39                 db = d;\r
40         }\r
41         // Create the table\r
42         public void createTable() {\r
43                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
44                 logger.log(logger.HIGH, "Creating table WatchFolder...");\r
45         if (!query.exec("Create table WatchFolders (folder varchar primary key, notebook varchar," +\r
46                         "keep boolean, depth integer)"));\r
47                 logger.log(logger.HIGH, "Table WatchFolders creation FAILED!!!"); \r
48         }\r
49         // Drop the table\r
50         public void dropTable() {\r
51                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
52                 query.exec("Drop table WatchFolders");\r
53         }\r
54         // Add an folder\r
55         public void addWatchFolder(String folder, String notebook, boolean keep, int depth) {\r
56         NSqlQuery query = new NSqlQuery(db.getConnection());\r
57                 query.prepare("Insert Into WatchFolders (folder, notebook, keep, depth) " +\r
58                                 "values (:folder, :notebook, :keep, :depth)");\r
59                 query.bindValue(":folder", folder);\r
60                 query.bindValue(":notebook", notebook);\r
61                 query.bindValue(":keep", keep);\r
62                 query.bindValue(":depth", depth);\r
63                 if (!query.exec()) {\r
64                         logger.log(logger.MEDIUM, "Insert into WatchFolder failed.");\r
65                 }\r
66         }\r
67         // remove an folder\r
68         public void expungeWatchFolder(String folder) {\r
69         NSqlQuery query = new NSqlQuery(db.getConnection());\r
70                 query.prepare("delete from WatchFolders where folder=:folder");\r
71                 query.bindValue(":folder", folder);\r
72                 if (!query.exec()) {\r
73                         logger.log(logger.MEDIUM, "Expunge WatchFolder failed.");\r
74                         logger.log(logger.MEDIUM, query.lastError());\r
75                 }\r
76         }\r
77         public void expungeAll() {\r
78         NSqlQuery query = new NSqlQuery(db.getConnection());\r
79                 if (!query.exec("delete from WatchFolders")) {\r
80                         logger.log(logger.MEDIUM, "Expunge all WatchFolder failed.");\r
81                         logger.log(logger.MEDIUM, query.lastError());\r
82                 }\r
83         }\r
84         public List<WatchFolderRecord> getAll() {\r
85                 logger.log(logger.HIGH, "Entering RWatchFolders.getAll");\r
86                 \r
87                 List<WatchFolderRecord> list = new ArrayList<WatchFolderRecord>();\r
88                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
89                 query.exec("Select folder, (select name from notebook where guid = notebook), keep, depth from WatchFolders");\r
90                 while (query.next()) {\r
91                         WatchFolderRecord record = new WatchFolderRecord();\r
92                         record.folder = query.valueString(0);\r
93                         record.notebook = query.valueString(1);\r
94                         record.keep = new Boolean(query.valueString(2));\r
95                         record.depth = new Integer(query.valueString(3));\r
96                         list.add(record);\r
97                 }\r
98                 logger.log(logger.HIGH, "Leaving RWatchFolders.getAll");\r
99                 return list;\r
100 \r
101         }\r
102         \r
103         public String getNotebook(String dir) {\r
104                 logger.log(logger.HIGH, "Entering RWatchFolders.getNotebook");\r
105                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
106                 query.prepare("Select notebook from WatchFolders where folder=:dir");\r
107                 query.bindValue(":dir", dir);\r
108                 query.exec();\r
109                 String response = null;\r
110                 while (query.next()) {\r
111                         response = query.valueString(0);\r
112                 }\r
113                 logger.log(logger.HIGH, "Leaving RWatchFolders.getNotebook");\r
114                 return response;\r
115 \r
116         }\r
117 }\r