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.util.List;
\r
25 import com.evernote.edam.type.Note;
\r
26 import com.trolltech.qt.core.QByteArray;
\r
27 import com.trolltech.qt.core.QDateTime;
\r
29 import cx.fbn.nevernote.Global;
\r
30 import cx.fbn.nevernote.sql.requests.NoteRequest;
\r
31 import cx.fbn.nevernote.utilities.Pair;
\r
33 public class NoteTable {
\r
34 public NoteResourceTable noteResourceTable;
\r
35 public NoteTagsTable noteTagsTable;
\r
40 public NoteTable(int i) {
\r
42 noteTagsTable = new NoteTagsTable(id);
\r
43 noteResourceTable = new NoteResourceTable(id);
\r
46 public void createTable() {
\r
47 NoteRequest request = new NoteRequest();
\r
48 request.requestor_id = id;
\r
49 request.type = NoteRequest.Create_Table;
\r
50 Global.dbRunner.addWork(request);
\r
53 public void dropTable() {
\r
54 NoteRequest request = new NoteRequest();
\r
55 request.requestor_id = id;
\r
56 request.type = NoteRequest.Drop_Table;
\r
57 Global.dbRunner.addWork(request);
\r
59 // Save Note List from Evernote
\r
60 public void addNote(Note n, boolean isDirty) {
\r
61 NoteRequest request = new NoteRequest();
\r
62 request.requestor_id = id;
\r
63 request.note = n.deepCopy();
\r
64 request.bool1 = isDirty;
\r
65 request.type = NoteRequest.Add_Note;
\r
66 Global.dbRunner.addWork(request);
\r
68 // Get a note by Guid
\r
69 public Note getNote(String noteGuid, boolean loadContent, boolean loadResources, boolean loadRecognition, boolean loadBinary, boolean loadTags) {
\r
70 NoteRequest request = new NoteRequest();
\r
71 request.requestor_id = id;
\r
72 request.type = NoteRequest.Get_Note;
\r
73 request.string1 = new String(noteGuid);
\r
74 request.bool1 = loadContent;
\r
75 request.bool2 = loadResources;
\r
76 request.bool3 = loadRecognition;
\r
77 request.bool4 = loadBinary;
\r
78 request.bool5 = loadTags;
\r
79 Global.dbRunner.addWork(request);
\r
80 Global.dbClientWait(id);
\r
81 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
82 return req.responseNote;
\r
84 // Update a note's title
\r
85 public void updateNoteTitle(String guid, String title) {
\r
86 NoteRequest request = new NoteRequest();
\r
87 request.requestor_id = id;
\r
88 request.type = NoteRequest.Update_Note_Title;
\r
89 request.string1 = new String(guid);
\r
90 request.string2 = new String(title);
\r
91 Global.dbRunner.addWork(request);
\r
93 // Update a note's creation date
\r
94 public void updateNoteCreatedDate(String guid, QDateTime date) {
\r
95 NoteRequest request = new NoteRequest();
\r
96 request.requestor_id = id;
\r
97 request.type = NoteRequest.Update_Note_Creation_Date;
\r
98 request.string1 = new String(guid);
\r
99 request.date = new QDateTime(date);
\r
100 Global.dbRunner.addWork(request);
\r
102 // Update a note's creation date
\r
103 public void updateNoteAlteredDate(String guid, QDateTime date) {
\r
104 NoteRequest request = new NoteRequest();
\r
105 request.requestor_id = id;
\r
106 request.type = NoteRequest.Update_Note_Altered_Date;
\r
107 request.string1 = new String(guid);
\r
108 request.date = new QDateTime(date);
\r
109 Global.dbRunner.addWork(request);
\r
111 // Update a note's creation date
\r
112 public void updateNoteSubjectDate(String guid, QDateTime date) {
\r
113 NoteRequest request = new NoteRequest();
\r
114 request.requestor_id = id;
\r
115 request.type = NoteRequest.Update_Note_Subject_Date;
\r
116 request.string1 = new String(guid);
\r
117 request.date = new QDateTime(date);
\r
118 Global.dbRunner.addWork(request);
\r
120 // Update a note's author
\r
121 public void updateNoteAuthor(String guid, String author) {
\r
122 NoteRequest request = new NoteRequest();
\r
123 request.requestor_id = id;
\r
124 request.type = NoteRequest.Update_Note_Author;
\r
125 request.string1 = new String(guid);
\r
126 request.string2 = new String(author);
\r
127 Global.dbRunner.addWork(request);
\r
129 // Update a note's creation date
\r
130 public void updateNoteSourceUrl(String guid, String url) {
\r
131 NoteRequest request = new NoteRequest();
\r
132 request.requestor_id = id;
\r
133 request.type = NoteRequest.Update_Note_Source_Url;
\r
134 request.string1 = new String(guid);
\r
135 request.string2 = new String(url);
\r
136 Global.dbRunner.addWork(request);
\r
138 // Update the notebook that a note is assigned to
\r
139 public void updateNoteNotebook(String guid, String notebookGuid, boolean expungeFromRemote) {
\r
140 NoteRequest request = new NoteRequest();
\r
141 request.requestor_id = id;
\r
142 request.type = NoteRequest.Update_Note_Notebook;
\r
143 request.string1 = new String(guid);
\r
144 request.string2 = new String(notebookGuid);
\r
145 request.bool1 = expungeFromRemote;
\r
146 Global.dbRunner.addWork(request);
\r
148 // Update a note's content
\r
149 public void updateNoteContent(String guid, String content) {
\r
150 NoteRequest request = new NoteRequest();
\r
151 request.requestor_id = id;
\r
152 request.type = NoteRequest.Update_Note_Content;
\r
153 request.string1 = new String(guid);
\r
154 request.string2 = new String(content);
\r
155 Global.dbRunner.addWork(request);
\r
157 // Get a note's contents as a binary string (useful for unicode on sync)
\r
158 public String getNoteContentBinary(String guid) {
\r
159 NoteRequest request = new NoteRequest();
\r
160 request.requestor_id = id;
\r
161 request.type = NoteRequest.Get_Note_Content_Binary;
\r
162 request.string1 = new String(guid);
\r
163 Global.dbRunner.addWork(request);
\r
164 Global.dbClientWait(id);
\r
165 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
166 return req.responseString;
\r
168 // Check a note to see if it passes the attribute selection criteria
\r
169 public boolean checkAttributeSelection(Note n) {
\r
170 if (Global.createdSinceFilter.check(n) &&
\r
171 Global.createdBeforeFilter.check(n) &&
\r
172 Global.changedSinceFilter.check(n) &&
\r
173 Global.changedBeforeFilter.check(n) &&
\r
174 Global.containsFilter.check(this, n))
\r
180 public void deleteNote(String guid) {
\r
181 NoteRequest request = new NoteRequest();
\r
182 request.requestor_id = id;
\r
183 request.type = NoteRequest.Delete_Note;
\r
184 request.string1 = new String(guid);
\r
185 Global.dbRunner.addWork(request);
\r
187 public void restoreNote(String guid) {
\r
188 NoteRequest request = new NoteRequest();
\r
189 request.requestor_id = id;
\r
190 request.type = NoteRequest.Restore_Note;
\r
191 request.string1 = new String(guid);
\r
192 Global.dbRunner.addWork(request);
\r
194 // Purge a note (actually delete it instead of just marking it deleted)
\r
195 public void expungeNote(String guid, boolean permanentExpunge, boolean needsSync) {
\r
196 NoteRequest request = new NoteRequest();
\r
197 request.requestor_id = id;
\r
198 request.type = NoteRequest.Expunge_Note;
\r
199 request.string1 = new String(guid);
\r
200 request.bool1 = permanentExpunge;
\r
201 request.bool2 = needsSync;
\r
202 Global.dbRunner.addWork(request);
\r
204 // Purge all deleted notes;
\r
205 public void expungeAllDeletedNotes() {
\r
206 NoteRequest request = new NoteRequest();
\r
207 request.requestor_id = id;
\r
208 request.type = NoteRequest.Expunge_All_Deleted_Notes;
\r
209 Global.dbRunner.addWork(request);
\r
211 // Update the note sequence number
\r
212 public void updateNoteSequence(String guid, int sequence) {
\r
213 NoteRequest request = new NoteRequest();
\r
214 request.requestor_id = id;
\r
215 request.type = NoteRequest.Update_Note_Sequence;
\r
216 request.string1 = new String(guid);
\r
217 request.int1 = sequence;
\r
218 Global.dbRunner.addWork(request);
\r
220 // Update the note Guid
\r
221 public void updateNoteGuid(String oldGuid, String newGuid) {
\r
222 NoteRequest request = new NoteRequest();
\r
223 request.requestor_id = id;
\r
224 request.type = NoteRequest.Update_Note_Guid;
\r
225 request.string1 = new String(oldGuid);
\r
226 request.string2 = new String(newGuid);
\r
227 Global.dbRunner.addWork(request);
\r
230 public void updateNote(Note n, boolean isNew) {
\r
231 NoteRequest request = new NoteRequest();
\r
232 request.requestor_id = id;
\r
233 request.type = NoteRequest.Update_Note;
\r
234 request.note = n.deepCopy();
\r
235 request.bool1 = isNew;
\r
236 Global.dbRunner.addWork(request);
\r
238 // Does a note exist?
\r
239 public boolean exists(String guid) {
\r
240 NoteRequest request = new NoteRequest();
\r
241 request.requestor_id = id;
\r
242 request.type = NoteRequest.Exists;
\r
243 request.string1 = new String(guid);
\r
244 Global.dbRunner.addWork(request);
\r
245 Global.dbClientWait(id);
\r
246 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
247 return req.responseBoolean;
\r
249 // This is a convience method to check if a tag exists & update/create based upon it
\r
250 public void syncNote(Note note, boolean isDirty) {
\r
251 NoteRequest request = new NoteRequest();
\r
252 request.requestor_id = id;
\r
253 request.type = NoteRequest.Sync_Note;
\r
254 request.note = note.deepCopy();
\r
255 request.bool1 = isDirty;
\r
256 Global.dbRunner.addWork(request);
\r
258 // Get a list of notes that need to be updated
\r
259 public List <Note> getDirty() {
\r
260 NoteRequest request = new NoteRequest();
\r
261 request.requestor_id = id;
\r
262 request.type = NoteRequest.Get_Dirty;
\r
263 Global.dbRunner.addWork(request);
\r
264 Global.dbClientWait(id);
\r
265 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
266 return req.responseNotes;
\r
268 // Get a list of notes that need to be updated
\r
269 public List <String> getUnsynchronizedGUIDs() {
\r
270 NoteRequest request = new NoteRequest();
\r
271 request.requestor_id = id;
\r
272 request.type = NoteRequest.Get_Unsynchronized_Guids;
\r
273 Global.dbRunner.addWork(request);
\r
274 Global.dbClientWait(id);
\r
275 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
276 return req.responseStrings;
\r
278 public boolean isNoteDirty(String guid) {
\r
279 NoteRequest request = new NoteRequest();
\r
280 request.requestor_id = id;
\r
281 request.type = NoteRequest.Is_Note_Dirty;
\r
282 request.string1 = new String(guid);
\r
283 Global.dbRunner.addWork(request);
\r
284 Global.dbClientWait(id);
\r
285 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
286 return req.responseBoolean;
\r
288 // Reset the dirty bit
\r
289 public void resetDirtyFlag(String guid) {
\r
290 NoteRequest request = new NoteRequest();
\r
291 request.requestor_id = id;
\r
292 request.type = NoteRequest.Reset_Dirty_Flag;
\r
293 request.string1 = new String(guid);
\r
294 Global.dbRunner.addWork(request);
\r
297 public List<String> getAllGuids() {
\r
298 NoteRequest request = new NoteRequest();
\r
299 request.requestor_id = id;
\r
300 request.type = NoteRequest.Get_All_Guids;
\r
301 Global.dbRunner.addWork(request);
\r
302 Global.dbClientWait(id);
\r
303 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
304 return req.responseStrings;
\r
307 public List<Note> getAllNotes() {
\r
308 NoteRequest request = new NoteRequest();
\r
309 request.requestor_id = id;
\r
310 request.type = NoteRequest.Get_All_Notes;
\r
311 Global.dbRunner.addWork(request);
\r
312 Global.dbClientWait(id);
\r
313 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
314 return req.responseNotes;
\r
316 // Count unindexed notes
\r
317 public int getUnindexedCount() {
\r
318 NoteRequest request = new NoteRequest();
\r
319 request.requestor_id = id;
\r
320 request.type = NoteRequest.Get_Unindexed_Count;
\r
321 Global.dbRunner.addWork(request);
\r
322 Global.dbClientWait(id);
\r
323 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
324 return req.responseInt;
\r
326 // Count unsynchronized count
\r
327 public int getDirtyCount() {
\r
328 NoteRequest request = new NoteRequest();
\r
329 request.requestor_id = id;
\r
330 request.type = NoteRequest.Get_Dirty_Count;
\r
331 Global.dbRunner.addWork(request);
\r
332 Global.dbClientWait(id);
\r
333 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
334 return req.responseInt;
\r
336 // Count unindexed notes
\r
337 public int getNoteCount() {
\r
338 NoteRequest request = new NoteRequest();
\r
339 request.requestor_id = id;
\r
340 request.type = NoteRequest.Get_Note_Count;
\r
341 Global.dbRunner.addWork(request);
\r
342 Global.dbClientWait(id);
\r
343 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
344 return req.responseInt;
\r
346 // Count deleted notes
\r
347 public int getDeletedCount() {
\r
348 NoteRequest request = new NoteRequest();
\r
349 request.requestor_id = id;
\r
350 request.type = NoteRequest.Get_Deleted_Count;
\r
351 Global.dbRunner.addWork(request);
\r
352 Global.dbClientWait(id);
\r
353 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
354 return req.responseInt;
\r
356 // Reset a note's sequence count to zero. This is useful when moving a conflicting note
\r
357 public void resetSequenceNumber(String guid) {
\r
358 NoteRequest request = new NoteRequest();
\r
359 request.requestor_id = id;
\r
360 request.string1 = new String(guid);
\r
361 request.type = NoteRequest.Reset_Note_Sequence;
\r
362 Global.dbRunner.addWork(request);
\r
365 // Update a note resource by the hash
\r
366 public void updateNoteResourceGuidbyHash(String noteGuid, String resGuid, String hash) {
\r
367 NoteRequest request = new NoteRequest();
\r
368 request.requestor_id = id;
\r
369 request.type = NoteRequest.Update_Resource_Guid_By_Hash;
\r
370 request.string1 = new String(noteGuid);
\r
371 request.string2 = new String(resGuid);
\r
372 request.string3 = new String(hash);
\r
373 Global.dbRunner.addWork(request);
\r
376 // Get the title color of notes
\r
377 public List<Pair<String,Integer>> getNoteTitleColors() {
\r
378 NoteRequest request = new NoteRequest();
\r
379 request.requestor_id = id;
\r
380 request.type = NoteRequest.Get_Title_Colors;
\r
381 Global.dbRunner.addWork(request);
\r
382 Global.dbClientWait(id);
\r
383 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
384 return req.responsePair;
\r
387 // Get the title color of notes
\r
388 public void setNoteTitleColor(String guid, int color) {
\r
389 NoteRequest request = new NoteRequest();
\r
390 request.requestor_id = id;
\r
391 request.string1 = new String(guid);
\r
392 request.int1 = color;
\r
393 request.type = NoteRequest.Set_Title_Colors;
\r
394 Global.dbRunner.addWork(request);
\r
398 //********************************************************************************
\r
399 //********************************************************************************
\r
400 //* Indexing Functions
\r
401 //********************************************************************************
\r
402 //********************************************************************************
\r
403 // set/unset a note to be reindexed
\r
404 public void setIndexNeeded(String guid, Boolean flag) {
\r
405 NoteRequest request = new NoteRequest();
\r
406 request.requestor_id = id;
\r
407 request.type = NoteRequest.Set_Index_Needed;
\r
408 request.string1 = new String(guid);
\r
409 request.bool1 = flag;
\r
410 Global.dbRunner.addWork(request);
\r
412 // Set all notes to be reindexed
\r
413 public void reindexAllNotes() {
\r
414 NoteRequest request = new NoteRequest();
\r
415 request.requestor_id = id;
\r
416 request.type = NoteRequest.Reindex_All_Notes;
\r
417 Global.dbRunner.addWork(request);
\r
419 // Get all unindexed notes
\r
420 public List <String> getUnindexed() {
\r
421 NoteRequest request = new NoteRequest();
\r
422 request.requestor_id = id;
\r
423 request.type = NoteRequest.Get_Unindexed;
\r
424 Global.dbRunner.addWork(request);
\r
425 Global.dbClientWait(id);
\r
426 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
427 return req.responseStrings;
\r
429 public List<String> getNextUnindexed(int limit) {
\r
430 NoteRequest request = new NoteRequest();
\r
431 request.requestor_id = id;
\r
432 request.type = NoteRequest.Get_Next_Unindexed;
\r
433 request.int1 = limit;
\r
434 Global.dbRunner.addWork(request);
\r
435 Global.dbClientWait(id);
\r
436 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
437 return req.responseStrings;
\r
441 //*********************************************************************************
\r
442 //* Thumbnail Functions
\r
443 //*********************************************************************************
\r
444 // Set if a new thumbnail is needed
\r
445 public void setThumbnailNeeded(String guid, boolean needed) {
\r
446 NoteRequest request = new NoteRequest();
\r
447 request.requestor_id = id;
\r
448 request.type = NoteRequest.Set_Thumbnail_Needed;
\r
449 request.string1 = new String(guid);
\r
450 request.bool1 = needed;
\r
451 Global.dbRunner.addWork(request);
\r
453 // Is a thumbail needed for this guid?
\r
454 public boolean isThumbnailNeeded(String guid) {
\r
455 NoteRequest request = new NoteRequest();
\r
456 request.requestor_id = id;
\r
457 request.type = NoteRequest.Is_Thumbail_Needed;
\r
458 request.string1 = new String(guid);
\r
459 Global.dbRunner.addWork(request);
\r
460 Global.dbClientWait(id);
\r
461 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
462 return req.responseBoolean;
\r
464 // Set if a new thumbnail is needed
\r
465 public void setThumbnail(String guid, QByteArray thumbnail) {
\r
466 NoteRequest request = new NoteRequest();
\r
467 request.requestor_id = id;
\r
468 request.type = NoteRequest.Set_Thumbnail;
\r
469 request.string1 = new String(guid);
\r
470 request.bytes = thumbnail;
\r
471 Global.dbRunner.addWork(request);
\r
473 // Set if a new thumbnail is needed
\r
474 public QByteArray getThumbnail(String guid) {
\r
475 NoteRequest request = new NoteRequest();
\r
476 request.requestor_id = id;
\r
477 request.type = NoteRequest.Get_Thumbnail;
\r
478 request.string1 = new String(guid);
\r
479 Global.dbRunner.addWork(request);
\r
480 Global.dbClientWait(id);
\r
481 NoteRequest req = Global.dbRunner.noteResponse.get(id).copy();
\r
482 return req.responseBytes;
\r
487 // Update a note content's hash. This happens if a resource is edited outside of NN
\r
488 public void updateResourceContentHash(String guid, String oldHash, String newHash) {
\r
489 NoteRequest request = new NoteRequest();
\r
490 request.requestor_id = id;
\r
491 request.type = NoteRequest.Update_Resource_Content_Hash;
\r
492 request.string1 = new String(guid);
\r
493 request.string2 = new String(oldHash);
\r
494 request.string3 = new String(newHash);
\r
495 Global.dbRunner.addWork(request);
\r