/**
     * Find the record with the specified primary keys
     *
     * @return DataPoint or null if no record is found
     */
    public DataPoint find(int id) {
      DataPoint rec = new DataPoint();

      // Create temp object and look in cache for it
      ((DataPoint_base) rec).initialize(id);
      rec = (DataPoint) GenOrmDataSource.getGenOrmConnection().getCachedRecord(rec.getRecordKey());

      java.sql.PreparedStatement genorm_statement = null;
      java.sql.ResultSet genorm_rs = null;

      if (rec == null) {
        try {
          // No cached object so look in db
          genorm_statement = GenOrmDataSource.prepareStatement(SELECT + FROM + KEY_WHERE);
          genorm_statement.setInt(1, id);

          s_logger.debug(genorm_statement.toString());

          genorm_rs = genorm_statement.executeQuery();
          if (genorm_rs.next()) rec = newDataPoint(genorm_rs);
        } catch (java.sql.SQLException sqle) {
          throw new GenOrmException(sqle);
        } finally {
          try {
            if (genorm_rs != null) genorm_rs.close();

            if (genorm_statement != null) genorm_statement.close();
          } catch (java.sql.SQLException sqle2) {
            throw new GenOrmException(sqle2);
          }
        }
      }

      return (rec);
    }
  // --------------------------------------------------------------------------
  public DataPoint setMetricRef(Metric table) {
    // We add the record to the transaction if one of the key values change
    if (m_metricId.setValue(table.getId())) {
      if ((m_dirtyFlags.isEmpty()) && (GenOrmDataSource.getGenOrmConnection() != null))
        GenOrmDataSource.getGenOrmConnection().addToTransaction(this);

      m_dirtyFlags.set(METRIC_ID_FIELD_META.getDirtyFlag());
    }

    return ((DataPoint) this);
  }
    /**
     * If the table has a primary key that has a key generator this method will return a new table
     * entry with a generated primary key.
     *
     * @return DataPoint with generated primary key
     */
    public DataPoint createWithGeneratedKey() {
      DataPoint rec = new DataPoint();

      rec.m_isNewRecord = true;

      GenOrmKeyGenerator keyGen = GenOrmDataSource.getKeyGenerator("data_point");
      if (keyGen != null) {
        rec.setId((Integer) keyGen.generateKey());
      }

      return ((DataPoint) GenOrmDataSource.getGenOrmConnection().getUniqueRecord(rec));
    }
    public ResultSet getForMetricId(
        String metricId, java.sql.Timestamp startTime, java.sql.Timestamp endTime) {
      String query =
          SELECT
              + "from data_point this\n				where\n				this.\"metric_id\" = ?\n				and this.\"timestamp\" >= ?\n				and this.\"timestamp\" <= ?\n				order by this.\"timestamp\"";

      java.sql.PreparedStatement genorm_statement = null;

      try {
        genorm_statement = GenOrmDataSource.prepareStatement(query);
        genorm_statement.setString(1, metricId);
        genorm_statement.setTimestamp(2, startTime);
        genorm_statement.setTimestamp(3, endTime);

        s_logger.debug(genorm_statement.toString());

        ResultSet rs = new SQLResultSet(genorm_statement.executeQuery(), query, genorm_statement);

        return (rs);
      } catch (java.sql.SQLException sqle) {
        try {
          if (genorm_statement != null) genorm_statement.close();
        } catch (java.sql.SQLException sqle2) {
        }

        if (s_logger.isDebug()) sqle.printStackTrace();
        throw new GenOrmException(sqle);
      }
    }
    /**
     * Convenience method for selecting records. Ideally this should not be use, instead a custom
     * query for this table should be used.
     *
     * @param where sql where statement.
     * @param orderBy sql order by statement
     */
    public ResultSet select(String where, String orderBy) {
      ResultSet rs = null;
      java.sql.Statement stmnt = null;

      try {
        stmnt = GenOrmDataSource.createStatement();
        StringBuilder sb = new StringBuilder();
        sb.append(SELECT);
        sb.append(FROM);
        if (where != null) {
          sb.append(WHERE);
          sb.append(where);
        }

        if (orderBy != null) {
          sb.append(" ");
          sb.append(orderBy);
        }

        String query = sb.toString();
        rs = new SQLResultSet(stmnt.executeQuery(query), query, stmnt);
      } catch (java.sql.SQLException sqle) {
        try {
          if (stmnt != null) stmnt.close();
        } catch (java.sql.SQLException sqle2) {
        }

        throw new GenOrmException(sqle);
      }

      return (rs);
    }
    /** Creates a new entry with the specified primary keys. */
    public DataPoint create(int id) {
      DataPoint rec = new DataPoint();
      rec.m_isNewRecord = true;

      ((DataPoint_base) rec).setId(id);

      return ((DataPoint) GenOrmDataSource.getGenOrmConnection().getUniqueRecord(rec));
    }
  public DataPoint setTimestampNull() {
    boolean changed = m_timestamp.setNull();

    if (changed) {
      if (m_dirtyFlags.isEmpty()) GenOrmDataSource.getGenOrmConnection().addToTransaction(this);

      m_dirtyFlags.set(TIMESTAMP_FIELD_META.getDirtyFlag());
    }

    return ((DataPoint) this);
  }
  public DataPoint setMetricIdNull() {
    boolean changed = m_metricId.setNull();

    if (changed) {
      if (m_dirtyFlags.isEmpty()) GenOrmDataSource.getGenOrmConnection().addToTransaction(this);

      m_dirtyFlags.set(METRIC_ID_FIELD_META.getDirtyFlag());
    }

    return ((DataPoint) this);
  }
  public DataPoint setDoubleValueNull() {
    boolean changed = m_doubleValue.setNull();

    if (changed) {
      if (m_dirtyFlags.isEmpty()) GenOrmDataSource.getGenOrmConnection().addToTransaction(this);

      m_dirtyFlags.set(DOUBLE_VALUE_FIELD_META.getDirtyFlag());
    }

    return ((DataPoint) this);
  }
  public DataPoint setDoubleValue(double data) {
    boolean changed = m_doubleValue.setValue(data);

    // Add the now dirty record to the transaction only if it is not previously dirty
    if (changed) {
      if (m_dirtyFlags.isEmpty()) GenOrmDataSource.getGenOrmConnection().addToTransaction(this);

      m_dirtyFlags.set(DOUBLE_VALUE_FIELD_META.getDirtyFlag());

      if (m_isNewRecord) // Force set the prev value
      m_doubleValue.setPrevValue(data);
    }

    return ((DataPoint) this);
  }
  public DataPoint setTimestamp(java.sql.Timestamp data) {
    boolean changed = m_timestamp.setValue(data);

    // Add the now dirty record to the transaction only if it is not previously dirty
    if (changed) {
      if (m_dirtyFlags.isEmpty()) GenOrmDataSource.getGenOrmConnection().addToTransaction(this);

      m_dirtyFlags.set(TIMESTAMP_FIELD_META.getDirtyFlag());

      if (m_isNewRecord) // Force set the prev value
      m_timestamp.setPrevValue(data);
    }

    return ((DataPoint) this);
  }
    /**
     * Deletes the record with the specified primary keys. The point of this api is to prevent a hit
     * on the db to see if the record is there. This call will add a record to the next transaction
     * that is marked for delete.
     *
     * @return Returns true if the record was previous created and existed either in the transaction
     *     cache or the db.
     */
    public boolean delete(int id) {
      boolean ret = false;
      DataPoint rec = new DataPoint();

      ((DataPoint_base) rec).initialize(id);
      GenOrmConnection con = GenOrmDataSource.getGenOrmConnection();
      DataPoint cachedRec = (DataPoint) con.getCachedRecord(rec.getRecordKey());

      if (cachedRec != null) {
        ret = true;
        cachedRec.delete();
      } else {
        rec = (DataPoint) con.getUniqueRecord(rec); // This adds the record to the cache
        rec.delete();
        ret = rec.flush();
        rec.setIgnored(true); // So the system does not try to delete it again at commmit
      }

      return (ret);
    }
    /**
     * This resets the key generator from the values in the database Usefull if the generated key
     * has been modified via some other means Connection must be open before calling this
     */
    public synchronized void reset() {
      m_nextKey = 0;
      java.sql.Statement stmnt = null;
      java.sql.ResultSet rs = null;
      try {
        stmnt = GenOrmDataSource.createStatement();
        rs = stmnt.executeQuery(MAX_QUERY);

        if (rs.next()) m_nextKey = rs.getInt(1);
      } catch (java.sql.SQLException sqle) {
        // The exception may occur if the table does not yet exist
        if (WARNINGS) System.out.println(sqle);
      } finally {
        try {
          if (rs != null) rs.close();

          if (stmnt != null) stmnt.close();
        } catch (java.sql.SQLException sqle2) {
          throw new GenOrmException(sqle2);
        }
      }
    }
    public ResultSet getByMetric(String metricId) {
      String query = SELECT + "FROM data_point this WHERE this.\"metric_id\" = ?";

      java.sql.PreparedStatement genorm_statement = null;

      try {
        genorm_statement = GenOrmDataSource.prepareStatement(query);
        genorm_statement.setString(1, metricId);

        s_logger.debug(genorm_statement.toString());

        ResultSet rs = new SQLResultSet(genorm_statement.executeQuery(), query, genorm_statement);

        return (rs);
      } catch (java.sql.SQLException sqle) {
        try {
          if (genorm_statement != null) genorm_statement.close();
        } catch (java.sql.SQLException sqle2) {
        }

        if (s_logger.isDebug()) sqle.printStackTrace();
        throw new GenOrmException(sqle);
      }
    }
 // ---------------------------------------------------------------------------
 @Override
 public GenOrmConnection getGenOrmConnection() {
   return (GenOrmDataSource.getGenOrmConnection());
 }
 protected DataPoint newDataPoint(java.sql.ResultSet rs) {
   DataPoint rec = new DataPoint();
   ((DataPoint_base) rec).initialize(rs);
   return ((DataPoint) GenOrmDataSource.getGenOrmConnection().getUniqueRecord(rec));
 }
    /** Creates a new entry that is empty */
    public DataPoint createRecord() {
      DataPoint rec = new DataPoint();
      rec.m_isNewRecord = true;

      return ((DataPoint) GenOrmDataSource.getGenOrmConnection().getUniqueRecord(rec));
    }