<%@ page import = "java.sql.*, javax.servlet.*, javax.servlet.http.*, java.io.*, org.apache.lucene.analysis.*, org.apache.lucene.document.*, org.apache.lucene.index.*, org.apache.lucene.search.*, org.apache.lucene.search.highlight.*, org.apache.lucene.queryParser.*, org.apache.lucene.demo.*, org.apache.lucene.demo.html.Entities, org.apache.lucene.analysis.standard.StandardAnalyzer" %>

<html>
<head>
<title>The Partonopeus de Blois project</title>
</head>
<body>
<%
// Get all the variables submitted to the page, and in some cases make sure they're not null
String a = request.getParameter("a");
if (a == null) { a = ""; }
String b = request.getParameter("b");
if (b == null) { b = ""; }
String g = request.getParameter("g");
if (g == null) { g = ""; }
String l = request.getParameter("l");
if (l == null) { l = ""; }
String p = request.getParameter("p");
if (p == null) { p = ""; }
String t = request.getParameter("t");
if (t == null) { t = ""; }
String v = request.getParameter("v");
if (v == null) { v = ""; }
String c = request.getParameter("c");
if (c == null) { c = ""; }
String f = request.getParameter("f");
if (f == null) { f = ""; }
String x = request.getParameter("x");
if (x == null) { x = ""; }

String di = "";
String di_db = "( subdoctype_1 = \"\"";
if (a.equals("1")) { di = di + "A"; di_db = di_db + " OR subdoctype_1 = \"A\""; }
if (b.equals("1")) { di = di + "B"; di_db = di_db + " OR subdoctype_1 = \"B\""; }
if (c.equals("1")) { di = di + "C"; di_db = di_db + " OR subdoctype_1 = \"C\""; }
if (f.equals("1")) { di = di + "F"; di_db = di_db + " OR subdoctype_1 = \"F\""; }
if (g.equals("1")) { di = di + "G"; di_db = di_db + " OR subdoctype_1 = \"G\""; }
if (l.equals("1")) { di = di + "L"; di_db = di_db + " OR subdoctype_1 = \"L\""; }
if (p.equals("1")) { di = di + "P"; di_db = di_db + " OR subdoctype_1 = \"P\""; }
if (t.equals("1")) { di = di + "T"; di_db = di_db + " OR subdoctype_1 = \"T\""; }
if (v.equals("1")) { di = di + "V"; di_db = di_db + " OR subdoctype_1 = \"V\""; }
if (x.equals("1")) { di = di + "X"; di_db = di_db + " OR subdoctype_1 = \"X\""; }
di_db = di_db + ")";

int sml = 1;
String start_ml = request.getParameter("start_ml");
if (start_ml == null) { start_ml = ""; }
try { sml = Integer.parseInt(start_ml); } catch (Exception e) { sml = 1; }

int eml = 200;
String end_ml = request.getParameter("end_ml");
if (end_ml == null) { end_ml = ""; }
try { eml = Integer.parseInt(end_ml); } catch (Exception e) { eml = 1; }

boolean go = true;

if ((eml - sml) > 200) {
	eml = sml + 200;
	%><b>Sorry, only 200 lines can be formatted for printing at a time. This printout therefore ends at line <%=eml%>.</b><br/><br/><%	
}
if (eml < sml) {
	%><b>The requested ending metaline was lower than the starting metaline.</b><br/><br/><%
	go = false;
}
if (di.length() > 4 || di.length() == 0) {
	%><b>Sorry, Only one to four manuscripts can be displayed at one time in printable format.</b><br/><br/><%
	go = false;
}

if (go) {
// First, we print the details of what's being printed
%>
<b>Metalines <%=sml%> to <%=eml%>.</b>
<br/><br/>
<%
	
// Here we have to do some database meddling
// First we just set up the database
try {
	Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception E) {
	out.println("Unable to load driver.");
	E.printStackTrace(new PrintWriter(out));
}
Connection Conn = null;
try {
	Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/part?user=part&password=blackout&DBname=part");
}
catch (SQLException E) {
	out.println("SQLException: " + E.getMessage());
	out.println("SQLState:     " + E.getSQLState());
	out.println("VendorError:  " + E.getErrorCode());
}
Statement Stmt = Conn.createStatement();
// Now, let's get our lines
ResultSet RS = Stmt.executeQuery("SELECT meta_string, subdoctype_1, print_html, notes_html FROM m_lines WHERE meta >= " + sml + " AND meta <= " + eml + " AND " + di_db + " ORDER BY meta, meta_string, subdoctype_1;");
// Now, a big and clever loop does the rest
String current_meta = "" + sml;
int di_pos = 0;
String[] notes = new String[100];
int notecode = 0;
%>
<table>
<th width="10%">&#160;</th>
<%
for (int i=0; i < di.length(); i++) {
	%><th><%=di.substring(i,i + 1)%></th><%
}
%>
<tr><td>&#160;<%=current_meta%></td>
<%
while (RS.next()) {
	String ms = RS.getString("subdoctype_1");
	String print_html = RS.getString("print_html");
	String notes_html = RS.getString("notes_html");
	String meta_string = RS.getString("meta_string");
			
	boolean cycling = true;
	while (cycling) {
		if (di_pos >= di.length()) { di_pos = 0; }
		String expected_ms = di.substring(di_pos, di_pos + 1);
			
		if (ms.equals(expected_ms) && meta_string.equals(current_meta)) {
			%><td><%=print_html%><%
			if (notes_html.length() > 0) {
				notes[notecode] = notes_html;
				%>&#160;<sup><b><%=(notecode + 1)%></b></sup><%
				notecode++;
			}
			%>
			</td>
			<%
			
			cycling = false;
			di_pos++;
		} else {
			if (meta_string.equals(current_meta) ? false : true) {
				if (di_pos < di.length() && di_pos > 0) {
					for (int i = 0; i < (di.length() - di_pos); i++) {
					%><td>&#160;</td><%
					}
				}
				String last_meta = current_meta;
				current_meta = meta_string;
				di_pos = 0;
				%></tr>
				<%
				// Now we need a few bits of code to decide how to display
				// the start of the next line
				try {
					int lm = Integer.parseInt(last_meta);
					int cm = Integer.parseInt(current_meta);
					for (int i = 0; i < ((cm - lm) - 1); i++) {
						%><tr><td>&#160;<%
						if (((lm + i + 1) % 5) == 0) {
							%><%=(lm + i + 1)%><%
						}
						%></td></tr><%
					}
				} catch (Exception e) {
					// if they're not int-able we just wont bother	
				}
				%><tr><td>&#160;<%
				try {
					int cm = Integer.parseInt(current_meta);
					if ((cm % 5) == 0) {
						%><%=cm%><%
					}
				} catch (Exception e) {
					// if it's not int-able we'd better print it anyway
					%><%=current_meta%><%
				}
				%></td><%
			} else {
				if (ms.equals(expected_ms) ? false : true) {
				%><td>&#160;</td><%
					di_pos++;
				}
			}
		}
	}
}
%>
</tr>
</table>
<br/><br/>
<%
for (int i=0; i < notecode; i++) {
	%><sup><b><%=(i + 1)%></b></sup>&#160;<%=notes[i]%><br/><%
}
}
%>
</body>
</html>