OSDN Git Service

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