package diskindex;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;


public class DiskIndexer {
	// being an experiment using Solr, java http and file structures! :-)

	private static String source = "/media/disk";
	private static String solr_url = "http://localhost:8080/solr/update/";

	public static void main(String[] argv) {
		try {
			indexFileSystem(new File(source));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void indexFileSystem(File start) throws FileNotFoundException {
		File[] filesAndDirs = start.listFiles();
		List<File> filesDirs = Arrays.asList(filesAndDirs);
		for(File file : filesDirs) {
			if ( ! file.isFile() ) {
				//must be a directory - keep burrowing!
				indexFileSystem(file);
			} else {
				// found a leaf
				// construct solr document and send it!
				String filename = file.getName(); // the filename, NOT the path
				String ext;

				if (filename.lastIndexOf('.') > -1) { // no extension probably
					ext = filename.substring(filename.lastIndexOf('.')+1, filename.length());
				} else {
					ext = "fa_none";
				}

				// add a lowercase one too - could get solr to do that for us!
				String norm_ext = ext.toLowerCase();

				// no point getting all XML builder here when this'll do!
				String solrDoc = String.format("<add>\n  <doc>\n    <field name=\"fullpath\">%s</field>\n    <field name=\"ext\">%s</field>\n    <field name=\"normext\">%s</field>\n  </doc>\n</add>\n", file.getAbsoluteFile(), ext, norm_ext);

				try {
					//System.out.println(solrDoc);
					// add the doc & commit - might've been quicker to not do that one at a time! :-)
					System.out.println(solrAdd(solrDoc));
					System.out.println(solrCommit());
					file = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private static String solrAdd(String doc) throws IOException {
		return sendPostCommand(doc, solr_url);
	}
	
	private static String solrCommit() throws IOException {
		return sendPostCommand("<commit />", solr_url);
	}

	/* sendPostCommand was ripped kicking screaming from Grant Ingersoll's
	 * ace introduction to Solr at: 
	 * 
	 * http://www.ibm.com/developerworks/java/library/j-solr1/
	 *  
	 * the code is http://www.apache.org/licenses/LICENSE-2.0 licensed, so
	 * I guess I can put it here! :-)
	 */
	
	private static String sendPostCommand(String command, String url)
	throws IOException	{

		String results = null;
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(url);

		RequestEntity re = new StringRequestEntity(command, "text/xml", "UTF-8");
		post.setRequestEntity(re);
		try {
			// Execute the method.
			int statusCode = client.executeMethod(post);

			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + post.getStatusLine());
			}

			// Read the response body.
			byte[] responseBody = post.getResponseBody();

			// Deal with the response.
			// Use caution: ensure correct character encoding and is not binary data
			results = new String(responseBody);
		}
		catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		}
		catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			post.releaseConnection();
		}
		return results;
	}
}
