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 264ae7a..54da38b 100644 (file)
@@ -151,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
@@ -176,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
@@ -189,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
@@ -203,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
@@ -223,6 +236,8 @@ public class NoteTable {
        }\r
        // Get a note by Guid\r
        public Note getNote(String noteGuid, boolean loadContent, boolean loadResources, boolean loadRecognition, boolean loadBinary, boolean loadTags) {\r
+\r
+//             extractMetadata("otherKey:{values};baumgarte:{titleColor=fff;pinned=true;};finalKey:{values1);");\r
                if (noteGuid == null)\r
                        return null;\r
                if (noteGuid.trim().equals(""))\r
@@ -250,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
@@ -290,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
@@ -314,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
@@ -341,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
@@ -560,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
@@ -747,13 +770,14 @@ public class NoteTable {
                }\r
        }\r
        // Update a note\r
-       public void updateNote(Note n, boolean isNew) {\r
-               int titleColor = getNoteTitleColor(n.getGuid());\r
+       public void updateNote(Note n) {\r
+               NoteMetadata meta = getNoteMetaInformation(n.getGuid());\r
                String originalGuid = findAlternateGuid(n.getGuid());\r
                expungeNote(n.getGuid(), true, false);\r
                addNote(n, false);\r
-               if (titleColor != -1)\r
-                       setNoteTitleColor(n.getGuid(), titleColor);\r
+               if (n!=null) {\r
+                       updateNoteMetadata(meta);\r
+               }\r
                if (originalGuid != null) {\r
                        updateNoteGuid(n.getGuid(), originalGuid);\r
                        updateNoteGuid(originalGuid, n.getGuid());\r
@@ -787,12 +811,29 @@ public class NoteTable {
                boolean retVal = query.next();\r
                return retVal;\r
        }\r
-       // This is a convience method to check if a tag exists & update/create based upon it\r
-       public void syncNote(Note tag, boolean isDirty) {\r
-               if (exists(tag.getGuid()))\r
-                       updateNote(tag, isDirty);\r
+       // This is a convience method to check if a note exists & update/create based upon it\r
+       public void syncNote(Note note) {\r
+               // If we got the note from Evernote we use its \r
+               // metadata instead of the local copy.\r
+               NoteMetadata meta = null;\r
+               if (note.getAttributes() != null && note.getAttributes().getSourceApplication() != null) {\r
+                       meta = extractMetadata(note.getAttributes().getSourceApplication());\r
+               } else \r
+                       meta = getNoteMetaInformation(note.getGuid());\r
+               \r
+               // Now, if the note exists we simply update it.  Otherwise we\r
+               // add a new note.\r
+               if (exists(note.getGuid())) {\r
+                       updateNote(note);\r
+               }\r
                else\r
-                       addNote(tag, isDirty);\r
+                       addNote(note, false);\r
+               \r
+               // If we have metadata, we write it out.\r
+               if (meta != null) {\r
+                       meta.setGuid(note.getGuid());\r
+                       updateNoteMetadata(meta);\r
+               }\r
        }\r
        // Get a list of notes that need to be updated\r
        public List <Note> getDirty() {\r
@@ -1120,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
@@ -1189,50 +1252,24 @@ public class NoteTable {
                }       \r
                return guids;   \r
        }\r
-       \r
-       \r
-       //**********************************************************************************\r
-       //* Title color functions\r
-       //**********************************************************************************\r
-       // Get the title color of all notes\r
-/*     public List<Pair<String, Integer>> getNoteTitleColors() {\r
-               List<Pair<String,Integer>> returnValue = new ArrayList<Pair<String,Integer>>();\r
-        NSqlQuery query = new NSqlQuery(db.getConnection());\r
-               \r
-               if (!query.exec("Select guid,titleColor from Note where titleColor != -1"))\r
-                       logger.log(logger.EXTREME, "Note SQL retrieve has failed on getUnindexed().");\r
-\r
-               String guid;\r
-               Integer color;\r
-               \r
-               // Get a list of the notes\r
-               while (query.next()) {\r
-                       Pair<String, Integer> pair = new Pair<String,Integer>();\r
-                       guid = query.valueString(0);\r
-                       color = query.valueInteger(1);\r
-                       pair.setFirst(guid);\r
-                       pair.setSecond(color);\r
-                       returnValue.add(pair); \r
-               }       \r
 \r
-               return returnValue;\r
-       }\r
-       */\r
        \r
        // Get note meta information\r
        public void updateNoteMetadata(NoteMetadata meta) {\r
         NSqlQuery query = new NSqlQuery(db.getConnection());\r
-               if (!query.prepare("Update Note set titleColor=:color, pinned=:pinned where guid=:guid"))\r
+               if (!query.prepare("Update Note set titleColor=:color, pinned=:pinned, attributeSourceApplication=:metaString where guid=:guid"))\r
                        logger.log(logger.EXTREME, "Note SQL prepare has failed on updateNoteMetadata.");\r
                query.bindValue(":color", meta.getColor());\r
                query.bindValue(":pinned", meta.isPinned());\r
                query.bindValue(":guid", meta.getGuid());\r
-               query.exec();\r
+               query.bindValue(":metaString", buildMetadataString(meta));\r
+               if (!query.exec()) \r
+                       logger.log(logger.EXTREME, "Note SQL exec has failed on updateNoteMetadata.");\r
                return;\r
        }\r
        \r
-       // Get note meta information\r
-       public HashMap<String, NoteMetadata> getNoteMetaInformation() {\r
+       // Get all note meta information\r
+       public HashMap<String, NoteMetadata> getNotesMetaInformation() {\r
                HashMap<String, NoteMetadata> returnValue = new HashMap<String, NoteMetadata>();\r
         NSqlQuery query = new NSqlQuery(db.getConnection());\r
                \r
@@ -1253,41 +1290,30 @@ public class NoteTable {
 \r
                return returnValue;\r
        }\r
-       // Set a title color\r
-       public void  setNoteTitleColor(String guid, int color) {\r
-               NSqlQuery query = new NSqlQuery(db.getConnection());\r
-               \r
-               query.prepare("Update note set titlecolor=:color where guid=:guid");\r
-               query.bindValue(":guid", guid);\r
-               query.bindValue(":color", color);\r
-               if (!query.exec())\r
-                       logger.log(logger.EXTREME, "Error updating title color.");\r
-       }\r
-       // Get in individual note's title color\r
-       // Get the title color of all notes\r
-       public Integer getNoteTitleColor(String guid) {\r
-               List<Pair<String,Integer>> returnValue = new ArrayList<Pair<String,Integer>>();\r
+       // Get note meta information\r
+       public NoteMetadata getNoteMetaInformation(String guid) {\r
         NSqlQuery query = new NSqlQuery(db.getConnection());\r
                \r
-        query.prepare("Select titleColor from Note where titleColor != -1 and guid=:guid");\r
-        query.bindValue(":guid", guid);\r
-               if (!query.exec())\r
-                       logger.log(logger.EXTREME, "Note SQL retrieve has failed on getNoteTitleColor(guid).");\r
+               if (!query.prepare("Select guid,titleColor, isDirty, pinned from Note where guid=:guid")) {\r
+                       logger.log(logger.EXTREME, "Note SQL retrieve has failed on getNoteMetaInformation.");\r
+                       return null;\r
+               }\r
+               query.bindValue(":guid", guid);\r
+               query.exec();\r
 \r
-               Integer color = -1;\r
-               \r
                // Get a list of the notes\r
                while (query.next()) {\r
-                       Pair<String, Integer> pair = new Pair<String,Integer>();\r
-                       guid = query.valueString(0);\r
-                       color = query.valueInteger(1);\r
-                       pair.setFirst(guid);\r
-                       pair.setSecond(color);\r
-                       returnValue.add(pair); \r
+                       NoteMetadata note = new NoteMetadata();\r
+                       note.setGuid(query.valueString(0));\r
+                       note.setColor(query.valueInteger(1));\r
+                       note.setDirty(query.valueBoolean(2, false));\r
+                       int pinned = query.valueInteger(3);\r
+                       if (pinned > 0) \r
+                               note.setPinned(true);\r
+                       return note;\r
                }       \r
 \r
-               \r
-               return color;\r
+               return null;\r
        }\r
        \r
        \r
@@ -1494,6 +1520,106 @@ public class NoteTable {
                }\r
        }\r
 \r
+       // Extract metadata from a note's Note.attributes.sourceApplication\r
+       private NoteMetadata extractMetadata(String sourceApplication) {\r
+               String consumerKey = "baumgarte:{";\r
+               int startPos = sourceApplication.indexOf(consumerKey);\r
+               if (startPos < 0 )\r
+                               return null;\r
+               \r
+               NoteMetadata meta = new NoteMetadata();\r
+               startPos = startPos+consumerKey.length();\r
+               \r
+//             String startString = sourceApplication.substring(0,startPos);\r
+               String metaString = sourceApplication.substring(startPos);\r
+//             String endString = metaString.substring(metaString.indexOf("};"));\r
+               int endPos = metaString.indexOf("};");\r
+               if (endPos > 0)\r
+                       metaString = metaString.substring(0,endPos);\r
+               \r
+               String value = parseMetaString(metaString, "titleColor");\r
+               if (value != null)\r
+                       meta.setColor(Integer.parseInt(value));\r
+               \r
+               value = parseMetaString(metaString, "pinned");\r
+               if (value != null && value.equals(true))\r
+                       meta.setPinned(true);\r
+                               \r
+               return meta;\r
+       }\r
+       \r
+       // Given a metadata string from attribute.sourceApplication, we\r
+       // extract the information for a given key.\r
+       private String parseMetaString(String metaString, String key) {\r
+               int startPos = metaString.indexOf(key);\r
+               if (startPos < 0)\r
+                       return null;\r
+               \r
+               String value = metaString.substring(startPos+key.length()+1);\r
+               int endPos = value.indexOf(";");\r
+               if (endPos > 0)\r
+                       value = value.substring(0,endPos);\r
+               \r
+               return value;\r
+       }\r
+       \r
+       // Given a set of metadata, we build a string that can be inserted\r
+       // into the attribute.sourceApplication string.\r
+       private String buildMetadataString(NoteMetadata meta) {\r
+               StringBuffer value = new StringBuffer(removeExistingMetaString(meta.getGuid()));\r
+               StringBuffer metaString = new StringBuffer();\r
+               \r
+               if (meta.isPinned()) {\r
+                       metaString.append("pinned=true;");\r
+               }\r
+               if (meta.getColor() != -1) {\r
+                       metaString.append("titleColor=" +new Integer(meta.getColor()).toString()+";");\r
+               }\r
+               if (metaString.length()>0) {\r
+                       \r
+                       // Adda any missing ";" or " " at the end of the existing \r
+                       // string.\r
+                       if (value.length()>1 && (!value.toString().trim().endsWith(";") || !value.toString().trim().endsWith(";")))   \r
+                               value.append("; ");\r
+                       \r
+                       value.append("baumgarte:{");\r
+                       value.append(metaString);\r
+                       value.append("};");\r
+                       return value.toString();\r
+               }\r
+               return null;\r
+       }\r
+\r
+       // This will remove the existing metadata string from the attribute.sourceApplication string.\r
+       private String removeExistingMetaString(String guid) {\r
+        NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.prepare("Select attributeSourceApplication from Note where guid=:guid")) {\r
+                       logger.log(logger.EXTREME, "Note SQL retrieve has failed in removeExistingMetaString.");\r
+                       return null;\r
+               }\r
+               query.bindValue(":guid", guid);\r
+               query.exec();\r
+\r
+               // Get the application source string\r
+               String sourceApplication = null;\r
+               while (query.next()) {\r
+                       sourceApplication = query.valueString(0);\r
+               }\r
+               if (sourceApplication == null) \r
+                       return "";\r
+               \r
+               String consumerKey = "baumgarte:{";\r
+               int startPos = sourceApplication.indexOf(consumerKey);\r
+               if (startPos < 0 )\r
+                               return sourceApplication;\r
+               String startString = sourceApplication.substring(0,startPos);\r
+               String metaString = sourceApplication.substring(startPos);\r
+               String endString = metaString.substring(metaString.indexOf("};")+2);\r
+\r
+               return startString+endString;\r
+       }\r
+\r
 }      \r
 \r
 \r