<%@	page import="com.google.soap.search.*, java.io.*" %>
<html>
<head>
<title>INFO 340 - A1 - Google API</title>
<link href="googlesearch.css" rel="stylesheet" type="text/css">
</head>
<body>
<div align="center">
<h1>pincess searches the web</h1>
<h4>"Your gateway to searching the internet!"</h4>
<form method=post action="googlesearch.jsp">
<strong>Query: </strong><input type=text name="query" size="20">
<input type=submit value="Search">
</form>
<%
	String query = request.getParameter("query");
	int queryLength		= 0;
	int queryNumWords	= 0;
	String[] queryWords;
	String[] siteArray	= {null,null,null,null};
	boolean fileExists 	= true;
	
	try{
		BufferedReader br 	= new BufferedReader(new FileReader("/usr/share/tomcat4/webapps/rprins/assignment1/domains.txt"));
		String currentWord 	= br.readLine();
		int n = 0;
		while(currentWord != null){
			siteArray[n]	= currentWord;
			currentWord 	= br.readLine();
			n++;
		}
	}
	catch(Exception e){
		fileExists = false;
		out.print("<strong>Error!</strong> Cannot find the domains.txt file! The provided error is: " + e);
	}	

	if(query != null){
		queryLength 	= query.length();
		queryWords 		= query.split("\\s");
		queryNumWords 	= queryWords.length;
	}
	
	if(query != null && queryLength != 0 && queryNumWords <= 5 && fileExists == true){
%>
<div class="query">
	The query is: <% out.print(query); %><br /><br />
	Your search will be over the following domains:<br /><% for(int t = 0; t < siteArray.length; t++){ out.print(siteArray[t] + "<br />"); } %>
</div>
<br />
<div align="left">
<%
	for(int k = 0; k < siteArray.length; k++){
		GoogleSearch search = new GoogleSearch();
		// Set mandatory attributes
		search.setKey("8Gwa9TtQFHJsqnNDBYjooh1QO4a3oPQh");
		search.setQueryString(query + " site:" + siteArray[k]);
		// Set optional attributes
		search.setSafeSearch(true);
		// Invoke the actual search
		GoogleSearchResult result = search.doSearch();
		// process the result
		// put results into an array
		GoogleSearchResultElement[] resultArray = result.getResultElements();
%>
<table>
	<tr>
		<td>Domain <a href="<% out.println(siteArray[k]); %>"><% out.println(siteArray[k]); %></a></td>
	</tr>
	<tr>
		<td>
			<ol>
				<%	for(int i = 0; i < resultArray.length; i++){	%>
				<li><a href="<% out.print(resultArray[i].getURL()); %>" target="_blank"><% out.print(resultArray[i].getTitle()); %></a></li>
				<% 	out.print(resultArray[i].getSnippet());  %><br /> <br />
				<%	}	%>			
			</ol>
		</td>
	</tr>
	<tr>
		<td> </td>
	</tr>
</table>
<%	}	// end internal for
	}	// end if
	else if(queryNumWords > 5){
%>
<p><strong>Error!</strong> The query, "<% out.print(query); %>", is longer than 5 words and must be revised to be 5 words or less.</p>
<%	}	// end else if	
	else if(queryLength == 0){	%>
<p><strong>Error!</strong> The query is empty. Please enter in a valid query (not empty and up to 5 words) and try again.</p>
<%	}	// end else if	%>
</div>
</div>
<div align="center">
<table class="footer">
	<tr>
		<td>
			INFO 340: Database Management and Information Retrieval<br />
			Assignment 1: Developing a Search Interface with the Google API<br />
			Completed for David Hendry on April 12, 2004<br />
			Assignment By: Ryan Prins (<a href="mailto:rprins@u.washington.edu">rprins at u dot washington dot edu</a>)<br />
			Informatics, University of Washington - Spring Quarter 2004<br />
			<a href="http://serv46.ischool.washington.edu:8180/rprins/assignment1/">http://serv46.ischool.washington.edu:8180/rprins/assignment1/</a><br />
			Links to <a href="./googlesearch.jsp.source">JSP Source</a> and <a href="./index.html.source">HTML</a> and <a href="./googlesearch.css">CSS</a><br />
			Credits: <a href="http://www.google.com/apis/" target="_blank">google.com API</a>, <a href="http://www.eclipse.org" target="_blank">eclipse.org</a> (IDE), <a href="http://jakarta.apache.org/tomcat/" target="_blank">Tomcat</a><br />
			Project description can be found <a href="./readme.html">here</a>.
		</td>
	</tr>
</table>
</div>
</body>
</html>