/**
  * Based on Criteria that has been set, this methods makes a copy and stores the settings for
  * running the specified report
  *
  * @param db Description of the Parameter
  * @param criteria Description of the Parameter
  * @return Description of the Return Value
  * @throws SQLException Description of the Exception
  */
 public static int insert(Connection db, Criteria criteria, int reportType, boolean sendEmail)
     throws SQLException {
   int position = -1;
   boolean commit = false;
   try {
     commit = db.getAutoCommit();
     if (commit) {
       db.setAutoCommit(false);
     }
     // Insert the new report into the queue
     int id = DatabaseUtils.getNextSeq(db, "report_queue_queue_id_seq");
     PreparedStatement pst =
         db.prepareStatement(
             "INSERT INTO report_queue "
                 + "("
                 + (id > -1 ? "queue_id, " : "")
                 + "report_id, enteredby, output_type, email) "
                 + "VALUES ("
                 + (id > -1 ? "?, " : "")
                 + "?, ?, ?, ?) ");
     int i = 0;
     if (id > -1) {
       pst.setInt(++i, id);
     }
     pst.setInt(++i, criteria.getReportId());
     pst.setInt(++i, criteria.getOwner());
     pst.setInt(++i, reportType);
     pst.setBoolean(++i, sendEmail);
     pst.execute();
     pst.close();
     id = DatabaseUtils.getCurrVal(db, "report_queue_queue_id_seq", id);
     // Insert the criteria for processing the report
     int rqcId = DatabaseUtils.getNextSeq(db, "report_queue_criteria_criteria_id_seq");
     pst =
         db.prepareStatement(
             "INSERT INTO report_queue_criteria "
                 + "("
                 + (rqcId > -1 ? "criteria_id, " : "")
                 + "queue_id, "
                 + DatabaseUtils.addQuotes(db, "parameter")
                 + ", "
                 + DatabaseUtils.addQuotes(db, "value")
                 + ") "
                 + "VALUES ("
                 + (rqcId > -1 ? "?, " : "")
                 + "?, ?, ?) ");
     Iterator params = criteria.getParameters().iterator();
     while (params.hasNext()) {
       Parameter param = (Parameter) params.next();
       int ip = 0;
       if (rqcId > -1) {
         pst.setInt(++ip, rqcId);
       }
       pst.setInt(++ip, id);
       pst.setString(++ip, param.getName());
       pst.setString(++ip, param.getValue());
       pst.execute();
       if (rqcId > -1 && params.hasNext()) {
         rqcId = DatabaseUtils.getNextSeq(db, "report_queue_criteria_criteria_id_seq");
       }
     }
     pst.close();
     // Get the total number of reports pending
     pst =
         db.prepareStatement(
             "SELECT count(*) AS "
                 + DatabaseUtils.addQuotes(db, "position")
                 + " "
                 + "FROM report_queue "
                 + "WHERE processed IS NULL ");
     ResultSet rs = pst.executeQuery();
     rs.next();
     position = rs.getInt("position");
     rs.close();
     pst.close();
     if (commit) {
       db.commit();
     }
   } catch (Exception e) {
     if (commit) {
       db.rollback();
     }
     throw new SQLException(e.getMessage());
   } finally {
     if (commit) {
       db.setAutoCommit(true);
     }
   }
   return position;
 }