<%@ page import = "java.sql.*, javax.servlet.*, javax.servlet.http.*, java.io.*" %>
<html>
<head>
<title>The Partonopeus de Blois project</title>
<script language="javascript">
<%
// 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 go_ms = request.getParameter("go_ms");
if (go_ms == null) { go_ms = "A"; }
if (go_ms.length() == 0) { go_ms = "A"; }
String go_unit = request.getParameter("go_unit");
if (go_unit == null) { go_unit = "meta"; }
if (go_unit.length() == 0) { go_unit = "meta"; }
String go_value = request.getParameter("go_value");
if (go_value == null) { go_value = "1"; }
if (go_value.length() == 0) { go_value = "1"; }
	
String meta = request.getParameter("meta");
if (meta == null) { meta = ""; }
String ms = request.getParameter("ms");
if (ms == null) { ms = ""; }

String collate = request.getParameter("collate.x");

String url = "?a=" + a + "&amp;b=" + b + "&amp;g=" + g + "&amp;l=" + l + "&amp;p=" + p + "&amp;t=" + t + "&amp;v=" + v + "&amp;c=" + c + "&amp;f=" + f + "&amp;x=" + x + "&amp;go_ms=" + go_ms + "&amp;go_unit=" + go_unit + "&amp;go_value=" + go_value + "&amp;meta=" + meta + "&amp;ms=" + ms + "&amp;collate.x=" + collate;

if (collate != null) {
	%>
	di_string = "&amp;a=<%=a%>&amp;b=<%=b%>&amp;g=<%=g%>&amp;l=<%=l%>&amp;p=<%=p%>&amp;t=<%=t%>&amp;v=<%=v%>&amp;c=<%=c%>&amp;f=<%=f%>&amp;x=<%=x%>&amp;meta=<%=meta%>&amp;ms=<%=ms%>&amp;collate.x=<%=collate%>";
	<%
} else {
	%>
	di_string = "&amp;<%=go_ms.toLowerCase()%>=1&amp;meta=<%=meta%>&amp;ms=<%=ms%>&amp;collate.x=<%=collate%>";
	<%
}

%>
</script>
</head>
<%
String full = request.getParameter("full");
String episode = request.getParameter("episode.x");
String print = request.getParameter("print.x");
if (full != null) { 
	// If the 'full' variable is set we do something completely different.
	%>
	<frameset cols="110px,*" frameborder="0" border="0">
		<frame name="side" src="http://www.hrionline.ac.uk/partonopeus/sidebar.html" marginheight="5" marginwidth="5" scrolling="no" noresize="noresize">
		<frameset rows="65px,*" frameborder="0" border="0">
			<frame name="top" src="http://www.hrionline.ac.uk/db/partonopeus/part_toolbar.jsp<%=url%>" scrolling="no" marginwidth="5" marginheight="5" noresize="noresize">
			<frame name="main" src="http://www.hrionline.ac.uk/db/partonopeus/part_texts.jsp<%=url%>" marginwidth="20" marginheight="10" noresize="noresize">
		</frameset>
	</frameset>
	<%
} else if (episode != null) {
	// We do something different if the 'episode' variable is set.
	%>
	<frameset frameborder="1" cols="100%">
	<frame name="one" src="http://www.hrionline.ac.uk/db/partonopeus/episodes.jsp<%=url%>">
	</frameset>
	<%
} else if (print != null) {
	// We do something different if the 'print' variable is set.
	%>
	<frameset frameborder="1" cols="100%">
	<frame name="one" src="http://www.hrionline.ac.uk/db/partonopeus/print.jsp<%=url%>">
	</frameset>
	<%
} else {
	// If there's no 'full' or 'episode' variable we just try and display the document frames as normal

	// Set up the variables which determine which documents we retrieve
	String di = "";
	String typecode = "";
	String folio = "";
	// Now we give these variables appropriate values using the info passed to the page.

	if (meta.equals("1")) { typecode = "m"; } else { typecode = "p"; }
	if (ms.equals("1")) { typecode = typecode + "c"; } else { typecode = typecode + "a"; }
	
	// We decide how to value di according to whether the 'go' button was clicked
	if (collate == null) {
		di = go_ms;
	} else {
		if (a.equals("1")) { di = di + "A"; }
		if (b.equals("1")) { di = di + "B"; }
		if (g.equals("1")) { di = di + "G"; }
		if (l.equals("1")) { di = di + "L"; }
		if (p.equals("1")) { di = di + "P"; }
		if (t.equals("1")) { di = di + "T"; }
		if (v.equals("1")) { di = di + "V"; }
		if (c.equals("1")) { di = di + "C"; }
		if (f.equals("1")) { di = di + "F"; }
		if (x.equals("1")) { di = di + "X"; }
	}

	// 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 a metaline number, by hook or by crook. Ahem.
	int target_meta = 0;
	if (go_unit.equals("episode_value")) {
		ResultSet RS = Stmt.executeQuery("SELECT meta FROM episodes WHERE value = \"" + go_value + "\"");
		if (RS.next()) { target_meta = RS.getInt("meta"); }
	} else if (go_unit.equals("subdoctype_2") || go_unit.equals("subdoctype_3") || go_unit.equals("number")) {
		// Start by adding quotes to the go_value if it's going to query a string field
		if (go_unit != "number") { go_value = "\"" + go_value + "\""; }
		ResultSet RS = Stmt.executeQuery("SELECT meta FROM m_lines WHERE " + go_unit + " = " + go_value + "AND subdoctype_1 = \"" + go_ms + "\" ORDER BY meta LIMIT 1;");
		if (RS.next()) { target_meta = RS.getInt("meta"); }
	} else if (go_unit.equals("meta_string")) {
		target_meta = Integer.parseInt(go_value);
	}
	// Now, if we have a sensible target_meta. we go about getting the folio details for each ms we are going to display.
	if (target_meta > 0) { 
		// So, we now know the target metaline. Now for each manuscript we are going to retrieve, we need to know the appropriate folio for that metaline
		String folio_1 = "blank";
		String msline_1 = "blank";
		String folio_2 = "blank";
		String msline_2 = "blank";
		String folio_3 = "blank";
		String msline_3 = "blank";
		String folio_4 = "blank";
		String msline_4 = "blank";
	
		// The following queries have been configured to return the nearest folio and *ms* line to the requested metaline, so there's some slack where the exact same metaline is not present in a certain ms. This might not be desired behaviour - need to check with the project team.
		if (di.length() > 0) {
			ResultSet RS = Stmt.executeQuery("SELECT meta_string, subdoctype_2 FROM m_lines WHERE subdoctype_1 = \"" + di.substring(0,1) + "\" AND meta >= " + target_meta + " ORDER BY meta LIMIT 1;");
			if (RS.next()) { 
				folio_1 = RS.getString("subdoctype_2");
				msline_1 = RS.getString("meta_string");
			}
		}
		if (di.length() > 1) {
			ResultSet RS = Stmt.executeQuery("SELECT meta_string, subdoctype_2 FROM m_lines WHERE subdoctype_1 = \"" + di.substring(1,2) + "\" AND meta >= " + target_meta + " ORDER BY meta LIMIT 1;");
			if (RS.next()) { 
				folio_2 = RS.getString("subdoctype_2");
				msline_2 = RS.getString("meta_string");
			}
		}
		if (di.length() > 2) {
			ResultSet RS = Stmt.executeQuery("SELECT meta_string, subdoctype_2 FROM m_lines WHERE subdoctype_1 = \"" + di.substring(2,3) + "\" AND meta >= " + target_meta + " ORDER BY meta LIMIT 1;");
			if (RS.next()) { 
				folio_3 = RS.getString("subdoctype_2");
				msline_3 = RS.getString("meta_string");
			}
		}
		if (di.length() > 3) {
			ResultSet RS = Stmt.executeQuery("SELECT meta_string, subdoctype_2 FROM m_lines WHERE subdoctype_1 = \"" + di.substring(3,4) + "\" AND meta >= " + target_meta + " ORDER BY meta LIMIT 1;");
			if (RS.next()) { 
				folio_4 = RS.getString("subdoctype_2");
				msline_4 = RS.getString("meta_string");
			}
		}
		// if we're in meta mode, all the mslines can be the target_meta, right?
		if (meta.equals("1")) { 
			msline_1 = "" + target_meta;
			msline_2 = "" + target_meta;
			msline_3 = "" + target_meta;
			msline_4 = "" + target_meta;
		}
		// Now we write out the appropriate frameset, easy!
		if (di.length() == 0) {
			%>
			<frameset frameborder="1" cols="100%">
			<frame name="one" src="http://www.hrionline.ac.uk/partonopeus/blank.html">
			</frameset>
			<%
		} else if (di.length() == 1) {
			%>
			<frameset frameborder=\"1\" cols=\"100%\">
			<frame name="one" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(0,1)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			</frameset>
			<%
		} else if (di.length() == 2) {
			%>
			<frameset frameborder="1" cols="50%,50%">
			<frame name="one" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(0,1)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			<frame name="two" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(1,2)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			</frameset>
			<%
		} else if (di.length() == 3) {
			%>
			<frameset frameborder="1" rows="50%,50%" cols="50%,50%">
			<frame name="one" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(0,1)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			<frame name="two" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(1,2)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			<frame name="three" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(2,3)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			<frame name="four" src="http://www.hrionline.ac.uk/partonopeus/blank.html">
			</frameset>
			<%
		} else if (di.length() == 4) {
			%>
			<frameset frameborder="1" rows="50%,50%" cols="50%,50%">
			<frame name="one" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(0,1)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			<frame name="two" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(1,2)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			<frame name="three" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(2,3)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			<frame name="four" src="http://www.hrionline.ac.uk/db/partonopeus/part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di.substring(3,4)%>&amp;l=<%=msline_1%>#l<%=msline_1%>">
			</frameset>
			<%
		} else {
			%>
			<body>
			<b>No manuscripts have been selected for display.</b>
			</body>
	 		<%
		}
	} else {
		// If we didn't get a metaline earler, this is what we print:
	 	%>
	 	<body bgcolor="#FAF3D5">
	 	<b>Manuscript <%=go_ms%> has no line which matches the specified criteria</b>
	 	</body>
	 	<%
	}
	 
}
%>
<noframes>This website currently requires frames to enable concurrent views of multiple manuscripts</noframes>
</html>