OSDN Git Service

Added "stack:" to supported search terms.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / REnSearch.java
index b45a3cb..7668f25 100644 (file)
@@ -27,6 +27,8 @@ import java.util.GregorianCalendar;
 import java.util.List;\r
 import java.util.regex.Pattern;\r
 \r
+import org.apache.commons.lang.StringEscapeUtils;\r
+\r
 import com.evernote.edam.type.Note;\r
 import com.evernote.edam.type.Notebook;\r
 import com.evernote.edam.type.Tag;\r
@@ -37,6 +39,7 @@ import cx.fbn.nevernote.utilities.ApplicationLogger;
 public class REnSearch {\r
        \r
        private final List<String>      searchWords;\r
+       private final List<String>  searchPhrases;\r
        private final List<String>      notebooks;\r
        private final List<String>      tags;\r
        private final List<String>      intitle;\r
@@ -52,21 +55,21 @@ public class REnSearch {
        private final List<String>      sourceApplication;\r
        private final List<String>      recoType;\r
        private final List<String>      todo;\r
+       private final List<String>  stack;\r
        private final List<Tag>         tagIndex;\r
        private final ApplicationLogger logger;\r
 //     private final DatabaseConnection db;\r
        private boolean any;\r
-       private int     minimumWordLength = 3;\r
        private int minimumRecognitionWeight = 80;\r
        private final DatabaseConnection conn;\r
        \r
-       public REnSearch(DatabaseConnection c, ApplicationLogger l, String s, List<Tag> t, int m, int r) {\r
+       public REnSearch(DatabaseConnection c, ApplicationLogger l, String s, List<Tag> t, int r) {\r
                logger = l;\r
                conn = c;\r
                tagIndex = t;\r
-               minimumWordLength = m;\r
                minimumRecognitionWeight = r;\r
                searchWords = new ArrayList<String>();\r
+               searchPhrases = new ArrayList<String>();\r
                notebooks = new ArrayList<String>();\r
                tags = new ArrayList<String>();\r
                intitle = new ArrayList<String>();\r
@@ -83,6 +86,7 @@ public class REnSearch {
                recoType = new ArrayList<String>();\r
                todo = new ArrayList<String>();\r
                any = false;\r
+               stack = new ArrayList<String>();\r
                \r
                if (s == null) \r
                        return;\r
@@ -108,7 +112,7 @@ public class REnSearch {
        public List<String> getCreated() { return created; }\r
        public List<String> getUpdated() { return updated; }\r
        public List<String> getSubjectDate() { return subjectDate; }\r
-       \r
+       public List<String> getStack() { return stack; }\r
 \r
        // match tag names\r
        private boolean matchTagsAll(List<String> tagNames) {\r
@@ -128,13 +132,16 @@ public class REnSearch {
                        if (tagNames.size() == 0 && negative)\r
                                return true;\r
                        \r
-                       for (int i=0; i<tagNames.size(); i++) {         \r
+                       boolean good = false;\r
+                       for (int i=0; i<tagNames.size() && !good; i++) {                \r
                                boolean matches = Pattern.matches(filterName.toLowerCase(),tagNames.get(i).toLowerCase());\r
-                               if (matches && negative)\r
-                                       return false;\r
-                               if (!matches && !negative)\r
-                                       return false;\r
+                               if (matches && !negative)\r
+                                       good = true;\r
+                               if (!matches && negative)\r
+                                       good = true;\r
                        }\r
+                       if (!good)\r
+                               return false;\r
                }\r
                return true;\r
        }\r
@@ -191,6 +198,28 @@ public class REnSearch {
                        return matchListAll(getNotebooks(), name);\r
        }\r
        // Match notebooks in search terms against notes\r
+       private boolean matchNotebookStack(String guid) {\r
+               if (getStack().size() == 0)\r
+                       return true;\r
+               NotebookTable bookTable = new NotebookTable(logger, conn);\r
+               List<Notebook> books = bookTable.getAll();\r
+\r
+               String name = new String("");\r
+               for (int i=0; i<books.size(); i++) {\r
+                       if (guid.equalsIgnoreCase(books.get(i).getGuid())) {\r
+                               name = books.get(i).getStack();\r
+                               i=books.size();\r
+                       }\r
+               }\r
+               if (name == null)\r
+                       name = "";\r
+               if (any)\r
+                       return matchListAny(getStack(), name);\r
+               else\r
+                       return matchListAll(getStack(), name);\r
+       }\r
+\r
+       // Match notebooks in search terms against notes\r
        private boolean matchListAny(List<String> list, String title) {\r
                if (list.size() == 0)\r
                        return true;\r
@@ -214,11 +243,34 @@ public class REnSearch {
        }\r
        // Match notebooks in search terms against notes\r
        private boolean matchContentAny(Note n) {\r
-               if (todo.size() == 0 && resource.size() == 0)\r
+               if (todo.size() == 0 && resource.size() == 0 && searchPhrases.size() == 0)\r
                        return true;\r
-               \r
-               \r
+\r
+               // pull back the record\r
                n = conn.getNoteTable().getNote(n.getGuid(), true, true, false, false, false);\r
+\r
+               // Check for search phrases\r
+               String text = StringEscapeUtils.unescapeHtml(n.getContent().replaceAll("\\<.*?\\>", "")).toLowerCase();\r
+               boolean negative = false;\r
+               for (int i=0; i<searchPhrases.size(); i++) {\r
+                       String phrase = searchPhrases.get(i);\r
+                       if (phrase.startsWith("-")) {\r
+                               negative = true;\r
+                               phrase = phrase.substring(1);\r
+                       } else\r
+                               negative = false;\r
+                       phrase = phrase.substring(1);\r
+                       phrase = phrase.substring(0,phrase.length()-1);\r
+                       if (text.indexOf(phrase)>=0) {\r
+                               if (negative)\r
+                                       return false;\r
+                               else\r
+                                       return true;\r
+                       }\r
+                       if (text.indexOf(phrase)<0 && negative)\r
+                               return true;\r
+               }\r
+               \r
                for (int i=0; i<todo.size(); i++) {\r
                        String value = todo.get(i);\r
                        value = value.replace("\"", "");\r
@@ -255,7 +307,6 @@ public class REnSearch {
                for (int i=0; i<resource.size(); i++) {\r
                        String resourceString = resource.get(i);\r
                        resourceString = resourceString.replace("\"", "");\r
-                       boolean negative = false;\r
                        if (resourceString.startsWith("-"))\r
                                negative = true;\r
                        resourceString = resourceString.substring(resourceString.indexOf(":")+1);\r
@@ -331,7 +382,6 @@ public class REnSearch {
        // subject date\r
 \r
        private void parseTerms(List<String> words) {\r
-               int minLen = minimumWordLength;\r
                \r
                for (int i=0; i<words.size(); i++) {\r
                        String word = words.get(i);\r
@@ -341,8 +391,14 @@ public class REnSearch {
                                word = word.substring(4).trim();\r
                                pos = word.indexOf(":");\r
                        }\r
-                       if (pos < 0 && (word.length() >= minLen || word.indexOf('*')>=0)) \r
-                               getWords().add(word);\r
+                       boolean searchPhrase = false;\r
+                       if (pos < 0 && word.indexOf(" ") > 0) {\r
+                               searchPhrase=true;\r
+                               searchPhrases.add(word.toLowerCase());\r
+                       }\r
+                       if (!searchPhrase && pos < 0) \r
+                               getWords().add(word); \r
+//                             getWords().add("*"+word+"*");           //// WILDCARD\r
                        if (word.startsWith("intitle:")) \r
                                intitle.add("*"+word+"*");\r
                        if (word.startsWith("-intitle:")) \r
@@ -379,6 +435,10 @@ public class REnSearch {
                                todo.add(word);\r
                        if (word.startsWith("-todo:")) \r
                                todo.add(word);\r
+                       if (word.startsWith("stack:"))\r
+                               stack.add(word);\r
+                       if (word.startsWith("-stack:"))\r
+                               stack.add(word);\r
 \r
                        if (word.startsWith("latitude:")) \r
                                latitude.add(word);\r
@@ -405,7 +465,7 @@ public class REnSearch {
                                created.add(word);\r
                        if (word.startsWith("-subjectdate:")) \r
                                created.add(word);\r
-               \r
+\r
                }\r
        }\r
        // Match notebooks in search terms against notes\r
@@ -433,19 +493,44 @@ public class REnSearch {
        }\r
        // Match notebooks in search terms against notes\r
        private boolean matchContentAll(Note n) {\r
-               if (todo.size() == 0 && resource.size() == 0)\r
+               if (todo.size() == 0 && resource.size() == 0 && searchPhrases.size() == 0)\r
                        return true;\r
                \r
                boolean returnTodo = false;\r
                boolean returnResource = false;\r
+               boolean returnPhrase = false;\r
                \r
                if (todo.size() == 0)\r
                        returnTodo = true;\r
                if (resource.size() == 0)\r
                        returnResource = true;\r
+               if (searchPhrases.size() == 0)\r
+                       returnPhrase = true;\r
                \r
                \r
                n = conn.getNoteTable().getNote(n.getGuid(), true, true, false, false, false);\r
+               \r
+               // Check for search phrases\r
+               String text = StringEscapeUtils.unescapeHtml(n.getContent().replaceAll("\\<.*?\\>", "")).toLowerCase();\r
+               boolean negative = false;\r
+               for (int i=0; i<searchPhrases.size(); i++) {\r
+                       String phrase = searchPhrases.get(i);\r
+                       if (phrase.startsWith("-")) {\r
+                               negative = true;\r
+                               phrase = phrase.substring(1);\r
+                       } else\r
+                               negative = false;\r
+                       phrase = phrase.substring(1);\r
+                       phrase = phrase.substring(0,phrase.length()-1);\r
+                       if (text.indexOf(phrase)>=0) {\r
+                               if (!negative)\r
+                                       returnPhrase = true;\r
+                       }\r
+                       if (text.indexOf(phrase)<0 && negative)\r
+                               returnPhrase = true;\r
+               }\r
+\r
+               \r
                for (int i=0; i<todo.size(); i++) {\r
                        String value = todo.get(i);\r
                        value = value.replace("\"", "");\r
@@ -486,7 +571,7 @@ public class REnSearch {
                for (int i=0; i<resource.size(); i++) {\r
                        String resourceString = resource.get(i);\r
                        resourceString = resourceString.replace("\"", "");\r
-                       boolean negative = false;\r
+                       negative = false;\r
                        if (resourceString.startsWith("-"))\r
                                negative = true;\r
                        resourceString = resourceString.substring(resourceString.indexOf(":")+1);\r
@@ -500,8 +585,7 @@ public class REnSearch {
                        }\r
                }\r
                \r
-               \r
-               return returnResource && returnTodo;\r
+               return returnResource && returnTodo && returnPhrase;\r
        }\r
        \r
        private boolean stringMatch(String content, String text, boolean negative) {\r
@@ -675,6 +759,8 @@ public class REnSearch {
                                        good = false;\r
                                if (good && !matchNotebook(n.getNotebookGuid()))\r
                                        good = false;\r
+                               if (good && !matchNotebookStack(n.getNotebookGuid()))\r
+                                       good = false;\r
                                if (good && !matchListAny(getIntitle(), n.getTitle()))\r
                                        good = false;\r
                                if (good && !matchListAny(getAuthor(), n.getAttributes().getAuthor()))\r
@@ -696,6 +782,8 @@ public class REnSearch {
                                        good = false;\r
                                if (good && !matchNotebook(n.getNotebookGuid()))\r
                                        good = false;\r
+                               if (good && !matchNotebookStack(n.getNotebookGuid()))\r
+                                       good = false;\r
                                if (good && !matchListAll(getIntitle(), n.getTitle()))\r
                                        good = false;\r
                                if (good && !matchListAll(getAuthor(), n.getAttributes().getAuthor()))\r