OSDN Git Service

Cleanup compiler warning messages and alter backup & restore to handle new database...
[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 (guid VarChar primary key, " +\r
46                         "shareName VarChar, " +\r
47                         "username VarChar, "+\r
48                         "shardID VarChar, " +\r
49                         "shareKey VarChar, " +\r
50                         "uri VarChar, " +\r
51                         "updateSequenceNumber Long," +\r
52                         "icon blob, " +\r
53                         "isDirty boolean)"))                            \r
54                 logger.log(logger.HIGH, "Table LinkedNotebook creation FAILED!!!");   \r
55         }\r
56         // Drop the table\r
57         public void dropTable() {\r
58                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
59                 query.exec("Drop table LinkedNotebook");\r
60         }\r
61         // Save an individual notebook\r
62         public void addNotebook(LinkedNotebook tempNotebook, boolean isDirty) {\r
63                 boolean check;\r
64                 \r
65         NSqlQuery query = new NSqlQuery(db.getConnection());\r
66                 check = query.prepare("Insert Into LinkedNotebook (guid, shareName, username,  "\r
67                                 +"shardId, shareKey, uri, updateSequenceNumber, isDirty) "   \r
68                                 + " Values("\r
69                                 +":guid, :shareName, :username, "\r
70                                 +":shardId, :shareKey, :uri,:usn, :isDirty)");\r
71                 query.bindValue(":guid", tempNotebook.getGuid());\r
72                 query.bindValue(":shareName", tempNotebook.getShareName());\r
73                 query.bindValue(":username", tempNotebook.getUsername());\r
74                 query.bindValue(":shardId", tempNotebook.getShardId());\r
75                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
76                 query.bindValue(":usn", tempNotebook.getUpdateSequenceNum());\r
77                 query.bindValue(":uri", tempNotebook.getUri());\r
78                 \r
79                 if (isDirty)\r
80                         query.bindValue(":isDirty", true);\r
81                 else\r
82                         query.bindValue(":isDirty", false);\r
83 \r
84                 check = query.exec();\r
85                 if (!check) {\r
86                         logger.log(logger.MEDIUM, "LinkedNotebook Table insert failed.");\r
87                         logger.log(logger.MEDIUM, query.lastError().toString());\r
88                 }\r
89         }\r
90         // Delete the notebook based on a guid\r
91         public void expungeNotebook(String id, boolean needsSync) {\r
92                 boolean check;\r
93         NSqlQuery query = new NSqlQuery(db.getConnection());\r
94 \r
95         check = query.prepare("delete from LinkedNotebook "\r
96                                 +"where guid=:guid");\r
97                 if (!check) {\r
98                         logger.log(logger.EXTREME, "LinkedNotebook SQL delete prepare has failed.");\r
99                         logger.log(logger.EXTREME, query.lastError().toString());\r
100                 }\r
101                 query.bindValue(":guid", id);\r
102                 check = query.exec();\r
103                 if (!check) \r
104                         logger.log(logger.MEDIUM, "LinkedNotebook delete failed.");\r
105                 \r
106                 // Signal the parent that work needs to be done\r
107                 if  (needsSync) {\r
108                         DeletedTable deletedTable = new DeletedTable(logger, db);\r
109                         deletedTable.addDeletedItem(new Long(id).toString(), "LinkedNotebook");\r
110                 }\r
111         }\r
112         // Check if a notebook exists\r
113         public boolean exists(String id) {\r
114         NSqlQuery query = new NSqlQuery(db.getConnection());\r
115         boolean check = query.prepare("Select guid from linkednotebook where guid=:guid");\r
116         query.bindValue(":guid", id);\r
117                 check = query.exec();\r
118                 if (!check) {\r
119                         logger.log(logger.MEDIUM, "LinkedNotebook Table exists check failed.");\r
120                         logger.log(logger.MEDIUM, query.lastError().toString());\r
121                 }\r
122                 if (query.next())\r
123                         return true;\r
124                 return false;\r
125         }\r
126         // Update a notebook\r
127         public void updateNotebook(LinkedNotebook tempNotebook, boolean isDirty) {\r
128                 boolean check;\r
129                 if (!exists(tempNotebook.getGuid())) {\r
130                         addNotebook(tempNotebook, isDirty);\r
131                         return;\r
132                 }\r
133                 \r
134         NSqlQuery query = new NSqlQuery(db.getConnection());\r
135         check = query.prepare("Update LinkedNotebook set guid=:guid, shareName=:shareName, " +\r
136                         "username=:username, shardID=:shardID, shareKey=:shareKey, uri=:uri, updateSequenceNumber=:usn, isDirty=:isDirty");\r
137                 query.bindValue(":guid", tempNotebook.getGuid());\r
138                 query.bindValue(":shareName", tempNotebook.getShareName());\r
139                 query.bindValue(":username", tempNotebook.getUsername());\r
140                 query.bindValue(":shardID", tempNotebook.getShardId());\r
141                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
142                 query.bindValue(":uri", tempNotebook.getUri());\r
143                 query.bindValue(":usn", tempNotebook.getUpdateSequenceNum());\r
144 \r
145                 query.bindValue(":isDirty", isDirty);\r
146                 \r
147                 check = query.exec();\r
148                 if (!check) {\r
149                         logger.log(logger.MEDIUM, "LinkedNotebook Table update failed.");\r
150                         logger.log(logger.MEDIUM, query.lastError().toString());\r
151                 }\r
152         }\r
153         // Load notebooks from the database\r
154         public List<LinkedNotebook> getAll() {\r
155                 LinkedNotebook tempNotebook;\r
156                 List<LinkedNotebook> index = new ArrayList<LinkedNotebook>();\r
157                 boolean check;\r
158                                         \r
159         NSqlQuery query = new NSqlQuery(db.getConnection());\r
160                                         \r
161                 check = query.exec("Select guid, shareName, username, shardID, shareKey uri, " +\r
162                                 " from LinkedNotebook");\r
163                 if (!check)\r
164                         logger.log(logger.EXTREME, "Notebook SQL retrieve has failed.");\r
165                 while (query.next()) {\r
166                         tempNotebook = new LinkedNotebook();\r
167                         tempNotebook.setGuid(query.valueString(0));\r
168                         tempNotebook.setUsername(query.valueString(1));\r
169                         tempNotebook.setShardId(query.valueString(2));\r
170                         tempNotebook.setShareKey(query.valueString(3));\r
171                         tempNotebook.setUri(query.valueString(4));\r
172 \r
173                         index.add(tempNotebook); \r
174                 }       \r
175                 return index;\r
176         }                       \r
177 \r
178         // does a record exist?\r
179         public String findNotebookByShareName(String name) {\r
180                 \r
181                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
182                 \r
183                 query.prepare("Select guid from Linkednotebook where shareName=:name");\r
184                 query.bindValue(":name", name);\r
185                 if (!query.exec())\r
186                         logger.log(logger.EXTREME, "Linked notebook SQL retrieve by share name has failed.");\r
187                 String val = null;\r
188                 if (query.next())\r
189                         val = query.valueString(0);\r
190                 return val;\r
191         }\r
192 \r
193         \r
194         // Get a list of linked notebooks that need to be updated\r
195         public List<String> getDirtyGuids() {\r
196                 List<String> index = new ArrayList<String>();\r
197                 boolean check;  \r
198         NSqlQuery query = new NSqlQuery(db.getConnection());\r
199                                         \r
200                 check = query.exec("Select id from LinkedNotebook where isDirty = true");\r
201                 if (!check) \r
202                         logger.log(logger.EXTREME, "LinkedNotebook SQL retrieve has failed in getdirtyIds.");\r
203                 while (query.next()) {\r
204                         index.add(query.valueString(0));\r
205                 }       \r
206                 return index;   \r
207         }\r
208 \r
209 }\r
210 \r