<%@ page language="java" import="java.awt.image.*,java.util.*,java.sql.*,java.beans.*,iDSS.*,iDSS.utils.*,iDSS.webBeans.*,iDSS.beans.*,iDSS.beans2.*,iDSS.grids.*,iDSS.tidalData.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
	<TITLE><%=IDSSAppConstants.programTitle%></TITLE>

<META HTTP-EQUIV="KEYWORDS" CONTENT="internet-based, DSS, decision support system, Java, Enterprise edition, J2EE">
<META HTTP-EQUIV="DESCRIPTION" CONTENT="">		
</HEAD>

<jsp:useBean id="gridManagerJBean" scope="session" class="iDSS.beans2.GridManagerJBean"/>
<jsp:useBean id="modelSetupControllerJBean" scope="session" class="iDSS.beans2.ModelSetupControllerJBean"/>
<jsp:useBean id="statusJBean" scope="session" class="iDSS.beans2.StatusJBean"/>
<jsp:useBean id="tidalStationsLocalJBean" scope="session" class="iDSS.beans2.TidalStationsLocalJBean"/>
<jsp:useBean id="dataManagerJBean" scope="session" class="iDSS.beans2.DataManagerJBean"/>
<jsp:useBean id="tidalModelsListJBean" scope="session" class="iDSS.beans2.TidalModelsListJBean"/>


<body>
<h2><%=IDSSAppConstants.dataManager%></h2>



<%

    String actionButtonName = request.getParameter(IDSSAppConstants.doItButtonText);

    gridManagerJBean.removePropertyChangeListener(modelSetupControllerJBean);
    gridManagerJBean.addPropertyChangeListener(modelSetupControllerJBean);
    statusJBean = modelSetupControllerJBean.getStatusBean();
    tidalStationsLocalJBean.removePropertyChangeListener(dataManagerJBean);
    tidalStationsLocalJBean.addPropertyChangeListener(dataManagerJBean);
    dataManagerJBean.addPropertyChangeListener(tidalModelsListJBean);


    if(actionButtonName != null) {

%>
        <p>The button you clicked is <%=actionButtonName%>. </p>
<%


        if (actionButtonName.equals(IDSSAppConstants.doItButtonText)) {

            String[] sqlStrings = request.getParameterValues(IDSSAppConstants.doItButtonCommand);
            String processType = request.getParameter("processType");

            if(sqlStrings != null) {

                if(sqlStrings.length > 0) {

                    String sqlString = sqlStrings[0];

                    int startYear = Integer.parseInt(request.getParameter("startYear"));
                    int startMonth = Integer.parseInt(request.getParameter("startMonth"))-1;
                    int startDay = Integer.parseInt(request.getParameter("startDay"));
                    int startHour = Integer.parseInt(request.getParameter("startHour"));
                    int startMinute = Integer.parseInt(request.getParameter("startMinute"));
                    int startSecond = Integer.parseInt(request.getParameter("startSecond"));

                    Calendar startCal = Calendar.getInstance();
                    startCal.set(startYear,startMonth,startDay,startHour,startMinute,startSecond);
                    Timestamp startTimestamp = new Timestamp(startCal.getTime().getTime());

                    int endYear = Integer.parseInt(request.getParameter("endYear"));
                    int endMonth = Integer.parseInt(request.getParameter("endMonth"))-1;
                    int endDay = Integer.parseInt(request.getParameter("endDay"));
                    int endHour = Integer.parseInt(request.getParameter("endHour"));
                    int endMinute = Integer.parseInt(request.getParameter("endMinute"));
                    int endSecond = Integer.parseInt(request.getParameter("endSecond"));

                    Calendar endCal = Calendar.getInstance();
                    endCal.set(endYear,endMonth,endDay,endHour,endMinute,endSecond);
                    Timestamp endTimestamp = new Timestamp(endCal.getTime().getTime());

                    String superimposeGraph = request.getParameter("superimposeGraph");
                    int numberOfIterations = Integer.parseInt(request.getParameter("numberOfIterations"));
/**
                    %>
                        <P>sqlString : <%=sqlString%></P>
                        <P>startTimestamp : <%=startTimestamp.toString()%></P>
                        <P>endTimestamp : <%=endTimestamp.toString()%></P>
                        <P>processType : <%=processType%></P>
                        <P>superimposeGraph : <%=superimposeGraph%></P>
                        <P>numberOfIterations : <%=numberOfIterations%></P>
                    <%
**/


                    if(startTimestamp.before(endTimestamp)) {

                        if(processType.equals(IDSSAppConstants.makeTable)) {

                            Vector v = dataManagerJBean.getTableRowsVector(sqlString,startTimestamp,endTimestamp);
                            request.setAttribute(IDSSAppConstants.tableVector,v);

                            %>
                                <jsp:forward page="../dataDisp/makeTable.jsp"/>
                            <%

                        } //if(processType.equals(IDSSAppConstants.makeTable))

                        else if(processType.equals(IDSSAppConstants.drawGraphImg)) {

                            //Vector v = dataManagerJBean.getXYGraphImg(sqlString,sqlString,startTimestamp,endTimestamp,superimposeGraph);
                            BufferedImage img = dataManagerJBean.getXYGraphImg2(sqlString,sqlString,startTimestamp,endTimestamp,superimposeGraph);

                            //String imgSrc = (String)v.elementAt(0);
                            //String imgWidth = ((Integer)v.elementAt(1)).toString();
                            //String imgHeight = ((Integer)v.elementAt(2)).toString();
                            //String imgAltText = (String)v.elementAt(3);

                            request.setAttribute(IDSSAppConstants.image,img);

                            %>
                                <jsp:forward page="../imgDisp/drawImg.jsp"/>
                            <%


                        } //if(processType.equals(IDSSAppConstants.drawGraphImg))


                        else if(processType.equals(IDSSAppConstants.deviseTidalModel)) {

                            Vector v = dataManagerJBean.makeTidalModel(
                                                sqlString,startTimestamp,
                                                endTimestamp,numberOfIterations);

                        } //if(processType.equals(IDSSAppConstants.deviseTidalModel))

                    } //if(startTimestamp.before(endTimestamp))

                } //if(sqlStrings.length > 0)

            } //if(sqlStrings != null)

        } //if (actionButtonName.equals(IDSSAppConstants.doItButtonText))






    } //if(actionButtonName != null)
%>








<form method=get>


    <table width="100%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">
        <tr BGCOLOR="#C0C0C0">
            <th width="5%">Sel</th>
            <th width="25%">Query string</th>
            <th width="10%">Station name</th>
            <th width="30%">Start date</th>
            <th width="30%">End date</th>
        </tr>

    <%

        Hashtable dataSets = dataManagerJBean.getDataSets();
        Hashtable xyGraphs = dataManagerJBean.getXyGraphsHashtable();
        Hashtable tidalModels = dataManagerJBean.getTidalModelsHashtable();

        xyGraphs.put(IDSSAppConstants.none,IDSSAppConstants.none);
        tidalModels.put(IDSSAppConstants.none,IDSSAppConstants.none);

        Enumeration sqlStrings = dataSets.keys();

        while(sqlStrings.hasMoreElements()) {

            String sqlString = (String)sqlStrings.nextElement();
            ClientDataSet cds = (ClientDataSet)dataSets.get(sqlString);
            Vector dataSetVector = cds.getDataSetVector();
            String stationName = (String)dataSetVector.elementAt(1);
            String startOfData = ((Timestamp)dataSetVector.elementAt(2)).toString();
            String endOfData = ((Timestamp)dataSetVector.elementAt(3)).toString();

    %>

            <tr width="100%" BGCOLOR="#80FFFF">
                <td><input type=checkbox name="<%=IDSSAppConstants.doItButtonCommand%>" value="<%=sqlString%>" justify="center"></td>
                <td><%=sqlString%> </td>
                <td><%=stationName%> </td>
                <td><%=startOfData%> </td>
                <td><%=endOfData%> </td>
            </tr>

    <%
        } //while(keys.hasMoreElements())
    %>

    </table>


    <table border="0">
        <tr>
            <td>
                <table border="0">
                    <tr>
                        <input type=submit value="<%=IDSSAppConstants.doItButtonText%>" name="<%=IDSSAppConstants.doItButtonText%>" >
                    </tr>
                    <tr>
                        <select name="processType" size="1">
                            <option value="<%=IDSSAppConstants.makeTable%>" ><%=IDSSAppConstants.makeTable%></option>
                            <option value="<%=IDSSAppConstants.drawGraphImg%>" ><%=IDSSAppConstants.drawGraphImg%></option>
                            <option value="<%=IDSSAppConstants.deviseTidalModel%>" ><%=IDSSAppConstants.deviseTidalModel%></option>
                        </select>
                    </tr>
                </table>
            </td>
            <td>
                <table border="0">

                    <tr>
                        <td>
                            Start:
                        </td>
                        <td>

                            <select name="startYear" size="1">
                                <%
                                    for(int i=1980;i<=1999;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=1900;i<=2000;i++)
                                %>

                            </select>

                        </td>
                        <td>

                            <select name="startMonth" size="1">
                                <%
                                    for(int i=1;i<=12;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=1;i<=12;i++)
                                %>

                            </select>

                        </td>
                        <td>

                            <select name="startDay" size="1">
                                <%
                                    for(int i=1;i<=31;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>"><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=1;i<=31;i++)
                                %>
                                </select>

                        </td>
                        <td>
                            <select name="startHour" size="1">
                                <%
                                    for(int i=0;i<=23;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=0;i<=23;i++)
                                %>
                            </select>
                        </td>
                        <td>
                            <select name="startMinute" size="1">
                                <%
                                    for(int i=0;i<=59;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=0;i<=59;i++)
                                %>
                            </select>
                        </td>
                        <td>
                            <select name="startSecond" size="1">
                                <%
                                    for(int i=0;i<=59;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=0;i<=59;i++)
                                %>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            End:
                        </td>
                        <td>

                            <select name="endYear" size="1">
                                <%
                                    for(int i=1980;i<=1999;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=1900;i<=2000;i++)
                                %>

                            </select>

                        </td>
                        <td>

                            <select name="endMonth" size="1">
                                <%
                                    for(int i=1;i<=12;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=1;i<=12;i++)
                                %>

                            </select>

                        </td>
                        <td>

                            <select name="endDay" size="1">
                                <%
                                    for(int i=1;i<=31;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=1;i<=31;i++)
                                %>
                                </select>

                        </td>
                        <td>
                            <select name="endHour" size="1">
                                <%
                                    for(int i=0;i<=23;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=0;i<=23;i++)
                                %>
                            </select>
                        </td>
                        <td>
                            <select name="endMinute" size="1">
                                <%
                                    for(int i=0;i<=59;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=0;i<=59;i++)
                                %>
                            </select>
                        </td>
                        <td>
                            <select name="endSecond" size="1">
                                <%
                                    for(int i=0;i<=59;i++) {
                                %>
                                        <option value="<%=String.valueOf(i)%>" ><%=String.valueOf(i)%></option>
                                <%
                                    } //for(int i=0;i<=59;i++)
                                %>
                            </select>
                        </td>
                    </tr>

                </table>
            </td>
            <td>
                <table border="0">
                    <tr>
                        <td>
                            <select name="superimposeGraph" size="1">
                                <%
                                    Enumeration xyGraphEnum = xyGraphs.keys();
                                    while(xyGraphEnum.hasMoreElements()) {
                                        String s = (String)xyGraphEnum.nextElement();
                                %>
                                        <option value="<%=s%>" ><%=s%></option>
                                <%
                                    } //while(xyGraphEnum.hasMoreElements())
                                %>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <select name="numberOfIterations" size="1">
                                <option value="0" >0</option>
                                <option value="100" >100</option>
                                <option value="200" >200</option>
                                <option value="300" >300</option>
                                <option value="400" >400</option>
                                <option value="500" >500</option>
                                <option value="1000" >1000</option>
                                <option value="1500" >1500</option>
                                <option value="2000" >2000</option>
                                <option value="2500" >2500</option>
                                <option value="3000" >3000</option>
                            </select>
                        </td>
                    </tr>

                </table>
            </td>
        </tr>
    </table>



</form>



<%

    String selectTMButtonName = request.getParameter(IDSSAppConstants.selectTidalModel);

    if(selectTMButtonName != null) {

%>
        <p>The button you clicked is <%=selectTMButtonName%>. </p>
<%


        if (selectTMButtonName.equals(IDSSAppConstants.selectTidalModel)) {

            String tidalModelKey = request.getParameter("tidalModelKey");

            if(!tidalModelKey.equals(IDSSAppConstants.none)) {

                %>
                    <p>tidalModelKey : <%=tidalModelKey%>. </p>
                <%

                dataManagerJBean.setTidalModel(tidalModelKey);

            } //if(!tidalModelKey.equals(IDSSAppConstants.none))


        } //if (actionButtonName.equals(IDSSAppConstants.selectTidalModel))


    } //if(selectTMButtonName != null)

%>


<form method=get>
    <table border="0">
        <tr>
            <td>
                <select name="tidalModelKey" size="1">
                    <%
                        Enumeration tidalModelsEnum = tidalModels.keys();
                        while(tidalModelsEnum.hasMoreElements()) {
                            String s = (String)tidalModelsEnum.nextElement();
                    %>
                            <option value="<%=s%>" ><%=s%></option>
                    <%
                        } //while(tidalModelsEnum.hasMoreElements())
                    %>
                </select>
            </td>
        </tr>
        <tr>
            <input type=submit value="<%=IDSSAppConstants.selectTidalModel%>" name="<%=IDSSAppConstants.selectTidalModel%>">
        </tr>
    </table>
</form>




</body>

</HTML>

