Adding search box, make searches case insensitive

This commit is contained in:
Erik Forkalsrud 2010-10-23 16:36:13 -07:00
parent 7eeb7d9bae
commit 1ce75d2da8
7 changed files with 33 additions and 13 deletions

View file

@ -21,7 +21,7 @@ import org.forkalsrud.album.db.DirectoryProps;
/** /**
* @author knut * @author knut
*/ */
public class DirectoryEntry extends EntryWithChildren { public class DirectoryEntry extends EntryWithChildren<Entry> {
final static String CACHE_FILE = "cache.properties"; final static String CACHE_FILE = "cache.properties";
final static String OVERRIDE_FILE = "album.properties"; final static String OVERRIDE_FILE = "album.properties";
@ -195,4 +195,9 @@ public class DirectoryEntry extends EntryWithChildren {
loadChildren(); loadChildren();
return earliest; return earliest;
} }
@Override
public boolean groupByYear() {
return parent == null;
}
} }

View file

@ -116,4 +116,9 @@ public abstract class Entry {
public Date getEarliest() { public Date getEarliest() {
return getDate(); return getDate();
} }
public boolean groupByYear() {
return false;
}
} }

View file

@ -11,9 +11,9 @@ import java.util.List;
* @author knut * @author knut
* *
*/ */
public class EntryWithChildren extends Entry { public class EntryWithChildren<T extends Entry> extends Entry {
protected List<Entry> children = new ArrayList<Entry>(); protected List<T> children = new ArrayList<T>();
protected EntryWithChildren(Entry root) { protected EntryWithChildren(Entry root) {
super(root); super(root);
@ -46,12 +46,12 @@ public class EntryWithChildren extends Entry {
} }
} }
public List<Entry> getContents() { public List<T> getContents() {
return children; return children;
} }
public void addContents(Entry entry) { public void addContents(T entry) {
children.add(entry); children.add(entry);
} }

View file

@ -22,6 +22,7 @@ public class SearchEngine {
DirectoryEntry next = queue.removeFirst(); DirectoryEntry next = queue.removeFirst();
processDirectory(next, query); processDirectory(next, query);
} }
results.sort();
results.fillLinkedList(); results.fillLinkedList();
return results; return results;
} }
@ -52,7 +53,7 @@ public class SearchEngine {
} }
boolean isMatch(String candidate, String query) { boolean isMatch(String candidate, String query) {
return candidate != null && candidate.indexOf(query) >= 0; return candidate != null && candidate.toUpperCase().contains(query.toUpperCase());
} }
} }

View file

@ -1,11 +1,12 @@
package org.forkalsrud.album.exif; package org.forkalsrud.album.exif;
import java.io.File; import java.io.File;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
public class SearchResults extends EntryWithChildren { public class SearchResults extends EntryWithChildren<SearchEntry> {
protected HashMap<File, SearchEntry> dupes = new HashMap<File, SearchEntry>(); protected HashMap<File, SearchEntry> dupes = new HashMap<File, SearchEntry>();
Comparator<SearchEntry> sort = new Comparator<SearchEntry>() { Comparator<SearchEntry> sort = new Comparator<SearchEntry>() {
@ -34,6 +35,10 @@ public class SearchResults extends EntryWithChildren {
} }
} }
public void sort() {
Collections.sort(children, sort);
}
} }

View file

@ -187,6 +187,9 @@ public class AlbumServlet
String pathInfo = req.getPathInfo(); String pathInfo = req.getPathInfo();
if (pathInfo != null && pathInfo.startsWith(basePrefix)) { if (pathInfo != null && pathInfo.startsWith(basePrefix)) {
pathInfo = pathInfo.substring(basePrefix.length()); pathInfo = pathInfo.substring(basePrefix.length());
} else if (pathInfo.equals("/search")) {
handleSearch(req, res, (DirectoryEntry)resolveEntry("/"));
return;
} else { } else {
res.sendError(HttpServletResponse.SC_NOT_FOUND, "pathinfo=" + pathInfo); res.sendError(HttpServletResponse.SC_NOT_FOUND, "pathinfo=" + pathInfo);
return; return;
@ -210,11 +213,6 @@ public class AlbumServlet
return; return;
} }
if (pathInfo.endsWith(".search")) {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".search".length());
handleSearch(req, res, (DirectoryEntry)resolveEntry(pathInfo));
return;
}
File file = new File(base, pathInfo); File file = new File(base, pathInfo);
if (!file.canRead()) { if (!file.canRead()) {
@ -299,6 +297,7 @@ public class AlbumServlet
SearchResults results = search.search(query); SearchResults results = search.search(query);
res.setContentType("text/html"); res.setContentType("text/html");
req.setAttribute("search", query);
req.setAttribute("entry", results); req.setAttribute("entry", results);
req.setAttribute("thmb", new Integer(250)); req.setAttribute("thmb", new Integer(250));
req.setAttribute("full", new Integer(800)); req.setAttribute("full", new Integer(800));

View file

@ -27,6 +27,10 @@
h1 { h1 {
text-align: left; text-align: left;
} }
form {
float: right;
display: inline;
}
h2 { h2 {
margin-left: 30px; margin-left: 30px;
text-align: left; text-align: left;
@ -89,6 +93,7 @@ $(document).ready(function() {
</script> </script>
</head> </head>
<body> <body>
<form action="${base}/search" method="get"><input name="q" value="$!search"></form>
#macro(navlink $entry)${base}$mapper.map(${entry.getPath()}).#if($entry.isFile())photo#{else}album#end#end #macro(navlink $entry)${base}$mapper.map(${entry.getPath()}).#if($entry.isFile())photo#{else}album#end#end
#macro(navbutton $entry $direction)#if($entry)<a href="#navlink($entry)"><img class="nav" width="26" height="32" src="${assets}/${direction}.png"/></a>#else<img class="nav" width="26" height="32" src="${assets}/${direction}-inactive.png"/>#end#end #macro(navbutton $entry $direction)#if($entry)<a href="#navlink($entry)"><img class="nav" width="26" height="32" src="${assets}/${direction}.png"/></a>#else<img class="nav" width="26" height="32" src="${assets}/${direction}-inactive.png"/>#end#end
<h1>#navbutton(${entry.prev()} "left")#navbutton(${entry.parent()} "up")#navbutton(${entry.next()} "right") $entry.name</h1> <h1>#navbutton(${entry.prev()} "left")#navbutton(${entry.parent()} "up")#navbutton(${entry.next()} "right") $entry.name</h1>
@ -100,7 +105,7 @@ $(document).ready(function() {
#set($thpath = $mapper.map(${en.thumbnail.getPath()})) #set($thpath = $mapper.map(${en.thumbnail.getPath()}))
#set($cdate = ${en.earliest}) #set($cdate = ${en.earliest})
#set($cyr = $mapper.year($cdate)) #set($cyr = $mapper.year($cdate))
#if($cyr != $yr && !${entry.parent()}) #if($cyr != $yr && ${entry.groupByYear()})
#set($yr = $cyr) #set($yr = $cyr)
<h2>$yr</h2> <h2>$yr</h2>
#end #end