/* filename: PhotoIndex.java
 * similar to Suzi's LuceneInterface.java 
 * info340 history places project*/
package historyPlaces;

import historyPlaces.*;

import java.io.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class PhotoIndex{

    private Connection conn = null;
    private Analyzer analyzer = new StandardAnalyzer();

    /* Initializes the connection */
    private void init() {
    try {

 /* This statement implicitly loads the driver */
 Class.forName("org.postgresql.Driver");

 /* Now, attempt to create a connection */
 conn = DriverManager.getConnection("jdbc:postgresql://localhost/hp", "dthtran", "dthtran");
  /* Now, attempt to create a connection */

 if (conn == null) 
     throw new Exception("Could not connect to database!");
  } catch(Exception e) {
      e.printStackTrace();
  }
    }

    private Connection getConn() {
   if (conn == null) {
    init();
   }
   return conn; 
    }

    

    private Document createPhotoDoc(String photoid, String placeid, String url, 
  String date, String comment, String photographer, String name) {
 Document document = new Document();
 document.add(Field.UnIndexed("photoid", photoid));
 document.add(Field.UnIndexed("placeid", placeid));
 document.add(Field.UnIndexed("url", url));
 document.add(Field.Keyword("date", date));
 document.add(Field.Keyword("name", name));
 document.add(Field.Text("comment", comment));
 document.add(Field.Keyword("photographer", photographer));
 return document;
    }


    private Document createLocDoc(String placeid, String pname) {
 Document document = new Document();
 document.add(Field.UnIndexed("placeid", placeid));
 document.add(Field.Text("pname", pname));
 return document;
    }

    private Document createCommDoc(String comment, String photoid) {
 Document document = new Document();
 //document.add(Field.UnIndexed("cid", cid));
 document.add(Field.Text("comment", comment));
 document.add(Field.UnIndexed("photoid", photoid));
 return document;
    }

/*	public void main(String[] args) throws Exception
	{
		boolean success = false;
		success = createIndex(args[0]);
	
		
	}
*/

    public boolean createIndex(String indexdir) throws Exception {

      try {
 //Create new write object
 //Set the name of index
 //Chose the tokenizer
 //Boolean create a new index
 IndexWriter iw = new IndexWriter(indexdir, analyzer, true);
 
 //Query SQL database for data to index
 Connection conn = getConn();
 Statement stmt = conn.createStatement();

 //Index place fields
        String sql = "SELECT place.pid, place.pname FROM place";
 ResultSet rs = stmt.executeQuery(sql);

 while (rs.next()) {
     String pid = rs.getString("pid");
     String pname = rs.getString("pname");
     
     //Turn query into lucene document object
     Document locdoc = createLocDoc(pid, pname);

     //Add documents to the index
     iw.addDocument(locdoc);
 }

 //Index photo fields
        sql = "SELECT photo.photoid, photo.placeid, photo.photographer, photo.url, photo.date, photo.comment, photo.name FROM photo";
 rs = stmt.executeQuery(sql);

 while (rs.next()) {
     String photoid = rs.getString("photoid");
     String placeid = rs.getString("placeid");
     String photographer = rs.getString("photographer");
     String url = rs.getString("url");
     String date = rs.getString("date");
     String comment = rs.getString("comment");
  String name = rs.getString("name");
     
     //Turn query into lucene document object
     Document photodoc = createPhotoDoc(photoid, placeid, url , date, comment, photographer,name);

     //Add documents to the index
     iw.addDocument(photodoc);
 }
 
 //Index Comment fields
 /*sql = "SELECT * FROM comment";
 rs = stmt.executeQuery(sql);
 while (rs.next()) {
     //String cid = rs.getString("cid");
     String email = rs.getString("email");
     String comment = rs.getString("comment");

     //Turn query into lucene document object
     Document document = createCommDoc(email, comment);

     //Add document to index
     iw.addDocument(document);
 }*/

 //Don't forget to close the index
 iw.optimize();
 iw.close();
 return true;

      } catch(Exception e) {
   e.printStackTrace();
   return false;
      }

    }

   /* public Hits searchIndex(String indexdir, String query) throws Exception {

 //Open an existing lucene 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 string
 Query parsedq = QueryParser.parse(query, "all", analyzer);
 
 //Perform the search
 Hits hits = is.search(parsedq);
 System.out.println(hits.length() + "total matches");

 
 //Don't forget to close the index
 is.close();

 //Return the results
 return hits;
    }*/


}