OSDN Git Service

Add logic to display stacks in notebook tree
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / LinkedNotebookTable.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 com.evernote.edam.type.LinkedNotebook;\r
27 \r
28 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
29 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
30 \r
31 public class LinkedNotebookTable {\r
32         \r
33         private final ApplicationLogger                 logger;\r
34         DatabaseConnection                                                      db;\r
35         \r
36         // Constructor\r
37         public LinkedNotebookTable(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 LinkedNotebook...");\r
45         if (!query.exec("Create table LinkedNotebook (id long primary key, " +\r
46                         "shareName VarChar, " +\r
47                         "username VarChar, "+\r
48                         "shardID VarChar, " +\r
49                         "shareKey VarChar, " +\r
50                         "url VarChar, " +\r
51                         "icon blob, " +\r
52                         "isDirty boolean)"))                            \r
53                 logger.log(logger.HIGH, "Table LinkedNotebook creation FAILED!!!");   \r
54         }\r
55         // Drop the table\r
56         public void dropTable() {\r
57                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
58                 query.exec("Drop table LinkedNotebook");\r
59         }\r
60         // Save an individual notebook\r
61         public void addNotebook(LinkedNotebook tempNotebook, boolean isDirty) {\r
62                 boolean check;\r
63                 \r
64         NSqlQuery query = new NSqlQuery(db.getConnection());\r
65                 check = query.prepare("Insert Into LinkedNotebook (id, shareName, username,  "\r
66                                 +"shardId, shareKey, url, isDirty) "   \r
67                                 + " Values("\r
68                                 +":id, :shareName, :username, "\r
69                                 +":shardId, :shareKey, :url, :isDirty)");\r
70                 query.bindValue(":id", tempNotebook.getId());\r
71                 query.bindValue(":shareName", tempNotebook.getShareName());\r
72                 query.bindValue(":username", tempNotebook.getUsername());\r
73                 query.bindValue(":shardId", tempNotebook.getShardId());\r
74                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
75                 query.bindValue(":url", tempNotebook.getUri());\r
76                 \r
77                 if (isDirty)\r
78                         query.bindValue(":isDirty", true);\r
79                 else\r
80                         query.bindValue(":isDirty", false);\r
81 \r
82                 check = query.exec();\r
83                 if (!check) {\r
84                         logger.log(logger.MEDIUM, "LinkedNotebook Table insert failed.");\r
85                         logger.log(logger.MEDIUM, query.lastError().toString());\r
86                 }\r
87         }\r
88         // Delete the notebook based on a guid\r
89         public void expungeNotebook(long id, boolean needsSync) {\r
90                 boolean check;\r
91         NSqlQuery query = new NSqlQuery(db.getConnection());\r
92 \r
93         check = query.prepare("delete from LinkedNotebook "\r
94                                 +"where id=:id");\r
95                 if (!check) {\r
96                         logger.log(logger.EXTREME, "LinkedNotebook SQL delete prepare has failed.");\r
97                         logger.log(logger.EXTREME, query.lastError().toString());\r
98                 }\r
99                 query.bindValue(":id", id);\r
100                 check = query.exec();\r
101                 if (!check) \r
102                         logger.log(logger.MEDIUM, "LinkedNotebook delete failed.");\r
103                 \r
104                 // Signal the parent that work needs to be done\r
105                 if  (needsSync) {\r
106                         DeletedTable deletedTable = new DeletedTable(logger, db);\r
107                         deletedTable.addDeletedItem(new Long(id).toString(), "LinkedNotebook");\r
108                 }\r
109         }\r
110         // Check if a notebook exists\r
111         public boolean exists(long id) {\r
112         NSqlQuery query = new NSqlQuery(db.getConnection());\r
113         boolean check = query.prepare("Select id from linkednotebook where id=:id");\r
114         query.bindValue(":id", id);\r
115                 check = query.exec();\r
116                 if (!check) {\r
117                         logger.log(logger.MEDIUM, "LinkedNotebook Table exists check failed.");\r
118                         logger.log(logger.MEDIUM, query.lastError().toString());\r
119                 }\r
120                 if (query.next())\r
121                         return true;\r
122                 return false;\r
123         }\r
124         // Update a notebook\r
125         public void updateNotebook(LinkedNotebook tempNotebook, boolean isDirty) {\r
126                 boolean check;\r
127                 if (!exists(tempNotebook.getId())) {\r
128                         addNotebook(tempNotebook, isDirty);\r
129                         return;\r
130                 }\r
131                 \r
132         NSqlQuery query = new NSqlQuery(db.getConnection());\r
133         check = query.prepare("Update LinkedNotebook set id=:id, shareName=:shareName, " +\r
134                         "username=:username, shardID=:shardID, shareKey=:shareKey, url=:url, isDirty=:isDirty");\r
135                 query.bindValue(":id", tempNotebook.getId());\r
136                 query.bindValue(":shareName", tempNotebook.getShareName());\r
137                 query.bindValue(":username", tempNotebook.getUsername());\r
138                 query.bindValue(":shardID", tempNotebook.getShardId());\r
139                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
140                 query.bindValue(":url", tempNotebook.getUri());\r
141 \r
142                 query.bindValue(":isDirty", isDirty);\r
143                 \r
144                 check = query.exec();\r
145                 if (!check) {\r
146                         logger.log(logger.MEDIUM, "LinkedNotebook Table update failed.");\r
147                         logger.log(logger.MEDIUM, query.lastError().toString());\r
148                 }\r
149         }\r
150         // Load notebooks from the database\r
151         public List<LinkedNotebook> getAll() {\r
152                 LinkedNotebook tempNotebook;\r
153                 List<LinkedNotebook> index = new ArrayList<LinkedNotebook>();\r
154                 boolean check;\r
155                                         \r
156         NSqlQuery query = new NSqlQuery(db.getConnection());\r
157                                         \r
158                 check = query.exec("Select id, shareName, username, shardID, shareKey url, " +\r
159                                 " from LinkedNotebook");\r
160                 if (!check)\r
161                         logger.log(logger.EXTREME, "Notebook SQL retrieve has failed.");\r
162                 while (query.next()) {\r
163                         tempNotebook = new LinkedNotebook();\r
164                         tempNotebook.setId(query.valueLong(0));\r
165                         tempNotebook.setUsername(query.valueString(1));\r
166                         tempNotebook.setShardId(query.valueString(2));\r
167                         tempNotebook.setShareKey(query.valueString(3));\r
168                         tempNotebook.setUri(query.valueString(4));\r
169 \r
170                         index.add(tempNotebook); \r
171                 }       \r
172                 return index;\r
173         }                       \r
174 \r
175         // does a record exist?\r
176         public String findNotebookByShareName(String name) {\r
177                 \r
178                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
179                 \r
180                 query.prepare("Select guid from Linkednotebook where shareName=:name");\r
181                 query.bindValue(":name", name);\r
182                 if (!query.exec())\r
183                         logger.log(logger.EXTREME, "Linked notebook SQL retrieve by share name has failed.");\r
184                 String val = null;\r
185                 if (query.next())\r
186                         val = query.valueString(0);\r
187                 return val;\r
188         }\r
189 \r
190 \r
191 }\r
192 \r