/* (non-Javadoc)
   * @see com.newgen.omni.jts.txn.NGOServerInterface#execute(java.sql.Connection, com.newgen.omni.jts.cmgr.XMLParser, com.newgen.omni.jts.cmgr.XMLGenerator)
   * Function Name : execute
   * Date Written : 03/04/2008
   * Author : yogvinder
   * Input Parameters : Connection, XMLPArser, XMLGenerator
   * Output Parameters : None
   * Return Values :
   * Description :
   */
  public String execute(Connection con, XMLParser xmlToParse, XMLGenerator fileProcessor)
      throws JTSException {
    StringBuffer outputXML = null;
    String errorMsg = null;
    String strColumnValue = null;
    String Flag = null;
    int errorCode = 0;
    ResultSet rs = null;

    Statement stmt = null;

    int dbStatus = 0;

    try {
      con.setAutoCommit(false);

      stmt = con.createStatement();

      outputXML = new StringBuffer(100);

      outputXML.append(fileProcessor.createOutputFile("NGOGetNextSynchData"));

      stmt.setQueryTimeout(
          ServerProperty.getReference().getQueryTimeout(xmlToParse.getValueOf("CabinetName")));

      rs =
          stmt.executeQuery(
              "select uid, destination, commanddata, command, transactionid,actiondatetime,requestdatetime,comment,requestcounter,requestType,source,caption,sourceIP,statusFlag from pdbsynchtable where statusflag in ('01', '03', '08', '11', '12', '13', '14', '16', '17', '18', '20' , '21', '23', '24', '25', '26', '27', '28', '29', '30', '34', '36', '38', '39', '40') and requestcounter < 05 order by actiondatetime, requestcounter,requestdatetime asc Limit 1");
      if (rs != null && rs.next()) {
        strColumnValue = rs.getString(1).trim();
        outputXML.append(fileProcessor.writeValueOf("UID", strColumnValue));
        outputXML.append(fileProcessor.writeValueOf("Destination", rs.getString(2)));
        outputXML.append(fileProcessor.writeValueOf("CommandData", rs.getString(3)));
        outputXML.append(fileProcessor.writeValueOf("Command", rs.getString(4)));
        outputXML.append(fileProcessor.writeValueOf("TransactionId", rs.getString(5)));
        outputXML.append(fileProcessor.writeValueOf("ActionDateTime", rs.getString(6)));
        outputXML.append(fileProcessor.writeValueOf("RequestDateTime", rs.getString(7)));
        outputXML.append(fileProcessor.writeValueOf("Comment", rs.getString(8)));
        String strRequestCounter = rs.getString(9);
        outputXML.append(fileProcessor.writeValueOf("RequestCounter", strRequestCounter));
        outputXML.append(fileProcessor.writeValueOf("RequestType", rs.getString(10)));
        outputXML.append(fileProcessor.writeValueOf("Source", rs.getString(11)));
        outputXML.append(fileProcessor.writeValueOf("Caption", rs.getString(12)));
        outputXML.append(fileProcessor.writeValueOf("SourceIP", rs.getString(13)));
        String strStatusFlag = rs.getString(14);
        outputXML.append(fileProcessor.writeValueOf("StatusFlag", strStatusFlag));
        int intRequestCounter = Integer.parseInt(strRequestCounter);
        int x = 0;

        // stmt.setQueryTimeout (ServerProperty.getReference ().getQueryTimeout
        // (xmlToParse.getValueOf ("CabinetName")));
        if (strStatusFlag.equalsIgnoreCase("01")) {
          if (intRequestCounter == 0)
            x =
                stmt.executeUpdate(
                    "Update pdbsynchtable set statusFlag = '02' , requestcounter = requestcounter +  1, actiondatetime = now(), Comment = 'Fetched for Processing' where UID = '"
                        + strColumnValue
                        + "'");
          else
            x =
                stmt.executeUpdate(
                    "Update pdbsynchtable set statusFlag = '02' , requestcounter = requestcounter +  1, actiondatetime = now() where UID = '"
                        + strColumnValue
                        + "'");
        } else {
          if (intRequestCounter == 0)
            x =
                stmt.executeUpdate(
                    "Update pdbsynchtable set requestcounter = requestcounter +  1, actiondatetime = now() where UID = '"
                        + strColumnValue
                        + "'");
          else
            x =
                stmt.executeUpdate(
                    "Update pdbsynchtable set requestcounter = requestcounter +  1, actiondatetime = now() where UID = '"
                        + strColumnValue
                        + "'");
        }
      }

      strColumnValue = null;
      errorMsg = null;
      con.commit();
      rs.close();

      outputXML.append(fileProcessor.writeValueOf("Status", String.valueOf(dbStatus)));

      // Error Handling to be completed.
    } catch (SQLException e) {
      e.printStackTrace();
      errorCode = JTSError.JTSE_SQL_ERR;
      if (e.getErrorCode() == 0)
        errorMsg =
            (new JTSSQLError(e.getSQLState())).getMessage()
                + "(SQL State : "
                + e.getSQLState()
                + ")";
      else errorMsg = e.getMessage();
    } catch (NumberFormatException e) {
      e.printStackTrace();
      errorCode = JTSError.JTSE_ILLEGAL_PARAM;
    } catch (NullPointerException e) {
      e.printStackTrace();
      errorCode = JTSError.JTSE_SYS_ERR;
    } catch (Exception e) {
      e.printStackTrace();
      errorCode = JTSError.JTSE_UNKNOWN_ERR;
      errorMsg = e.toString();
    } catch (Error e) {
      e.printStackTrace();
      errorCode = JTSError.JTSE_UNKNOWN_ERR;
      errorMsg = e.toString();
    } finally {
      try {
        con.rollback();
        if (stmt != null) stmt.close();
        if (rs != null) rs.close();

      } catch (Exception e) {
      }
      if (errorCode != 0) {
        throw new JTSException(errorCode, errorMsg);
      }
    }

    outputXML.append(fileProcessor.closeOutputFile("NGOGetNextSynchData"));

    return outputXML.toString();
  }