<%@ page import="com.atlassian.confluence.setup.BootstrapManager,
				 com.atlassian.confluence.setup.ConfluenceBootstrapConstants,
				 com.atlassian.confluence.util.GeneralUtil,
				 com.atlassian.spring.container.ContainerManager,
				 org.apache.lucene.document.Document,
                 org.apache.lucene.index.IndexReader,
                 java.io.IOException" %>

<%@ page contentType="text/html; charset=UTF-8" %>

<%
	String indexDir = null;

	if (request.getParameter("lucene.index.dir") != null)
	{
		session.setAttribute("lucene.index.dir", request.getParameter("lucene.index.dir"));
	}

	if (session.getAttribute("lucene.index.dir") != null)
	{
		indexDir = (String) session.getAttribute("lucene.index.dir");
	}

	BootstrapManager bootstrapManager = (BootstrapManager) ContainerManager.getComponent("bootstrapManager");
	indexDir = bootstrapManager.getFilePathProperty(ConfluenceBootstrapConstants.LUCENE_INDEX_DIR_PROP);
%>

<html>

	<head>

		<title>Index Browser</title>

	</head>

	<body>

	<p>

	<form action="indexbrowser.jsp">
		Which index would you like to browse?

        <input name="lucene.index.dir" value="<%= indexDir != null ? indexDir : "" %>" size="30">

		<input type="submit" value="Refresh">
	</form>

	<hr>

	<%
        IndexReader reader = null;

        if (indexDir != null)
        {

			try
			{
                // IndexWriter writer = new IndexWriter(indexDir, new DefaultLuceneAnalyzerFactory().createAnalyzer(), false);
                // writer.optimize();
                // writer.close();

				reader = IndexReader.open(indexDir);
			}
			catch (IOException e)
			{
				out.println("<strong>Error</strong>: Could not open index: " + e);
			}
		}
	%>

	<% if (reader != null) {
        int numDocs = reader.numDocs();
    %>

	<strong>Index Location</strong>: <%=  indexDir %><br/>
	<strong>Number of Documents</strong>: <%= numDocs %><br/>


	<ol>
	<%
		for (int i = 0; i < numDocs; i++) {
			try {
                Document doc = reader.document(i);
	%>
		<li><strong><a href="viewdocument.jsp?doc=<%= i %>"><%= doc.get("handle") + " : " + GeneralUtil.escapeXml(doc.get("title")) %></a></strong></li>
	<%
		    } catch (Exception e)
            {
    %>
        <li><strong>Error: <%= e %></strong></li>
    <%
            }
        }
	%>
	</ol>

	<% reader.close(); %>
	<% } %>


</body>

</html>

