2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\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
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
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
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
31 import cx.fbn.nevernote.sql.driver.NSqlQuery;
\r
32 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
36 public class NoteResourceTable {
\r
40 private static final long serialVersionUID = 1L;
\r
41 private final ApplicationLogger logger;
\r
42 private final DatabaseConnection db;
\r
45 public NoteResourceTable(ApplicationLogger l, DatabaseConnection d) {
\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
72 public void dropTable() {
\r
73 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
74 query.exec("Drop table NoteResources");
\r
76 // Reset the dirty flag
\r
77 public void resetDirtyFlag(String guid) {
\r
78 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
80 query.prepare("Update noteresources set isdirty=false where guid=:guid");
\r
81 query.bindValue(":guid", guid);
\r
83 logger.log(logger.EXTREME, "Error resetting noteresource dirty field. " +query.lastError());
\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
92 logger.log(logger.EXTREME, "Error setting noteresource indexneeded field: " +query.lastError());
\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
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
102 // Get a list of the notes
\r
104 while (query.next()) {
\r
105 guid = new String();
\r
106 guid = query.valueString(0);
\r
114 public void saveNoteResource(Resource r, boolean isDirty) {
\r
115 logger.log(logger.HIGH, "Entering DBRunner.saveNoteResources");
\r
117 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
118 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
\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
136 logger.log(logger.EXTREME, "NoteResource SQL insert prepare has failed.");
\r
137 logger.log(logger.MEDIUM, query.lastError());
\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
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
161 query.bindValue(":recognitionBody", "");
\r
163 query.bindValue(":recognitionHash", "");
\r
164 query.bindValue(":recognitionSize", 0);
\r
165 query.bindValue(":recognitionBody", "");
\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
181 query.bindValue(":isDirty", isDirty);
\r
183 check = query.exec();
\r
185 logger.log(logger.MEDIUM, "*** NoteResource Table insert failed.");
\r
186 logger.log(logger.MEDIUM, query.lastError());
\r
190 logger.log(logger.HIGH, "Leaving DBRunner.saveNoteResources");
\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
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
206 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
208 check = query.prepare("Select guid from NoteResources " +
\r
209 "where noteGuid=:noteGuid and dataHash=:hash");
\r
211 logger.log(logger.EXTREME, "NoteResource SQL select prepare was successful.");
\r
213 logger.log(logger.EXTREME, "NoteResource SQL select prepare has failed.");
\r
214 logger.log(logger.MEDIUM, query.lastError());
\r
216 query.bindValue(":noteGuid", noteGuid);
\r
217 query.bindValue(":hash", hash);
\r
219 check = query.exec();
\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
226 if (!query.next()) {
\r
227 logger.log(logger.MEDIUM, "Note Resource not found.");
\r
230 return query.valueString(0);
\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
238 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
240 check = query.prepare("Select guid, mime, from NoteResources " +
\r
241 "where noteGuid=:noteGuid and dataHash=:hash");
\r
243 logger.log(logger.EXTREME, "NoteResource SQL select prepare has failed.");
\r
244 logger.log(logger.MEDIUM, query.lastError());
\r
246 query.bindValue(":noteGuid", noteGuid);
\r
247 query.bindValue(":hash", hash);
\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
255 if (!query.next()) {
\r
256 logger.log(logger.MEDIUM, "Note Resource not found.");
\r
260 Resource r = new Resource();
\r
261 r.setGuid(query.valueString(0));
\r
262 r.setMime(query.valueString(1));
\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
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
277 if (!binary.next()) {
\r
278 logger.log(logger.MEDIUM, "Note Resource Binary not found.");
\r
282 Data d = new Data();
\r
284 d.setBody(binary.valueString(0).getBytes());
\r
285 logger.log(logger.HIGH, "Leaving DBRunner.getNoteResourceDataBodyByHash");
\r
290 // Get a note's resourcesby Guid
\r
291 public Resource getNoteResource(String guid, boolean withBinary) {
\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
306 query.prepare(queryString);
\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
315 if (query.next()) {
\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
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
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
342 r.setRecognition(rec);
\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
361 query.prepare("Select dataBinary from NoteResources where guid=:guid");
\r
362 query.bindValue(":guid", r.getGuid());
\r
364 if (query.next()) {
\r
365 byte[] b = query.getBlob(0);
\r
366 r.getData().setBody(b);
\r
374 // Get a note's resourcesby Guid
\r
375 public List<Resource> getNoteResources(String noteGuid, boolean withBinary) {
\r
376 if (noteGuid == null)
\r
378 List<Resource> res = new ArrayList<Resource>();
\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
389 while (query.next()) {
\r
390 String guid = (query.valueString(0));
\r
391 res.add(getNoteResource(guid, withBinary));
\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
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
406 logger.log(logger.EXTREME, "NoteTable.getNoteRecognition SQL prepare has failed.");
\r
407 logger.log(logger.MEDIUM, query.lastError());
\r
410 query.bindValue(":guid", noteGuid);
\r
412 logger.log(logger.EXTREME, "NoteTable.getNoteRecognition exec has failed.");
\r
413 logger.log(logger.MEDIUM, query.lastError());
\r
416 while (query.next()) {
\r
417 Resource r = new Resource();
\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
427 r.setRecognition(rec);
\r
433 // Get a note's recognition data by it's guid.
\r
434 public Resource getNoteResourceRecognition(String guid) {
\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
443 logger.log(logger.EXTREME, "NoteTable.getNoteRecognition SQL prepare has failed.");
\r
444 logger.log(logger.MEDIUM, query.lastError());
\r
447 query.bindValue(":guid", guid);
\r
450 logger.log(logger.EXTREME, "NoteTable.getNoteRecognition exec has failed.");
\r
451 logger.log(logger.MEDIUM, query.lastError());
\r
455 while (query.next()) {
\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
466 r.setRecognition(rec);
\r
467 r.setNoteGuid(query.valueString(3));
\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
479 saveNoteResource(r, isDirty);
\r
480 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResource");
\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
491 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResourceGuid");
\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
501 logger.log(logger.HIGH, "Leaving RNoteResourceTable.updateNoteResourceGuid");
\r
505 public void reindexAll() {
\r
506 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
507 query.exec("Update NoteResources set indexneeded=true");
\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
514 int returnValue = new Integer(query.valueString(0));
\r
515 return returnValue;
\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
528 if ((0 <= halfbyte) && (halfbyte <= 9))
\r
529 buf.append((char) ('0' + halfbyte));
\r
531 buf.append((char) ('a' + (halfbyte - 10)));
\r
532 halfbyte = element & 0x0F;
\r
533 } while(two_halfs++ < 1);
\r
535 return buf.toString();
\r
539 private String stringValue(Object value) {
\r
540 if (value != null && value.toString() != null)
\r
541 return value.toString();
\r
546 private boolean booleanValue(Object value, boolean unknown) {
\r
547 if (value != null && value.toString() != null) {
\r
549 if ((Integer)value > 0)
\r
553 } catch (java.lang.ClassCastException e) {
\r
555 String stringValue = (String)value;
\r
556 if (stringValue.equalsIgnoreCase("true"))
\r
560 } catch (java.lang.ClassCastException e1) {
\r