OSDN Git Service

Resync Linked & shared notebook tables.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / SharedNotebookTable.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.text.DateFormat;\r
24 import java.text.ParseException;\r
25 import java.text.SimpleDateFormat;\r
26 import java.util.ArrayList;\r
27 import java.util.List;\r
28 \r
29 import com.evernote.edam.type.SharedNotebook;\r
30 \r
31 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
32 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
33 \r
34 public class SharedNotebookTable {\r
35         \r
36         private final ApplicationLogger                 logger;\r
37         DatabaseConnection                                                      db;\r
38         \r
39         // Constructor\r
40         public SharedNotebookTable(ApplicationLogger l, DatabaseConnection d) {\r
41                 logger = l;\r
42                 db = d;\r
43         }\r
44         // Create the table\r
45         public void createTable() {\r
46                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
47         logger.log(logger.HIGH, "Creating table SharedNotebook...");\r
48         if (!query.exec("Create table SharedNotebook (id long primary key, " +\r
49                         "userid Integer, " +\r
50                         "notebookGuid VarChar, "+\r
51                         "email VarChar, "+\r
52                         "notebookModifiable boolean, " +\r
53                         "requireLogin boolean, "+\r
54                         "serviceCreated timestamp, "+\r
55                         "shareKey VarChar,  "+\r
56                         "username VarChar, "+\r
57                         "icon blob, " +\r
58                         "isDirty boolean)"))                            \r
59                 logger.log(logger.HIGH, "Table SharedNotebook creation FAILED!!!");   \r
60         }\r
61         // Drop the table\r
62         public void dropTable() {\r
63                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
64                 query.exec("Drop table SharedNotebook");\r
65         }\r
66         // Save an individual notebook\r
67         public void addNotebook(SharedNotebook tempNotebook, boolean isDirty) {\r
68                 boolean check;\r
69                 \r
70                 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
71         NSqlQuery query = new NSqlQuery(db.getConnection());\r
72                 check = query.prepare("Insert Into SharedNotebook (id, userid, notebookGuid, email,  "\r
73                                 +"notebookModifiable, requireLogin, serviceCreated, shareKey, username, isDirty) "   \r
74                                 + " Values("\r
75                                 +":id, :userid, :notebookGuid, :email, "\r
76                                 +":notebookModifiable, :requireLogin, :serviceCreated, "\r
77                                 +":shareKey, :username, :isDirty)");\r
78                 query.bindValue(":id", tempNotebook.getId());\r
79                 query.bindValue(":userid", tempNotebook.getUserId());\r
80                 query.bindValue(":notebookGuid", tempNotebook.getNotebookGuid());\r
81                 query.bindValue(":email", tempNotebook.getEmail());\r
82                 query.bindValue(":notebookModifiable", tempNotebook.isNotebookModifiable());\r
83                 query.bindValue(":requireLogin", tempNotebook.isRequireLogin());\r
84 \r
85                 StringBuilder serviceCreated = new StringBuilder(simple.format(tempNotebook.getServiceCreated()));                      \r
86                 query.bindValue(":serviceCreated", serviceCreated.toString());\r
87                 \r
88                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
89                 query.bindValue(":username", tempNotebook.getUsername());\r
90                 \r
91                 if (isDirty)\r
92                         query.bindValue(":isDirty", true);\r
93                 else\r
94                         query.bindValue(":isDirty", false);\r
95 \r
96                 check = query.exec();\r
97                 if (!check) {\r
98                         logger.log(logger.MEDIUM, "SharedNotebook Table insert failed.");\r
99                         logger.log(logger.MEDIUM, query.lastError().toString());\r
100                 }\r
101         }\r
102         // Check if a notebook exists\r
103         public boolean exists(long id) {\r
104         NSqlQuery query = new NSqlQuery(db.getConnection());\r
105         boolean check = query.prepare("Select id from sharednotebook where id=:id");\r
106         query.bindValue(":id", id);\r
107                 check = query.exec();\r
108                 if (!check) {\r
109                         logger.log(logger.MEDIUM, "SharedNotebook Table exists check failed.");\r
110                         logger.log(logger.MEDIUM, query.lastError().toString());\r
111                 }\r
112                 if (query.next())\r
113                         return true;\r
114                 return false;\r
115         }\r
116         // Delete the notebook based on a id\r
117         public void expungeNotebook(long id, boolean needsSync) {\r
118                 boolean check;\r
119         NSqlQuery query = new NSqlQuery(db.getConnection());\r
120 \r
121         check = query.prepare("delete from SharedNotebook "\r
122                                 +"where id=:id");\r
123                 if (!check) {\r
124                         logger.log(logger.EXTREME, "SharedNotebook SQL delete prepare has failed.");\r
125                         logger.log(logger.EXTREME, query.lastError().toString());\r
126                 }\r
127                 query.bindValue(":id", id);\r
128                 check = query.exec();\r
129                 if (!check) \r
130                         logger.log(logger.MEDIUM, "SharedNotebook delete failed.");\r
131                 \r
132                 // Signal the parent that work needs to be done\r
133                 if  (needsSync) {\r
134                         DeletedTable deletedTable = new DeletedTable(logger, db);\r
135                         deletedTable.addDeletedItem(new Long(id).toString(), "SharedNotebook");\r
136                 }\r
137         }\r
138         // Delete the notebook based on a id\r
139         public void expungeNotebookByGuid(String id, boolean needsSync) {\r
140                 boolean check;\r
141         NSqlQuery query = new NSqlQuery(db.getConnection());\r
142 \r
143         check = query.prepare("delete from SharedNotebook "\r
144                                 +"where guid=:id");\r
145                 if (!check) {\r
146                         logger.log(logger.EXTREME, "SharedNotebook SQL delete by notebook guid prepare has failed.");\r
147                         logger.log(logger.EXTREME, query.lastError().toString());\r
148                 }\r
149                 query.bindValue(":id", id);\r
150                 check = query.exec();\r
151                 if (!check) \r
152                         logger.log(logger.MEDIUM, "SharedNotebook delete by notebook guid failed.");\r
153                 \r
154                 // Signal the parent that work needs to be done\r
155                 if  (needsSync) {\r
156                         DeletedTable deletedTable = new DeletedTable(logger, db);\r
157                         deletedTable.addDeletedItem(new Long(id).toString(), "SharedNotebook");\r
158                 }\r
159         }\r
160 \r
161         \r
162         // Update a notebook\r
163         public void updateNotebook(SharedNotebook tempNotebook, boolean isDirty) {\r
164                 boolean check;\r
165                 if (!exists(tempNotebook.getId())) {\r
166                         addNotebook(tempNotebook, isDirty);\r
167                         return;\r
168                 }\r
169                 \r
170                 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
171                 \r
172                 StringBuilder serviceCreated = new StringBuilder(simple.format(tempNotebook.getServiceCreated()));                                              \r
173         NSqlQuery query = new NSqlQuery(db.getConnection());\r
174         check = query.prepare("Update SharedNotebook set id=:id, userid=:userid, notebookGuid=:notebook, "\r
175                         + "email=:email, notebookModifiable=:mod, requireLogin=:rlogin, serviceCreated=:serviceCreated, "\r
176                         + "shareKey=:shareKey, username=:username, isDirty=:isdirty");\r
177                 query.bindValue(":id", tempNotebook.getId());\r
178                 query.bindValue(":userid", tempNotebook.getUserId());\r
179                 query.bindValue(":notebook", tempNotebook.getNotebookGuid());\r
180                 query.bindValue(":email", tempNotebook.getEmail());\r
181                 query.bindValue(":mod", tempNotebook.isNotebookModifiable());\r
182                 query.bindValue(":rlogin", tempNotebook.isRequireLogin());\r
183                 query.bindValue(":serviceCreated", serviceCreated.toString());\r
184                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
185                 query.bindValue(":username", tempNotebook.getUsername());\r
186                 \r
187                 query.bindValue(":isDirty", isDirty);\r
188                 \r
189                 check = query.exec();\r
190                 if (!check) {\r
191                         logger.log(logger.MEDIUM, "SharedNotebook Table update failed.");\r
192                         logger.log(logger.MEDIUM, query.lastError().toString());\r
193                 }\r
194         }\r
195         // Load notebooks from the database\r
196         public List<SharedNotebook> getAll() {\r
197                 SharedNotebook tempNotebook;\r
198                 List<SharedNotebook> index = new ArrayList<SharedNotebook>();\r
199                 boolean check;\r
200                                         \r
201         NSqlQuery query = new NSqlQuery(db.getConnection());\r
202                                         \r
203                 check = query.exec("Select id, userid, notebookGuid, email, notebookModifiable, requireLogin, " +\r
204                                 "serviceCreated, "+\r
205                                 "shareKey, username from SharedNotebook");\r
206                 if (!check)\r
207                         logger.log(logger.EXTREME, "Notebook SQL retrieve has failed.");\r
208                 while (query.next()) {\r
209                         tempNotebook = new SharedNotebook();\r
210                         tempNotebook.setId(query.valueLong(0));\r
211                         tempNotebook.setUserId(query.valueInteger(1));\r
212                         tempNotebook.setNotebookGuid(query.valueString(2));\r
213                         tempNotebook.setEmail(query.valueString(3));\r
214                         tempNotebook.setNotebookModifiable(query.valueBoolean(4,false));\r
215                         tempNotebook.setRequireLogin(query.valueBoolean(5,true));\r
216                         DateFormat indfm = null;\r
217                         try {\r
218                                 indfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
219                         } catch (Exception e) { }\r
220                         try {\r
221                                 tempNotebook.setServiceCreated(indfm.parse(query.valueString(6)).getTime());\r
222                         } catch (ParseException e) {\r
223                                 e.printStackTrace();\r
224                         }\r
225                         tempNotebook.setShareKey(query.valueString(7));\r
226                         tempNotebook.setUsername(query.valueString(8));\r
227 \r
228                         index.add(tempNotebook); \r
229                 }       \r
230                 return index;\r
231         }                       \r
232 \r
233         // Load notebooks from the database\r
234         public List<SharedNotebook> getForNotebook(String guid) {\r
235                 SharedNotebook tempNotebook;\r
236                 List<SharedNotebook> index = new ArrayList<SharedNotebook>();\r
237                 boolean check;\r
238                                         \r
239         NSqlQuery query = new NSqlQuery(db.getConnection());\r
240                                         \r
241                 check = query.prepare("Select id, userid, notebookGuid, email, notebookModifiable, requireLogin, " +\r
242                                 "serviceCreated, "+\r
243                                 "shareKey, username from SharedNotebook where notebookGuid=:notebookGuid ");\r
244                 if (!check)\r
245                         logger.log(logger.EXTREME, "SharedNotebook getForNotebook SQL prepare has failed.");\r
246                 query.bindValue(":notebookGuid", guid);\r
247                 check = query.exec();\r
248                 if (!check)\r
249                         logger.log(logger.EXTREME, "SharedNotebook getForNotebook SQL exec has failed.");\r
250                 \r
251                 while (query.next()) {\r
252                         tempNotebook = new SharedNotebook();\r
253                         tempNotebook.setId(query.valueLong(0));\r
254                         tempNotebook.setUserId(query.valueInteger(1));\r
255                         tempNotebook.setNotebookGuid(query.valueString(2));\r
256                         tempNotebook.setEmail(query.valueString(3));\r
257                         tempNotebook.setNotebookModifiable(query.valueBoolean(4,false));\r
258                         tempNotebook.setRequireLogin(query.valueBoolean(5,true));\r
259                         DateFormat indfm = null;\r
260                         try {\r
261                                 indfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
262                         } catch (Exception e) { }\r
263                         try {\r
264                                 tempNotebook.setServiceCreated(indfm.parse(query.valueString(6)).getTime());\r
265                         } catch (ParseException e) {\r
266                                 e.printStackTrace();\r
267                         }\r
268                         tempNotebook.setShareKey(query.valueString(7));\r
269                         tempNotebook.setUsername(query.valueString(8));\r
270 \r
271                         index.add(tempNotebook); \r
272                 }       \r
273                 return index;\r
274         }                       \r
275 \r
276         \r
277         // Get a list of shared notebooks that need to be updated\r
278         public List <Long> getDirtyIds() {\r
279                 List<Long> index = new ArrayList<Long>();\r
280                 boolean check;  \r
281         NSqlQuery query = new NSqlQuery(db.getConnection());\r
282                                         \r
283                 check = query.exec("Select id from SharedNotebook where isDirty = true");\r
284                 if (!check) \r
285                         logger.log(logger.EXTREME, "SharedNotebook SQL retrieve has failed in getdirtyIds.");\r
286                 while (query.next()) {\r
287                         index.add(query.valueLong(0));\r
288                 }       \r
289                 return index;   \r
290         }\r
291 \r
292 }\r
293 \r