/*
 * Created on 25 mai 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.injac.indexing.index.webdav;

import java.io.File;
import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

//import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.injac.indexing.util.FileTools;


/**
 * unDelete
 * @author marius
 */
public class UnErase extends GenericServlet {

	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		java.io.PrintWriter writer = response.getWriter();
		writer.println("<head><title>Supprimation</title></head>");
		writer.println("<body>");
		
		FileTools dc = new FileTools();
		File index = new File(dc.getParent() + 
				"/webapps/proiectTomcat/indexSaroumane");
		if(!index.exists()){
			writer.println("<p>Error: index not created yet ! " + 
					"Please create index first and try again.'/p>");
			}
		else{
			boolean undeleted = undeleteAll(index);
			if(undeleted){
				writer.println("<p>index has been successfully recreated !</p>" );
				}
			else{
				writer.println("<p>An error occured...</p>");
				}
			}
		writer.println("</body></html>");
		writer.close();
	}

	private boolean undeleteAll(File indexDir){
		try{
			Directory directory = FSDirectory.getDirectory(indexDir, false);
			IndexReader reader = IndexReader.open(directory);
			reader.undeleteAll();
			reader.close();
			directory.close();
			return true;
			}
	
		catch(IOException ioe){
			ioe.printStackTrace();
			}
		
		return false;
		}
}


