OSDN Git Service

Added function to find the children of a tag. Not currently used, but will be in...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / NoteTable.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 \r
21 package cx.fbn.nevernote.sql;\r
22 \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
29 \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
38 \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
44 \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
50         int id;\r
51 \r
52         // Prepared Queries to improve speed\r
53         private NSqlQuery                                               getQueryWithContent;\r
54         private NSqlQuery                                               getQueryWithoutContent;\r
55         private NSqlQuery                                               getAllQueryWithoutContent;\r
56         \r
57         // Constructor\r
58         public NoteTable(ApplicationLogger l, DatabaseConnection d) {\r
59                 logger = l;\r
60                 db = d;\r
61                 id = 0;\r
62                 noteResourceTable = new NoteResourceTable(logger, db);\r
63                 noteTagsTable = new NoteTagsTable(logger, db);\r
64                 getQueryWithContent = null;\r
65                 getQueryWithoutContent = null;\r
66         }\r
67         // Create the table\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
88         }\r
89         // Drop the table\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
95         }\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
99                 if (n == null)\r
100                         return;\r
101                 \r
102                 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");\r
103 \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
111                                 +") Values("\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
117 \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
121 \r
122                 \r
123                 \r
124                 query.bindValue(":guid", n.getGuid());\r
125                 query.bindValue(":updateSequenceNumber", n.getUpdateSequenceNum());\r
126                 query.bindValue(":title", n.getTitle());\r
127                 if (isDirty) {\r
128                         EnmlConverter enml = new EnmlConverter(logger);\r
129                         query.bindValue(":content", enml.fixEnXMLCrap(enml.fixEnMediaCrap(n.getContent())));\r
130                 }\r
131                 else\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
140                 \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
151                 }\r
152                 query.bindValue(":indexNeeded", true);\r
153                 query.bindValue(":isExpunged", false);\r
154                 query.bindValue(":isDirty", isDirty);\r
155 \r
156                 \r
157                 if (!query.exec())\r
158                         logger.log(logger.MEDIUM, query.lastError());\r
159                 \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
164                 }\r
165                 \r
166                 logger.log(logger.EXTREME, "Leaving addNote");\r
167         } \r
168         // Setup queries for get to save time later\r
169         private void prepareQueries() {\r
170                 if (getQueryWithContent != null)\r
171                         return;\r
172                 getQueryWithContent = new NSqlQuery(db.getConnection());\r
173                 getQueryWithoutContent = new NSqlQuery(db.getConnection());\r
174                 getAllQueryWithoutContent = new NSqlQuery(db.getConnection());\r
175                 \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
185                 }\r
186                 \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
195                 }\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
204                 }\r
205         }\r
206 \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
212                 query.exec();           \r
213                 query.next();\r
214                 return query.valueString(0);\r
215         }\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
221                 query.exec();           \r
222                 query.next();\r
223                 return query.valueString(0);\r
224         }\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
228                         return null;\r
229                 if (noteGuid.trim().equals(""))\r
230                         return null;\r
231 \r
232                 prepareQueries();\r
233                 NSqlQuery query;\r
234                 if (loadContent) {\r
235                         query = getQueryWithContent;\r
236                 } else {\r
237                         query = getQueryWithoutContent;\r
238                 }\r
239                 \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
244                         return null;\r
245                 }\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
250                         return null;\r
251                 }\r
252                 Note n = mapNoteFromQuery(query, loadContent, loadResources, loadRecognition, loadBinary, loadTags);\r
253                 n.setContent(fixCarriageReturn(n.getContent()));\r
254                 return n;\r
255         }\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
260 \r
261                 Note n = new Note();\r
262                 NoteAttributes na = new NoteAttributes();\r
263                 n.setAttributes(na);\r
264                 \r
265                 n.setGuid(query.valueString(0));\r
266                 n.setUpdateSequenceNum(new Integer(query.valueString(1)));\r
267                 n.setTitle(query.valueString(2));\r
268 \r
269                 try {\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
275                 }\r
276 \r
277                 n.setActive(query.valueBoolean(6,true));\r
278                 n.setNotebookGuid(query.valueString(7));\r
279                 \r
280                 try {\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
286                 }\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
294                 \r
295                 if (loadTags) {\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
303                         }\r
304                         n.setTagNames(tagNames);\r
305                 }\r
306                 \r
307                 if (loadContent) {\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
313                         \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
318                 }\r
319                 if (loadResources)\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
325                         } else {\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
330                                 }\r
331                         }\r
332                 }\r
333                 n.setContent(fixCarriageReturn(n.getContent()));\r
334                 return n;\r
335         }\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
340                 if (!check) {\r
341                         logger.log(logger.EXTREME, "Update note title sql prepare has failed.");\r
342                         logger.log(logger.MEDIUM, query.lastError());\r
343                 }\r
344                 query.bindValue(":title", title);\r
345                 query.bindValue(":guid", guid);\r
346                 check = query.exec();\r
347                 if (!check) {\r
348                         logger.log(logger.EXTREME, "Update note title has failed.");\r
349                         logger.log(logger.MEDIUM, query.lastError());\r
350                 }\r
351         }\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
356                 if (!check) {\r
357                         logger.log(logger.EXTREME, "Update note creation update sql prepare has failed.");\r
358                         logger.log(logger.MEDIUM, query.lastError());\r
359                 }\r
360                 \r
361                 query.bindValue(":created", date.toString("yyyy-MM-dd HH:mm:ss"));\r
362                 query.bindValue(":guid", guid);\r
363                 \r
364                 check = query.exec();\r
365                 if (!check) {\r
366                         logger.log(logger.EXTREME, "Update note creation date has failed.");\r
367                         logger.log(logger.MEDIUM, query.lastError());\r
368                 }\r
369         }\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
374                 if (!check) {\r
375                         logger.log(logger.EXTREME, "Update note altered sql prepare has failed.");\r
376                         logger.log(logger.MEDIUM, query.lastError());\r
377                 }\r
378                 \r
379                 query.bindValue(":altered", date.toString("yyyy-MM-dd HH:mm:ss"));\r
380                 query.bindValue(":guid", guid);\r
381                 \r
382                 check = query.exec();\r
383                 if (!check) {\r
384                         logger.log(logger.EXTREME, "Update note altered date has failed.");\r
385                         logger.log(logger.MEDIUM, query.lastError());\r
386                 }\r
387         }\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
392                 if (!check) {\r
393                         logger.log(logger.EXTREME, "Update note subject date sql prepare has failed.");\r
394                         logger.log(logger.MEDIUM, query.lastError());\r
395                 }\r
396         \r
397                 query.bindValue(":altered", date.toString("yyyy-MM-dd HH:mm:ss"));\r
398                 query.bindValue(":guid", guid);\r
399                 \r
400                 check = query.exec();\r
401                 if (!check) {\r
402                         logger.log(logger.EXTREME, "Update note subject date date has failed.");\r
403                         logger.log(logger.MEDIUM, query.lastError());\r
404                 }\r
405         }\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
410                 if (!check) {\r
411                         logger.log(logger.EXTREME, "Update note author sql prepare has failed.");\r
412                         logger.log(logger.MEDIUM, query.lastError());\r
413                 }\r
414 \r
415                 query.bindValue(":author", author);\r
416                 query.bindValue(":guid", guid);\r
417 \r
418                 check = query.exec();\r
419                 if (!check) {\r
420                         logger.log(logger.EXTREME, "Update note author has failed.");\r
421                         logger.log(logger.MEDIUM, query.lastError());\r
422                 }\r
423                 \r
424         }\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
430                 if (!check) {\r
431                         logger.log(logger.EXTREME, "Update note author sql prepare has failed.");\r
432                         logger.log(logger.MEDIUM, query.lastError());\r
433                 }\r
434 \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
439 \r
440                 check = query.exec();\r
441                 if (!check) {\r
442                         logger.log(logger.EXTREME, "Update note geo tag has failed.");\r
443                         logger.log(logger.MEDIUM, query.lastError());\r
444                 }\r
445                 \r
446         }\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
451                 if (!check) {\r
452                         logger.log(logger.EXTREME, "Update note url sql prepare has failed.");\r
453                         logger.log(logger.MEDIUM, query.lastError());\r
454                 }\r
455                 \r
456                 query.bindValue(":url", url);\r
457                 query.bindValue(":guid", guid);\r
458 \r
459                 check = query.exec();\r
460                 if (!check) {\r
461                         logger.log(logger.EXTREME, "Update note url has failed.");\r
462                         logger.log(logger.MEDIUM, query.lastError());\r
463                 }\r
464                 \r
465         }\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
469                 \r
470                 \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
480                         }\r
481                 }\r
482                 \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
485                 if (!check) {\r
486                         logger.log(logger.EXTREME, "Update note notebook sql prepare has failed.");\r
487                         logger.log(logger.MEDIUM, query.lastError());\r
488                 }\r
489                 query.bindValue(":notebook", notebookGuid);\r
490                 query.bindValue(":guid", guid);\r
491                 \r
492                 check = query.exec();\r
493                 if (!check) {\r
494                         logger.log(logger.EXTREME, "Update note notebook has failed.");\r
495                         logger.log(logger.MEDIUM, query.lastError());\r
496                 };\r
497         }\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
503                 if (!check) {\r
504                         logger.log(logger.EXTREME, "Update note content sql prepare has failed.");\r
505                         logger.log(logger.MEDIUM, query.lastError());\r
506                 }\r
507                 \r
508                 query.bindValue(":content", content);\r
509                 query.bindValue(":guid", guid);\r
510 \r
511                 check = query.exec();\r
512                 if (!check) {\r
513                         logger.log(logger.EXTREME, "Update note content has failed.");\r
514                         logger.log(logger.MEDIUM, query.lastError());\r
515                 }\r
516         }\r
517 \r
518         // Delete a note\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
526                 }\r
527         }\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
536                 }\r
537         }\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
540                 \r
541                 if (!permanentExpunge) {\r
542                         hideExpungedNote(guid, needsSync);\r
543                         return;\r
544                 }\r
545                 \r
546                 \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
551         \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
556 \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
561         \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
566                 }\r
567                 if (!resources.exec()) {\r
568                                 logger.log(logger.MEDIUM, "Purge from resources failed.");\r
569                         logger.log(logger.MEDIUM, resources.lastError());\r
570                 }\r
571                 if (!tags.exec()) {\r
572                         logger.log(logger.MEDIUM, "Note tags delete failed.");\r
573                         logger.log(logger.MEDIUM, tags.lastError());\r
574                 }\r
575                 if (!words.exec()) {\r
576                         logger.log(logger.MEDIUM, "Word delete failed.");\r
577                         logger.log(logger.MEDIUM, words.lastError());\r
578                 }\r
579                 if (needsSync) {\r
580                         DeletedTable deletedTable = new DeletedTable(logger, db);\r
581                         deletedTable.addDeletedItem(guid, "Note");\r
582                 }\r
583 \r
584         }\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
591         \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
596 \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
601 \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
606                 }\r
607                 if (!resources.exec()) {\r
608                                 logger.log(logger.MEDIUM, "Purge from resources failed.");\r
609                         logger.log(logger.MEDIUM, resources.lastError());\r
610                 }\r
611                 if (!tags.exec()) {\r
612                         logger.log(logger.MEDIUM, "Note tags delete failed.");\r
613                         logger.log(logger.MEDIUM, tags.lastError());\r
614                 }\r
615                 if (!words.exec()) {\r
616                         logger.log(logger.MEDIUM, "Word delete failed.");\r
617                         logger.log(logger.MEDIUM, words.lastError());\r
618                 }\r
619                 if (needsSync) {\r
620                         DeletedTable deletedTable = new DeletedTable(logger, db);\r
621                         deletedTable.addDeletedItem(guid, "Note");\r
622                 }\r
623         }\r
624 \r
625                 \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
633                         if (usn == 0)\r
634                                 expungeNote(guid, true, false);\r
635                         else\r
636                                 expungeNote(guid, false, true);\r
637                 }\r
638         }\r
639         // Update the note sequence number\r
640         public void updateNoteSequence(String guid, int sequence) {\r
641                 boolean check;\r
642         NSqlQuery query = new NSqlQuery(db.getConnection());\r
643                 check = query.prepare("Update Note set updateSequenceNumber=:sequence where guid=:guid");\r
644 \r
645                 query.bindValue(":sequence", sequence);\r
646                 query.bindValue(":guid", guid);\r
647                 \r
648                 query.exec();\r
649                 if (!check) {\r
650                         logger.log(logger.MEDIUM, "Note sequence update failed.");\r
651                         logger.log(logger.MEDIUM, query.lastError());\r
652                 } \r
653         }\r
654         // Update the note Guid\r
655         public void updateNoteGuid(String oldGuid, String newGuid) {\r
656                 boolean check;\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
661 \r
662                 query.bindValue(":newGuid", newGuid);\r
663                 query.bindValue(":oldGuid", oldGuid);\r
664 \r
665                 check = query.exec();\r
666                 if (!check) {\r
667                         logger.log(logger.MEDIUM, "Note Guid update failed.");\r
668                         logger.log(logger.MEDIUM, query.lastError());\r
669                 } \r
670                 \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
675                 if (!check) {\r
676                         logger.log(logger.MEDIUM, "Note guid update failed for NoteTags.");\r
677                         logger.log(logger.MEDIUM, query.lastError());\r
678                 }\r
679                 \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
683                 wordQuery.exec();\r
684                 if (!check) {\r
685                         logger.log(logger.MEDIUM, "Note guid update failed for Words.");\r
686                         logger.log(logger.MEDIUM, wordQuery.lastError());\r
687                 }\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
691                 resQuery.exec();\r
692                 if (!check) {\r
693                         logger.log(logger.MEDIUM, "Note guid update failed for noteresources.");\r
694                         logger.log(logger.MEDIUM, resQuery.lastError());\r
695                 }\r
696         }\r
697         // Update a note\r
698         public void updateNote(Note n, boolean isNew) {\r
699                 int titleColor = getNoteTitleColor(n.getGuid());\r
700                 expungeNote(n.getGuid(), true, false);\r
701                 addNote(n, false);\r
702                 if (titleColor != -1)\r
703                         setNoteTitleColor(n.getGuid(), titleColor);\r
704         }\r
705         // Does a note exist?\r
706         public boolean exists(String guid) {\r
707                 if (guid == null)\r
708                         return false;\r
709                 if (guid.trim().equals(""))\r
710                         return false;\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
714                 if (!query.exec())\r
715                         logger.log(logger.EXTREME, "note.exists SQL retrieve has failed.");\r
716                 boolean retVal = query.next();\r
717                 return retVal;\r
718         }\r
719         // Does a note exist?\r
720         public boolean isNoteExpunged(String guid) {\r
721                 if (guid == null)\r
722                         return false;\r
723                 if (guid.trim().equals(""))\r
724                         return false;\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
728                 if (!query.exec())\r
729                         logger.log(logger.EXTREME, "note.isNoteExpunged SQL retrieve has failed.");\r
730                 boolean retVal = query.next();\r
731                 return retVal;\r
732         }\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
737                 else\r
738                         addNote(tag, isDirty);\r
739         }\r
740         // Get a list of notes that need to be updated\r
741         public List <Note> getDirty() {\r
742                 String guid;\r
743                 Note tempNote;\r
744                 List<Note> notes = new ArrayList<Note>();\r
745                 List<String> index = new ArrayList<String>();\r
746                 \r
747                 boolean check;                  \r
748         NSqlQuery query = new NSqlQuery(db.getConnection());\r
749                                         \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
751                 if (!check) \r
752                         logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());\r
753                 \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
758                         index.add(guid); \r
759                 }       \r
760                 \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
765                 }\r
766                 return notes;   \r
767         }\r
768         // Get a list of notes that need to be updated\r
769         public List <Note> getDirtyLinkedNotes() {\r
770                 String guid;\r
771                 Note tempNote;\r
772                 List<Note> notes = new ArrayList<Note>();\r
773                 List<String> index = new ArrayList<String>();\r
774                 \r
775                 boolean check;                  \r
776         NSqlQuery query = new NSqlQuery(db.getConnection());\r
777                                         \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
779                 if (!check) \r
780                         logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());\r
781                 \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
786                         index.add(guid); \r
787                 }       \r
788                 \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
793                 }\r
794                 return notes;   \r
795         }\r
796         // Get a list of notes that need to be updated\r
797         public List <Note> getDirtyLinked(String notebookGuid) {\r
798                 String guid;\r
799                 Note tempNote;\r
800                 List<Note> notes = new ArrayList<Note>();\r
801                 List<String> index = new ArrayList<String>();\r
802                 \r
803                 boolean check;                  \r
804         NSqlQuery query = new NSqlQuery(db.getConnection());\r
805                                         \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
809                 if (!check) \r
810                         logger.log(logger.EXTREME, "Note SQL retrieve has failed getting dirty linked notes: " +query.lastError().toString());\r
811                 \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
816                         index.add(guid); \r
817                 }       \r
818                 \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
823                 }\r
824                 return notes;   \r
825         }\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
830                 \r
831                 boolean check;                  \r
832         NSqlQuery query = new NSqlQuery(db.getConnection());\r
833                                         \r
834                 check = query.prepare("Select guid from Note where notebookguid=:notebookguid");\r
835                 if (!check) \r
836                         logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());\r
837                 query.bindValue(":notebookguid", notebookGuid);\r
838                 query. exec();\r
839                 \r
840                 // Get a list of the notes\r
841                 while (query.next()) {\r
842                         index.add(query.valueString(0)); \r
843                 }       \r
844                 \r
845                 return notes;   \r
846         }\r
847         // Get a list of notes that need to be updated\r
848         public boolean isNoteDirty(String guid) {\r
849                 \r
850                 boolean check;                  \r
851         NSqlQuery query = new NSqlQuery(db.getConnection());\r
852                                         \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
856                 if (!check) \r
857                         logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());\r
858                 \r
859                 boolean returnValue;\r
860                 // Get a list of the notes\r
861                 if (query.next()) \r
862                         returnValue = true; \r
863                 else\r
864                         returnValue = false;\r
865 \r
866                 return returnValue;     \r
867         }\r
868         // Get a list of notes that need to be updated\r
869         public List <String> getUnsynchronizedGUIDs() {\r
870                 String guid;\r
871                 List<String> index = new ArrayList<String>();\r
872                 \r
873                 boolean check;                  \r
874         NSqlQuery query = new NSqlQuery(db.getConnection());\r
875                                         \r
876                 check = query.exec("Select guid from Note where isDirty=true");\r
877                 if (!check) \r
878                         logger.log(logger.EXTREME, "Note SQL retrieve has failed: " +query.lastError().toString());\r
879                 \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
884                         index.add(guid); \r
885                 }       \r
886                 return index;   \r
887         }\r
888         // Reset the dirty bit\r
889         public void  resetDirtyFlag(String guid) {\r
890                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
891                 \r
892                 query.prepare("Update note set isdirty=false where guid=:guid");\r
893                 query.bindValue(":guid", guid);\r
894                 if (!query.exec())\r
895                         logger.log(logger.EXTREME, "Error resetting note dirty field.");\r
896         }\r
897         // Get all notes\r
898         public List<String> getAllGuids() {\r
899                 List<String> notes = new ArrayList<String>();\r
900                 \r
901                 boolean check;                                  \r
902         NSqlQuery query = new NSqlQuery(db.getConnection());\r
903                                         \r
904                 check = query.exec("Select guid from Note");\r
905                 if (!check)\r
906                         logger.log(logger.EXTREME, "Notebook SQL retrieve has failed: "+query.lastError());\r
907 \r
908                 // Get a list of the notes\r
909                 while (query.next()) {\r
910                         notes.add(new String(query.valueString(0))); \r
911                 }\r
912                 return notes;\r
913         }\r
914         // Get all notes\r
915         public List<Note> getAllNotes() {\r
916                 List<Note> notes = new ArrayList<Note>();\r
917                 prepareQueries();\r
918                 boolean check;                                  \r
919         NSqlQuery query = getAllQueryWithoutContent;\r
920                 check = query.exec();\r
921                 if (!check)\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
926                 }\r
927                 return notes;\r
928         }\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
933                 query.next(); \r
934                 int returnValue = new Integer(query.valueString(0));\r
935                 return returnValue;\r
936         }\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
941                 query.next(); \r
942                 int returnValue = new Integer(query.valueString(0));\r
943                 return returnValue;\r
944         }\r
945         // Count notes\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
949                 query.next(); \r
950                 int returnValue = new Integer(query.valueString(0));\r
951                 return returnValue;\r
952         }\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
958                         return 0;\r
959                 int returnValue = new Integer(query.valueString(0));\r
960                 return returnValue;\r
961         }\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
966                 if (!check) {\r
967                         logger.log(logger.EXTREME, "Update note ResetSequence sql prepare has failed.");\r
968                         logger.log(logger.MEDIUM, query.lastError());\r
969                 }\r
970                 query.bindValue(":guid", guid);\r
971                 check = query.exec();\r
972                 if (!check) {\r
973                         logger.log(logger.EXTREME, "Update note sequence number has failed.");\r
974                         logger.log(logger.MEDIUM, query.lastError());\r
975                 }\r
976         }\r
977         \r
978         \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
985                 query.exec();\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
988                         return;\r
989                 }\r
990                 String guid = query.valueString(0);\r
991 */              \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
999                 }\r
1000         }\r
1001 \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
1005                         return note;\r
1006                 QByteArray a0Hex = new QByteArray("a0");\r
1007                 String a0 = QByteArray.fromHex(a0Hex).toString();\r
1008                 note = note.replace("<div>"+a0+"</div>", "<div>&nbsp;</div>");\r
1009                 return note.replace("<div/>", "<div>&nbsp;</div>");\r
1010         }\r
1011         \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
1014                 \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
1022                                 }\r
1023                         }\r
1024                 }\r
1025                 \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
1032                                 }\r
1033                         }\r
1034                 }\r
1035                 \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
1042                                 \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
1048                                         }\r
1049                                 }\r
1050                         }\r
1051                 }\r
1052                 return noteGuids;\r
1053         }\r
1054         \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
1061 \r
1062                 query.bindValue(":notebook", notebook);\r
1063                 query.exec();\r
1064                 while (query.next()) {\r
1065                         values.add(query.valueString(0));\r
1066                 }\r
1067                 return values;\r
1068         }\r
1069         \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
1074 \r
1075                 query.bindValue(":tag", tag);\r
1076                 query.exec();\r
1077                 while (query.next()) {\r
1078                         values.add(query.valueString(0));\r
1079                 }\r
1080                 return values;\r
1081         }\r
1082         \r
1083         \r
1084         \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
1094 \r
1095                 if (flag)\r
1096                         query.bindValue(":flag", 1);\r
1097                 else\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
1103                 } \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
1107                 }\r
1108         }\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
1115                 } \r
1116         }\r
1117 \r
1118         // Get all unindexed notes\r
1119         public List <String> getUnindexed() {\r
1120                 String guid;\r
1121                 List<String> index = new ArrayList<String>();\r
1122         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1123                                         \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
1126 \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
1131                         index.add(guid); \r
1132                 }       \r
1133                 return index;   \r
1134         }\r
1135         public List<String> getNextUnindexed(int limit) {\r
1136                 List<String> guids = new ArrayList<String>();\r
1137                         \r
1138         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1139                                         \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
1142                 \r
1143                 // Get a list of the notes\r
1144                 String guid;\r
1145                 while (query.next()) {\r
1146                         guid = new String();\r
1147                         guid = query.valueString(0);\r
1148                         guids.add(guid);\r
1149                 }       \r
1150                 return guids;   \r
1151         }\r
1152         \r
1153         \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
1161                 \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
1164 \r
1165                 String guid;\r
1166                 Integer color;\r
1167                 \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
1176                 }       \r
1177 \r
1178                 return returnValue;\r
1179         }\r
1180         // Set a title color\r
1181         public void  setNoteTitleColor(String guid, int color) {\r
1182                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
1183                 \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
1189         }\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
1195                 \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
1200 \r
1201                 Integer color = -1;\r
1202                 \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
1211                 }       \r
1212 \r
1213                 \r
1214                 return color;\r
1215         }\r
1216         \r
1217         \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
1223                 \r
1224                 boolean check;                  \r
1225         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1226                                         \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
1231                 if (!check) \r
1232                         logger.log(logger.EXTREME, "Note SQL set thumbail needed failed: " +query.lastError().toString());\r
1233 \r
1234         }\r
1235         // Is a thumbail needed for this guid?\r
1236         public boolean isThumbnailNeeded(String guid) {\r
1237                 \r
1238                 boolean check;                  \r
1239         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1240                                         \r
1241                 check = query.prepare("select thumbnailneeded from note where guid=:guid");\r
1242                 query.bindValue(":guid", guid);\r
1243                 check = query.exec();\r
1244                 if (!check) \r
1245                         logger.log(logger.EXTREME, "Note SQL isThumbnailNeeded query failed: " +query.lastError().toString());\r
1246                 \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
1251                 else\r
1252                         returnValue = false;\r
1253 \r
1254                 return returnValue;     \r
1255         }\r
1256         // Set if a new thumbnail is needed\r
1257         public void setThumbnail(String guid, QByteArray thumbnail) {\r
1258                 \r
1259                 boolean check;                  \r
1260         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1261                                         \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
1266                 if (!check) \r
1267                         logger.log(logger.EXTREME, "Note SQL set thumbail failed: " +query.lastError().toString());\r
1268 \r
1269         }\r
1270         // Set if a new thumbnail is needed\r
1271         public QByteArray getThumbnail(String guid) {\r
1272                 \r
1273                 boolean check;                  \r
1274         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1275                                         \r
1276                 check = query.prepare("Select thumbnail from note where guid=:guid");\r
1277                 query.bindValue(":guid", guid);\r
1278                 check = query.exec();\r
1279                 if (!check) \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
1283                         try {\r
1284                                 if (query.getBlob(0) != null) {\r
1285                                         return new QByteArray(query.getBlob(0)); \r
1286                                 }\r
1287                         } catch (java.lang.IllegalArgumentException e) {\r
1288                                 return null;\r
1289                         }\r
1290                 }\r
1291                 return null;\r
1292         }\r
1293         // Get all thumbnails\r
1294         public HashMap<String, QPixmap> getThumbnails() {\r
1295                 boolean check;                  \r
1296         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1297         HashMap<String, QPixmap> map = new HashMap<String,QPixmap>();\r
1298                                         \r
1299                 check = query.prepare("Select guid,thumbnail from note where thumbnailneeded=false and isExpunged=false");\r
1300                 check = query.exec();\r
1301                 if (!check) \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
1305                         try {\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
1312                                         }\r
1313                                 }       \r
1314                         } catch (java.lang.IllegalArgumentException e) {\r
1315                                 logger.log(logger.HIGH, "Error retrieving thumbnail " +e.getMessage());\r
1316                         }\r
1317                 }\r
1318                 return map;\r
1319         }\r
1320         // Get a list of notes that need thumbnails\r
1321         public List<String> findThumbnailsNeeded() {\r
1322                 \r
1323                 boolean check;\r
1324         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1325                                         \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
1328                 if (!check) \r
1329                         logger.log(logger.EXTREME, "Note SQL findThumbnailsNeeded query failed: " +query.lastError().toString());\r
1330                 \r
1331 \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
1336                 }\r
1337 \r
1338                 return values;  \r
1339         }\r
1340         // Get a count of thumbnails needed\r
1341         public int getThumbnailNeededCount() {\r
1342                 \r
1343                 boolean check;\r
1344         NSqlQuery query = new NSqlQuery(db.getConnection());\r
1345                                         \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
1348                 if (!check) \r
1349                         logger.log(logger.EXTREME, "Note SQL findThumbnailNeededCount query failed: " +query.lastError().toString());\r
1350                 \r
1351                 if (query.next()) {\r
1352                         return query.valueInteger(0); \r
1353                 }\r
1354 \r
1355                 return 0;       \r
1356         }\r
1357 \r
1358         \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
1363                 int endPos;\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
1373                                                  newSegment +\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
1379                                 query.exec();\r
1380                         }\r
1381                         \r
1382                         position = n.getContent().indexOf("<en-media", position+1);\r
1383                 }\r
1384         }\r
1385 }       \r