| 
									
										
										
										
											2016-12-20 06:42:23 +00:00
										 |  |  | <!DOCTYPE html> | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | <html> | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  | 	<title>Wordsearch builder</title> | 
					
						
							| 
									
										
										
										
											2016-12-19 15:44:06 +00:00
										 |  |  | 	<link rel="stylesheet" type="text/css" href="base.css" /> | 
					
						
							| 
									
										
										
										
											2020-04-05 13:26:37 +01:00
										 |  |  | 	<link href="https://fonts.googleapis.com/css2?family=Fira+Mono:wght@400;500&family=Roboto:wght@500&display=swap" rel="stylesheet"> | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | </head> | 
					
						
							|  |  |  | <%@ page import="uk.org.mafoo.wordsearch.*" %> | 
					
						
							|  |  |  | <%@ page import="java.util.*" %> | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | <%@ page import="org.apache.commons.lang.StringUtils" %> | 
					
						
							| 
									
										
										
										
											2016-12-19 12:28:56 +00:00
										 |  |  | <%@ page import="org.apache.commons.lang.StringEscapeUtils" %> | 
					
						
							| 
									
										
										
										
											2020-05-29 13:50:41 +01:00
										 |  |  | <%@ page import="java.sql.*" %> | 
					
						
							|  |  |  | <%@ page import="org.sqlite.*" %> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-19 14:26:25 +00:00
										 |  |  | <%@ page errorPage="error.jsp" %> | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | <% | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int height = Integer.parseInt(request.getParameter("height")); | 
					
						
							|  |  |  | 	int width  = Integer.parseInt(request.getParameter("width")); | 
					
						
							| 
									
										
										
										
											2016-12-19 12:28:56 +00:00
										 |  |  | 	boolean simple = request.getParameter("simple") != null; | 
					
						
							|  |  |  | 	String name = StringEscapeUtils.escapeHtml(request.getParameter("name")); | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (request.getParameter("words").length() > 2048) { throw new Exception("Input too large"); } | 
					
						
							| 
									
										
										
										
											2017-10-08 21:53:08 +01:00
										 |  |  | 	if (height > 100 || width > 100) { throw new Exception("Dimensions too large"); } | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	List<String> words = new ArrayList<String>(); | 
					
						
							|  |  |  | 	for ( String line : request.getParameter("words").split("\r\n")) { | 
					
						
							| 
									
										
										
										
											2020-04-05 13:26:37 +01:00
										 |  |  | 		words.add(line.trim().toLowerCase()); | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-05-29 13:50:41 +01:00
										 |  |  | 	Collections.sort(words); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Connection conn = DriverManager.getConnection( | 
					
						
							| 
									
										
										
										
											2020-05-29 14:10:58 +01:00
										 |  |  | 		"jdbc:sqlite:" + getServletContext().getRealPath("/WEB-INF/files/database.sqlite")); | 
					
						
							| 
									
										
										
										
											2020-05-29 13:50:41 +01:00
										 |  |  | 	PreparedStatement stmt = conn.prepareStatement( | 
					
						
							|  |  |  | 		"INSERT INTO grids (remotehost, input, size_x, size_y, simple) VALUES (?, ?, ?, ?, ?)", | 
					
						
							|  |  |  | 		Statement.RETURN_GENERATED_KEYS); | 
					
						
							|  |  |  | 	// PreparedStatement getlastid = conn.prepareStatement("select last_insert_rowid();"); | 
					
						
							|  |  |  | 	PreparedStatement stmt2 = conn.prepareStatement("UPDATE grids SET result=? WHERE id=?"); | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 13:50:41 +01:00
										 |  |  | 	stmt.setString(1, request.getRemoteHost()); | 
					
						
							|  |  |  | 	stmt.setString(2, words.toString()); | 
					
						
							|  |  |  | 	stmt.setInt(3, height); | 
					
						
							|  |  |  | 	stmt.setInt(4, width); | 
					
						
							|  |  |  | 	stmt.setBoolean(5, simple); | 
					
						
							|  |  |  | 	stmt.executeUpdate(); | 
					
						
							|  |  |  | 	ResultSet record_id_rs = stmt.getGeneratedKeys(); | 
					
						
							|  |  |  | 	int record_id = -1; | 
					
						
							|  |  |  | 	if(record_id_rs.next()){ | 
					
						
							|  |  |  | 		record_id = record_id_rs.getInt(1); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 13:50:41 +01:00
										 |  |  | 	// Actually generate the grid | 
					
						
							|  |  |  | 	char[][] grid = GridFactory.makeGrid(words, height, width, simple); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	StringBuilder _csv = new StringBuilder(); | 
					
						
							|  |  |  | 	for (char[] cs : grid) { | 
					
						
							|  |  |  | 		for(char c : cs) { | 
					
						
							|  |  |  | 			_csv.append(c); | 
					
						
							|  |  |  | 			_csv.append(","); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		_csv.append("\r\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String csv = _csv.toString(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	stmt2.setString(1, csv); | 
					
						
							|  |  |  | 	stmt2.setInt(2, record_id); | 
					
						
							|  |  |  | 	stmt2.executeUpdate(); | 
					
						
							|  |  |  | 	conn.close(); | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | %> | 
					
						
							|  |  |  | <body> | 
					
						
							| 
									
										
										
										
											2016-12-19 12:28:56 +00:00
										 |  |  | <h1><%= name %></h1> | 
					
						
							| 
									
										
										
										
											2016-12-19 16:03:38 +00:00
										 |  |  | <div class="noprint"> | 
					
						
							| 
									
										
										
										
											2020-05-29 13:50:41 +01:00
										 |  |  | 	[ <a href="index.jsp">Start again</a> | <a id='csvdownload' href="data:text/csv;base64,<%= Base64.getEncoder().encodeToString(csv.getBytes()) %>">Download CSV</a> ] | 
					
						
							| 
									
										
										
										
											2016-12-19 16:03:38 +00:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2020-05-29 13:50:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-18 07:54:41 +00:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | <div id="wrapper"> | 
					
						
							|  |  |  | <div id="wordsearch"> | 
					
						
							|  |  |  | 	<h2>Grid</h2> | 
					
						
							|  |  |  | 	<table id="grid"> | 
					
						
							|  |  |  | 	<% for(char[] row : grid) { %> | 
					
						
							|  |  |  | 		<tr> | 
					
						
							| 
									
										
										
										
											2020-04-05 13:26:37 +01:00
										 |  |  | 			<% for(char c : row) { | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | 				csv += "" + c + ','; | 
					
						
							|  |  |  | 			%> | 
					
						
							| 
									
										
										
										
											2016-12-19 16:03:38 +00:00
										 |  |  | 			<td class="cell"><%= c %></td> | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | 			<% 	} %> | 
					
						
							|  |  |  | 		</tr> | 
					
						
							| 
									
										
										
										
											2020-04-05 13:26:37 +01:00
										 |  |  | 	<% | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | 		csv += "\\n"; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	%> | 
					
						
							|  |  |  | 	</table> | 
					
						
							| 
									
										
										
										
											2020-04-05 13:26:37 +01:00
										 |  |  | </div> <!-- end wordsearch --> | 
					
						
							| 
									
										
										
										
											2016-12-18 07:54:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-18 20:55:35 +00:00
										 |  |  | <div id="words"> | 
					
						
							|  |  |  | 	<h2>Words</h2> | 
					
						
							|  |  |  | 		<ul> | 
					
						
							|  |  |  | 		<% for (String word : words) { %> | 
					
						
							|  |  |  | 		   <li><%= word.trim() %></li> | 
					
						
							|  |  |  | 		<% } %> | 
					
						
							|  |  |  | 		</ul> | 
					
						
							|  |  |  | </div> <!-- end words --> | 
					
						
							| 
									
										
										
										
											2020-04-05 13:26:37 +01:00
										 |  |  | </div> <!-- end wrapper --> | 
					
						
							| 
									
										
										
										
											2016-12-22 07:52:15 +00:00
										 |  |  | <br /> | 
					
						
							|  |  |  | <br /> | 
					
						
							|  |  |  | <%@include file="/WEB-INF/jspf/footer.jspf" %> | 
					
						
							| 
									
										
										
										
											2016-12-18 07:43:01 +00:00
										 |  |  | </body> | 
					
						
							|  |  |  | </html> |