<%@ page language="java" import="java.sql.*,java.net.*,java.util.*,java.beans.*,java.io.*,iDSS.*,iDSS.utils.*,iDSS.tidalData.*,iDSS.webBeans.*,iDSS.beans.*,iDSS.beans2.*,iDSS.grids.*,iDSS.webApplets.*,iDSS.sim.*" %>

<!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="modelSetupControllerJBean" scope="session" class="iDSS.beans2.ModelSetupControllerJBean"/>
<jsp:useBean id="statusJBean" scope="session" class="iDSS.beans2.StatusJBean"/>
<jsp:useBean id="simRun" scope="session" class="iDSS.sim.SimulationRun"/>
<jsp:useBean id="simValidate" scope="session" class="iDSS.sim.InputValidation"/>
<jsp:useBean id="simJBean" scope="session" class="iDSS.beans2.SimulationJBean"/>

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




<%!


    String simulationName = "";
    String timeStepHrs = "";
    String simulateUntil = "";
    String floodStartPoints = "";
    String resultStep = "";


%>







<%



    String actionButtonName = request.getParameter("action");
    statusJBean = modelSetupControllerJBean.getStatusBean();
    boolean modelStatus = statusJBean.checkStatus();


    if(modelStatus) {


        if(actionButtonName != null) {

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

                simulationName = request.getParameter("simulationName");
                timeStepHrs = request.getParameter("timeStepHrs");
                simulateUntil = request.getParameter("simulateUntil");
                floodStartPoints = request.getParameter("floodStartPoints");
                resultStep = "1";

%>

                <jsp:setProperty
                    name="simValidate" property="simName" value="<%=simulationName%>"
                />
                <jsp:setProperty
                    name="simValidate" property="timestepString" value="<%=timeStepHrs%>"
                />
                <jsp:setProperty
                    name="simValidate" property="resultStep" value="<%=resultStep%>"
                />
                <jsp:setProperty
                    name="simValidate" property="coordsString" value="<%=floodStartPoints%>"
                />
                <jsp:setProperty
                    name="simValidate" property="simulateUntil" value="<%=simulateUntil%>"
                />
<%


                Vector v = simValidate.validateInput();

                boolean validInput = ((Boolean)v.elementAt(0)).booleanValue();
                String message = (String)v.elementAt(1);


                %>
                    <p>validInput : <%=validInput%></p>
                    <p>message : <%=message%></p>
                    <p>simulationName : <%=simValidate.getSimName()%></p>
                    <p>timeStep : <%=simValidate.getTimestepString()%></p>
                    <p>resultStep : <%=simValidate.getResultStep()%></p>
                    <p>floodCoords : <%=simValidate.getCoordsString()%></p>
                    <p>simulateUntil : <%=simValidate.getSimulateUntil()%></p>

                <%


                if (validInput) {


                    SimulationJBean simJBean2 = modelSetupControllerJBean.getSimulationJBean();

                    simJBean.setDem(simJBean2.getDem());
                    simJBean.setLcGrid2(simJBean2.getLcGrid2());
                    simJBean.setTidalModelHashtable(simJBean2.getTidalModelHashtable());
                    simJBean.setIsostaticMovement(simJBean2.getIsostaticMovement());
                    simJBean.setSlrDataSet(simJBean2.getSlrDataSet());


                    File saveLocationDir = new File("e:\\j2sdkee1.2.1\\public_html\\idss\\results");
                    simJBean.setSaveLocationDir(saveLocationDir);


                    %>
                        <p>saveLocationDir : <%=saveLocationDir%></p>
                        <p>simJBean.getSaveLocationDir() : <%=simJBean.getSaveLocationDir()%></p>
                    <%



                    if(saveLocationDir != null) {

                        simJBean.setSimulationName(simValidate.getSimName());

                        String ts = simValidate.getTimestepString();
                        int tsMinutes = Integer.parseInt(ts)*60;

                        simJBean.setTimestepMinutes(tsMinutes);

                        simJBean.setResultsTimestepYears(Integer.parseInt(simValidate.getResultStep()));

                        simJBean.setFloodStartVector(simValidate.getCoordsString());

                        simJBean.setSimulateUntil(Integer.parseInt(simValidate.getSimulateUntil()));

                        simJBean.setViewFG(false);
                        simJBean.setViewDD(false);
                        simJBean.setSaveFG(false);
                        simJBean.setSaveDD(false);

                        Vector fsVec = simJBean.getFloodStartVector();


                        %>
                            <p>simJBean.getSimulationName() : <%=simJBean.getSimulationName()%></p>
                            <p>getSimulateUntil() : <%=String.valueOf(simJBean.getSimulateUntil())%></p>
                            <p>simJBean.getTimestepMinutes() : <%=String.valueOf(simJBean.getTimestepMinutes())%></p>
                            <p>simJBean.getResultsTimestepYears() : <%=String.valueOf(simJBean.getResultsTimestepYears())%></p>
                            <p>FloodStart points : <%=simJBean.getFloodStartVector().toString()%></p>
                        <%


                        simRun.xxSimData(simJBean);

                        simRun.addPropertyChangeListener(simJBean);

                        Thread simulationThread = new Thread(simRun);
                        simulationThread.start();

                        %>

                        <a href="/idss/bin/simulationStatus.jsp">View simulation status</a>

                        <%


                    } //if(saveLocationFile != null)


                } //if (validInput)

            } //else if (actionButtonName.equals(IDSSAppConstants.startSimulationButtonText))

        } //if(actionButtonName != null)



%>





            <form>
                <table width="70%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">

                    <tr>
                        <td BGCOLOR="#C0C0C0">
                            Name of simulation
                        </td>
                        <td BGCOLOR="#80FFFF">
                            <input type="text" name="simulationName" size="40">
                        </td>
                    </tr>
                    <tr>
                        <td BGCOLOR="#C0C0C0">
                            Simulate until
                        </td>
                        <td BGCOLOR="#80FFFF">
                            <select name="simulateUntil" size="1">
                                <%
                                    for(int i=2000;i<=2100;i++) {
                                        if(i == 2000) {
                                %>
                                            <option value="<%=i%>" selected><%=i%></option>
                                <%
                                        } //if(i == Integer.parseInt(simulateUntil))
                                        else {
                                %>
                                            <option value="<%=i%>"><%=i%></option>
                                <%
                                        } //else
                                    } //for(int i=2000;i<=2100;i++)
                                %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td BGCOLOR="#C0C0C0">
                            Timestep (hrs)
                        </td>
                        <td BGCOLOR="#80FFFF">
                            <input type="text" name=timeStepHrs size="10">
                        </td>
                    </tr>
                    <tr>
                        <td BGCOLOR="#C0C0C0">
                            Flood start point(s), x,y,x,y...
                        </td>
                        <td BGCOLOR="#80FFFF">
                            <input type="text" name=floodStartPoints size="40">
                        </td>
                    </tr>

                </table>

                <input type=submit name="action" value="<%=IDSSAppConstants.startSimulationButtonText%>">

            </form>




        <%



    } //if(modelStatus)

    else {
        %>
            <p>modelStatus : <%=modelStatus%></p>
            <p>Model setup incomplete</p>
        <%
    } //else


%>

</body>

</HTML>

