<%@ Page language="c#" Codebehind="SimpleSearch.aspx.cs" AutoEventWireup="false"  Inherits="PRCSearchPage.SimpleSearch" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<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>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			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";
			
	//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
	Plumtree.Remote.PRC.IRemoteSession prcSession =
			Plumtree.Remote.PRC.RemoteSessionFactory.GetExplicitLoginContext(
				new System.Uri(endpoint),
				username,
				password);			
	Plumtree.Remote.PRC.Search.ISearchFactory searchFactory = prcSession.GetSearchFactory();
	Plumtree.Remote.PRC.Search.IPortalSearchRequest searchRequest = searchFactory.CreatePortalSearchRequest();
	
	//if we have a date, make a filter statement
	if (null != startDate && (!(startDate.Equals(""))))
	{
	  DateTime createdDate = DateTime.Parse(startDate);
	  Plumtree.Remote.PRC.Search.IFilterClause filterClause = searchFactory.CreateAndFilterClause();
	  filterClause.AddStatement(Plumtree.Remote.PRC.Search.PlumtreeField.CREATED, Plumtree.Remote.PRC.Operator.GreaterThan, createdDate);
	  searchRequest.SetQuery(searchString, filterClause);
	}
	else
	{
	  searchRequest.SetQuery(searchString); 	
	}
	
  
    //use 10 results at a time
    searchRequest.SetResultsCount(startCount, 10);
	
	Plumtree.Remote.PRC.Search.ISearchResponse searchResponse = searchRequest.Execute();
	int numResults = searchResponse.GetReturnedCount();
	int first_result = searchResponse.GetFirstResultIndex();
	int totalMatches = searchResponse.GetTotalCount();
	
	//print out overall info
	Response.Write("<h3>Overall Info</h3>");
	Response.Write("<table border=1>");
	Response.Write("<tr><td><b>Total Matches:</b></td><td>" + totalMatches + "</td></tr>");
	Response.Write("<tr><td><b>Number Returned:</b></td><td>" + numResults + "</td></tr>");
	Response.Write("<tr><td><b>Warning:</b></td><td>" + searchResponse.GetWarning() + "</td></tr>");
	Response.Write("<tr><td><b>First Result (0-based):</b></td><td>" + first_result + "</td></tr>");
	Response.Write("</table>");

	//print out results if we have them
	if (numResults > 0)
	{
		Response.Write("<h3>Results</h3>");
	    Response.Write("<table border=1>");
        Plumtree.Remote.PRC.Search.ISearchResultSet resultSet = searchResponse.GetResultSet();
	    IEnumerator results = resultSet.GetResults();

		//print values and put results into hashmap to obtain properties in a new window
		Hashtable map = new Hashtable();
		while (results.MoveNext())
		{			
			Plumtree.Remote.PRC.Search.IPortalSearchResult result = (Plumtree.Remote.PRC.Search.IPortalSearchResult) results.Current;
			map.Add("" + result.GetRank(), result);
			//results are 0 based- but we present them here to the end-user as 1 based. 
			Response.Write("<tr><td width='2'>" + (startCount + result.GetRank() + 1) + "</td>");			
            Response.Write("<td witdh='5'><img src='" + result.GetIconURL() + "'/></td>");
			Response.Write("<td><a href='" + result.GetURL() + "'>" + result.GetName() +"</a></td>");
			Response.Write("<td><a href=\"javascript:openWin('Properties.aspx?ptrank=" + result.GetRank() + "')\">Properties</a></td>");			
			Response.Write("</tr>");
			Response.Write("<tr><td colspan='4'>" + result.GetExcerpt() + "</td></tr>");
			
		}//while (results.MoveNext())
		
		Session["resultsMap"] =  map;
		
	}//	if (numResults > 0)
	Response.Write("</table>");
	
	//print the links at the bottom up to 150 results
		Response.Write("<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;


    Response.Write("<tr>");
    Response.Write("<td>");

    for (int i = 1; i <= total; i++)
    {
      if (i == currentPos)
      {
        Response.Write(i);
      }
      else
      {
         Response.Write("<a href=\"");
        Response.Write("SimpleSearch.aspx?searchString=" + searchString + "&startPage=" + i + "&startDate=" + startDate);
        Response.Write("\">");
        Response.Write(i);
        Response.Write("</a>");
      }

      Response.Write("&nbsp;");
    }

    Response.Write("</td>");
    Response.Write("</tr>");
    Response.Write("</table>");
}
%>
		</p>
	</body>
</HTML>
