<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page language="java"
	import="com.myeclipseide.batch.PayrollInputRecord"%>
<%@ page language="java" import="com.myeclipseide.batch.PayrollRecord"%>
<%@ page language="java"
	import="com.myeclipseide.batch.SampleDataHolderBean"%>
<%@ page language="java"
	import="com.myeclipseide.batch.PayRollConstants"%>
<%@ page import="javax.naming.*,javax.rmi.PortableRemoteObject"%>
<%!//declare a "global" reference to an instance of the home interface of the session bean
	SampleDataHolderBean initializer = null;

	public void jspInit() {
		//obtain an instance of the home interface
		try {
			InitialContext cntxt = new InitialContext();
			Object ref = cntxt.lookup(PayRollConstants.BEAN_LOOKUP_NAME);
			initializer = (SampleDataHolderBean) PortableRemoteObject.narrow(
					ref, SampleDataHolderBean.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}%>



<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>BatchExample JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body marginwidth="100">
	<center>
		<h1>Payroll Form</h1>
	</center>
	<hr noshade="noshade" />
	When this application is run, the input data is read from the CSV file and populated on the user interface by the by the DataHolder bean
	(see class <strong>SampleDataHolderBean.java</strong>). Pressing the <i>Calculate Payroll</i> button, initiates the Java EE 7 batch process (as configured on the /META-INF/batch-jobs/PayrollJob.xml file) which runs through the following set of steps:
	<ul>
	<li>ItemReader (see class <strong>SimpleItemReader.java</strong>) reads the PayrollInput object and passes it to the processor for processing
	<li>ItemProcessor (see class <strong>SimpleItemProcessor.java</strong>)  processes the data and calculates taxes and other details for the given ID and month</li>
	<li>ItemWrtiter (see class <strong>SimpleItemWriter.java</strong>) collects the processed data from the processor and sends it back to the bean for display and upload to database.</li> 
	</ul>
	<hr noshade="noshade" />
	<center>
		<form name="monthYearForm">
			<h2>Data from CSV</h2>
			<table border="yes">
				<tr>
					<td colspan="2">Input Records For <strong><select
							name="inputMonthYear">
								<%
									initializer.onApplicationStartup();
									String[] monthYear = initializer.getAllMonthYear();
									String selectedMonthYear = (String) request
											.getParameter("inputMonthYear") == null ? "JAN-2013"
											: (String) request.getParameter("inputMonthYear");
									for (String monYr : monthYear) {
										out.println("<option "
												+ (monYr.equals(selectedMonthYear) ? "Selected" : " ")
												+ " value=\"" + monYr + "\">" + monYr + "</option>");
									}
								%>
						</select></strong> <input type=hidden name="inputMonthYear" value=selectedMonthYear>
						<input type=submit name=DisplayInputRecords
						value="Display Input Records"></td>
				</tr>
				<tr>
					<td><strong>MONTH-YEAR</strong></td>
					<td><strong>EMP ID</strong></td>
					<td><strong>Base Salary</strong></td>
					
				</tr>
				<%
					/* String selectedMonthYear = request.getParameter("inputMonthYear"); */
					if (selectedMonthYear == null) {
						return;
					}
					for (PayrollInputRecord r : initializer
							.getPayrollInputRecords(selectedMonthYear)) {
						out.println("<tr><td>" + selectedMonthYear + "</td>" + "<td>"
								+ r.getId() + "</td>" + "<td>" + r.getBaseSalary()
								+ "</td></tr>");
					}
				%>

				<tr>
					<td colspan="2"><input type="submit" name="submitAction"
						value="Calculate Payroll"
						onclick="
					document.forms[0].action='calcpayroll' ; return true;"></td>
				</tr>
				<%
					if (request.getAttribute(PayRollConstants.SUCCESS) != null) {
						out.println("<div>");
						// 			out.println("<form>");
						out.println("<table>");
						out.println("<tr><td><h2>Processed Payroll Records</h2></td></tr>");
						out.println("<table border=\"yes\">");
						out.println("<tr><td><strong>MONTH-YEAR</strong></td><td><strong>EMP ID</strong></td>"
								+ "<td><strong>Base Salary</strong></td><td><strong>Bonus</strong></td><td><strong>Tax</strong></td>"
								+ "<td><strong>Net</strong></td>" + "</tr>");
						for (PayrollRecord r : initializer
								.getPayrollRecords(selectedMonthYear)) {
							out.println("<tr><td>" + r.getMonthYear() + "</td>");
							out.println("<td>" + r.getEmpId() + "</td>");
							out.println("<td>" + r.getBase() + "</td>");
							out.println("<td>" + r.getBonus() + "</td>");
							out.println("<td>" + r.getTax() + "</td>");
							out.println("<td>" + r.getNet() + "</td></tr>");
						}
						out.println("</table>");
						out.println("<tr>");
						out.println("<td colspan=\"2\">");
						out.println("<input type=\"submit\" name=\"submitAction\" value=\"Upload\" onclick=\"document.forms[0].action='calcpayroll' ; return true;\">");
						out.println("</td>");
						out.println("</tr>");
						out.println("</table>");
						// 			out.println("</form>");
						out.println("</div>");
					}
				%>
			</table>
			<%
			if (PayRollConstants.SUCCESS.equals(request
					.getParameter(PayRollConstants.STATUS))) {
				out.println("<center><h2>Data uploaded to database sucessfully!</h2></center>");
			}
		%>
		</form>
	</center>
</body>
</html>
