OSDN Git Service

Correct problem where altering a tag for a note may not set the note as dirty.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / NoteTable.java
index ac0aeb1..54da38b 100644 (file)
@@ -43,6 +43,7 @@ import cx.fbn.nevernote.evernote.EnmlConverter;
 import cx.fbn.nevernote.evernote.NoteMetadata;\r
 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
+import cx.fbn.nevernote.utilities.Pair;\r
 \r
 public class NoteTable {\r
        private final ApplicationLogger                 logger;\r
@@ -150,6 +151,16 @@ public class NoteTable {
                        query.bindValue(":attributeSource", n.getAttributes().getSource());\r
                        query.bindValue(":attributeSourceUrl", n.getAttributes().getSourceURL());\r
                        query.bindValue(":attributeSourceApplication", n.getAttributes().getSourceApplication());\r
+               } else {\r
+                       created = new StringBuilder(simple.format(n.getCreated()));     \r
+                       query.bindValue(":attributeSubjectDate", created.toString());\r
+                       query.bindValue(":attributeLatitude", 0.0);\r
+                       query.bindValue(":attributeLongitude", 0.0);\r
+                       query.bindValue(":attributeAltitude", 0.0);\r
+                       query.bindValue(":attributeAuthor", "");\r
+                       query.bindValue(":attributeSource", "");\r
+                       query.bindValue(":attributeSourceUrl", "");\r
+                       query.bindValue(":attributeSourceApplication", "");\r
                }\r
                query.bindValue(":indexNeeded", true);\r
                query.bindValue(":isExpunged", false);\r
@@ -175,6 +186,7 @@ public class NoteTable {
                                        +"created, updated, deleted, active, notebookGuid, "\r
                                        +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
                                        +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "\r
+                                       +"attributeContentClass, "\r
                                        +"content, contentHash, contentLength"\r
                                        +" from Note where guid=:guid and isExpunged=false")) {\r
                                                logger.log(logger.EXTREME, "Note SQL select prepare with content has failed.");\r
@@ -188,7 +200,8 @@ public class NoteTable {
                                        +"guid, updateSequenceNumber, title, "\r
                                        +"created, updated, deleted, active, notebookGuid, "\r
                                        +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
-                                       +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication "\r
+                                       +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "\r
+                                       +"attributeContentClass"\r
                                        +" from Note where guid=:guid and isExpunged=false")) {\r
                                                logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");\r
                                                logger.log(logger.MEDIUM, getQueryWithoutContent.lastError());\r
@@ -202,7 +215,8 @@ public class NoteTable {
                                +"guid, updateSequenceNumber, title, "\r
                                +"created, updated, deleted, active, notebookGuid, "\r
                                +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
-                               +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication "\r
+                               +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "\r
+                               +"attributeContentClass "\r
                                +" from Note where isExpunged = false")) {\r
                                logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");\r
                                        logger.log(logger.MEDIUM, getQueryWithoutContent.lastError());\r
@@ -251,6 +265,7 @@ public class NoteTable {
                }\r
                Note n = mapNoteFromQuery(query, loadContent, loadResources, loadRecognition, loadBinary, loadTags);\r
                n.setContent(fixCarriageReturn(n.getContent()));\r
+               n.getAttributes().setContentClassIsSet(false);\r
                return n;\r
        }\r
        // Get a note by Guid\r
@@ -291,23 +306,29 @@ public class NoteTable {
                na.setSource(query.valueString(13));\r
                na.setSourceURL(query.valueString(14));\r
                na.setSourceApplication(query.valueString(15));\r
+               na.setContentClass(query.valueString(16));\r
                \r
                if (loadTags) {\r
-                       n.setTagGuids(noteTagsTable.getNoteTags(n.getGuid()));\r
+                       List<String> tagGuids = noteTagsTable.getNoteTags(n.getGuid());\r
                        List<String> tagNames = new ArrayList<String>();\r
                        TagTable tagTable = db.getTagTable();\r
-                       for (int i=0; i<n.getTagGuids().size(); i++) {\r
-                               String currentGuid = n.getTagGuids().get(i);\r
+                       for (int i=0; i<tagGuids.size(); i++) {\r
+                               String currentGuid = tagGuids.get(i);\r
                                Tag tag = tagTable.getTag(currentGuid);\r
-                               tagNames.add(tag.getName());\r
+                               if (tag.getName() != null)\r
+                                       tagNames.add(tag.getName());\r
+                               else\r
+                                       tagNames.add("");\r
                        }\r
+\r
                        n.setTagNames(tagNames);\r
+                       n.setTagGuids(tagGuids);                \r
                }\r
                \r
                if (loadContent) {\r
                        QTextCodec codec = QTextCodec.codecForLocale();\r
                        codec = QTextCodec.codecForName("UTF-8");\r
-               String unicode =  codec.fromUnicode(query.valueString(16)).toString();\r
+               String unicode =  codec.fromUnicode(query.valueString(17)).toString();\r
 \r
                // This is a hack.  Basically I need to convert HTML Entities to "normal" text, but if I\r
                // convert the &lt; character to < it will mess up the XML parsing.  So, to get around this\r
@@ -315,7 +336,7 @@ public class NoteTable {
                // I'm done I convert it back.\r
                StringBuffer buffer = new StringBuffer(unicode);\r
                if (Global.enableHTMLEntitiesFix && unicode.indexOf("&#") > 0) {\r
-                       unicode = query.valueString(16);\r
+                       unicode = query.valueString(17);\r
                        //System.out.println(unicode);\r
                        //unicode = unicode.replace("&lt;", "&_lt;");\r
                        //unicode = codec.fromUnicode(StringEscapeUtils.unescapeHtml(unicode)).toString();\r
@@ -342,10 +363,10 @@ public class NoteTable {
                n.setContent(unicode);\r
 //                     n.setContent(query.valueString(16).toString());\r
                        \r
-                       String contentHash = query.valueString(17);\r
+                       String contentHash = query.valueString(18);\r
                        if (contentHash != null)\r
                                n.setContentHash(contentHash.getBytes());\r
-                       n.setContentLength(new Integer(query.valueString(18)));\r
+                       n.setContentLength(new Integer(query.valueString(19)));\r
                }\r
                if (loadResources)\r
                        n.setResources(noteResourceTable.getNoteResources(n.getGuid(), loadBinary));\r
@@ -561,9 +582,10 @@ public class NoteTable {
        }\r
        public void restoreNote(String guid) {\r
         NSqlQuery query = new NSqlQuery(db.getConnection());\r
-               query.prepare("Update Note set deleted='1969-12-31 19.00.00', active=true, isDirty=true where guid=:guid");\r
+               query.prepare("Update Note set deleted=:reset, active=true, isDirty=true where guid=:guid");\r
 //             query.prepare("Update Note set deleted=0, active=true, isDirty=true where guid=:guid");\r
                query.bindValue(":guid", guid);\r
+               query.bindValue(":reset", "1969-12-31 19:00:00");\r
                if (!query.exec()) {\r
                        logger.log(logger.MEDIUM, "Note restore failed.");\r
                        logger.log(logger.MEDIUM, query.lastError());\r
@@ -1139,6 +1161,28 @@ public class NoteTable {
                return values;\r
        }\r
        \r
+       // Find a note based upon its title.\r
+       public List<Pair<String,String>> findNotesByTitle(String text) {\r
+               List<Pair<String,String>> results = new ArrayList<Pair<String,String>>();\r
+               boolean check;                  \r
+        NSqlQuery query = new NSqlQuery(db.getConnection());\r
+                                       \r
+               check = query.prepare("Select guid,title from Note where lower(title) like :title");\r
+               if (!check) \r
+                       logger.log(logger.EXTREME, "Note SQL prepare for search by title has failed: " +query.lastError().toString());\r
+               \r
+               query.bindValue(":title", "%"+text.toLowerCase()+"%");\r
+               query.exec();\r
+               // Get a list of the notes\r
+               while (query.next()) {\r
+                       Pair<String,String> p = new Pair<String,String>();\r
+                       p.setFirst(query.valueString(0));\r
+                       p.setSecond(query.valueString(1));                      \r
+                       results.add(p); \r
+               }       \r
+               return results;\r
+       }\r
+\r
        \r
        \r
        //********************************************************************************\r