<%@ page import="java.io.*,
                 java.util.*,
                 java.text.*,
				 java.net.URL,
				 com.plumtree.remote.prc.*,
				 com.plumtree.remote.prc.search.*,
				 com.plumtree.openfoundation.util.*"
%>
 
<html>
<head>
<title>Test Search Page</title>
<!-- hilight search results-->
<style>
<!--
.listHiliteText {background=yellow}
-->
</style>
<script language="javascript">
<!--simple function to open props in new window-->
function openWin(href)
{
  var newwin = window.open(href);
}
</script>
</head>
<body bgcolor='#ffffff'>

<%
//get the request params of search string and start page

 String searchString = request.getParameter("searchString");
 if (null == searchString)
 {
   searchString = "";
 }

 String startPage = request.getParameter("startPage");
 if (null == startPage)
 { 
   startPage = "1";
 }
 
 String startDate = request.getParameter("startDate");
 if (null == startDate)
 {
   startDate = "";
 }
 
 int pageInt = 0;
 pageInt = Integer.parseInt(startPage);
 //startCount is the value we use for the first parameter is setResultsCount
 int startCount = (pageInt * 10) -10;
 %>
 
 <form name="form1" method="post" action="simple_search.jsp">
  Enter your search query: <input type="text" name="searchString" value="<%=searchString%>"/>
 <BR>
 Enter the earliest created date for retrieved documents (in the format mm/dd/yyyy): <input type="text" name="startDate" value="<%=startDate%>"/>
 <BR><br> 
 <input type="submit" value="submit" name="submit"/>
 </form>
 
 <% 
 //proceed if we have a search string
if (!(searchString.equals("")))
{
 %>
<h3>Search for cards containing '<%= searchString %>' </h3>
<p>
<%
	//set the endpoint to the value of your web services server
	String endpoint =
				"http://IP-GW-AS08:9080/ptapi/services/QueryInterfaceAPI";
	URL url = new URL(endpoint);			
	//set username and password to log into your portal
	//hard-coding the values is only for demo purposes
	String username = "Administrator";
	String password = "";
	//get a remote session
	IRemoteSession prcSession =
			RemoteSessionFactory.getExplicitLoginContext(
				url,
				username,
				password);			
	ISearchFactory searchFactory = prcSession.getSearchFactory();
	IPortalSearchRequest searchRequest = searchFactory.createPortalSearchRequest();
	
	//if we have a date, make a filter statement
	if (null != startDate && (!(startDate.equals(""))))
	{
		DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
		Date createdDate = format.parse(startDate);
		IFilterClause filterClause = searchFactory.createAndFilterClause();
		filterClause.addStatement(PlumtreeField.CREATED, Operator.GreaterThan, createdDate);
		searchRequest.setQuery(searchString, filterClause);
	}
	else
	{
		searchRequest.setQuery(searchString); 	
	}

  
  //use 10 results at a time
  searchRequest.setResultsCount(startCount, 10);
	
	ISearchResponse searchResponse = searchRequest.execute();
	int numResults = searchResponse.getReturnedCount();
	int first_result = searchResponse.getFirstResultIndex();
	int totalMatches = searchResponse.getTotalCount();
	
	//print out overall info
	out.println("<h3>Overall Info</h3>");
	out.println("<table border=1>");
	out.println("<tr><td><b>Total Matches:</b></td><td>" + totalMatches + "</td></tr>");
	out.println("<tr><td><b>Number Returned:</b></td><td>" + numResults + "</td></tr>");
	out.println("<tr><td><b>Warning:</b></td><td>" + searchResponse.getWarning() + "</td></tr>");
	out.println("<tr><td><b>First Result (0-based):</b></td><td>" + first_result + "</td></tr>");
	out.println("</table>");

	//print out results if we have them
	if (numResults > 0)
	{
		out.println("<h3>Results</h3>");
	  out.print("<table border=1>");
    ISearchResultSet resultSet = searchResponse.getResultSet();
		Enumeration results = resultSet.getResults();

		//print values and put results into hashmap to obtain properties in a new window
		HashMap map = new HashMap();
		while (results.hasMoreElements())
		{		
			IPortalSearchResult result = (IPortalSearchResult) results.nextElement();
			map.put("" + result.getRank(), result);
			//results are 0 based- but we present them here to the end-user as 1 based. 
			out.print("<tr><td width='2'>" + (startCount + result.getRank() + 1) + "</td>");			
      out.print("<td witdh='5'><img src='" + result.getIconURL() + "'/></td>");
			out.print("<td><a href='" + result.getURL() + "'>" + result.getName() +"</a></td>");
			out.print("<td><a href=\"javascript:openWin('properties.jsp?ptrank=" + result.getRank() + "')\">Properties</a></td>");			
			out.println("</tr>");
			out.println("<tr><td colspan='4'>" + result.getExcerpt() + "</td></tr>");
		}//while (results.hasMoreElements())
		
		session.setAttribute("resultsMap", map);
		
	}//	if (numResults > 0)
	out.println("</table>");
	
	//print the links at the bottom up to 150 results
		out.println("<table border='0'>");
		
    int total = 15;
    if (totalMatches < 150)
    {
      if ((totalMatches % 10) == 0)
      {
        total =  totalMatches / 10;
      }
      else
      {
        total =  (totalMatches / 10) + 1;  
      }    
    }
    int currentPos = first_result;
    currentPos = (currentPos  / 10) + 1;

    out.println("<tr>");
    out.println("<td>");

    for (int i = 1; i <= total; i++)
    {
      if (i == currentPos)
      {
        out.print(i);
      }
      else
      {
         out.print("<a href=\"");
        out.print("simple_search.jsp?searchString=" + searchString + "&startPage=" + i + "&startDate=" + startDate);
        out.print("\">");
        out.print(i);
        out.println("</a>");
      }

      out.print("&nbsp;");
    }

    out.println("</td>");
    out.println("</tr>");
    out.println("</table>");
}
%>
</body>
</html>


