Observation.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package cz.hsrs.db.model;
  2. // default package
  3. // Generated 3.6.2008 8:30:06 by Hibernate Tools 3.2.2.GA
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.logging.Level;
  10. import cz.hsrs.db.DBObject;
  11. import cz.hsrs.db.pool.SQLExecutor;
  12. /**
  13. * Observation generated by hbm2java
  14. */
  15. public class Observation implements DBObject {
  16. //private int id;
  17. private long sensor_id;
  18. private Date timeStamp;
  19. private int gid;
  20. private long unit_id;
  21. private Double observedValue;
  22. private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
  23. @Override
  24. public DBObject getDBObject(ResultSet set) throws SQLException {
  25. return new Observation(set);
  26. }
  27. /**
  28. * Constructor creates object with attributes
  29. * @param timeStamp - time stamp when observation was measured
  30. * @param observedValue - observed value, can be NaN
  31. * @param sensor_id - id of sensor
  32. * @param unit_id - id of unit
  33. */
  34. public Observation(Date timeStamp, Double observedValue, long sensor_id, long unit_id) {
  35. this.sensor_id = sensor_id;
  36. this.timeStamp = timeStamp;
  37. this.unit_id = unit_id;
  38. this.observedValue = observedValue;
  39. }
  40. /**
  41. * Constructor creates object from ResultSet
  42. * @param set ResultSet with all mandatory attributes
  43. * @throws SQLException
  44. */
  45. public Observation(ResultSet set) throws SQLException {
  46. this.sensor_id = set.getLong("sensor_id");
  47. String time_string = set.getString("time_stamp");
  48. try {
  49. timeStamp = FORMATTER.parse(time_string+"00");
  50. } catch (ParseException e) {
  51. // Should never happpend
  52. SQLExecutor.logger.log(Level.SEVERE, e.getMessage());
  53. }
  54. this.unit_id = set.getLong("unit_id");
  55. this.observedValue = set.getDouble("observed_value");
  56. this.gid = set.getInt("gid");
  57. }
  58. public Observation() { }
  59. public long getUnitId(){
  60. return unit_id;
  61. }
  62. public long getSensorId(){
  63. return sensor_id;
  64. }
  65. public Date getTimeStamp() {
  66. return timeStamp;
  67. }
  68. public int getGid() {
  69. return gid;
  70. }
  71. public Double getObservedValue() {
  72. return observedValue;
  73. }
  74. public void setObservedValue(Double observedValue) {
  75. this.observedValue = observedValue;
  76. }
  77. public boolean insertToDb() throws SQLException{
  78. int i = SQLExecutor.executeUpdate(String.format(
  79. "INSERT INTO observations(time_stamp, observed_value, sensor_id, unit_id) VALUES ('%s', %s, %s, %s);",
  80. FORMATTER.format(timeStamp), observedValue.isNaN() ? "'NaN'" : observedValue, sensor_id, unit_id
  81. ));
  82. return i == 1 || i == 0;
  83. }
  84. }