Commit 691d83f5 authored by Pamelpreet Jhinger's avatar Pamelpreet Jhinger
Browse files

started choices implementation for search queries that have more than one possible result

parent f22ecce5
......@@ -66,26 +66,29 @@ public class QueryProcessor {
}
public Query process(String query) {
return search(query);
}
private Query search(String query) {
public List<Query> process(String query) {
/*For same surnames, add to a list instead of returning, if list length
is more than 1, then generate choice page, else return answer*/
List<Query> possibilities = new ArrayList<>();
for(Query q:queriesMap) {
List<String> name = q.getQuery();
boolean found = true;
StringTokenizer queryTokens = new StringTokenizer(query);
for(int i = 0; queryTokens.hasMoreTokens(); i++){
while (queryTokens.hasMoreTokens()) {
found &= name.contains(queryTokens.nextToken().toLowerCase());
}
if (found) {
return q;
possibilities.add(q);
}
}
return new Query(null, "","","");
if (possibilities.isEmpty()) {
List<Query> noQueryFound = new ArrayList<>()
noQueryFound.add(new Query(null, "", "", ""));
return noQueryFound;
}
return possibilities;
}
}
package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownResultPage;
import ic.doc.web.PDFResultPage;
import ic.doc.web.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -12,6 +9,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class WebServer {
......@@ -33,12 +31,18 @@ public class WebServer {
if (query == null || type == null) {
new IndexPage().writeTo(resp);
} else {
if (type.equals("html")) {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else if (type.equals("markdown")) {
new MarkdownResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
QueryProcessor queryProcessor = new QueryProcessor();
List<Query> possibilities = queryProcessor.process(query);
if (possibilities.size() == 1) {
if (type.equals("html")) {
new HTMLResultPage(query, queryProcessor.process(query)).writeTo(resp);
} else if (type.equals("markdown")) {
new MarkdownResultPage(query, queryProcessor.process(query)).writeTo(resp);
} else {
new PDFResultPage(query, queryProcessor.process(query)).writeTo(resp);
}
} else {
new PDFResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
new ChoicePage(possibilities);
}
}
}
......
package ic.doc.web;
import ic.doc.Query;
import java.util.List;
public class ChoicePage {
public ChoicePage(List<Query> possibilities) {
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment