gt:         SQLiteDatabase database = DatabaseDataSource.getDatabase();
context: /**
   * AWBB Droid - Android manager for AWBB.
   * 
   * Copyright (c) 2014 Benoit Garrigues <bgarrigues@gmail.com>
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package awbb.droid.dao;
  
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  import android.content.ContentValues;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.util.Log;
  import awbb.droid.bm.History;
  import awbb.droid.bm.Location;
  import awbb.droid.bm.SensorData;
  
  /**
   * Sensor data DAO.
   * 
   * @author Benoit Garrigues <bgarrigues@gmail.com>
   */
  public class SensorDataDao {
  
      private static final String TAG = SensorDataDao.class.getSimpleName();
  
      static final String CREATE_TABLE_LOCATION = "create table sensor_data (" + "_id integer primary key autoincrement,"
              + "history_id integer not null," + "date integer not null," + "gps_fix integer," + "gps_sat integer,"
              + "latitude number," + "longitude number," + "altitude number," + "light number," + "sound number,"
              + "temperature number," + "humidity number," + "co2 number," + "rate integer" + ")";
  
      static final String DROP_TABLE_LOCATION = "drop table if exists sensor_data";
  
      /**
       * Constructor.
       */
      private SensorDataDao() {
      }
  
      /**
       * Add.
       * 
       * @param data
       * @return
       */
      public static long add(SensorData data) {
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
  
          ContentValues values = toContentValues(data);
          long id = database.insert("sensor_data", null, values);
  
          Log.d(TAG, "Add sensorData id=" + id);
  
          data.setId(id);
  
          return id;
      }
  
      /**
       * Delete.
       * 
       * @param data
       */
      public static void delete(SensorData data) {
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
  
          Log.d(TAG, "Delete sensorData id=" + data.getId());
  
          database.delete("sensor_data", "_id=?", new String[] { String.valueOf(data.getId()) });
      }
  
      /**
       * Get.
       * 
       * @param id
       * @return
       */
      public static SensorData get(long id) {
          SensorData data = null;
          final String sql = "select * from sensor_data where _id=?";
  
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
          Cursor cursor = database.rawQuery(sql, new String[] { String.valueOf(id) });
          if (cursor != null) {
              cursor.moveToFirst();
              data = toObject(cursor);
          }
          cursor.close();
  
          return data;
      }
  
      /**
       * Get all.
       * 
       * @return
       */
      public static List<SensorData> getAll() {
          List<SensorData> list = new ArrayList<SensorData>();
          final String sql = "select * from sensor_data order by history_id,date asc";
  
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
          Cursor cursor = database.rawQuery(sql, null);
          if (cursor.moveToFirst()) {
              do {
                  SensorData data = toObject(cursor);
                  list.add(data);
              } while (cursor.moveToNext());
          }
          cursor.close();
  
          return list;
      }
  
      /**
       * 
       * @param history
       * @return
       */
      public static List<SensorData> get(History history) {
          List<SensorData> list = new ArrayList<SensorData>();
          final String sql = "select * from sensor_data where history_id=? order by date asc";
  
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
          Cursor cursor = database.rawQuery(sql, new String[] { String.valueOf(history.getId()) });
          if (cursor.moveToFirst()) {
              do {
                  SensorData data = toObject(cursor);
                  list.add(data);
              } while (cursor.moveToNext());
          }
          cursor.close();
  
          return list;
      }
  
      /**
       * 
       * @param history
       * @param startDate
       * @param endDate
       * @return
       */
      public static List<SensorData> get(History history, Date startDate, Date endDate) {
          List<SensorData> list = new ArrayList<SensorData>();
          final String sql = "select * from sensor_data where history_id=? and date>=? and date<=? order by date asc";
  
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
          Cursor cursor = database.rawQuery(
                  sql,
                  new String[] { String.valueOf(history.getId()), String.valueOf(startDate.getTime()),
                          String.valueOf(endDate.getTime()) });
          if (cursor.moveToFirst()) {
              do {
                  SensorData data = toObject(cursor);
                  list.add(data);
              } while (cursor.moveToNext());
          }
          cursor.close();
  
          return list;
      }
  
      /**
       * 
       * @param history
       * @return
       */
      public static float getRate(History history) {
          float rate = 0;
          final String sql = "select avg(rate) from sensor_data where history_id=?";
  
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
          Cursor cursor = database.rawQuery(sql, new String[] { String.valueOf(history.getId()) });
          if (cursor != null) {
              cursor.moveToFirst();
              rate = cursor.getFloat(0);
          }
          cursor.close();
  
          return rate;
      }
  
      /**
       * 
       * @param location
       * @return
       */
      public static float getRate(Location location) {
          float rate = 0;
          final String sql = "select avg(rate) from sensor_data"
                  + " where history_id in (select _id from history where location_id=?)";
  
          SQLiteDatabase database = DatabaseDataSource.getDatabase();
          Cursor cursor = database.rawQuery(sql, new String[] { String.valueOf(location.getId()) });
          if (cursor != null) {
              cursor.moveToFirst();
              rate = cursor.getFloat(0);
          }
          cursor.close();
  
          return rate;
      }
  
      /**
       * 
       * @param history
       * @return
       */
      public static Date getBeginDate(History history) {
          Date date = null;
          final String sql = "select min(date) from sensor_data where history_id=?";
  
  