2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
21 package cx.fbn.nevernote.sql;
\r
23 import java.text.SimpleDateFormat;
\r
24 import java.util.ArrayList;
\r
25 import java.util.Calendar;
\r
26 import java.util.GregorianCalendar;
\r
27 import java.util.List;
\r
28 import java.util.regex.Pattern;
\r
30 import org.apache.commons.lang.StringEscapeUtils;
\r
32 import com.evernote.edam.type.Note;
\r
33 import com.evernote.edam.type.Notebook;
\r
34 import com.evernote.edam.type.Tag;
\r
36 import cx.fbn.nevernote.sql.driver.NSqlQuery;
\r
37 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
39 public class REnSearch {
\r
41 private final List<String> searchWords;
\r
42 private final List<String> searchPhrases;
\r
43 private final List<String> notebooks;
\r
44 private final List<String> tags;
\r
45 private final List<String> intitle;
\r
46 private final List<String> created;
\r
47 private final List<String> updated;
\r
48 private final List<String> resource;
\r
49 private final List<String> subjectDate;
\r
50 private final List<String> longitude;
\r
51 private final List<String> latitude;
\r
52 private final List<String> altitude;
\r
53 private final List<String> author;
\r
54 private final List<String> source;
\r
55 private final List<String> sourceApplication;
\r
56 private final List<String> recoType;
\r
57 private final List<String> todo;
\r
58 private final List<String> stack;
\r
59 private final List<Tag> tagIndex;
\r
60 private final ApplicationLogger logger;
\r
61 // private final DatabaseConnection db;
\r
62 private boolean any;
\r
63 private int minimumRecognitionWeight = 80;
\r
64 private final DatabaseConnection conn;
\r
66 public REnSearch(DatabaseConnection c, ApplicationLogger l, String s, List<Tag> t, int r) {
\r
70 minimumRecognitionWeight = r;
\r
71 searchWords = new ArrayList<String>();
\r
72 searchPhrases = new ArrayList<String>();
\r
73 notebooks = new ArrayList<String>();
\r
74 tags = new ArrayList<String>();
\r
75 intitle = new ArrayList<String>();
\r
76 created = new ArrayList<String>();
\r
77 updated = new ArrayList<String>();
\r
78 resource = new ArrayList<String>();
\r
79 subjectDate = new ArrayList<String>();
\r
80 longitude = new ArrayList<String>();
\r
81 latitude = new ArrayList<String>();
\r
82 altitude = new ArrayList<String>();
\r
83 author = new ArrayList<String>();
\r
84 source = new ArrayList<String>();
\r
85 sourceApplication = new ArrayList<String>();
\r
86 recoType = new ArrayList<String>();
\r
87 todo = new ArrayList<String>();
\r
89 stack = new ArrayList<String>();
\r
93 if (s.trim().equals(""))
\r
99 public List<String> getWords() { return searchWords; }
\r
100 public List<String> getNotebooks() { return notebooks; }
\r
101 public List<String> getIntitle() { return intitle; }
\r
102 public List<String> getTags() { return tags; }
\r
103 public List<String> getResource() { return resource; }
\r
104 public List<String> getAuthor() { return author; }
\r
105 public List<String> getSource() { return source; }
\r
106 public List<String> getSourceApplication() { return sourceApplication; }
\r
107 public List<String> getRecoType() { return recoType; }
\r
108 public List<String> getToDo() { return todo; }
\r
109 public List<String> getLongitude() { return longitude; }
\r
110 public List<String> getLatitude() { return latitude; }
\r
111 public List<String> getAltitude() { return altitude; }
\r
112 public List<String> getCreated() { return created; }
\r
113 public List<String> getUpdated() { return updated; }
\r
114 public List<String> getSubjectDate() { return subjectDate; }
\r
115 public List<String> getStack() { return stack; }
\r
118 private boolean matchTagsAll(List<String> tagNames) {
\r
119 List<String> list = getTags();
\r
121 for (int j=0; j<list.size(); j++) {
\r
122 boolean negative = false;
\r
124 if (list.get(j).startsWith("-"))
\r
126 int pos = list.get(j).indexOf(":");
\r
127 String filterName = cleanupWord(list.get(j).substring(pos+1));
\r
128 filterName = filterName.replace("*", ".*"); // setup for regular expression pattern match
\r
130 if (tagNames.size() == 0 && !negative)
\r
132 if (tagNames.size() == 0 && negative)
\r
135 boolean good = false;
\r
136 for (int i=0; i<tagNames.size() && !good; i++) {
\r
137 boolean matches = Pattern.matches(filterName.toLowerCase(),tagNames.get(i).toLowerCase());
\r
138 if (matches && !negative)
\r
140 if (!matches && negative)
\r
150 private boolean matchTagsAny(List<String> tagNames) {
\r
151 List<String> list = getTags();
\r
152 if (list.size() == 0)
\r
155 boolean negative = false;
\r
156 boolean found = false;
\r
158 for (int j=0; j<list.size(); j++) {
\r
160 if (list.get(j).startsWith("-"))
\r
162 int pos = list.get(j).indexOf(":");
\r
163 String filterName = cleanupWord(list.get(j).substring(pos+1));
\r
164 filterName = filterName.replace("*", ".*"); // setup for regular expression pattern match
\r
166 if (tagNames.size() == 0)
\r
169 for (int i=0; i<tagNames.size(); i++) {
\r
170 boolean matches = Pattern.matches(filterName.toLowerCase(),tagNames.get(i).toLowerCase());
\r
181 // Match notebooks in search terms against notes
\r
182 private boolean matchNotebook(String guid) {
\r
183 if (getNotebooks().size() == 0)
\r
185 NotebookTable bookTable = new NotebookTable(logger, conn);
\r
186 List<Notebook> books = bookTable.getAll();
\r
188 String name = new String("");
\r
189 for (int i=0; i<books.size(); i++) {
\r
190 if (guid.equalsIgnoreCase(books.get(i).getGuid())) {
\r
191 name = books.get(i).getName();
\r
196 return matchListAny(getNotebooks(), name);
\r
198 return matchListAll(getNotebooks(), name);
\r
200 // Match notebooks in search terms against notes
\r
201 private boolean matchNotebookStack(String guid) {
\r
202 if (getStack().size() == 0)
\r
204 NotebookTable bookTable = new NotebookTable(logger, conn);
\r
205 List<Notebook> books = bookTable.getAll();
\r
207 String name = new String("");
\r
208 for (int i=0; i<books.size(); i++) {
\r
209 if (guid.equalsIgnoreCase(books.get(i).getGuid())) {
\r
210 name = books.get(i).getStack();
\r
217 return matchListAny(getStack(), name);
\r
219 return matchListAll(getStack(), name);
\r
222 // Match notebooks in search terms against notes
\r
223 private boolean matchListAny(List<String> list, String title) {
\r
224 if (list.size() == 0)
\r
226 boolean negative = false;
\r
227 boolean found = false;
\r
228 for (int i=0; i<list.size(); i++) {
\r
229 int pos = list.get(i).indexOf(":");
\r
231 if (list.get(i).startsWith("-"))
\r
233 String filterName = cleanupWord(list.get(i).substring(pos+1));
\r
234 filterName = filterName.replace("*", ".*"); // setup for regular expression pattern match
\r
235 boolean matches = Pattern.matches(filterName.toLowerCase(),title.toLowerCase());
\r
244 // Match notebooks in search terms against notes
\r
245 private boolean matchContentAny(Note n) {
\r
246 if (todo.size() == 0 && resource.size() == 0 && searchPhrases.size() == 0)
\r
249 // pull back the record
\r
250 n = conn.getNoteTable().getNote(n.getGuid(), true, true, false, false, false);
\r
252 // Check for search phrases
\r
253 String text = StringEscapeUtils.unescapeHtml(n.getContent().replaceAll("\\<.*?\\>", "")).toLowerCase();
\r
254 boolean negative = false;
\r
255 for (int i=0; i<searchPhrases.size(); i++) {
\r
256 String phrase = searchPhrases.get(i);
\r
257 if (phrase.startsWith("-")) {
\r
259 phrase = phrase.substring(1);
\r
262 phrase = phrase.substring(1);
\r
263 phrase = phrase.substring(0,phrase.length()-1);
\r
264 if (text.indexOf(phrase)>=0) {
\r
270 if (text.indexOf(phrase)<0 && negative)
\r
274 for (int i=0; i<todo.size(); i++) {
\r
275 String value = todo.get(i);
\r
276 value = value.replace("\"", "");
\r
277 boolean desiredState;
\r
278 if (!value.endsWith(":false") && !value.endsWith(":true") && !value.endsWith(":*") && !value.endsWith("*"))
\r
280 if (value.endsWith(":false"))
\r
281 desiredState = false;
\r
283 desiredState = true;
\r
284 if (value.startsWith("-"))
\r
285 desiredState = !desiredState;
\r
286 int pos = n.getContent().indexOf("<en-todo");
\r
287 if (pos == -1 && value.startsWith("-") && (value.endsWith("*") || value.endsWith(":")))
\r
289 if (value.endsWith("*"))
\r
292 int endPos = n.getContent().indexOf("/>", pos);
\r
293 String segment = n.getContent().substring(pos, endPos);
\r
294 boolean currentState;
\r
295 if (segment.toLowerCase().indexOf("checked=\"true\"") == -1)
\r
296 currentState = false;
\r
298 currentState = true;
\r
299 if (desiredState == currentState)
\r
302 pos = n.getContent().indexOf("<en-todo", pos+1);
\r
307 for (int i=0; i<resource.size(); i++) {
\r
308 String resourceString = resource.get(i);
\r
309 resourceString = resourceString.replace("\"", "");
\r
310 if (resourceString.startsWith("-"))
\r
312 resourceString = resourceString.substring(resourceString.indexOf(":")+1);
\r
313 for (int j=0; j<n.getResourcesSize(); j++) {
\r
314 boolean match = stringMatch(n.getResources().get(j).getMime(), resourceString, negative);
\r
323 // Take the initial search & split it apart
\r
324 private void resolveSearch(String search) {
\r
325 List<String> words = new ArrayList<String>();
\r
326 StringBuffer b = new StringBuffer(search);
\r
328 int len = search.length();
\r
329 char nextChar = ' ';
\r
330 boolean quote = false;
\r
331 for (int i=0, j=0; i<len; i++, j++) {
\r
332 if (search.charAt(i)==nextChar && !quote) {
\r
333 b.setCharAt(j,'\0');
\r
336 if (search.charAt(i)=='\"') {
\r
346 if (((i+2)<len) && search.charAt(i) == '\\') {
\r
351 search = b.toString();
\r
353 for (int i=0; i<search.length(); i++) {
\r
354 if (search.charAt(i) == '\0') {
\r
355 search = search.substring(1);
\r
358 pos = search.indexOf('\0');
\r
360 words.add(search.substring(0,pos).toLowerCase());
\r
361 search = search.substring(pos);
\r
366 if (search.charAt(0)=='\0')
\r
367 words.add(search.substring(1).toLowerCase());
\r
369 words.add(search.toLowerCase());
\r
374 // Parse out individual words into separate lists
\r
375 // Supported options
\r
381 // source application
\r
386 private void parseTerms(List<String> words) {
\r
387 for (int i=0; i<words.size(); i++) {
\r
388 String word = words.get(i);
\r
389 int pos = word.indexOf(":");
\r
390 if (word.startsWith("any:")) {
\r
392 word = word.substring(4).trim();
\r
393 pos = word.indexOf(":");
\r
395 boolean searchPhrase = false;
\r
396 if (pos < 0 && word.indexOf(" ") > 0) {
\r
398 searchPhrases.add(word.toLowerCase());
\r
400 if (!searchPhrase && pos < 0)
\r
401 if (word != null && word.length() > 0)
\r
402 getWords().add(word);
\r
403 // getWords().add("*"+word+"*"); //// WILDCARD
\r
404 if (word.startsWith("intitle:"))
\r
405 intitle.add("*"+word+"*");
\r
406 if (word.startsWith("-intitle:"))
\r
407 intitle.add("*"+word+"*");
\r
408 if (word.startsWith("notebook:"))
\r
409 notebooks.add(word);
\r
410 if (word.startsWith("-notebook:"))
\r
411 notebooks.add(word);
\r
412 if (word.startsWith("tag:"))
\r
414 if (word.startsWith("-tag:"))
\r
416 if (word.startsWith("resource:"))
\r
417 resource.add(word);
\r
418 if (word.startsWith("-resource:"))
\r
419 resource.add(word);
\r
420 if (word.startsWith("author:"))
\r
422 if (word.startsWith("-author:"))
\r
424 if (word.startsWith("source:"))
\r
426 if (word.startsWith("-source:"))
\r
428 if (word.startsWith("sourceapplication:"))
\r
429 sourceApplication.add(word);
\r
430 if (word.startsWith("-sourceapplication:"))
\r
431 sourceApplication.add(word);
\r
432 if (word.startsWith("recotype:"))
\r
433 recoType.add(word);
\r
434 if (word.startsWith("-recotype:"))
\r
435 recoType.add(word);
\r
436 if (word.startsWith("todo:"))
\r
438 if (word.startsWith("-todo:"))
\r
440 if (word.startsWith("stack:"))
\r
442 if (word.startsWith("-stack:"))
\r
445 if (word.startsWith("latitude:"))
\r
446 latitude.add(word);
\r
447 if (word.startsWith("-latitude:"))
\r
448 latitude.add(word);
\r
449 if (word.startsWith("longitude:"))
\r
450 longitude.add(word);
\r
451 if (word.startsWith("-longitude:"))
\r
452 longitude.add(word);
\r
453 if (word.startsWith("altitude:"))
\r
454 altitude.add(word);
\r
455 if (word.startsWith("-altitude:"))
\r
456 altitude.add(word);
\r
458 if (word.startsWith("created:"))
\r
460 if (word.startsWith("-created:"))
\r
462 if (word.startsWith("updated:"))
\r
464 if (word.startsWith("-updated:"))
\r
466 if (word.startsWith("subjectdate:"))
\r
468 if (word.startsWith("-subjectdate:"))
\r
473 // Match notebooks in search terms against notes
\r
474 private boolean matchListAll(List<String> list, String title) {
\r
475 if (list.size() == 0)
\r
477 boolean negative = false;
\r
478 for (int i=0; i<list.size(); i++) {
\r
479 int pos = list.get(i).indexOf(":");
\r
481 if (list.get(i).startsWith("-"))
\r
483 String filterName = cleanupWord(list.get(i).substring(pos+1));
\r
484 filterName = filterName.replace("*", ".*"); // setup for regular expression pattern match
\r
485 boolean matches = Pattern.matches(filterName.toLowerCase(),title.toLowerCase());
\r
486 if (matches && negative)
\r
488 if (matches && !negative)
\r
496 // Match notebooks in search terms against notes
\r
497 private boolean matchContentAll(Note n) {
\r
498 if (todo.size() == 0 && resource.size() == 0 && searchPhrases.size() == 0)
\r
501 boolean returnTodo = false;
\r
502 boolean returnResource = false;
\r
503 boolean returnPhrase = false;
\r
505 if (todo.size() == 0)
\r
507 if (resource.size() == 0)
\r
508 returnResource = true;
\r
509 if (searchPhrases.size() == 0)
\r
510 returnPhrase = true;
\r
513 n = conn.getNoteTable().getNote(n.getGuid(), true, true, false, false, false);
\r
515 // Check for search phrases
\r
516 String text = StringEscapeUtils.unescapeHtml(n.getContent().replaceAll("\\<.*?\\>", "")).toLowerCase();
\r
517 boolean negative = false;
\r
518 for (int i=0; i<searchPhrases.size(); i++) {
\r
519 String phrase = searchPhrases.get(i);
\r
520 if (phrase.startsWith("-")) {
\r
522 phrase = phrase.substring(1);
\r
525 phrase = phrase.substring(1);
\r
526 phrase = phrase.substring(0,phrase.length()-1);
\r
527 if (text.indexOf(phrase)>=0) {
\r
529 returnPhrase = true;
\r
531 if (text.indexOf(phrase)<0 && negative)
\r
532 returnPhrase = true;
\r
536 for (int i=0; i<todo.size(); i++) {
\r
537 String value = todo.get(i);
\r
538 value = value.replace("\"", "");
\r
539 boolean desiredState;
\r
540 if (!value.endsWith(":false") && !value.endsWith(":true") && !value.endsWith(":*") && !value.endsWith("*"))
\r
542 if (value.endsWith(":false"))
\r
543 desiredState = false;
\r
545 desiredState = true;
\r
546 if (value.startsWith("-"))
\r
547 desiredState = !desiredState;
\r
548 int pos = n.getContent().indexOf("<en-todo");
\r
549 if (pos == -1 && value.startsWith("-") && (value.endsWith("*") || value.endsWith(":")))
\r
551 if (pos > -1 && value.startsWith("-") && (value.endsWith("*") || value.endsWith(":")))
\r
555 if (value.endsWith("*"))
\r
558 int endPos = n.getContent().indexOf("/>", pos);
\r
559 String segment = n.getContent().substring(pos, endPos);
\r
560 boolean currentState;
\r
561 if (segment.toLowerCase().indexOf("checked=\"true\"") == -1)
\r
562 currentState = false;
\r
564 currentState = true;
\r
565 if (desiredState == currentState)
\r
568 pos = n.getContent().indexOf("<en-todo", pos+1);
\r
573 for (int i=0; i<resource.size(); i++) {
\r
574 String resourceString = resource.get(i);
\r
575 resourceString = resourceString.replace("\"", "");
\r
577 if (resourceString.startsWith("-"))
\r
579 resourceString = resourceString.substring(resourceString.indexOf(":")+1);
\r
580 if (resourceString.equals(""))
\r
582 for (int j=0; j<n.getResourcesSize(); j++) {
\r
583 boolean match = stringMatch(n.getResources().get(j).getMime(), resourceString, negative);
\r
586 returnResource = true;
\r
590 return returnResource && returnTodo && returnPhrase;
\r
593 private boolean stringMatch(String content, String text, boolean negative) {
\r
595 if (content == null && !negative)
\r
597 if (content == null && negative)
\r
600 if (text.endsWith("*")) {
\r
601 text = text.substring(0,text.length()-1);
\r
606 content = content.toLowerCase();
\r
607 regex = regex.toLowerCase();
\r
608 boolean matches = content.startsWith(regex);
\r
614 // Remove odd strings from search terms
\r
615 private String cleanupWord(String word) {
\r
616 if (word.startsWith("\""))
\r
617 word = word.substring(1);
\r
618 if (word.endsWith("\""))
\r
619 word = word.substring(0,word.length()-1);
\r
620 word = word.replace("\\\"","\"");
\r
621 word = word.replace("\\\\","\\");
\r
628 private boolean matchDatesAll(List<String> dates, long noteDate) {
\r
629 if (dates.size()== 0)
\r
632 boolean negative = false;
\r
633 for (int i=0; i<dates.size(); i++) {
\r
634 String requiredDate = dates.get(i);
\r
635 if (requiredDate.startsWith("-"))
\r
639 requiredDate = requiredDate.substring(requiredDate.indexOf(":")+1);
\r
641 response = dateCheck(requiredDate, noteDate);
\r
642 } catch (java.lang.NumberFormatException e) {return false;} {
\r
643 if (negative && response < 0)
\r
645 if (!negative && response > 0)
\r
651 private boolean matchDatesAny(List<String> dates, long noteDate) {
\r
652 if (dates.size()== 0)
\r
655 boolean negative = false;
\r
656 for (int i=0; i<dates.size(); i++) {
\r
657 String requiredDate = dates.get(i);
\r
658 if (requiredDate.startsWith("-"))
\r
662 requiredDate = requiredDate.substring(requiredDate.indexOf(":")+1);
\r
664 response = dateCheck(requiredDate, noteDate);
\r
665 } catch (java.lang.NumberFormatException e) {return false;} {
\r
666 if (negative && response > 0)
\r
668 if (!negative && response < 0)
\r
675 @SuppressWarnings("unused")
\r
676 private void printCalendar(Calendar calendar) {
\r
677 // define output format and print
\r
678 SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy hh:mm:ss aaa");
\r
679 String date = sdf.format(calendar.getTime());
\r
680 System.err.print(date);
\r
681 calendar = new GregorianCalendar();
\r
685 //****************************************
\r
686 //****************************************
\r
687 // Match search terms against notes
\r
688 //****************************************
\r
689 //****************************************
\r
690 public List<Note> matchWords() {
\r
691 logger.log(logger.EXTREME, "Inside EnSearch.matchWords()");
\r
692 boolean subSelect = false;
\r
694 NoteTable noteTable = new NoteTable(logger, conn);
\r
695 List<String> validGuids = new ArrayList<String>();
\r
697 if (searchWords.size() > 0)
\r
700 NSqlQuery query = new NSqlQuery(conn.getConnection());
\r
701 // Build a temp table for GUID results
\r
702 if (!conn.dbTableExists("SEARCH_RESULTS")) {
\r
703 query.exec("create temporary table SEARCH_RESULTS (guid varchar)");
\r
704 query.exec("create temporary table SEARCH_RESULTS_MERGE (guid varchar)");
\r
706 query. exec("Delete from SEARCH_RESULTS");
\r
707 query. exec("Delete from SEARCH_RESULTS_MERGE");
\r
710 NSqlQuery insertQuery = new NSqlQuery(conn.getConnection());
\r
711 NSqlQuery indexQuery = new NSqlQuery(conn.getIndexConnection());
\r
712 NSqlQuery mergeQuery = new NSqlQuery(conn.getConnection());
\r
713 NSqlQuery deleteQuery = new NSqlQuery(conn.getConnection());
\r
715 insertQuery.prepare("Insert into SEARCH_RESULTS (guid) values (:guid)");
\r
716 mergeQuery.prepare("Insert into SEARCH_RESULTS_MERGE (guid) values (:guid)");
\r
719 for (int i=0; i<getWords().size(); i++) {
\r
720 if (getWords().get(i).indexOf("*") == 0) {
\r
721 indexQuery.prepare("Select distinct guid from words where weight >= " +minimumRecognitionWeight +
\r
722 " and word=:word");
\r
723 indexQuery.bindValue(":word", getWords().get(i));
\r
725 indexQuery.prepare("Select distinct guid from words where weight >= " +minimumRecognitionWeight +
\r
726 " and word like :word");
\r
727 indexQuery.bindValue(":word", getWords().get(i).replace("*", "%"));
\r
730 String guid = null;
\r
731 while(indexQuery.next()) {
\r
732 guid = indexQuery.valueString(0);
\r
734 insertQuery.bindValue(":guid", guid);
\r
735 insertQuery.exec();
\r
737 mergeQuery.bindValue(":guid", guid);
\r
742 deleteQuery.exec("Delete from SEARCH_RESULTS where guid not in (select guid from SEARCH_RESULTS_MERGE)");
\r
743 deleteQuery.exec("Delete from SEARCH_RESULTS_MERGE");
\r
747 query.prepare("Select distinct guid from Note where guid in (Select guid from SEARCH_RESULTS)");
\r
748 if (!query.exec())
\r
749 logger.log(logger.LOW, "Error merging search results:" + query.lastError());
\r
751 while (query.next()) {
\r
752 validGuids.add(query.valueString(0));
\r
756 List<Note> noteIndex = noteTable.getAllNotes();
\r
757 List<Note> guids = new ArrayList<Note>();
\r
758 for (int i=0; i<noteIndex.size(); i++) {
\r
759 Note n = noteIndex.get(i);
\r
760 boolean good = true;
\r
762 if (!validGuids.contains(n.getGuid()) && subSelect)
\r
765 // Start matching special stuff, like tags & notebooks
\r
767 if (good && !matchTagsAny(n.getTagNames()))
\r
769 if (good && !matchNotebook(n.getNotebookGuid()))
\r
771 if (good && !matchNotebookStack(n.getNotebookGuid()))
\r
773 if (good && !matchListAny(getIntitle(), n.getTitle()))
\r
775 if (good && !matchListAny(getAuthor(), n.getAttributes().getAuthor()))
\r
777 if (good && !matchListAny(getSource(), n.getAttributes().getSource()))
\r
779 if (good && !matchListAny(getSourceApplication(), n.getAttributes().getSourceApplication()))
\r
781 if (good && !matchContentAny(n))
\r
783 if (good && !matchDatesAny(getCreated(), n.getCreated()))
\r
785 if (good && !matchDatesAny(getUpdated(), n.getUpdated()))
\r
787 if (good && n.getAttributes() != null && !matchDatesAny(getSubjectDate(), n.getAttributes().getSubjectDate()))
\r
790 if (good && !matchTagsAll(n.getTagNames()))
\r
792 if (good && !matchNotebook(n.getNotebookGuid()))
\r
794 if (good && !matchNotebookStack(n.getNotebookGuid()))
\r
796 if (good && !matchListAll(getIntitle(), n.getTitle()))
\r
798 if (good && !matchListAll(getAuthor(), n.getAttributes().getAuthor()))
\r
800 if (good && !matchListAll(getSource(), n.getAttributes().getSource()))
\r
802 if (good && !matchListAll(getSourceApplication(), n.getAttributes().getSourceApplication()))
\r
804 if (good && !matchContentAll(n))
\r
806 if (good && !matchDatesAll(getCreated(), n.getCreated()))
\r
808 if (good && !matchDatesAll(getUpdated(), n.getUpdated()))
\r
810 if (good && n.getAttributes() != null && !matchDatesAll(getSubjectDate(), n.getAttributes().getSubjectDate()))
\r
818 // For performance reasons, we didn't get the tags for every note individually. We now need to
\r
820 List<NoteTagsRecord> noteTags = noteTable.noteTagsTable.getAllNoteTags();
\r
821 for (int i=0; i<guids.size(); i++) {
\r
822 List<String> tags = new ArrayList<String>();
\r
823 List<String> names = new ArrayList<String>();
\r
824 for (int j=0; j<noteTags.size(); j++) {
\r
825 if (guids.get(i).getGuid().equals(noteTags.get(j).noteGuid)) {
\r
826 tags.add(noteTags.get(j).tagGuid);
\r
827 names.add(getTagNameByGuid(noteTags.get(j).tagGuid));
\r
831 guids.get(i).setTagGuids(tags);
\r
832 guids.get(i).setTagNames(names);
\r
834 logger.log(logger.EXTREME, "Leaving EnSearch.matchWords()");
\r
840 private String getTagNameByGuid(String guid) {
\r
841 for (int i=0; i<tagIndex.size(); i++) {
\r
842 if (tagIndex.get(i).getGuid().equals(guid))
\r
843 return tagIndex.get(i).getName();
\r
849 public int dateCheck(String date, long noteDate) throws java.lang.NumberFormatException {
\r
851 boolean found = false;
\r
852 GregorianCalendar calendar = new GregorianCalendar();
\r
854 if (date.contains("-")) {
\r
855 String modifier = date.substring(date.indexOf("-")+1);
\r
856 offset = new Integer(modifier);
\r
858 date = date.substring(0,date.indexOf("-"));
\r
861 if (date.contains("+")) {
\r
862 String modifier = date.substring(date.indexOf("+")+1);
\r
863 offset = new Integer(modifier);
\r
864 date = date.substring(0,date.indexOf("+"));
\r
867 if (date.equalsIgnoreCase("today")) {
\r
868 calendar.add(Calendar.DATE, offset);
\r
869 calendar.set(Calendar.HOUR, 0);
\r
870 calendar.set(Calendar.MINUTE, 0);
\r
871 calendar.set(Calendar.SECOND, 1);
\r
875 if (date.equalsIgnoreCase("month")) {
\r
876 calendar.add(Calendar.MONTH, offset);
\r
877 calendar.set(Calendar.DAY_OF_MONTH, 1);
\r
878 calendar.set(Calendar.HOUR, 0);
\r
879 calendar.set(Calendar.MINUTE, 0);
\r
880 calendar.set(Calendar.SECOND, 1);
\r
884 if (date.equalsIgnoreCase("year")) {
\r
885 calendar.add(Calendar.YEAR, offset);
\r
886 calendar.set(Calendar.MONTH, Calendar.JANUARY);
\r
887 calendar.set(Calendar.DAY_OF_MONTH, 1);
\r
888 calendar.set(Calendar.HOUR, 0);
\r
889 calendar.set(Calendar.MINUTE, 0);
\r
890 calendar.set(Calendar.SECOND, 1);
\r
894 if (date.equalsIgnoreCase("week")) {
\r
895 calendar.add(Calendar.DATE, 0-calendar.get(Calendar.DAY_OF_WEEK)+1);
\r
896 calendar.add(Calendar.DATE,(offset*7));
\r
897 calendar.set(Calendar.HOUR, 0);
\r
898 calendar.set(Calendar.MINUTE, 0);
\r
899 calendar.set(Calendar.SECOND, 1);
\r
904 // If nothing was found, then we have a date number
\r
906 calendar = stringToGregorianCalendar(date);
\r
910 String dateTimeFormat = new String("yyyyMMdd-HHmmss");
\r
911 SimpleDateFormat simple = new SimpleDateFormat(dateTimeFormat);
\r
912 StringBuilder creationDate = new StringBuilder(simple.format(noteDate));
\r
913 GregorianCalendar nCalendar = stringToGregorianCalendar(creationDate.toString().replace("-", "T"));
\r
914 if (calendar == null || nCalendar == null) // If we have something invalid, it automatically fails
\r
916 return calendar.compareTo(nCalendar);
\r
918 private GregorianCalendar stringToGregorianCalendar(String date) {
\r
919 String datePart = date;
\r
920 GregorianCalendar calendar = new GregorianCalendar();
\r
921 boolean GMT = false;
\r
922 String timePart = "";
\r
923 if (date.contains("T")) {
\r
924 datePart = date.substring(0,date.indexOf("T"));
\r
925 timePart = date.substring(date.indexOf("T")+1);
\r
927 timePart = "000001";
\r
929 if (datePart.length() != 8)
\r
931 calendar.set(Calendar.YEAR, new Integer(datePart.substring(0,4)));
\r
932 calendar.set(Calendar.MONTH, new Integer(datePart.substring(4,6))-1);
\r
933 calendar.set(Calendar.DAY_OF_MONTH, new Integer(datePart.substring(6)));
\r
934 if (timePart.endsWith("Z")) {
\r
936 timePart = timePart.substring(0,timePart.length()-1);
\r
938 timePart = timePart.concat("000000");
\r
939 timePart = timePart.substring(0,6);
\r
940 calendar.set(Calendar.HOUR, new Integer(timePart.substring(0,2)));
\r
941 calendar.set(Calendar.MINUTE, new Integer(timePart.substring(2,4)));
\r
942 calendar.set(Calendar.SECOND, new Integer(timePart.substring(4)));
\r
944 calendar.set(Calendar.ZONE_OFFSET, -1*(calendar.get(Calendar.ZONE_OFFSET)/(1000*60*60)));
\r