<%@ page language="java" import="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.lang.Object.*, java.io.*, java.util.*, java.text.*,
  historyPlaces.*" %>
 <%@ include file="header.inc" %>

<%

boolean search = false;
if (request.getParameter("action") != null 
 && request.getParameter("action").equals("search")) 

 search = true;
%>

<html>
<title>Photo Search Page</title>
<head>
</head>

<body>
<%
 hp db = new hp();
 PhotoIndex pi = new PhotoIndex();
 String indexDir = "/usr/share/tomcat4/webapps/dthtran/historyplaces/photo-index/index/";
 pi.createIndex(indexDir);
%>
<%  
String q ="";
String datef ="";
String datet ="";
String range ="";
out.println("Search Photos by keyword and date range!");
out.println("<br/>Please enter date in this format :yyyy-mo-dd");
out.println("<br>sample search:McCarty from 1999-12-04 to 2004-06-01<br>");
%>
 <form action="photoSearch.jsp" method="post">
<input type = "hidden" name = "action" value = "search">
Keyword: <input name="query" type="text" size="40"><br>
Date from: <input name = "from" type="text" size = "10">
Date to:<input name = "to" type = "text" size = "10"></input><br>
<input type=SUBMIT value="Submit">
</form>



<%
if (search) {
  // Get the query from the textbox
  q = request.getParameter("query").trim();
  datef = request.getParameter("from").trim();
  datet=request.getParameter("to").trim();
  
  SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition p = new ParsePosition(0);
  java.util.Date start = d1.parse(datef, p);
  
  SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition p2 = new ParsePosition(0);
  java.util.Date end = d2.parse(datet, p2);
  
  java.util.Date current = new java.util.Date();
  
  
  
  
  // Don't run if no query entered
  if (q.length() == 0 || datef.length() == 0 || datet.length()==0) {
 out.println("Please enter a query with a valid date");

  } 
  else {
 out.println("<br>Your query:  " + q + " From "+ datef + " To " + datet);
 if (start.compareTo(end) > 0) { out.println("<br>Requested start date is after end date<br>");}
 else if (start.compareTo(current)>0) { out.println("<br>Requested start date is after today's date<br>");}
 else if (end.compareTo(current)>0) { out.println("<br>Requested end date is after today's date<br>");}
 else {

     // Open up existing index
   File myIndex = new File(indexDir);
   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());
      Hits hits = is.search(query);
      
      Query querydate = QueryParser.parse("[ " + datef + " TO " + datet+ " ]", "date", new StandardAnalyzer());
      Hits datehits = is.search(querydate);
      //out.println("This is query " + querydate); 
  
      // Perform the search
    
      int match = 0;
	  int dateInt,queryInt;
      String queryid;
      String dateid;
      // Print out results
    
      out.println("<p>Search results:</p>");
   
      if (hits.length() > 0 && datehits.length() > 0) {
     
        for (int i=0; i < hits.length(); i++) {
          Document doc = hits.doc(i);
          queryid = doc.get("photoid");
		  queryInt=Integer.valueOf(queryid).intValue(); 
          for (int j = 0; j < datehits.length(); j++) {
            Document date = datehits.doc(j);
  
            dateid = date.get("photoid");
            dateInt=Integer.valueOf(dateid).intValue();
          
            if (dateInt==queryInt) {

              out.println("<p><strong>Photo Name :</strong>" + doc.get("name") +
      				"<br /><strong>Comment:</strong>" + doc.get("comment") + 
					"<br /><strong>Date:</strong>" + doc.get("date") + 
					". By <strong> Photographer:</strong>" + doc.get("photographer")
					);
			String link = doc.get("url");
			 if (link !="") 
   				//out.write("<img src =" + link + ">Photo URL</src>");
				out.write("<a href =" + link + ">Photo</a>");
				out.write("<br><img width = 100 height = 100 src =" + link + "></src>");
				/*out.println("<strong>Place Photo</strong><br/><img width = " + 
				"100" +"height =" + "100" + "src=" + link + "></src>");*/
              match=match + 1;
            }
 
          }
        }
        if (match > 0){
          out.println("<br>Your search returns " + match + "results");
        }
        else {
          out.println("Your search returns 0 result");
        }
    
      }
      else if (hits.length()> 0 && datehits.length() <= 0) {
        out.println("Please enter a different date range");
      }
  
      else {
        out.println("<p>Lucene found no documents!</p>");
      }

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


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

</html>