<%@page contentType="text/html"%><%@page pageEncoding="UTF-8"%><%@include file="TreeGridFramework.jsp"%><%
/*-----------------------------------------------------------------------------------------------------------------
Sample JSP web page, using java.sql.ResultSet object to read and write TreeGrid data from and to database.
By default, connection string is ODBC to MS Access db in ../Data/CalcData.mdf.
Shows sending data by page submit to server in hidden input.
Form is submitted by TreeGrid Save button or by external submit button.
------------------------------------------------------------------------------------------------------------------*/

//------------------------------------------------------------------------------------------------------------------
response.addHeader("Cache-Control","max-age=1, must-revalidate");

// --- Database connection ---
java.sql.Statement Cmd = getHsqlStatement(request,out,"Database","sa","",true);

// --- Save data to database ---
org.w3c.dom.Element[] Ch = getChanges(request.getParameter("TGData"));
if(Ch!=null){
   try {
      for(int i=0;i<Ch.length;i++){
         org.w3c.dom.Element I = Ch[i];
         String id = getId(I); if(id.equals("")) continue; // Error
         if(isDeleted(I)){ // Deleting
	         Cmd.executeUpdate("DELETE FROM TableData WHERE ID="+id);  
            }
         else if(isAdded(I)){ // Adding
            String[] Names = {"id","Project","Resource","Week","Hours"};
            boolean[] IsString = {false,true,true,false,false};
            Cmd.executeUpdate("INSERT INTO TableData(ID,Project,Resource,Week,Hours) VALUES(" + toSQLInsert(I,Names,IsString) + ")");
            }
         else if(isChanged(I)){ // Updating
            String[] Names = {"Project","Resource","Week","Hours"};
            boolean[] IsString = {true,true,false,false};
            Cmd.executeUpdate("UPDATE TableData SET " + toSQLUpdate(I,Names,Names,IsString) + " WHERE ID=" + id);
            }
         }
      }
   catch(Exception ex){
      out.print("Error in saving data !<br>");
      out.print(ex.getMessage());
      }
   }

// --- Load data from database ---
java.sql.ResultSet R = Cmd.executeQuery("SELECT * FROM TableData");
String[] Names = {"id","Project","Resource","Week","Hours"};
String Str = toHTMLString(getTableXML(R,Names));
R.close();
//------------------------------------------------------------------------------------------------------------------
%><html>
   <head>
      <link id="GridCSS" href="../../Grid/Grid.css" title="GridStyle" type="text/css" rel="stylesheet">
      <script src="../../Grid/GridE.js"> </script>
   </head>
   <body>
      <div style="WIDTH:100%;HEIGHT:90%">
         <bdo 
				Layout_Url="TableDef.xml" 
				Data_Tag="TGData" 
				Upload_Tag="TGData" Upload_Format="Internal"
				Export_Url="Export.jsp" Export_Data="TGData" Export_Param_File="SubmitTable.xls"
				></bdo>
      </div>
      <form method="post">
         <input name="TGData" type="hidden" value="<%=Str%>">
         <input type="submit" value="Submit changes to server"/>
      </form>
   </body>
</html>

