<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="Common.jsp" %><%!
//
//   Filename: newthread.jsp


static final String sFileName = "newthread.jsp";
              
static final String PageBODY = "link=\"#000000\" vlink=\"#0033cc\" alink=\"red\"";
static final String FormTABLE = "border=\"0\" cellspacing=\"1\" cellpadding=\"3\"";
static final String FormHeaderTD = "bgcolor=\"#c2c2c2\"";
static final String FormHeaderFONT = "face=\"arial\" style=\"font:bold\" color=\"#0033cc\"";
static final String FieldCaptionTD = "bgcolor=\"#f2f2f2\"";
static final String FieldCaptionFONT = "face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\"";
static final String DataTD = "";
static final String DataFONT = "face=\"arial\" size=\"2\"";
static final String ColumnFONT = "face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\"";
static final String ColumnTD = "bgcolor=\"#f2f2f2\"";
%><%

boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String smessagesErr = "";

java.sql.Connection conn = null;
java.sql.Statement stat = null;
String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}
if ( sForm.equals("messages") ) {
  smessagesErr = messagesAction(request, response, session, out, sAction, sForm, conn, stat);
  if ( "sendRedirect".equals(smessagesErr)) return;
}

%>            
<html>
<head>
<title>Forum</title>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body link="#000000" vlink="#0033cc" alink="red">
<jsp:include page="header.jsp" flush="true"/>
 <table>
  <tr>
   
   <td valign="top">
<% messages_Show(request, response, session, out, smessagesErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>


</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!


  String messagesAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
  
    String smessagesErr ="";
    try {

      if (sAction.equals("")) return "";

      String sSQL="";
      String transitParams = "";
      String primaryKeyParams = "";
      String sQueryString = "";
      String sPage = "";
      String sParams = "";
      String sActionFileName = "index.jsp";
      String sWhere = " ";
      boolean bErr = false;
      long iCount = 0;

  
      String pPKmessage_id = "";

      final int iinsertAction = 1;
      final int iupdateAction = 2;
      final int ideleteAction = 3;
      int iAction = 0;

      if ( sAction.equalsIgnoreCase("insert") ) { iAction = iinsertAction; }
      if ( sAction.equalsIgnoreCase("update") ) { iAction = iupdateAction; }
      if ( sAction.equalsIgnoreCase("delete") ) { iAction = ideleteAction; }

      // Create WHERE statement


      String fldtopic="";
      String fldauthor="";
      String fldmessage="";
      String flddate_entered="";
      String fldmessage_id="";

      // Load all form fields into variables
    
      fldtopic = getParam(request, "topic");
      fldauthor = getParam(request, "author");
      fldmessage = getParam(request, "message");
      flddate_entered = getParam(request, "date_entered");
      // Validate fields
      if ( iAction == iinsertAction || iAction == iupdateAction ) {
        if (smessagesErr.length() > 0 ) {
          return (smessagesErr);
        }
      }


      sSQL = "";
      // Create SQL statement

      switch (iAction) {
  
        case iinsertAction :
          
            sSQL = "insert into messages (" + 
                "topic," +
                "author," +
                "message," +
                "date_entered)" +

                " values (" + 
                toSQL(fldtopic, adText) + "," +
                toSQL(fldauthor, adText) + "," +
                toSQL(fldmessage, adText) + "," +
                toSQL(flddate_entered, adDate) + ")";
          break;
  
      }

      if ( smessagesErr.length() > 0 ) return smessagesErr;
      try {
        // Execute SQL statement
        stat.executeUpdate(sSQL);
      }
      catch(java.sql.SQLException e) {
        smessagesErr = e.toString(); return (smessagesErr);
      }
  
      try {
        if ( stat != null ) stat.close();
        if ( conn != null ) conn.close();
      }
      catch ( java.sql.SQLException ignore ) {}
      response.sendRedirect (sActionFileName);

      return "sendRedirect";
    }
    catch (Exception e) {out.println(e.toString()); }
    return (smessagesErr);
  }

  


  void messages_Show(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String smessagesErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {

      String sSQL="";
      String sQueryString = "";
      String sPage = "";
      String sWhere = "";
      String transitParams = "";
      String transitParamsHidden = "";
      String requiredParams = "";
      String primaryKeyParams ="";
      java.util.Hashtable rsHash = new java.util.Hashtable();
      
      String pmessage_id = "";

      String fldmessage_id="";
      String fldtopic="";
      String fldauthor="";
      String fldmessage="";
      String flddate_entered="";


      boolean bPK = true;

      if ( "".equals(smessagesErr)) {
        // Load primary key and form parameters
        pmessage_id = getParam( request, "message_id");
      }
      else {
        // Load primary key, form parameters and form fields
        fldmessage_id = getParam( request, "message_id");
        fldtopic = getParam( request, "topic");
        fldauthor = getParam( request, "author");
        fldmessage = getParam( request, "message");
        flddate_entered = getParam( request, "date_entered");
        pmessage_id = getParam( request, "PK_message_id");
      }

      
      if ( isEmpty(pmessage_id)) { bPK = false; }
      
      sWhere += "message_id=" + toSQL(pmessage_id, adNumber);
      primaryKeyParams += "<input type=\"hidden\" name=\"PK_message_id\" value=\""+pmessage_id+"\"/>";

      sSQL = "select * from messages where " + sWhere;


      out.println("    <table border=\"0\" cellspacing=\"1\" cellpadding=\"3\">");
      out.println("     <tr>\n      <td bgcolor=\"#c2c2c2\" colspan=\"2\"><font face=\"arial\" style=\"font:bold\" color=\"#0033cc\">发表新文章</font></td>\n     </tr>");
      if ( ! smessagesErr.equals("")) {
        out.println("     <tr>\n      <td  colspan=\"2\"><font face=\"arial\" size=\"2\">"+smessagesErr+"</font></td>\n     </tr>");
      }
      smessagesErr="";
      out.println("     <form method=\"get\" action=\""+sFileName+"\" name=\"messages\">");

      java.sql.ResultSet rs = null;

      if ( bPK &&  ! (sAction.equals("insert") && "messages".equals(sForm))) {

        // Open recordset
        rs = openrs( stat, sSQL);
        rs.next();
        String[] aFields = getFieldsName( rs );
        getRecordToHash( rs, rsHash, aFields );
        rs.close();
        fldmessage_id = (String) rsHash.get("message_id");
        flddate_entered = (String) rsHash.get("date_entered");
        if ( "".equals(smessagesErr)) {
          // Load data from recordset when form displayed first time
          fldtopic = (String) rsHash.get("topic");
          fldauthor = (String) rsHash.get("author");
          fldmessage = (String) rsHash.get("message");
        }

        if (sAction.equals("") || ! "messages".equals(sForm)) {
      
          fldmessage_id = (String) rsHash.get("message_id");
          fldtopic = (String) rsHash.get("topic");
          fldauthor = (String) rsHash.get("author");
          fldmessage = (String) rsHash.get("message");
          flddate_entered = (String) rsHash.get("date_entered");
        }
        
      }
      else {
flddate_entered = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date());
      }
      


      // Show form field
      
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">文章标题</font></td><td >"); out.print("<input type=\"text\"  name=\"topic\" maxlength=\"50\" value=\""+toHTML(fldtopic)+"\" size=\"50\">");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">作者</font></td><td >"); out.print("<input type=\"text\"  name=\"author\" maxlength=\"50\" value=\""+toHTML(fldauthor)+"\" size=\"20\">");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td bgcolor=\"#f2f2f2\"><font face=\"arial\" size=\"2\" style=\"font:bold\" color=\"#0033cc\">文章内容</font></td><td >"); out.print("<textarea name=\"message\" cols=\"80\" rows=\"12\">"+toHTML(fldmessage)+"</textarea>");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td colspan=\"2\" align=\"right\">");
      

      if ( bPK && ! (sAction.equals("insert") && "messages".equals(sForm))) {
        
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"messages\"><input type=\"hidden\" value=\"\" name=\"FormAction\">");
      }
      
      else {
        out.print("<input type=\"submit\" value=\"Submit\" onclick=\"document.messages.FormAction.value = 'insert';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"messages\"><input type=\"hidden\" value=\"insert\" name=\"FormAction\">");
      }out.print("<input type=\"hidden\" name=\"message_id\" value=\""+toHTML(fldmessage_id)+"\">");out.print("<input type=\"hidden\" name=\"date_entered\" value=\""+toHTML(flddate_entered)+"\">");
      out.print(transitParamsHidden+requiredParams+primaryKeyParams);
      out.println("</td>\n     </tr>\n     </form>\n    </table>");
      



    }
    catch (Exception e) { out.println(e.toString()); }
  } %>