OSDN Git Service

Add initial logic for setting up shared/published notebooks. Added logic to download...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / NoteResourceTable.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 import java.text.SimpleDateFormat;\r
23 import java.util.ArrayList;\r
24 import java.util.List;\r
25 \r
26 import com.evernote.edam.type.Data;\r
27 import com.evernote.edam.type.Resource;\r
28 import com.evernote.edam.type.ResourceAttributes;\r
29 import com.trolltech.qt.core.QByteArray;\r
30 \r
31 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
32 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
33 \r
34 \r
35 \r
36 public class NoteResourceTable  {\r
37         /**\r
38          * \r
39          */\r
40         private static final long serialVersionUID = 1L;\r
41         private final ApplicationLogger                 logger;\r
42         private final DatabaseConnection                db;     \r
43         \r
44         // Constructor\r
45         public NoteResourceTable(ApplicationLogger l, DatabaseConnection d) {\r
46                 logger = l;\r
47                 db = d;\r
48         }\r
49         // Create the table\r
50         public void createTable() {\r
51                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
52         // Create the NoteResource table\r
53         logger.log(logger.HIGH, "Creating table NoteResource...");\r
54         if (!query.exec("Create table NoteResources (guid varchar primary key, " +\r
55                         "noteGuid varchar, updateSequenceNumber integer, dataHash varchar, "+\r
56                         "dataSize integer, dataBinary blob, "+\r
57                         "mime varchar, width integer, height integer, duration integer, "+\r
58                         "active integer, recognitionHash varchar, recognitionSize integer, " +\r
59                         "recognitionBinary varchar, attributeSourceUrl varchar, attributeTimestamp timestamp, " +\r
60                         "attributeLatitude double, attributeLongitude double, "+\r
61                         "attributeAltitude double, attributeCameraMake varchar, attributeCameraModel varchar, "\r
62                         +"attributeClientWillIndex varchar, attributeRecoType varchar, attributeFileName varchar,"+\r
63                         "attributeAttachment boolean, isDirty boolean, indexNeeded boolean)"))\r
64                 logger.log(logger.HIGH, "Table NoteResource creation FAILED!!!"); \r
65         if (!query.exec("CREATE INDEX unindexed_resources on noteresources (indexneeded desc, guid);"))\r
66                 logger.log(logger.HIGH, "Noteresources unindexed_resources index creation FAILED!!!");\r
67         if (!query.exec("CREATE INDEX resources_dataheshhex on noteresources (datahash, guid);"))\r
68                 logger.log(logger.HIGH, "Noteresources resources_datahash index creation FAILED!!!");  \r
69         \r
70         }\r
71         // Drop the table\r
72         public void dropTable() {               \r
73                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
74                 query.exec("Drop table NoteResources");\r
75         }\r
76         // Reset the dirty flag\r
77         public void  resetDirtyFlag(String guid) {\r
78                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
79                 \r
80                 query.prepare("Update noteresources set isdirty=false where guid=:guid");\r
81                 query.bindValue(":guid", guid);\r
82                 if (!query.exec())\r
83                         logger.log(logger.EXTREME, "Error resetting noteresource dirty field. " +query.lastError());\r
84         }\r
85         // Set if the resource should be indexed\r
86         public void  setIndexNeeded(String guid, Boolean indexNeeded) {\r
87                 NSqlQuery query = new NSqlQuery(db.getConnection());            \r
88                 query.prepare("Update noteresources set indexNeeded=:needed where guid=:guid");\r
89                 query.bindValue(":needed", indexNeeded);\r
90                 query.bindValue(":guid", guid);\r
91                 if (!query.exec())\r
92                         logger.log(logger.EXTREME, "Error setting noteresource indexneeded field: " +query.lastError());\r
93         }\r
94         // get any unindexed resource\r
95         public List<String> getNextUnindexed(int limit) {\r
96                 List<String> guids = new ArrayList<String>();\r
97         NSqlQuery query = new NSqlQuery(db.getConnection());\r
98                                         \r
99                 if (!query.exec("Select guid from NoteResources where indexNeeded = true limit " +limit))\r
100                         logger.log(logger.EXTREME, "NoteResources SQL retrieve has failed on getNextUnindexed(): " +query.lastError());\r
101 \r
102                 // Get a list of the notes\r
103                 String guid;\r
104                 while (query.next()) {\r
105                         guid = new String();\r
106                         guid = query.valueString(0);\r
107                         guids.add(guid);\r
108                 }       \r
109                 return guids;   \r
110         }\r
111 \r
112         public List<String> findInkNotes() {\r
113                 List<String> guids = new ArrayList<String>();\r
114                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
115                 \r
116                 query.prepare("Select guid from noteresources where mime='application/vnd.evernote.ink'");\r
117                 if (!query.exec())\r
118                         logger.log(logger.EXTREME, "Error searching for ink notes. " +query.lastError());\r
119                 \r
120                 while (query.next()) {\r
121                         guids.add(query.valueString(0));\r
122                 }\r
123                 return guids;\r
124         }\r
125         \r
126         public void saveNoteResource(Resource r, boolean isDirty) {\r
127                 logger.log(logger.HIGH, "Entering DBRunner.saveNoteResources");\r
128                 boolean check;\r
129                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
130                 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");\r
131                 \r
132                 check = query.prepare("Insert Into NoteResources ("\r
133                                 +"guid, noteGuid, dataHash, dataSize, dataBinary, updateSequenceNumber, "\r
134                                 +"mime, width, height, duration, active, recognitionHash, "                             \r
135                                 +"recognitionSize, recognitionBinary, attributeSourceUrl, attributeTimestamp, "\r
136                                 +"attributeLatitude, attributeLongitude, attributeAltitude, attributeCameraMake, "\r
137                                 +"attributeCameraModel, "\r
138                                 +"attributeClientWillIndex, attributeRecoType, attributeFileName, attributeAttachment, isDirty, "\r
139                                 +"indexNeeded) Values("\r
140                                 +":guid, :noteGuid, :dataHash,:dataSize, :dataBody, :updateSequenceNumber, "\r
141                                 +":mime, :width, :height, :duration, :active, :recognitionHash, "                               \r
142                                 +":recognitionSize, :recognitionBody, :attributeSourceUrl, :attributeTimestamp, "\r
143                                 +":attributeLatitude, :attributeLongitude, :attributeAltitude, :attributeCameraMake, "\r
144                                 +":attributeCameraModel, "\r
145                                 +":attributeClientWillIndex, :attributeRecoType, :attributeFileName, :attributeAttachment, "\r
146                                 +":isDirty, true)");\r
147                 if (!check) {\r
148                         logger.log(logger.EXTREME, "NoteResource SQL insert prepare has failed.");\r
149                         logger.log(logger.MEDIUM, query.lastError());\r
150                 }\r
151         \r
152                         query.bindValue(":guid", r.getGuid());\r
153                         query.bindValue(":noteGuid", r.getNoteGuid());\r
154                         if (r.getData() != null) {\r
155 //                              query.bindValue(":dataHash", new QByteArray(r.getData().getBodyHash()).toHex());\r
156 //                              query.bindValue(":dataHash", "");\r
157                                 query.bindValue(":dataHash", byteArrayToHexString(r.getData().getBodyHash()));\r
158                                 query.bindValue(":dataSize", r.getData().getSize());\r
159                                 query.bindBlob(":dataBody", r.getData().getBody());\r
160                         }\r
161                         query.bindValue(":updateSequenceNumber", r.getUpdateSequenceNum());\r
162                         query.bindValue(":mime", r.getMime());\r
163                         query.bindValue(":width", new Integer(r.getWidth()));\r
164                         query.bindValue(":height", new Integer(r.getHeight()));\r
165                         query.bindValue(":duration", new Integer(r.getDuration()));\r
166                         query.bindValue(":active", r.isActive());\r
167                         if (r.getRecognition() != null) {\r
168                                 query.bindValue(":recognitionHash", r.getRecognition().getBodyHash());\r
169                                 query.bindValue(":recognitionSize", r.getRecognition().getSize());\r
170                                 if (r.getRecognition().getBody() != null)\r
171                                         query.bindValue(":recognitionBody", new String(r.getRecognition().getBody()));\r
172                                 else\r
173                                         query.bindValue(":recognitionBody", "");\r
174                         } else {\r
175                                 query.bindValue(":recognitionHash", "");\r
176                                 query.bindValue(":recognitionSize", 0);\r
177                                 query.bindValue(":recognitionBody", "");\r
178                         }\r
179                         if (r.getAttributes() != null) {\r
180                                 query.bindValue(":attributeSourceUrl", r.getAttributes().getSourceURL());\r
181                                 StringBuilder ts = new StringBuilder(simple.format(r.getAttributes().getTimestamp()));\r
182                                 query.bindValue(":attributeTimestamp", ts.toString());\r
183                                 query.bindValue(":attributeLatitude", r.getAttributes().getLatitude());\r
184                                 query.bindValue(":attributeLongitude", r.getAttributes().getLongitude());\r
185                                 query.bindValue(":attributeAltitude", r.getAttributes().getAltitude());\r
186                                 query.bindValue(":attributeCameraMake", r.getAttributes().getCameraMake());\r
187                                 query.bindValue(":attributeCameraModel", r.getAttributes().getCameraModel());\r
188                                 query.bindValue(":attributeClientWillIndex", r.getAttributes().isClientWillIndex());\r
189                                 query.bindValue(":attributeRecoType", r.getAttributes().getRecoType());\r
190                                 query.bindValue(":attributeFileName", r.getAttributes().getFileName());\r
191                                 query.bindValue(":attributeAttachment", r.getAttributes().isAttachment());                      \r
192                         }\r
193                         query.bindValue(":isDirty", isDirty);\r
194                                                 \r
195                         check = query.exec();\r
196                         if (!check) {\r
197                                 logger.log(logger.MEDIUM, "*** NoteResource Table insert failed.");             \r
198                                 logger.log(logger.MEDIUM, query.lastError());\r
199                         }\r
200                         \r
201                                                 \r
202                         logger.log(logger.HIGH, "Leaving DBRunner.saveNoteResources");\r
203         }\r
204         // delete an old resource\r
205         public void expungeNoteResource(String guid) {\r
206                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
207                 query.prepare("delete from NoteResources where guid=:guid");\r
208                 query.bindValue(":guid", guid);\r
209                 query.exec();\r
210 \r
211                 query.prepare("delete from InkImages where guid=:guid");\r
212                 query.bindValue(":guid", guid);\r
213                 query.exec();\r
214 \r
215         }\r
216 \r
217         \r
218         // Get a note resource from the database by it's hash value\r
219         public String getNoteResourceGuidByHashHex(String noteGuid, String hash) {\r
220                 logger.log(logger.HIGH, "Entering DBRunner.getNoteResourceGuidByHashHex");\r
221 \r
222                 boolean check;\r
223                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
224                 \r
225                 check = query.prepare("Select guid from NoteResources " +\r
226                                         "where noteGuid=:noteGuid and dataHash=:hash");\r
227                 if (check)\r
228                         logger.log(logger.EXTREME, "NoteResource SQL select prepare was successful.");\r
229                 else {\r
230                         logger.log(logger.EXTREME, "NoteResource SQL select prepare has failed.");\r
231                         logger.log(logger.MEDIUM, query.lastError());\r
232                 }\r
233                 query.bindValue(":noteGuid", noteGuid);\r
234                 query.bindValue(":hash", hash);\r
235         \r
236                 check = query.exec();\r
237                 if (!check)      {\r
238                         logger.log(logger.MEDIUM, "dbRunner.getNoteResourceGuidByHashHex Select failed." +\r
239                                         "Note Guid:" +noteGuid+\r
240                                         "Data Body Hash:" +hash);               \r
241                         logger.log(logger.MEDIUM, query.lastError());\r
242                 }\r
243                 if (!query.next()) {\r
244                         logger.log(logger.MEDIUM, "Note Resource not found.");\r
245                         return null;\r
246                 }\r
247                 return query.valueString(0);\r
248         }\r
249 \r
250         // Get a note resource from the database by it's hash value\r
251         public Resource getNoteResourceDataBodyByHashHex(String noteGuid, String hash) {\r
252                 logger.log(logger.HIGH, "Entering DBRunner.getNoteResourceDataBodyByHash");\r
253 \r
254                 boolean check;\r
255                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
256                 \r
257                 check = query.prepare("Select guid, mime, from NoteResources " +\r
258                                         "where noteGuid=:noteGuid and dataHash=:hash");\r
259                 if (!check) {\r
260                         logger.log(logger.EXTREME, "NoteResource SQL select prepare has failed.");\r
261                         logger.log(logger.MEDIUM, query.lastError());\r
262                 }\r
263                 query.bindValue(":noteGuid", noteGuid);\r
264                 query.bindValue(":hash", hash);\r
265         \r
266                 if (!query.exec()) {\r
267                         logger.log(logger.MEDIUM, "NoteResource Select failed." +\r
268                                         "Note Guid:" +noteGuid+\r
269                                         "Data Body Hash:" +hash);               \r
270                         logger.log(logger.MEDIUM, query.lastError());\r
271                 }\r
272                 if (!query.next()) {\r
273                         logger.log(logger.MEDIUM, "Note Resource not found.");\r
274                         return null;\r
275                 }\r
276                 \r
277                 Resource r = new Resource();\r
278                 r.setGuid(query.valueString(0));\r
279                 r.setMime(query.valueString(1));\r
280                 \r
281                 NSqlQuery binary = new NSqlQuery(db.getConnection());\r
282                 if (!binary.prepare("Select databinary from NoteResources " +\r
283                                         "where guid=:guid")) {\r
284                         logger.log(logger.MEDIUM, "Prepare for NoteResources Binary failed");\r
285                         logger.log(logger.MEDIUM, binary.lastError());\r
286                 }\r
287                 \r
288                 if (!binary.exec()) {\r
289                         logger.log(logger.MEDIUM, "NoteResources Binary Select failed." +\r
290                                         "Note Guid:" +noteGuid+\r
291                                         "Data Body Hash:" +hash);               \r
292                         logger.log(logger.MEDIUM, binary.lastError());\r
293                 }\r
294                 if (!binary.next()) {\r
295                         logger.log(logger.MEDIUM, "Note Resource Binary not found.");\r
296                         return null;\r
297                 }\r
298                 \r
299                 Data d = new Data();\r
300                 r.setData(d);\r
301                 d.setBody(binary.valueString(0).getBytes());\r
302                 logger.log(logger.HIGH, "Leaving DBRunner.getNoteResourceDataBodyByHash");\r
303                 return r;\r
304         }\r
305 \r
306         \r
307         // Get a note's resourcesby Guid\r
308         public Resource getNoteResource(String guid, boolean withBinary) {\r
309                 if (guid == null)\r
310                         return null;\r
311                 \r
312                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
313                 String queryString;\r
314                 queryString = new String("Select guid, noteGuid, mime, width, height, duration, "\r
315                                 +"active, updateSequenceNumber, dataHash, dataSize, "\r
316                                 +"recognitionHash, recognitionSize, "\r
317                                 +"attributeLatitude, attributeLongitude, attributeAltitude, "\r
318                                 +"attributeCameraMake, attributeCameraModel, attributeClientWillIndex, "\r
319                                 +"attributeRecoType, attributeFileName, attributeAttachment, recognitionBinary "\r
320                                 +" from NoteResources where guid=:guid");\r
321 \r
322                 \r
323                 query.prepare(queryString);\r
324                 \r
325                 query.bindValue(":guid", guid);\r
326                 if (!query.exec()) {\r
327                         logger.log(logger.EXTREME, "NoteResources SQL select has failed.");\r
328                         logger.log(logger.MEDIUM, query.lastError());\r
329                         return null;\r
330                 }\r
331                 Resource r = null;\r
332                 if (query.next()) {\r
333                                                                         \r
334                         r = new Resource();\r
335                         r.setGuid(query.valueString(0));        // Resource Guid\r
336                         r.setNoteGuid(query.valueString(1));   // note Guid\r
337                         r.setMime(query.valueString(2));       // Mime Type\r
338                         r.setWidth(new Short(query.valueString(3)));  // Width\r
339                         r.setHeight(new Short(query.valueString(4)));  // Height\r
340                         r.setDuration(new Short(query.valueString(5)));  // Duration\r
341                         r.setActive(new Boolean(query.valueString(6)));  // active\r
342                         r.setUpdateSequenceNum(new Integer(query.valueString(7)));  // update sequence number\r
343                         \r
344                         Data d = new Data();\r
345                         byte[] h = query.valueString(8).getBytes();    // data hash\r
346                         QByteArray hData = new QByteArray(h);\r
347                         QByteArray bData = new QByteArray(QByteArray.fromHex(hData));\r
348                         d.setBodyHash(bData.toByteArray());\r
349                         d.setSize(new Integer(query.valueString(9)));\r
350                         r.setData(d);\r
351                         \r
352                         Data rec = new Data();\r
353                         if (query.valueObject(10) != null)\r
354                                 rec.setBodyHash(query.valueString(10).getBytes());   // Recognition Hash\r
355                         if (query.valueObject(11) != null)\r
356                                 rec.setSize(new Integer(query.valueString(11)));\r
357                         else\r
358                                 rec.setSize(0);\r
359                         r.setRecognition(rec);\r
360 \r
361                         ResourceAttributes a = new ResourceAttributes();\r
362                         if (!query.valueString(12).equals(""))              // Latitude\r
363                                 a.setLatitude(new Float(query.valueString(12)));\r
364                         if (!query.valueString(13).equals(""))              // Longitude\r
365                                 a.setLongitude(new Float(query.valueString(13)));\r
366                         if (!query.valueString(14).equals(""))              // Altitude\r
367                                 a.setAltitude(new Float(query.valueString(14)));\r
368                         a.setCameraMake(stringValue(query.valueString(15)));              // Camera Make\r
369                         a.setCameraModel(stringValue(query.valueString(16)));\r
370                         a.setClientWillIndex(booleanValue(query.valueString(17).toString(),false));  // Camera Model\r
371                         a.setRecoType(stringValue(query.valueString(18)));                 // Recognition Type\r
372                         a.setFileName(stringValue(query.valueString(19)));                  // File Name\r
373                         a.setAttachment(booleanValue(query.valueString(20).toString(),false));\r
374                         r.setAttributes(a);\r
375                 \r
376                         if (withBinary) {\r
377                             \r
378                                 query.prepare("Select dataBinary from NoteResources where guid=:guid");\r
379                                 query.bindValue(":guid", r.getGuid());\r
380                                 query.exec();\r
381                                 if (query.next()) {\r
382                                         byte[] b = query.getBlob(0);\r
383                                         r.getData().setBody(b);\r
384                                 }\r
385                         } \r
386                 }\r
387                 return r;\r
388         }\r
389         \r
390         \r
391         // Get a note's resourcesby Guid\r
392         public List<Resource> getNoteResources(String noteGuid, boolean withBinary) {\r
393                 if (noteGuid == null)\r
394                         return null;\r
395                 List<Resource> res = new ArrayList<Resource>();\r
396                 \r
397                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
398                 query.prepare("Select guid"\r
399                                 +" from NoteResources where noteGuid = :noteGuid");\r
400                 query.bindValue(":noteGuid", noteGuid);\r
401                 if (!query.exec()) {\r
402                         logger.log(logger.EXTREME, "NoteResources SQL select has failed.");\r
403                         logger.log(logger.MEDIUM, query.lastError());\r
404                         return null;\r
405                 }\r
406                 while (query.next()) {\r
407                         String guid = (query.valueString(0));\r
408                         res.add(getNoteResource(guid, withBinary));\r
409                 }       \r
410                 return res;\r
411         }\r
412         // Get all of a note's recognition data by the note guid\r
413         public List<Resource> getNoteResourcesRecognition(String noteGuid) {\r
414                 if (noteGuid == null)\r
415                         return null;\r
416                 boolean check;\r
417                 List<Resource> res = new ArrayList<Resource>();\r
418                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
419                 check = query.prepare("Select "\r
420                                 +"recognitionHash, recognitionSize, recognitionBinary "\r
421                                 +" from NoteResources where noteGuid=:guid");\r
422                 if (!check) {\r
423                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition SQL prepare has failed.");\r
424                         logger.log(logger.MEDIUM, query.lastError());\r
425                         return null;\r
426                 }\r
427                 query.bindValue(":guid", noteGuid);\r
428                 if (!check) {\r
429                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition exec has failed.");\r
430                         logger.log(logger.MEDIUM, query.lastError());\r
431                         return null;\r
432                 }\r
433                 while (query.next()) {  \r
434                         Resource r = new Resource();            \r
435                         res.add(r);\r
436                         Data rec = new Data();\r
437                         rec.setBodyHash(query.valueString(0).getBytes());\r
438                         String x = new String(query.valueString(1));\r
439                         if (!x.equals("")) {\r
440                                 rec.setSize(new Integer(x));\r
441                                 rec.setBody(query.valueString(2).getBytes());\r
442                         } else\r
443                                 rec.setSize(0);\r
444                         r.setRecognition(rec);\r
445                 }       \r
446                 return res;\r
447         }\r
448         \r
449         \r
450         // Get a note's recognition data by it's guid.\r
451         public Resource getNoteResourceRecognition(String guid) {\r
452                 if (guid == null)\r
453                         return null;\r
454                 boolean check;\r
455                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
456                 check = query.prepare("Select "\r
457                                 +"recognitionHash, recognitionSize, recognitionBinary, noteGuid "\r
458                                 +" from NoteResources where guid=:guid");\r
459                 if (!check) {\r
460                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition SQL prepare has failed.");\r
461                         logger.log(logger.MEDIUM, query.lastError());\r
462                         return null;\r
463                 }\r
464                 query.bindValue(":guid", guid);\r
465                 query.exec();\r
466                 if (!check) {\r
467                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition exec has failed.");\r
468                         logger.log(logger.MEDIUM, query.lastError());\r
469                         return null;\r
470                 }\r
471                 Resource r = null;\r
472                 while (query.next()) {\r
473                                                                         \r
474                         r = new Resource();             \r
475                         Data rec = new Data();\r
476                         rec.setBodyHash(query.valueString(0).getBytes());\r
477                         String x = new String(query.valueString(1));\r
478                         if (!x.equals("")) {\r
479                                 rec.setSize(new Integer(x));\r
480                                 rec.setBody(query.valueString(2).getBytes());\r
481                         } else\r
482                                 rec.setSize(0);\r
483                         r.setRecognition(rec);\r
484                         r.setNoteGuid(query.valueString(3));\r
485                 }       \r
486                 return r;\r
487         }\r
488 \r
489         // Save Note Resource\r
490         public void updateNoteResource(Resource r, boolean isDirty) {\r
491                 logger.log(logger.HIGH, "Entering ListManager.updateNoteResource");\r
492                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
493                 query.prepare("delete from NoteResources where guid=:recGuid");\r
494                 query.bindValue(":recGuid", r.getGuid());\r
495                 query.exec();\r
496                 saveNoteResource(r, isDirty);\r
497                 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResource");\r
498         }\r
499         // Update note resource GUID\r
500         public void updateNoteResourceGuid(String oldGuid, String newGuid, boolean isDirty) {\r
501                 logger.log(logger.HIGH, "Entering RNoteResourceTable.updateNoteResourceGuid");\r
502                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
503                 query.prepare("update NoteResources set guid=:newGuid, isDirty=:isDirty where guid=:oldGuid");\r
504                 query.bindValue(":newGuid", newGuid);\r
505                 query.bindValue(":isDirty", isDirty);\r
506                 query.bindValue(":oldGuid", oldGuid);\r
507                 query.exec();\r
508                 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResourceGuid");\r
509         }\r
510         // Update note resource GUID\r
511         public void resetUpdateSequenceNumber(String guid, boolean isDirty) {\r
512                 logger.log(logger.HIGH, "Entering RNoteResourceTable.updateNoteResourceGuid");\r
513                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
514                 query.prepare("update NoteResources set updateSequenceNumber=0, isDirty=:isDirty where guid=:guid");\r
515                 query.bindValue(":isDirty", isDirty);\r
516                 query.bindValue(":guid", guid);\r
517                 query.exec();\r
518                 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResourceGuid");\r
519         }\r
520         \r
521         // Drop the table\r
522         public void reindexAll() {              \r
523                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
524                 query.exec("Update NoteResources set indexneeded=true");\r
525         }\r
526         // Count unindexed notes\r
527         public int getResourceCount() {\r
528         NSqlQuery query = new NSqlQuery(db.getConnection());\r
529                 query.exec("select count(*) from noteresources");\r
530                 query.next(); \r
531                 int returnValue = new Integer(query.valueString(0));\r
532                 return returnValue;\r
533         }\r
534 \r
535         //********************************************\r
536         //** Utility Functions\r
537         //********************************************\r
538         // Convert a byte array to a hex string\r
539         private static String byteArrayToHexString(byte data[]) {\r
540                 StringBuffer buf = new StringBuffer();\r
541             for (byte element : data) {\r
542                 int halfbyte = (element >>> 4) & 0x0F;\r
543                 int two_halfs = 0;\r
544                 do {\r
545                         if ((0 <= halfbyte) && (halfbyte <= 9))\r
546                                buf.append((char) ('0' + halfbyte));\r
547                            else\r
548                                 buf.append((char) ('a' + (halfbyte - 10)));\r
549                         halfbyte = element & 0x0F;\r
550                 } while(two_halfs++ < 1);\r
551             }\r
552             return buf.toString();              \r
553         }\r
554 \r
555         \r
556         private String stringValue(Object value) {\r
557                 if (value != null && value.toString() != null) \r
558                         return value.toString();\r
559                 else\r
560                         return null;\r
561         }\r
562         \r
563         private  boolean booleanValue(Object value, boolean unknown) {\r
564                 if (value != null && value.toString() != null) {\r
565                         try {\r
566                                 if ((Integer)value > 0)\r
567                                         return true;\r
568                                 else\r
569                                         return false;\r
570                         } catch (java.lang.ClassCastException e) {\r
571                                 try {\r
572                                         String stringValue = (String)value;\r
573                                         if (stringValue.equalsIgnoreCase("true"))\r
574                                                 return true;\r
575                                         else \r
576                                                 return false;\r
577                                 } catch (java.lang.ClassCastException e1) { \r
578                                         return unknown;\r
579                                 }\r
580                         }\r
581                 }\r
582                 else\r
583                         return unknown;\r
584         }\r
585 \r
586 }\r