OSDN Git Service

Add logic to display stacks in notebook tree
[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 \r
113         \r
114         public void saveNoteResource(Resource r, boolean isDirty) {\r
115                 logger.log(logger.HIGH, "Entering DBRunner.saveNoteResources");\r
116                 boolean check;\r
117                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
118                 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");\r
119                 \r
120                 check = query.prepare("Insert Into NoteResources ("\r
121                                 +"guid, noteGuid, dataHash, dataSize, dataBinary, updateSequenceNumber, "\r
122                                 +"mime, width, height, duration, active, recognitionHash, "                             \r
123                                 +"recognitionSize, recognitionBinary, attributeSourceUrl, attributeTimestamp, "\r
124                                 +"attributeLatitude, attributeLongitude, attributeAltitude, attributeCameraMake, "\r
125                                 +"attributeCameraModel, "\r
126                                 +"attributeClientWillIndex, attributeRecoType, attributeFileName, attributeAttachment, isDirty, "\r
127                                 +"indexNeeded) Values("\r
128                                 +":guid, :noteGuid, :dataHash,:dataSize, :dataBody, :updateSequenceNumber, "\r
129                                 +":mime, :width, :height, :duration, :active, :recognitionHash, "                               \r
130                                 +":recognitionSize, :recognitionBody, :attributeSourceUrl, :attributeTimestamp, "\r
131                                 +":attributeLatitude, :attributeLongitude, :attributeAltitude, :attributeCameraMake, "\r
132                                 +":attributeCameraModel, "\r
133                                 +":attributeClientWillIndex, :attributeRecoType, :attributeFileName, :attributeAttachment, "\r
134                                 +":isDirty, true)");\r
135                 if (!check) {\r
136                         logger.log(logger.EXTREME, "NoteResource SQL insert prepare has failed.");\r
137                         logger.log(logger.MEDIUM, query.lastError());\r
138                 }\r
139         \r
140                         query.bindValue(":guid", r.getGuid());\r
141                         query.bindValue(":noteGuid", r.getNoteGuid());\r
142                         if (r.getData() != null) {\r
143 //                              query.bindValue(":dataHash", new QByteArray(r.getData().getBodyHash()).toHex());\r
144 //                              query.bindValue(":dataHash", "");\r
145                                 query.bindValue(":dataHash", byteArrayToHexString(r.getData().getBodyHash()));\r
146                                 query.bindValue(":dataSize", r.getData().getSize());\r
147                                 query.bindBlob(":dataBody", r.getData().getBody());\r
148                         }\r
149                         query.bindValue(":updateSequenceNumber", r.getUpdateSequenceNum());\r
150                         query.bindValue(":mime", r.getMime());\r
151                         query.bindValue(":width", new Integer(r.getWidth()));\r
152                         query.bindValue(":height", new Integer(r.getHeight()));\r
153                         query.bindValue(":duration", new Integer(r.getDuration()));\r
154                         query.bindValue(":active", r.isActive());\r
155                         if (r.getRecognition() != null) {\r
156                                 query.bindValue(":recognitionHash", r.getRecognition().getBodyHash());\r
157                                 query.bindValue(":recognitionSize", r.getRecognition().getSize());\r
158                                 if (r.getRecognition().getBody() != null)\r
159                                         query.bindValue(":recognitionBody", new String(r.getRecognition().getBody()));\r
160                                 else\r
161                                         query.bindValue(":recognitionBody", "");\r
162                         } else {\r
163                                 query.bindValue(":recognitionHash", "");\r
164                                 query.bindValue(":recognitionSize", 0);\r
165                                 query.bindValue(":recognitionBody", "");\r
166                         }\r
167                         if (r.getAttributes() != null) {\r
168                                 query.bindValue(":attributeSourceUrl", r.getAttributes().getSourceURL());\r
169                                 StringBuilder ts = new StringBuilder(simple.format(r.getAttributes().getTimestamp()));\r
170                                 query.bindValue(":attributeTimestamp", ts.toString());\r
171                                 query.bindValue(":attributeLatitude", r.getAttributes().getLatitude());\r
172                                 query.bindValue(":attributeLongitude", r.getAttributes().getLongitude());\r
173                                 query.bindValue(":attributeAltitude", r.getAttributes().getAltitude());\r
174                                 query.bindValue(":attributeCameraMake", r.getAttributes().getCameraMake());\r
175                                 query.bindValue(":attributeCameraModel", r.getAttributes().getCameraModel());\r
176                                 query.bindValue(":attributeClientWillIndex", r.getAttributes().isClientWillIndex());\r
177                                 query.bindValue(":attributeRecoType", r.getAttributes().getRecoType());\r
178                                 query.bindValue(":attributeFileName", r.getAttributes().getFileName());\r
179                                 query.bindValue(":attributeAttachment", r.getAttributes().isAttachment());                      \r
180                         }\r
181                         query.bindValue(":isDirty", isDirty);\r
182                                                 \r
183                         check = query.exec();\r
184                         if (!check) {\r
185                                 logger.log(logger.MEDIUM, "*** NoteResource Table insert failed.");             \r
186                                 logger.log(logger.MEDIUM, query.lastError());\r
187                         }\r
188                         \r
189                                                 \r
190                         logger.log(logger.HIGH, "Leaving DBRunner.saveNoteResources");\r
191         }\r
192         // delete an old resource\r
193         public void expungeNoteResource(String guid) {\r
194                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
195                 query.prepare("delete from NoteResources where guid=:guid");\r
196                 query.bindValue(":guid", guid);\r
197                 query.exec();\r
198         }\r
199 \r
200         \r
201         // Get a note resource from the database by it's hash value\r
202         public String getNoteResourceGuidByHashHex(String noteGuid, String hash) {\r
203                 logger.log(logger.HIGH, "Entering DBRunner.getNoteResourceGuidByHashHex");\r
204 \r
205                 boolean check;\r
206                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
207                 \r
208                 check = query.prepare("Select guid from NoteResources " +\r
209                                         "where noteGuid=:noteGuid and dataHash=:hash");\r
210                 if (check)\r
211                         logger.log(logger.EXTREME, "NoteResource SQL select prepare was successful.");\r
212                 else {\r
213                         logger.log(logger.EXTREME, "NoteResource SQL select prepare has failed.");\r
214                         logger.log(logger.MEDIUM, query.lastError());\r
215                 }\r
216                 query.bindValue(":noteGuid", noteGuid);\r
217                 query.bindValue(":hash", hash);\r
218         \r
219                 check = query.exec();\r
220                 if (!check)      {\r
221                         logger.log(logger.MEDIUM, "dbRunner.getNoteResourceGuidByHashHex Select failed." +\r
222                                         "Note Guid:" +noteGuid+\r
223                                         "Data Body Hash:" +hash);               \r
224                         logger.log(logger.MEDIUM, query.lastError());\r
225                 }\r
226                 if (!query.next()) {\r
227                         logger.log(logger.MEDIUM, "Note Resource not found.");\r
228                         return null;\r
229                 }\r
230                 return query.valueString(0);\r
231         }\r
232 \r
233         // Get a note resource from the database by it's hash value\r
234         public Resource getNoteResourceDataBodyByHashHex(String noteGuid, String hash) {\r
235                 logger.log(logger.HIGH, "Entering DBRunner.getNoteResourceDataBodyByHash");\r
236 \r
237                 boolean check;\r
238                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
239                 \r
240                 check = query.prepare("Select guid, mime, from NoteResources " +\r
241                                         "where noteGuid=:noteGuid and dataHash=:hash");\r
242                 if (!check) {\r
243                         logger.log(logger.EXTREME, "NoteResource SQL select prepare has failed.");\r
244                         logger.log(logger.MEDIUM, query.lastError());\r
245                 }\r
246                 query.bindValue(":noteGuid", noteGuid);\r
247                 query.bindValue(":hash", hash);\r
248         \r
249                 if (!query.exec()) {\r
250                         logger.log(logger.MEDIUM, "NoteResource Select failed." +\r
251                                         "Note Guid:" +noteGuid+\r
252                                         "Data Body Hash:" +hash);               \r
253                         logger.log(logger.MEDIUM, query.lastError());\r
254                 }\r
255                 if (!query.next()) {\r
256                         logger.log(logger.MEDIUM, "Note Resource not found.");\r
257                         return null;\r
258                 }\r
259                 \r
260                 Resource r = new Resource();\r
261                 r.setGuid(query.valueString(0));\r
262                 r.setMime(query.valueString(1));\r
263                 \r
264                 NSqlQuery binary = new NSqlQuery(db.getConnection());\r
265                 if (!binary.prepare("Select databinary from NoteResources " +\r
266                                         "where guid=:guid")) {\r
267                         logger.log(logger.MEDIUM, "Prepare for NoteResources Binary failed");\r
268                         logger.log(logger.MEDIUM, binary.lastError());\r
269                 }\r
270                 \r
271                 if (!binary.exec()) {\r
272                         logger.log(logger.MEDIUM, "NoteResources Binary Select failed." +\r
273                                         "Note Guid:" +noteGuid+\r
274                                         "Data Body Hash:" +hash);               \r
275                         logger.log(logger.MEDIUM, binary.lastError());\r
276                 }\r
277                 if (!binary.next()) {\r
278                         logger.log(logger.MEDIUM, "Note Resource Binary not found.");\r
279                         return null;\r
280                 }\r
281                 \r
282                 Data d = new Data();\r
283                 r.setData(d);\r
284                 d.setBody(binary.valueString(0).getBytes());\r
285                 logger.log(logger.HIGH, "Leaving DBRunner.getNoteResourceDataBodyByHash");\r
286                 return r;\r
287         }\r
288 \r
289         \r
290         // Get a note's resourcesby Guid\r
291         public Resource getNoteResource(String guid, boolean withBinary) {\r
292                 if (guid == null)\r
293                         return null;\r
294                 \r
295                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
296                 String queryString;\r
297                 queryString = new String("Select guid, noteGuid, mime, width, height, duration, "\r
298                                 +"active, updateSequenceNumber, dataHash, dataSize, "\r
299                                 +"recognitionHash, recognitionSize, "\r
300                                 +"attributeLatitude, attributeLongitude, attributeAltitude, "\r
301                                 +"attributeCameraMake, attributeCameraModel, attributeClientWillIndex, "\r
302                                 +"attributeRecoType, attributeFileName, attributeAttachment, recognitionBinary "\r
303                                 +" from NoteResources where guid=:guid");\r
304 \r
305                 \r
306                 query.prepare(queryString);\r
307                 \r
308                 query.bindValue(":guid", guid);\r
309                 if (!query.exec()) {\r
310                         logger.log(logger.EXTREME, "NoteResources SQL select has failed.");\r
311                         logger.log(logger.MEDIUM, query.lastError());\r
312                         return null;\r
313                 }\r
314                 Resource r = null;\r
315                 if (query.next()) {\r
316                                                                         \r
317                         r = new Resource();\r
318                         r.setGuid(query.valueString(0));        // Resource Guid\r
319                         r.setNoteGuid(query.valueString(1));   // note Guid\r
320                         r.setMime(query.valueString(2));       // Mime Type\r
321                         r.setWidth(new Short(query.valueString(3)));  // Width\r
322                         r.setHeight(new Short(query.valueString(4)));  // Height\r
323                         r.setDuration(new Short(query.valueString(5)));  // Duration\r
324                         r.setActive(new Boolean(query.valueString(6)));  // active\r
325                         r.setUpdateSequenceNum(new Integer(query.valueString(7)));  // update sequence number\r
326                         \r
327                         Data d = new Data();\r
328                         byte[] h = query.valueString(8).getBytes();    // data hash\r
329                         QByteArray hData = new QByteArray(h);\r
330                         QByteArray bData = new QByteArray(QByteArray.fromHex(hData));\r
331                         d.setBodyHash(bData.toByteArray());\r
332                         d.setSize(new Integer(query.valueString(9)));\r
333                         r.setData(d);\r
334                         \r
335                         Data rec = new Data();\r
336                         if (query.valueObject(10) != null)\r
337                                 rec.setBodyHash(query.valueString(10).getBytes());   // Recognition Hash\r
338                         if (query.valueObject(11) != null)\r
339                                 rec.setSize(new Integer(query.valueString(11)));\r
340                         else\r
341                                 rec.setSize(0);\r
342                         r.setRecognition(rec);\r
343 \r
344                         ResourceAttributes a = new ResourceAttributes();\r
345                         if (!query.valueString(12).equals(""))              // Latitude\r
346                                 a.setLatitude(new Float(query.valueString(12)));\r
347                         if (!query.valueString(13).equals(""))              // Longitude\r
348                                 a.setLongitude(new Float(query.valueString(13)));\r
349                         if (!query.valueString(14).equals(""))              // Altitude\r
350                                 a.setAltitude(new Float(query.valueString(14)));\r
351                         a.setCameraMake(stringValue(query.valueString(15)));              // Camera Make\r
352                         a.setCameraModel(stringValue(query.valueString(16)));\r
353                         a.setClientWillIndex(booleanValue(query.valueString(17).toString(),false));  // Camera Model\r
354                         a.setRecoType(stringValue(query.valueString(18)));                 // Recognition Type\r
355                         a.setFileName(stringValue(query.valueString(19)));                  // File Name\r
356                         a.setAttachment(booleanValue(query.valueString(20).toString(),false));\r
357                         r.setAttributes(a);\r
358                 \r
359                         if (withBinary) {\r
360                             \r
361                                 query.prepare("Select dataBinary from NoteResources where guid=:guid");\r
362                                 query.bindValue(":guid", r.getGuid());\r
363                                 query.exec();\r
364                                 if (query.next()) {\r
365                                         byte[] b = query.getBlob(0);\r
366                                         r.getData().setBody(b);\r
367                                 }\r
368                         } \r
369                 }\r
370                 return r;\r
371         }\r
372         \r
373         \r
374         // Get a note's resourcesby Guid\r
375         public List<Resource> getNoteResources(String noteGuid, boolean withBinary) {\r
376                 if (noteGuid == null)\r
377                         return null;\r
378                 List<Resource> res = new ArrayList<Resource>();\r
379                 \r
380                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
381                 query.prepare("Select guid"\r
382                                 +" from NoteResources where noteGuid = :noteGuid");\r
383                 query.bindValue(":noteGuid", noteGuid);\r
384                 if (!query.exec()) {\r
385                         logger.log(logger.EXTREME, "NoteResources SQL select has failed.");\r
386                         logger.log(logger.MEDIUM, query.lastError());\r
387                         return null;\r
388                 }\r
389                 while (query.next()) {\r
390                         String guid = (query.valueString(0));\r
391                         res.add(getNoteResource(guid, withBinary));\r
392                 }       \r
393                 return res;\r
394         }\r
395         // Get all of a note's recognition data by the note guid\r
396         public List<Resource> getNoteResourcesRecognition(String noteGuid) {\r
397                 if (noteGuid == null)\r
398                         return null;\r
399                 boolean check;\r
400                 List<Resource> res = new ArrayList<Resource>();\r
401                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
402                 check = query.prepare("Select "\r
403                                 +"recognitionHash, recognitionSize, recognitionBinary "\r
404                                 +" from NoteResources where noteGuid=:guid");\r
405                 if (!check) {\r
406                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition SQL prepare has failed.");\r
407                         logger.log(logger.MEDIUM, query.lastError());\r
408                         return null;\r
409                 }\r
410                 query.bindValue(":guid", noteGuid);\r
411                 if (!check) {\r
412                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition exec has failed.");\r
413                         logger.log(logger.MEDIUM, query.lastError());\r
414                         return null;\r
415                 }\r
416                 while (query.next()) {  \r
417                         Resource r = new Resource();            \r
418                         res.add(r);\r
419                         Data rec = new Data();\r
420                         rec.setBodyHash(query.valueString(0).getBytes());\r
421                         String x = new String(query.valueString(1));\r
422                         if (!x.equals("")) {\r
423                                 rec.setSize(new Integer(x));\r
424                                 rec.setBody(query.valueString(2).getBytes());\r
425                         } else\r
426                                 rec.setSize(0);\r
427                         r.setRecognition(rec);\r
428                 }       \r
429                 return res;\r
430         }\r
431         \r
432         \r
433         // Get a note's recognition data by it's guid.\r
434         public Resource getNoteResourceRecognition(String guid) {\r
435                 if (guid == null)\r
436                         return null;\r
437                 boolean check;\r
438                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
439                 check = query.prepare("Select "\r
440                                 +"recognitionHash, recognitionSize, recognitionBinary, noteGuid "\r
441                                 +" from NoteResources where guid=:guid");\r
442                 if (!check) {\r
443                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition SQL prepare has failed.");\r
444                         logger.log(logger.MEDIUM, query.lastError());\r
445                         return null;\r
446                 }\r
447                 query.bindValue(":guid", guid);\r
448                 query.exec();\r
449                 if (!check) {\r
450                         logger.log(logger.EXTREME, "NoteTable.getNoteRecognition exec has failed.");\r
451                         logger.log(logger.MEDIUM, query.lastError());\r
452                         return null;\r
453                 }\r
454                 Resource r = null;\r
455                 while (query.next()) {\r
456                                                                         \r
457                         r = new Resource();             \r
458                         Data rec = new Data();\r
459                         rec.setBodyHash(query.valueString(0).getBytes());\r
460                         String x = new String(query.valueString(1));\r
461                         if (!x.equals("")) {\r
462                                 rec.setSize(new Integer(x));\r
463                                 rec.setBody(query.valueString(2).getBytes());\r
464                         } else\r
465                                 rec.setSize(0);\r
466                         r.setRecognition(rec);\r
467                         r.setNoteGuid(query.valueString(3));\r
468                 }       \r
469                 return r;\r
470         }\r
471 \r
472         // Save Note Resource\r
473         public void updateNoteResource(Resource r, boolean isDirty) {\r
474                 logger.log(logger.HIGH, "Entering ListManager.updateNoteResource");\r
475                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
476                 query.prepare("delete from NoteResources where guid=:recGuid");\r
477                 query.bindValue(":recGuid", r.getGuid());\r
478                 query.exec();\r
479                 saveNoteResource(r, isDirty);\r
480                 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResource");\r
481         }\r
482         // Update note resource GUID\r
483         public void updateNoteResourceGuid(String oldGuid, String newGuid, boolean isDirty) {\r
484                 logger.log(logger.HIGH, "Entering RNoteResourceTable.updateNoteResourceGuid");\r
485                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
486                 query.prepare("update NoteResources set guid=:newGuid, isDirty=:isDirty where guid=:oldGuid");\r
487                 query.bindValue(":newGuid", newGuid);\r
488                 query.bindValue(":isDirty", isDirty);\r
489                 query.bindValue(":oldGuid", oldGuid);\r
490                 query.exec();\r
491                 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResourceGuid");\r
492         }\r
493         // Update note resource GUID\r
494         public void resetUpdateSequenceNumber(String guid, boolean isDirty) {\r
495                 logger.log(logger.HIGH, "Entering RNoteResourceTable.updateNoteResourceGuid");\r
496                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
497                 query.prepare("update NoteResources set updateSequenceNumber=0, isDirty=:isDirty where guid=:guid");\r
498                 query.bindValue(":isDirty", isDirty);\r
499                 query.bindValue(":guid", guid);\r
500                 query.exec();\r
501                 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResourceGuid");\r
502         }\r
503         \r
504         // Drop the table\r
505         public void reindexAll() {              \r
506                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
507                 query.exec("Update NoteResources set indexneeded=true");\r
508         }\r
509         // Count unindexed notes\r
510         public int getResourceCount() {\r
511         NSqlQuery query = new NSqlQuery(db.getConnection());\r
512                 query.exec("select count(*) from noteresources");\r
513                 query.next(); \r
514                 int returnValue = new Integer(query.valueString(0));\r
515                 return returnValue;\r
516         }\r
517 \r
518         //********************************************\r
519         //** Utility Functions\r
520         //********************************************\r
521         // Convert a byte array to a hex string\r
522         private static String byteArrayToHexString(byte data[]) {\r
523                 StringBuffer buf = new StringBuffer();\r
524             for (byte element : data) {\r
525                 int halfbyte = (element >>> 4) & 0x0F;\r
526                 int two_halfs = 0;\r
527                 do {\r
528                         if ((0 <= halfbyte) && (halfbyte <= 9))\r
529                                buf.append((char) ('0' + halfbyte));\r
530                            else\r
531                                 buf.append((char) ('a' + (halfbyte - 10)));\r
532                         halfbyte = element & 0x0F;\r
533                 } while(two_halfs++ < 1);\r
534             }\r
535             return buf.toString();              \r
536         }\r
537 \r
538         \r
539         private String stringValue(Object value) {\r
540                 if (value != null && value.toString() != null) \r
541                         return value.toString();\r
542                 else\r
543                         return null;\r
544         }\r
545         \r
546         private  boolean booleanValue(Object value, boolean unknown) {\r
547                 if (value != null && value.toString() != null) {\r
548                         try {\r
549                                 if ((Integer)value > 0)\r
550                                         return true;\r
551                                 else\r
552                                         return false;\r
553                         } catch (java.lang.ClassCastException e) {\r
554                                 try {\r
555                                         String stringValue = (String)value;\r
556                                         if (stringValue.equalsIgnoreCase("true"))\r
557                                                 return true;\r
558                                         else \r
559                                                 return false;\r
560                                 } catch (java.lang.ClassCastException e1) { \r
561                                         return unknown;\r
562                                 }\r
563                         }\r
564                 }\r
565                 else\r
566                         return unknown;\r
567         }\r
568 \r
569 }\r