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
23 import java.text.DateFormat;
\r
24 import java.text.ParseException;
\r
25 import java.text.SimpleDateFormat;
\r
26 import java.util.ArrayList;
\r
27 import java.util.HashMap;
\r
28 import java.util.List;
\r
30 import com.evernote.edam.type.Note;
\r
31 import com.evernote.edam.type.NoteAttributes;
\r
32 import com.evernote.edam.type.Resource;
\r
33 import com.evernote.edam.type.Tag;
\r
34 import com.trolltech.qt.core.QByteArray;
\r
35 import com.trolltech.qt.core.QDateTime;
\r
36 import com.trolltech.qt.core.QTextCodec;
\r
37 import com.trolltech.qt.gui.QPixmap;
\r
39 import cx.fbn.nevernote.Global;
\r
40 import cx.fbn.nevernote.evernote.EnmlConverter;
\r
41 import cx.fbn.nevernote.sql.driver.NSqlQuery;
\r
42 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
43 import cx.fbn.nevernote.utilities.Pair;
\r
45 public class NoteTable {
\r
46 private final ApplicationLogger logger;
\r
47 public final NoteTagsTable noteTagsTable;
\r
48 public NoteResourceTable noteResourceTable;
\r
49 private final DatabaseConnection db;
\r
52 // Prepared Queries to improve speed
\r
53 private NSqlQuery getQueryWithContent;
\r
54 private NSqlQuery getQueryWithoutContent;
\r
55 private NSqlQuery getAllQueryWithoutContent;
\r
58 public NoteTable(ApplicationLogger l, DatabaseConnection d) {
\r
62 noteResourceTable = new NoteResourceTable(logger, db);
\r
63 noteTagsTable = new NoteTagsTable(logger, db);
\r
64 getQueryWithContent = null;
\r
65 getQueryWithoutContent = null;
\r
68 public void createTable() {
\r
69 getQueryWithContent = new NSqlQuery(db.getConnection());
\r
70 getQueryWithoutContent = new NSqlQuery(db.getConnection());
\r
71 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
72 logger.log(logger.HIGH, "Creating table Note...");
\r
73 if (!query.exec("Create table Note (guid varchar primary key, " +
\r
74 "updateSequenceNumber integer, title varchar, content varchar, contentHash varchar, "+
\r
75 "contentLength integer, created timestamp, updated timestamp, deleted timestamp, "
\r
76 +"active integer, notebookGuid varchar, attributeSubjectDate timestamp, "+
\r
77 "attributeLatitude double, attributeLongitude double, attributeAltitude double,"+
\r
78 "attributeAuthor varchar, attributeSource varchar, attributeSourceUrl varchar, "+
\r
79 "attributeSourceApplication varchar, indexNeeded boolean, isExpunged boolean, " +
\r
80 "isDirty boolean)"))
\r
81 logger.log(logger.HIGH, "Table Note creation FAILED!!!");
\r
82 if (!query.exec("CREATE INDEX unindexed_notess on note (indexneeded desc, guid);"))
\r
83 logger.log(logger.HIGH, "Note unindexed_notes index creation FAILED!!!");
\r
84 if (!query.exec("CREATE INDEX unsynchronized_notes on note (isDirty desc, guid);"))
\r
85 logger.log(logger.HIGH, "note unsynchronized_notes index creation FAILED!!!");
\r
86 noteTagsTable.createTable();
\r
87 // noteResourceTable.createTable();
\r
90 public void dropTable() {
\r
91 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
92 query.exec("Drop table Note");
\r
93 noteTagsTable.dropTable();
\r
94 noteResourceTable.dropTable();
\r
96 // Save Note List from Evernote
\r
97 public void addNote(Note n, boolean isDirty) {
\r
98 logger.log(logger.EXTREME, "Inside addNote");
\r
102 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
\r
104 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
105 query.prepare("Insert Into Note ("
\r
106 +"guid, updateSequenceNumber, title, content, "
\r
107 +"contentHash, contentLength, created, updated, deleted, active, notebookGuid, "
\r
108 +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "
\r
109 +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "
\r
110 +"indexNeeded, isExpunged, isDirty, titlecolor, thumbnailneeded"
\r
112 +":guid, :updateSequenceNumber, :title, :content, "
\r
113 +":contentHash, :contentLength, :created, :updated, :deleted, :active, :notebookGuid, "
\r
114 +":attributeSubjectDate, :attributeLatitude, :attributeLongitude, :attributeAltitude, "
\r
115 +":attributeAuthor, :attributeSource, :attributeSourceUrl, :attributeSourceApplication, "
\r
116 +":indexNeeded, :isExpunged, :isDirty, -1, true) ");
\r
118 StringBuilder created = new StringBuilder(simple.format(n.getCreated()));
\r
119 StringBuilder updated = new StringBuilder(simple.format(n.getUpdated()));
\r
120 StringBuilder deleted = new StringBuilder(simple.format(n.getDeleted()));
\r
124 query.bindValue(":guid", n.getGuid());
\r
125 query.bindValue(":updateSequenceNumber", n.getUpdateSequenceNum());
\r
126 query.bindValue(":title", n.getTitle());
\r
128 EnmlConverter enml = new EnmlConverter(logger);
\r
129 query.bindValue(":content", enml.fixEnXMLCrap(enml.fixEnMediaCrap(n.getContent())));
\r
132 query.bindValue(":content", n.getContent());
\r
133 query.bindValue(":contentHash", n.getContentHash());
\r
134 query.bindValue(":contentLength", n.getContentLength());
\r
135 query.bindValue(":created", created.toString());
\r
136 query.bindValue(":updated", updated.toString());
\r
137 query.bindValue(":deleted", deleted.toString());
\r
138 query.bindValue(":active", n.isActive());
\r
139 query.bindValue(":notebookGuid", n.getNotebookGuid());
\r
141 if (n.getAttributes() != null) {
\r
142 created = new StringBuilder(simple.format(n.getAttributes().getSubjectDate()));
\r
143 query.bindValue(":attributeSubjectDate", created.toString());
\r
144 query.bindValue(":attributeLatitude", n.getAttributes().getLatitude());
\r
145 query.bindValue(":attributeLongitude", n.getAttributes().getLongitude());
\r
146 query.bindValue(":attributeAltitude", n.getAttributes().getAltitude());
\r
147 query.bindValue(":attributeAuthor", n.getAttributes().getAuthor());
\r
148 query.bindValue(":attributeSource", n.getAttributes().getSource());
\r
149 query.bindValue(":attributeSourceUrl", n.getAttributes().getSourceURL());
\r
150 query.bindValue(":attributeSourceApplication", n.getAttributes().getSourceApplication());
\r
152 query.bindValue(":indexNeeded", true);
\r
153 query.bindValue(":isExpunged", false);
\r
154 query.bindValue(":isDirty", isDirty);
\r
158 logger.log(logger.MEDIUM, query.lastError());
\r
160 // Save the note tags
\r
161 if (n.getTagGuids() != null) {
\r
162 for (int i=0; i<n.getTagGuids().size(); i++)
\r
163 noteTagsTable.saveNoteTag(n.getGuid(), n.getTagGuids().get(i));
\r
166 logger.log(logger.EXTREME, "Leaving addNote");
\r
168 // Setup queries for get to save time later
\r
169 private void prepareQueries() {
\r
170 if (getQueryWithContent != null)
\r
172 getQueryWithContent = new NSqlQuery(db.getConnection());
\r
173 getQueryWithoutContent = new NSqlQuery(db.getConnection());
\r
174 getAllQueryWithoutContent = new NSqlQuery(db.getConnection());
\r
176 if (!getQueryWithContent.prepare("Select "
\r
177 +"guid, updateSequenceNumber, title, "
\r
178 +"created, updated, deleted, active, notebookGuid, "
\r
179 +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "
\r
180 +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "
\r
181 +"content, contentHash, contentLength"
\r
182 +" from Note where guid=:guid and isExpunged=false")) {
\r
183 logger.log(logger.EXTREME, "Note SQL select prepare with content has failed.");
\r
184 logger.log(logger.MEDIUM, getQueryWithContent.lastError());
\r
187 if (!getQueryWithoutContent.prepare("Select "
\r
188 +"guid, updateSequenceNumber, title, "
\r
189 +"created, updated, deleted, active, notebookGuid, "
\r
190 +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "
\r
191 +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication "
\r
192 +" from Note where guid=:guid and isExpunged=false")) {
\r
193 logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");
\r
194 logger.log(logger.MEDIUM, getQueryWithoutContent.lastError());
\r
196 if (!getAllQueryWithoutContent.prepare("Select "
\r
197 +"guid, updateSequenceNumber, title, "
\r
198 +"created, updated, deleted, active, notebookGuid, "
\r
199 +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "
\r
200 +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication "
\r
201 +" from Note where isExpunged = false")) {
\r
202 logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");
\r
203 logger.log(logger.MEDIUM, getQueryWithoutContent.lastError());
\r
207 // Get a note's content in raw, binary format for the sync.
\r
208 public String getNoteContentBinary(String guid) {
\r
209 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
210 query.prepare("Select content from note where guid=:guid");
\r
211 query.bindValue(":guid", guid);
\r
214 return query.valueString(0);
\r
216 // Get a note's content in blob format for index.
\r
217 public String getNoteContentNoUTFConversion(String guid) {
\r
218 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
219 query.prepare("Select content from note where guid=:guid");
\r
220 query.bindValue(":guid", guid);
\r
223 return query.valueString(0);
\r
225 // Get a note by Guid
\r
226 public Note getNote(String noteGuid, boolean loadContent, boolean loadResources, boolean loadRecognition, boolean loadBinary, boolean loadTags) {
\r
227 if (noteGuid == null)
\r
229 if (noteGuid.trim().equals(""))
\r
235 query = getQueryWithContent;
\r
237 query = getQueryWithoutContent;
\r
240 query.bindValue(":guid", noteGuid);
\r
241 if (!query.exec()) {
\r
242 logger.log(logger.EXTREME, "Note SQL select exec has failed.");
\r
243 logger.log(logger.MEDIUM, query.lastError());
\r
246 if (!query.next()) {
\r
247 logger.log(logger.EXTREME, "SQL Retrieve failed for note guid " +noteGuid + " in getNote()");
\r
248 logger.log(logger.EXTREME, " -> " +query.lastError().toString());
\r
249 logger.log(logger.EXTREME, " -> " +query.lastError());
\r
252 Note n = mapNoteFromQuery(query, loadContent, loadResources, loadRecognition, loadBinary, loadTags);
\r
253 n.setContent(fixCarriageReturn(n.getContent()));
\r
256 // Get a note by Guid
\r
257 public Note mapNoteFromQuery(NSqlQuery query, boolean loadContent, boolean loadResources, boolean loadRecognition, boolean loadBinary, boolean loadTags) {
\r
258 DateFormat indfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
\r
259 // indfm = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
\r
261 Note n = new Note();
\r
262 NoteAttributes na = new NoteAttributes();
\r
263 n.setAttributes(na);
\r
265 n.setGuid(query.valueString(0));
\r
266 n.setUpdateSequenceNum(new Integer(query.valueString(1)));
\r
267 n.setTitle(query.valueString(2));
\r
270 n.setCreated(indfm.parse(query.valueString(3)).getTime());
\r
271 n.setUpdated(indfm.parse(query.valueString(4)).getTime());
\r
272 n.setDeleted(indfm.parse(query.valueString(5)).getTime());
\r
273 } catch (ParseException e) {
\r
274 e.printStackTrace();
\r
277 n.setActive(query.valueBoolean(6,true));
\r
278 n.setNotebookGuid(query.valueString(7));
\r
281 String attributeSubjectDate = query.valueString(8);
\r
282 if (!attributeSubjectDate.equals(""))
\r
283 na.setSubjectDate(indfm.parse(attributeSubjectDate).getTime());
\r
284 } catch (ParseException e) {
\r
285 e.printStackTrace();
\r
287 na.setLatitude(new Float(query.valueString(9)));
\r
288 na.setLongitude(new Float(query.valueString(10)));
\r
289 na.setAltitude(new Float(query.valueString(11)));
\r
290 na.setAuthor(query.valueString(12));
\r
291 na.setSource(query.valueString(13));
\r
292 na.setSourceURL(query.valueString(14));
\r
293 na.setSourceApplication(query.valueString(15));
\r
296 n.setTagGuids(noteTagsTable.getNoteTags(n.getGuid()));
\r
297 List<String> tagNames = new ArrayList<String>();
\r
298 TagTable tagTable = db.getTagTable();
\r
299 for (int i=0; i<n.getTagGuids().size(); i++) {
\r
300 String currentGuid = n.getTagGuids().get(i);
\r
301 Tag tag = tagTable.getTag(currentGuid);
\r
302 tagNames.add(tag.getName());
\r
304 n.setTagNames(tagNames);
\r
308 QTextCodec codec = QTextCodec.codecForLocale();
\r
309 codec = QTextCodec.codecForName("UTF-8");
\r
310 String unicode = codec.fromUnicode(query.valueString(16)).toString();
\r
311 n.setContent(unicode);
\r
312 // n.setContent(query.valueString(16).toString());
\r
314 String contentHash = query.valueString(17);
\r
315 if (contentHash != null)
\r
316 n.setContentHash(contentHash.getBytes());
\r
317 n.setContentLength(new Integer(query.valueString(18)));
\r
320 n.setResources(noteResourceTable.getNoteResources(n.getGuid(), loadBinary));
\r
321 if (loadRecognition) {
\r
322 if (n.getResources() == null) {
\r
323 List<Resource> resources = noteResourceTable.getNoteResourcesRecognition(n.getGuid());
\r
324 n.setResources(resources);
\r
326 // We need to merge the recognition resources with the note resources retrieved earlier
\r
327 for (int i=0; i<n.getResources().size(); i++) {
\r
328 Resource r = noteResourceTable.getNoteResourceRecognition(n.getResources().get(i).getGuid());
\r
329 n.getResources().get(i).setRecognition(r.getRecognition());
\r
333 n.setContent(fixCarriageReturn(n.getContent()));
\r
336 // Update a note's title
\r
337 public void updateNoteTitle(String guid, String title) {
\r
338 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
339 boolean check = query.prepare("Update Note set title=:title, isDirty=true where guid=:guid");
\r
341 logger.log(logger.EXTREME, "Update note title sql prepare has failed.");
\r
342 logger.log(logger.MEDIUM, query.lastError());
\r
344 query.bindValue(":title", title);
\r
345 query.bindValue(":guid", guid);
\r
346 check = query.exec();
\r
348 logger.log(logger.EXTREME, "Update note title has failed.");
\r
349 logger.log(logger.MEDIUM, query.lastError());
\r
352 // Update a note's creation date
\r
353 public void updateNoteCreatedDate(String guid, QDateTime date) {
\r
354 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
355 boolean check = query.prepare("Update Note set created=:created, isDirty=true where guid=:guid");
\r
357 logger.log(logger.EXTREME, "Update note creation update sql prepare has failed.");
\r
358 logger.log(logger.MEDIUM, query.lastError());
\r
361 query.bindValue(":created", date.toString("yyyy-MM-dd HH:mm:ss"));
\r
362 query.bindValue(":guid", guid);
\r
364 check = query.exec();
\r
366 logger.log(logger.EXTREME, "Update note creation date has failed.");
\r
367 logger.log(logger.MEDIUM, query.lastError());
\r
370 // Update a note's creation date
\r
371 public void updateNoteAlteredDate(String guid, QDateTime date) {
\r
372 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
373 boolean check = query.prepare("Update Note set updated=:altered, isDirty=true where guid=:guid");
\r
375 logger.log(logger.EXTREME, "Update note altered sql prepare has failed.");
\r
376 logger.log(logger.MEDIUM, query.lastError());
\r
379 query.bindValue(":altered", date.toString("yyyy-MM-dd HH:mm:ss"));
\r
380 query.bindValue(":guid", guid);
\r
382 check = query.exec();
\r
384 logger.log(logger.EXTREME, "Update note altered date has failed.");
\r
385 logger.log(logger.MEDIUM, query.lastError());
\r
388 // Update a note's creation date
\r
389 public void updateNoteSubjectDate(String guid, QDateTime date) {
\r
390 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
391 boolean check = query.prepare("Update Note set attributeSubjectDate=:altered, isDirty=true where guid=:guid");
\r
393 logger.log(logger.EXTREME, "Update note subject date sql prepare has failed.");
\r
394 logger.log(logger.MEDIUM, query.lastError());
\r
397 query.bindValue(":altered", date.toString("yyyy-MM-dd HH:mm:ss"));
\r
398 query.bindValue(":guid", guid);
\r
400 check = query.exec();
\r
402 logger.log(logger.EXTREME, "Update note subject date date has failed.");
\r
403 logger.log(logger.MEDIUM, query.lastError());
\r
406 // Update a note's creation date
\r
407 public void updateNoteAuthor(String guid, String author) {
\r
408 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
409 boolean check = query.prepare("Update Note set attributeAuthor=:author, isDirty=true where guid=:guid");
\r
411 logger.log(logger.EXTREME, "Update note author sql prepare has failed.");
\r
412 logger.log(logger.MEDIUM, query.lastError());
\r
415 query.bindValue(":author", author);
\r
416 query.bindValue(":guid", guid);
\r
418 check = query.exec();
\r
420 logger.log(logger.EXTREME, "Update note author has failed.");
\r
421 logger.log(logger.MEDIUM, query.lastError());
\r
425 // Update a note's geo tags
\r
426 public void updateNoteGeoTags(String guid, Double lon, Double lat, Double alt) {
\r
427 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
428 boolean check = query.prepare("Update Note set attributeLongitude=:longitude, "+
\r
429 "attributeLatitude=:latitude, attributeAltitude=:altitude, isDirty=true where guid=:guid");
\r
431 logger.log(logger.EXTREME, "Update note author sql prepare has failed.");
\r
432 logger.log(logger.MEDIUM, query.lastError());
\r
435 query.bindValue(":longitude", lon);
\r
436 query.bindValue(":latitude", lat);
\r
437 query.bindValue(":altitude", alt);
\r
438 query.bindValue(":guid", guid);
\r
440 check = query.exec();
\r
442 logger.log(logger.EXTREME, "Update note geo tag has failed.");
\r
443 logger.log(logger.MEDIUM, query.lastError());
\r
447 // Update a note's creation date
\r
448 public void updateNoteSourceUrl(String guid, String url) {
\r
449 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
450 boolean check = query.prepare("Update Note set attributeSourceUrl=:url, isDirty=true where guid=:guid");
\r
452 logger.log(logger.EXTREME, "Update note url sql prepare has failed.");
\r
453 logger.log(logger.MEDIUM, query.lastError());
\r
456 query.bindValue(":url", url);
\r
457 query.bindValue(":guid", guid);
\r
459 check = query.exec();
\r
461 logger.log(logger.EXTREME, "Update note url has failed.");
\r
462 logger.log(logger.MEDIUM, query.lastError());
\r
466 // Update the notebook that a note is assigned to
\r
467 public void updateNoteNotebook(String guid, String notebookGuid, boolean expungeFromRemote) {
\r
468 String currentNotebookGuid = new String("");
\r
471 // If we are going from a synchronized notebook to a local notebook, we
\r
472 // need to tell Evernote to purge the note online. However, if this is
\r
473 // conflicting change we move it to the local notebook without deleting it
\r
474 // or it would then delete the copy on the remote server.
\r
475 NotebookTable notebookTable = new NotebookTable(logger, db);
\r
476 DeletedTable deletedTable = new DeletedTable(logger, db);
\r
477 if (expungeFromRemote) {
\r
478 if (!notebookTable.isNotebookLocal(currentNotebookGuid) & notebookTable.isNotebookLocal(notebookGuid)) {
\r
479 deletedTable.addDeletedItem(guid, "NOTE");
\r
483 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
484 boolean check = query.prepare("Update Note set notebookGuid=:notebook, isDirty=true where guid=:guid");
\r
486 logger.log(logger.EXTREME, "Update note notebook sql prepare has failed.");
\r
487 logger.log(logger.MEDIUM, query.lastError());
\r
489 query.bindValue(":notebook", notebookGuid);
\r
490 query.bindValue(":guid", guid);
\r
492 check = query.exec();
\r
494 logger.log(logger.EXTREME, "Update note notebook has failed.");
\r
495 logger.log(logger.MEDIUM, query.lastError());
\r
498 // Update a note's title
\r
499 public void updateNoteContent(String guid, String content) {
\r
500 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
501 boolean check = query.prepare("Update Note set content=:content, updated=CURRENT_TIMESTAMP(), isDirty=true, indexNeeded=true, " +
\r
502 " thumbnailneeded=true where guid=:guid");
\r
504 logger.log(logger.EXTREME, "Update note content sql prepare has failed.");
\r
505 logger.log(logger.MEDIUM, query.lastError());
\r
508 query.bindValue(":content", content);
\r
509 query.bindValue(":guid", guid);
\r
511 check = query.exec();
\r
513 logger.log(logger.EXTREME, "Update note content has failed.");
\r
514 logger.log(logger.MEDIUM, query.lastError());
\r
519 public void deleteNote(String guid) {
\r
520 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
521 query.prepare("Update Note set deleted=CURRENT_TIMESTAMP(), active=false, isDirty=true where guid=:guid");
\r
522 query.bindValue(":guid", guid);
\r
523 if (!query.exec()) {
\r
524 logger.log(logger.MEDIUM, "Note delete failed.");
\r
525 logger.log(logger.MEDIUM, query.lastError());
\r
528 public void restoreNote(String guid) {
\r
529 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
530 query.prepare("Update Note set deleted='1969-12-31 19.00.00', active=true, isDirty=true where guid=:guid");
\r
531 // query.prepare("Update Note set deleted=0, active=true, isDirty=true where guid=:guid");
\r
532 query.bindValue(":guid", guid);
\r
533 if (!query.exec()) {
\r
534 logger.log(logger.MEDIUM, "Note restore failed.");
\r
535 logger.log(logger.MEDIUM, query.lastError());
\r
538 // Purge a note (actually delete it instead of just marking it deleted)
\r
539 public void expungeNote(String guid, boolean permanentExpunge, boolean needsSync) {
\r
541 if (!permanentExpunge) {
\r
542 hideExpungedNote(guid, needsSync);
\r
547 NSqlQuery note = new NSqlQuery(db.getConnection());
\r
548 NSqlQuery resources = new NSqlQuery(db.getResourceConnection());
\r
549 NSqlQuery tags = new NSqlQuery(db.getConnection());
\r
550 NSqlQuery words = new NSqlQuery(db.getIndexConnection());
\r
552 note.prepare("Delete from Note where guid=:guid");
\r
553 resources.prepare("Delete from NoteResources where noteGuid=:guid");
\r
554 tags.prepare("Delete from NoteTags where noteGuid=:guid");
\r
555 words.prepare("Delete from words where guid=:guid");
\r
557 note.bindValue(":guid", guid);
\r
558 resources.bindValue(":guid", guid);
\r
559 tags.bindValue(":guid", guid);
\r
560 words.bindValue(":guid", guid);
\r
562 // Start purging notes.
\r
563 if (!note.exec()) {
\r
564 logger.log(logger.MEDIUM, "Purge from note failed.");
\r
565 logger.log(logger.MEDIUM, note.lastError());
\r
567 if (!resources.exec()) {
\r
568 logger.log(logger.MEDIUM, "Purge from resources failed.");
\r
569 logger.log(logger.MEDIUM, resources.lastError());
\r
571 if (!tags.exec()) {
\r
572 logger.log(logger.MEDIUM, "Note tags delete failed.");
\r
573 logger.log(logger.MEDIUM, tags.lastError());
\r
575 if (!words.exec()) {
\r
576 logger.log(logger.MEDIUM, "Word delete failed.");
\r
577 logger.log(logger.MEDIUM, words.lastError());
\r
580 DeletedTable deletedTable = new DeletedTable(logger, db);
\r
581 deletedTable.addDeletedItem(guid, "Note");
\r
585 // Purge a note (actually delete it instead of just marking it deleted)
\r
586 public void hideExpungedNote(String guid, boolean needsSync) {
\r
587 NSqlQuery note = new NSqlQuery(db.getConnection());
\r
588 NSqlQuery resources = new NSqlQuery(db.getResourceConnection());
\r
589 NSqlQuery tags = new NSqlQuery(db.getConnection());
\r
590 NSqlQuery words = new NSqlQuery(db.getIndexConnection());
\r
592 note.prepare("Update Note set isExpunged=true where guid=:guid");
\r
593 resources.prepare("Delete from NoteResources where noteGuid=:guid");
\r
594 tags.prepare("Delete from NoteTags where noteGuid=:guid");
\r
595 words.prepare("Delete from words where guid=:guid");
\r
597 note.bindValue(":guid", guid);
\r
598 resources.bindValue(":guid", guid);
\r
599 tags.bindValue(":guid", guid);
\r
600 words.bindValue(":guid", guid);
\r
602 // Start purging notes.
\r
603 if (!note.exec()) {
\r
604 logger.log(logger.MEDIUM, "Purge from note failed.");
\r
605 logger.log(logger.MEDIUM, note.lastError());
\r
607 if (!resources.exec()) {
\r
608 logger.log(logger.MEDIUM, "Purge from resources failed.");
\r
609 logger.log(logger.MEDIUM, resources.lastError());
\r
611 if (!tags.exec()) {
\r
612 logger.log(logger.MEDIUM, "Note tags delete failed.");
\r
613 logger.log(logger.MEDIUM, tags.lastError());
\r
615 if (!words.exec()) {
\r
616 logger.log(logger.MEDIUM, "Word delete failed.");
\r
617 logger.log(logger.MEDIUM, words.lastError());
\r
620 DeletedTable deletedTable = new DeletedTable(logger, db);
\r
621 deletedTable.addDeletedItem(guid, "Note");
\r
626 // Purge all deleted notes;
\r
627 public void expungeAllDeletedNotes() {
\r
628 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
629 query.exec("select guid, updateSequenceNumber from note where active = false");
\r
630 while (query.next()) {
\r
631 String guid = query.valueString(0);
\r
632 Integer usn = new Integer(query.valueString(1));
\r
634 expungeNote(guid, true, false);
\r
636 expungeNote(guid, false, true);
\r
639 // Update the note sequence number
\r
640 public void updateNoteSequence(String guid, int sequence) {
\r
642 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
643 check = query.prepare("Update Note set updateSequenceNumber=:sequence where guid=:guid");
\r
645 query.bindValue(":sequence", sequence);
\r
646 query.bindValue(":guid", guid);
\r
650 logger.log(logger.MEDIUM, "Note sequence update failed.");
\r
651 logger.log(logger.MEDIUM, query.lastError());
\r
654 // Update the note Guid
\r
655 public void updateNoteGuid(String oldGuid, String newGuid) {
\r
657 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
658 NSqlQuery resQuery = new NSqlQuery(db.getResourceConnection());
\r
659 NSqlQuery wordQuery = new NSqlQuery(db.getIndexConnection());
\r
660 query.prepare("Update Note set guid=:newGuid where guid=:oldGuid");
\r
662 query.bindValue(":newGuid", newGuid);
\r
663 query.bindValue(":oldGuid", oldGuid);
\r
665 check = query.exec();
\r
667 logger.log(logger.MEDIUM, "Note Guid update failed.");
\r
668 logger.log(logger.MEDIUM, query.lastError());
\r
671 query.prepare("Update NoteTags set noteGuid=:newGuid where noteGuid=:oldGuid");
\r
672 query.bindValue(":newGuid", newGuid);
\r
673 query.bindValue(":oldGuid", oldGuid);
\r
674 check = query.exec();
\r
676 logger.log(logger.MEDIUM, "Note guid update failed for NoteTags.");
\r
677 logger.log(logger.MEDIUM, query.lastError());
\r
680 wordQuery.prepare("Update words set guid=:newGuid where guid=:oldGuid");
\r
681 wordQuery.bindValue(":newGuid", newGuid);
\r
682 wordQuery.bindValue(":oldGuid", oldGuid);
\r
685 logger.log(logger.MEDIUM, "Note guid update failed for Words.");
\r
686 logger.log(logger.MEDIUM, wordQuery.lastError());
\r
688 resQuery.prepare("Update noteresources set noteguid=:newGuid where noteguid=:oldGuid");
\r
689 resQuery.bindValue(":newGuid", newGuid);
\r
690 resQuery.bindValue(":oldGuid", oldGuid);
\r
693 logger.log(logger.MEDIUM, "Note guid update failed for noteresources.");
\r
694 logger.log(logger.MEDIUM, resQuery.lastError());
\r
698 public void updateNote(Note n, boolean isNew) {
\r
699 int titleColor = getNoteTitleColor(n.getGuid());
\r
700 expungeNote(n.getGuid(), true, false);
\r
702 if (titleColor != -1)
\r
703 setNoteTitleColor(n.getGuid(), titleColor);
\r
705 // Does a note exist?
\r
706 public boolean exists(String guid) {
\r
709 if (guid.trim().equals(""))
\r
711 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
712 query.prepare("Select guid from note where guid=:guid");
\r
713 query.bindValue(":guid", guid);
\r
715 logger.log(logger.EXTREME, "note.exists SQL retrieve has failed.");
\r
716 boolean retVal = query.next();
\r
719 // Does a note exist?
\r
720 public boolean isNoteExpunged(String guid) {
\r
723 if (guid.trim().equals(""))
\r
725 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
726 query.prepare("Select isExpunged from note where guid=:guid and isExpunged = true");
\r
727 query.bindValue(":guid", guid);
\r
729 logger.log(logger.EXTREME, "note.isNoteExpunged SQL retrieve has failed.");
\r
730 boolean retVal = query.next();
\r
733 // This is a convience method to check if a tag exists & update/create based upon it
\r
734 public void syncNote(Note tag, boolean isDirty) {
\r
735 if (exists(tag.getGuid()))
\r
736 updateNote(tag, isDirty);
\r
738 addNote(tag, isDirty);
\r
740 // Get a list of notes that need to be updated
\r
741 public List <Note> getDirty() {
\r
744 List<Note> notes = new ArrayList<Note>();
\r
745 List<String> index = new ArrayList<String>();
\r
748 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
750 check = query.exec("Select guid from Note where isDirty = true and isExpunged = false and notebookGuid not in (select guid from notebook where local = true or linked = true)");
\r
752 logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());
\r
754 // Get a list of the notes
\r
755 while (query.next()) {
\r
756 guid = new String();
\r
757 guid = query.valueString(0);
\r
761 // Start getting notes
\r
762 for (int i=0; i<index.size(); i++) {
\r
763 tempNote = getNote(index.get(i), true,true,false,true,true);
\r
764 notes.add(tempNote);
\r
768 // Get a list of notes that need to be updated
\r
769 public List <Note> getDirtyLinkedNotes() {
\r
772 List<Note> notes = new ArrayList<Note>();
\r
773 List<String> index = new ArrayList<String>();
\r
776 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
778 check = query.exec("Select guid from Note where isDirty = true and isExpunged = false and notebookGuid in (select guid from notebook where linked = true)");
\r
780 logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());
\r
782 // Get a list of the notes
\r
783 while (query.next()) {
\r
784 guid = new String();
\r
785 guid = query.valueString(0);
\r
789 // Start getting notes
\r
790 for (int i=0; i<index.size(); i++) {
\r
791 tempNote = getNote(index.get(i), true,true,false,true,true);
\r
792 notes.add(tempNote);
\r
796 // Get a list of notes that need to be updated
\r
797 public List <Note> getDirtyLinked(String notebookGuid) {
\r
800 List<Note> notes = new ArrayList<Note>();
\r
801 List<String> index = new ArrayList<String>();
\r
804 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
806 query.prepare("Select guid from Note where isDirty = true and isExpunged = false and notebookGuid=:notebookGuid");
\r
807 query.bindValue(":notebookGuid", notebookGuid);
\r
808 check = query.exec();
\r
810 logger.log(logger.EXTREME, "Note SQL retrieve has failed getting dirty linked notes: " +query.lastError().toString());
\r
812 // Get a list of the notes
\r
813 while (query.next()) {
\r
814 guid = new String();
\r
815 guid = query.valueString(0);
\r
819 // Start getting notes
\r
820 for (int i=0; i<index.size(); i++) {
\r
821 tempNote = getNote(index.get(i), true,true,false,true,true);
\r
822 notes.add(tempNote);
\r
826 // Get a list of notes that need to be updated
\r
827 public List <String> getNotesByNotebook(String notebookGuid) {
\r
828 List<String> notes = new ArrayList<String>();
\r
829 List<String> index = new ArrayList<String>();
\r
832 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
834 check = query.prepare("Select guid from Note where notebookguid=:notebookguid");
\r
836 logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());
\r
837 query.bindValue(":notebookguid", notebookGuid);
\r
840 // Get a list of the notes
\r
841 while (query.next()) {
\r
842 index.add(query.valueString(0));
\r
847 // Get a list of notes that need to be updated
\r
848 public boolean isNoteDirty(String guid) {
\r
851 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
853 check = query.prepare("Select guid from Note where isDirty = true and guid=:guid");
\r
854 query.bindValue(":guid", guid);
\r
855 check = query.exec();
\r
857 logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());
\r
859 boolean returnValue;
\r
860 // Get a list of the notes
\r
862 returnValue = true;
\r
864 returnValue = false;
\r
866 return returnValue;
\r
868 // Get a list of notes that need to be updated
\r
869 public List <String> getUnsynchronizedGUIDs() {
\r
871 List<String> index = new ArrayList<String>();
\r
874 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
876 check = query.exec("Select guid from Note where isDirty=true");
\r
878 logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());
\r
880 // Get a list of the notes
\r
881 while (query.next()) {
\r
882 guid = new String();
\r
883 guid = query.valueString(0);
\r
888 // Reset the dirty bit
\r
889 public void resetDirtyFlag(String guid) {
\r
890 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
892 query.prepare("Update note set isdirty=false where guid=:guid");
\r
893 query.bindValue(":guid", guid);
\r
895 logger.log(logger.EXTREME, "Error resetting note dirty field.");
\r
898 public List<String> getAllGuids() {
\r
899 List<String> notes = new ArrayList<String>();
\r
902 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
904 check = query.exec("Select guid from Note");
\r
906 logger.log(logger.EXTREME, "Notebook SQL retrieve has failed: "+query.lastError());
\r
908 // Get a list of the notes
\r
909 while (query.next()) {
\r
910 notes.add(new String(query.valueString(0)));
\r
915 public List<Note> getAllNotes() {
\r
916 List<Note> notes = new ArrayList<Note>();
\r
919 NSqlQuery query = getAllQueryWithoutContent;
\r
920 check = query.exec();
\r
922 logger.log(logger.EXTREME, "Notebook SQL retrieve has failed: "+query.lastError());
\r
923 // Get a list of the notes
\r
924 while (query.next()) {
\r
925 notes.add(mapNoteFromQuery(query, false, false, false, false, true));
\r
929 // Count unindexed notes
\r
930 public int getUnindexedCount() {
\r
931 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
932 query.exec("select count(*) from note where indexneeded=true and isExpunged = false");
\r
934 int returnValue = new Integer(query.valueString(0));
\r
935 return returnValue;
\r
937 // Count unsynchronized notes
\r
938 public int getDirtyCount() {
\r
939 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
940 query.exec("select count(guid) from note where isDirty=true and isExpunged = false");
\r
942 int returnValue = new Integer(query.valueString(0));
\r
943 return returnValue;
\r
946 public int getNoteCount() {
\r
947 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
948 query.exec("select count(*) from note where isExpunged = false");
\r
950 int returnValue = new Integer(query.valueString(0));
\r
951 return returnValue;
\r
953 // Count deleted notes
\r
954 public int getDeletedCount() {
\r
955 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
956 query.exec("select count(*) from note where isExpunged = false and active = false");
\r
957 if (!query.next())
\r
959 int returnValue = new Integer(query.valueString(0));
\r
960 return returnValue;
\r
962 // Reset a note sequence number to zero. This is useful for moving conflicting notes
\r
963 public void resetNoteSequence(String guid) {
\r
964 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
965 boolean check = query.prepare("Update Note set updateSequenceNumber=0, isDirty=true where guid=:guid");
\r
967 logger.log(logger.EXTREME, "Update note ResetSequence sql prepare has failed.");
\r
968 logger.log(logger.MEDIUM, query.lastError());
\r
970 query.bindValue(":guid", guid);
\r
971 check = query.exec();
\r
973 logger.log(logger.EXTREME, "Update note sequence number has failed.");
\r
974 logger.log(logger.MEDIUM, query.lastError());
\r
979 // Update a note resource by the hash
\r
980 public void updateNoteResourceGuidbyHash(String noteGuid, String resGuid, String hash) {
\r
981 NSqlQuery query = new NSqlQuery(db.getResourceConnection());
\r
982 /* query.prepare("Select guid from NoteResources where noteGuid=:noteGuid and datahash=:hex");
\r
983 query.bindValue(":noteGuid", noteGuid);
\r
984 query.bindValue(":hex", hash);
\r
986 if (!query.next()) {
\r
987 logger.log(logger.LOW, "Error finding note resource in RNoteTable.updateNoteResourceGuidbyHash. GUID="+noteGuid +" resGuid="+ resGuid+" hash="+hash);
\r
990 String guid = query.valueString(0);
\r
992 query.prepare("update noteresources set guid=:guid where noteGuid=:noteGuid and datahash=:hex");
\r
993 query.bindValue(":guid", resGuid);
\r
994 query.bindValue(":noteGuid", noteGuid);
\r
995 query.bindValue(":hex", hash);
\r
996 if (!query.exec()) {
\r
997 logger.log(logger.EXTREME, "Note Resource Update by Hash failed");
\r
998 logger.log(logger.EXTREME, query.lastError().toString());
\r
1002 // Fix CRLF problem that is on some notes
\r
1003 private String fixCarriageReturn(String note) {
\r
1004 if (note == null || !Global.enableCarriageReturnFix)
\r
1006 QByteArray a0Hex = new QByteArray("a0");
\r
1007 String a0 = QByteArray.fromHex(a0Hex).toString();
\r
1008 note = note.replace("<div>"+a0+"</div>", "<div> </div>");
\r
1009 return note.replace("<div/>", "<div> </div>");
\r
1012 // Expunge notes that we don't want to synchronize
\r
1013 public List<String> expungeIgnoreSynchronizedNotes(List<String> notebooks, List<String>tags, List<String> linked) {
\r
1015 List<String> noteGuids = new ArrayList<String>();
\r
1016 for (int i=0; i<notebooks.size(); i++) {
\r
1017 List<String> notes = findNotesByNotebook(notebooks.get(i));
\r
1018 for (int j=0; j<notes.size(); j++) {
\r
1019 if (!isNoteDirty(notes.get(j))) {
\r
1020 expungeNote(notes.get(j), true, false);
\r
1021 noteGuids.add(notes.get(j));
\r
1026 for (int i=0; i<tags.size(); i++) {
\r
1027 List<String> notes = findNotesByTag(tags.get(i));
\r
1028 for (int j=0; j<notes.size(); j++) {
\r
1029 if (!isNoteDirty(notes.get(j))) {
\r
1030 expungeNote(notes.get(j), true, false);
\r
1031 noteGuids.add(notes.get(j));
\r
1036 for (int i=0; i<linked.size(); i++) {
\r
1037 String notebookGuid = db.getLinkedNotebookTable().getNotebookGuid(linked.get(i));
\r
1038 if (notebookGuid != null && !notebookGuid.trim().equals("")) {
\r
1039 List<Tag> linkedTags = db.getTagTable().getTagsForNotebook(notebookGuid);
\r
1040 for (int j=0; j<linkedTags.size(); j++)
\r
1041 db.getTagTable().expungeTag(linkedTags.get(j).getGuid(), false);
\r
1043 List<String> notes = findNotesByNotebook(notebookGuid);
\r
1044 for (int j=0; j<notes.size(); j++) {
\r
1045 if (!isNoteDirty(notes.get(j))) {
\r
1046 expungeNote(notes.get(j), true, false);
\r
1047 noteGuids.add(notes.get(j));
\r
1055 // Find a note by its notebook
\r
1056 // Expunge notes that we don't want to synchronize
\r
1057 public List<String> findNotesByNotebook(String notebook) {
\r
1058 List<String> values = new ArrayList<String>();
\r
1059 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1060 query.prepare("Select guid from note where notebookguid=:notebook");
\r
1062 query.bindValue(":notebook", notebook);
\r
1064 while (query.next()) {
\r
1065 values.add(query.valueString(0));
\r
1070 public List<String> findNotesByTag(String tag) {
\r
1071 List<String> values = new ArrayList<String>();
\r
1072 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1073 query.prepare("Select distinct noteguid from notetags where tagguid=:tag");
\r
1075 query.bindValue(":tag", tag);
\r
1077 while (query.next()) {
\r
1078 values.add(query.valueString(0));
\r
1085 //********************************************************************************
\r
1086 //********************************************************************************
\r
1087 //* Indexing Functions
\r
1088 //********************************************************************************
\r
1089 //********************************************************************************
\r
1090 // set/unset a note to be reindexed
\r
1091 public void setIndexNeeded(String guid, Boolean flag) {
\r
1092 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1093 query.prepare("Update Note set indexNeeded=:flag where guid=:guid");
\r
1096 query.bindValue(":flag", 1);
\r
1098 query.bindValue(":flag", 0);
\r
1099 query.bindValue(":guid", guid);
\r
1100 if (!query.exec()) {
\r
1101 logger.log(logger.MEDIUM, "Note indexNeeded update failed.");
\r
1102 logger.log(logger.MEDIUM, query.lastError());
\r
1104 List<Resource> r = noteResourceTable.getNoteResources(guid, false);
\r
1105 for (int i=0; r!= null && i<r.size(); i++) {
\r
1106 noteResourceTable.setIndexNeeded(r.get(i).getGuid(), true);
\r
1109 // Set all notes to be reindexed
\r
1110 public void reindexAllNotes() {
\r
1111 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1112 if (!query.exec("Update Note set indexNeeded=true")) {
\r
1113 logger.log(logger.MEDIUM, "Note reindexAllNotes update failed.");
\r
1114 logger.log(logger.MEDIUM, query.lastError());
\r
1118 // Get all unindexed notes
\r
1119 public List <String> getUnindexed() {
\r
1121 List<String> index = new ArrayList<String>();
\r
1122 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1124 if (!query.exec("Select guid from Note where isExpunged = false and indexNeeded = true and DATEDIFF('MINUTE',updated,CURRENT_TIMESTAMP)>5"))
\r
1125 logger.log(logger.EXTREME, "Note SQL retrieve has failed on getUnindexed().");
\r
1127 // Get a list of the notes
\r
1128 while (query.next()) {
\r
1129 guid = new String();
\r
1130 guid = query.valueString(0);
\r
1135 public List<String> getNextUnindexed(int limit) {
\r
1136 List<String> guids = new ArrayList<String>();
\r
1138 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1140 if (!query.exec("Select guid from Note where isExpunged = false and indexNeeded = true and DATEDIFF('MINUTE',Updated,CURRENT_TIMESTAMP)>5 limit " +limit))
\r
1141 logger.log(logger.EXTREME, "Note SQL retrieve has failed on getUnindexed().");
\r
1143 // Get a list of the notes
\r
1145 while (query.next()) {
\r
1146 guid = new String();
\r
1147 guid = query.valueString(0);
\r
1154 //**********************************************************************************
\r
1155 //* Title color functions
\r
1156 //**********************************************************************************
\r
1157 // Get the title color of all notes
\r
1158 public List<Pair<String, Integer>> getNoteTitleColors() {
\r
1159 List<Pair<String,Integer>> returnValue = new ArrayList<Pair<String,Integer>>();
\r
1160 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1162 if (!query.exec("Select guid,titleColor from Note where titleColor != -1"))
\r
1163 logger.log(logger.EXTREME, "Note SQL retrieve has failed on getUnindexed().");
\r
1168 // Get a list of the notes
\r
1169 while (query.next()) {
\r
1170 Pair<String, Integer> pair = new Pair<String,Integer>();
\r
1171 guid = query.valueString(0);
\r
1172 color = query.valueInteger(1);
\r
1173 pair.setFirst(guid);
\r
1174 pair.setSecond(color);
\r
1175 returnValue.add(pair);
\r
1178 return returnValue;
\r
1180 // Set a title color
\r
1181 public void setNoteTitleColor(String guid, int color) {
\r
1182 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1184 query.prepare("Update note set titlecolor=:color where guid=:guid");
\r
1185 query.bindValue(":guid", guid);
\r
1186 query.bindValue(":color", color);
\r
1187 if (!query.exec())
\r
1188 logger.log(logger.EXTREME, "Error updating title color.");
\r
1190 // Get in individual note's title color
\r
1191 // Get the title color of all notes
\r
1192 public Integer getNoteTitleColor(String guid) {
\r
1193 List<Pair<String,Integer>> returnValue = new ArrayList<Pair<String,Integer>>();
\r
1194 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1196 query.prepare("Select titleColor from Note where titleColor != -1 and guid=:guid");
\r
1197 query.bindValue(":guid", guid);
\r
1198 if (!query.exec())
\r
1199 logger.log(logger.EXTREME, "Note SQL retrieve has failed on getNoteTitleColor(guid).");
\r
1201 Integer color = -1;
\r
1203 // Get a list of the notes
\r
1204 while (query.next()) {
\r
1205 Pair<String, Integer> pair = new Pair<String,Integer>();
\r
1206 guid = query.valueString(0);
\r
1207 color = query.valueInteger(1);
\r
1208 pair.setFirst(guid);
\r
1209 pair.setSecond(color);
\r
1210 returnValue.add(pair);
\r
1218 //**********************************************************************************
\r
1219 //* Thumbnail functions
\r
1220 //**********************************************************************************
\r
1221 // Set if a new thumbnail is needed
\r
1222 public void setThumbnailNeeded(String guid, boolean needed) {
\r
1225 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1227 check = query.prepare("Update note set thumbnailneeded = :needed where guid=:guid");
\r
1228 query.bindValue(":guid", guid);
\r
1229 query.bindValue(":needed", needed);
\r
1230 check = query.exec();
\r
1232 logger.log(logger.EXTREME, "Note SQL set thumbail needed failed: " +query.lastError().toString());
\r
1235 // Is a thumbail needed for this guid?
\r
1236 public boolean isThumbnailNeeded(String guid) {
\r
1239 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1241 check = query.prepare("select thumbnailneeded from note where guid=:guid");
\r
1242 query.bindValue(":guid", guid);
\r
1243 check = query.exec();
\r
1245 logger.log(logger.EXTREME, "Note SQL isThumbnailNeeded query failed: " +query.lastError().toString());
\r
1247 boolean returnValue;
\r
1248 // Get a list of the notes
\r
1249 if (query.next())
\r
1250 returnValue = query.valueBoolean(0, false);
\r
1252 returnValue = false;
\r
1254 return returnValue;
\r
1256 // Set if a new thumbnail is needed
\r
1257 public void setThumbnail(String guid, QByteArray thumbnail) {
\r
1260 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1262 check = query.prepare("Update note set thumbnail = :thumbnail where guid=:guid");
\r
1263 query.bindValue(":guid", guid);
\r
1264 query.bindValue(":thumbnail", thumbnail.toByteArray());
\r
1265 check = query.exec();
\r
1267 logger.log(logger.EXTREME, "Note SQL set thumbail failed: " +query.lastError().toString());
\r
1270 // Set if a new thumbnail is needed
\r
1271 public QByteArray getThumbnail(String guid) {
\r
1274 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1276 check = query.prepare("Select thumbnail from note where guid=:guid");
\r
1277 query.bindValue(":guid", guid);
\r
1278 check = query.exec();
\r
1280 logger.log(logger.EXTREME, "Note SQL get thumbail failed: " +query.lastError().toString());
\r
1281 // Get a list of the notes
\r
1282 if (query.next()) {
\r
1284 if (query.getBlob(0) != null) {
\r
1285 return new QByteArray(query.getBlob(0));
\r
1287 } catch (java.lang.IllegalArgumentException e) {
\r
1293 // Get all thumbnails
\r
1294 public HashMap<String, QPixmap> getThumbnails() {
\r
1296 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1297 HashMap<String, QPixmap> map = new HashMap<String,QPixmap>();
\r
1299 check = query.prepare("Select guid,thumbnail from note where thumbnailneeded=false and isExpunged=false");
\r
1300 check = query.exec();
\r
1302 logger.log(logger.EXTREME, "Note SQL get thumbail failed: " +query.lastError().toString());
\r
1303 // Get a list of the notes
\r
1304 while (query.next()) {
\r
1306 if (query.getBlob(1) != null) {
\r
1307 QByteArray data = new QByteArray(query.getBlob(1));
\r
1308 QPixmap img = new QPixmap();
\r
1309 if (img.loadFromData(data)) {
\r
1310 img = img.scaled(Global.largeThumbnailSize);
\r
1311 map.put(query.valueString(0), img);
\r
1314 } catch (java.lang.IllegalArgumentException e) {
\r
1315 logger.log(logger.HIGH, "Error retrieving thumbnail " +e.getMessage());
\r
1320 // Get a list of notes that need thumbnails
\r
1321 public List<String> findThumbnailsNeeded() {
\r
1324 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1326 check = query.prepare("select guid from note where thumbnailneeded=true and isExpunged=false and DATEDIFF('MINUTE',updated,CURRENT_TIMESTAMP)>5 limit 5");
\r
1327 check = query.exec();
\r
1329 logger.log(logger.EXTREME, "Note SQL findThumbnailsNeeded query failed: " +query.lastError().toString());
\r
1332 // Get a list of the notes
\r
1333 List<String> values = new ArrayList<String>();
\r
1334 while (query.next()) {
\r
1335 values.add(query.valueString(0));
\r
1340 // Get a count of thumbnails needed
\r
1341 public int getThumbnailNeededCount() {
\r
1344 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1346 check = query.prepare("select count(guid) from note where thumbnailneeded=true and isExpunged=false and DATEDIFF('MINUTE',updated,CURRENT_TIMESTAMP)>5 limit 2");
\r
1347 check = query.exec();
\r
1349 logger.log(logger.EXTREME, "Note SQL findThumbnailNeededCount query failed: " +query.lastError().toString());
\r
1351 if (query.next()) {
\r
1352 return query.valueInteger(0);
\r
1359 // Update a note content's hash. This happens if a resource is edited outside of NN
\r
1360 public void updateResourceContentHash(String guid, String oldHash, String newHash) {
\r
1361 Note n = getNote(guid, true, false, false, false,false);
\r
1362 int position = n.getContent().indexOf("<en-media");
\r
1364 for (;position>-1;) {
\r
1365 endPos = n.getContent().indexOf(">", position+1);
\r
1366 String oldSegment = n.getContent().substring(position,endPos);
\r
1367 int hashPos = oldSegment.indexOf("hash=\"");
\r
1368 int hashEnd = oldSegment.indexOf("\"", hashPos+7);
\r
1369 String hash = oldSegment.substring(hashPos+6, hashEnd);
\r
1370 if (hash.equalsIgnoreCase(oldHash)) {
\r
1371 String newSegment = oldSegment.replace(oldHash, newHash);
\r
1372 String content = n.getContent().substring(0,position) +
\r
1374 n.getContent().substring(endPos);
\r
1375 NSqlQuery query = new NSqlQuery(db.getConnection());
\r
1376 query.prepare("update note set isdirty=true, thumbnailneeded=true, content=:content where guid=:guid");
\r
1377 query.bindValue(":content", content);
\r
1378 query.bindValue(":guid", n.getGuid());
\r
1382 position = n.getContent().indexOf("<en-media", position+1);
\r