<%@ page import="historyPlaces.*,java.util.*, org.apache.lucene.document.*, org.apache.lucene.index.*, org.apache.lucene.search.*, org.apache.lucene.store.*, org.apache.lucene.analysis.*, org.apache.lucene.analysis.standard.*, org.apache.lucene.queryParser.*, java.io.*, java.util.*" %>
<%@ include file="header.inc" %>
<%  
engine db = new engine();
String q = null;
out.print("<h2>Search a Place</h2>");
%>
<form action="placeSearch.jsp" method="get">

<input name="query" type="text" size="40" value="<%= (q == null)?"":q %>"/>
<input type="submit" value="Search"/>
</form>

<%


// Get the query from the textbox
q = request.getParameter("query").trim();

// Don't run if no query entered
if (q.length() == 0) {
  out.println("Please enter a query!");

} else {

  // Open up existing index
  File myIndex = new File("/usr/share/tomcat4/webapps/dthtran/historyplaces/places-index/index/");
  Directory dir = FSDirectory.getDirectory(myIndex, false);

  // Create a new Search object
  IndexSearcher is = new IndexSearcher(dir);
  // Parse the query for the search object
  // q is the query
  // contents is the name of the default field with content
  // use StandardAnalyzer
  String [] searchFields = {"comment", "name"};
  Query query = MultiFieldQueryParser.parse(q, searchFields, new StandardAnalyzer());

  // Perform the search
  Hits hits = is.search(query);

  // Print out results
  out.println("Your query:  " + q);
  if (hits.length() > 0) {
    out.println("<p>Lucene found the following documents:</p>");
    for (int i=0; i<hits.length(); i++) {
      Document doc = hits.doc(i);
      out.println("<p><strong>Place:</strong>" + doc.get("name") +
      				"<br /><strong>Comment:</strong>" + doc.get("comment") + 
      				"<br /><strong>[<a href=\"./create.jsp?parent=" + doc.get("pid") + "\">Add Child to this Place</a>]</strong> " + 
      				"| <strong>[<a href=\"./hierarchy.jsp?pid=" + doc.get("pid") + "\">View in Hierarchy</a>]</strong><br />");
      
      Integer pidInteger = new Integer(doc.get("pid"));
      int pidInt = pidInteger.intValue();
      LinkedList curPhotos = db.getAllPhoto(pidInt);
      if(curPhotos.size() == 0){
      	out.println("<strong>No Photos for Place!</strong><br />");
      }else{
      	for(int j = 0; j < curPhotos.size(); j++){
      		photo thisPhoto = (photo)curPhotos.get(j);
      		out.println("<br /><div style=\"padding-left:40px; padding-bottom:20px; font-size:12px;\"><img src=\"" + thisPhoto.getURL() + "\"><br /><strong>Comment: </strong>" + thisPhoto.getComment() + "<br /><strong>Date Taken: </strong>" + thisPhoto.getDate() + "<br /></div>");
      	}
      curPhotos.clear();
      }
      out.println("</p>");
    }
  } else {
    out.println("<h3>Lucene did not find any results that matched your query</h3>");
  }

  // Remember to close the index!
  is.close();
}

%>
<%@ include file="footer.inc" %>