BeTrainsDatabase.java :  » UnTagged » betrains-android » be » irail » betrains » provider » Android Open Source

Android Open Source » UnTagged » betrains android 
betrains android » be » irail » betrains » provider » BeTrainsDatabase.java
/*
 * BeTrains for Android
 * Copyright (C) 2010 IRAL VZW/ASBL.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.irail.betrains.provider;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
import be.irail.api.data.Location;
import be.irail.api.data.Station;

/**
 * Handler for database interaction
 * 
 * @author Kevin Van Wilder - kevin@van-wilder.be
 *
 */
public class BeTrainsDatabase  {

  private Context context;
  private SQLiteDatabase db;
  
  private static final String DATABASE_NAME = "betrains.db";  
  private static final int DATABASE_VERSION = 2;

  public interface Tables {
    String STATIONS = "stations";
  }
  
  public interface Stations {
    static final String STATION_ID = "station_id";
    static final String STATION_NAME = "station_name";
    static final String STATION_LONG = "station_long";
    static final String STATION_LAT = "station_lat";
  }
  
  
  public BeTrainsDatabase(Context context) {
    this.context = context;
    DBOpenHelper helper = new DBOpenHelper(this.context);
    this.db = helper.getWritableDatabase();
  }
  
  public void addStations(List<Station> stations) {
    Log.v("BeTrainsDatabase", "Saving stations");
    ContentValues values = new ContentValues();
    for (Station station : stations) {
      values.put(Stations.STATION_ID, station.getId());
      values.put(Stations.STATION_NAME, station.getName());
      Location loc = station.getLocation();
      values.put(Stations.STATION_LONG, loc.getLongitude());
      values.put(Stations.STATION_LAT, loc.getLatitude());
      try {
        db.insert(Tables.STATIONS, null, values);
      } catch (Exception e) {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
      }
    }
    db.close();
  }

  public ArrayList<Station> getStations() {
    ArrayList<Station> stations = new ArrayList<Station>();
    Cursor cur;
    
    try {
      cur = db.query(
          Tables.STATIONS, 
          new String[] { Stations.STATION_NAME, Stations.STATION_ID, Stations.STATION_LONG, Stations.STATION_LAT}, 
          null, null, null, null, null);
      
      cur.moveToFirst();
      
      if (!cur.isAfterLast())
        do {
          Station s = new Station(cur.getString(0), cur.getString(1), new Location(cur.getDouble(2), cur.getDouble(3)));          
          stations.add(s);
        } while (cur.moveToNext());
      
      cur.close();
    } catch (SQLException e) {
      Log.e("DB ERROR", e.toString());
      e.printStackTrace();
    } 
    db.close();
    Log.v("BeTrainsDatabase", "Fetched stations: " + stations.toString());
    return stations;
  }
  

  private class DBOpenHelper extends SQLiteOpenHelper {
    
    public DBOpenHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE " + Tables.STATIONS + " ("
          + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
          + Stations.STATION_ID + " TEXT NOT NULL,"
          + Stations.STATION_NAME + " TEXT NOT NULL,"
          + Stations.STATION_LONG + " DOUBLE NOT NULL,"
          + Stations.STATION_LAT + " DOUBLE NOT NULL,"
          + "UNIQUE (" 
          + Stations.STATION_ID + ") ON CONFLICT REPLACE)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {      
      db.execSQL("DROP TABLE " + Tables.STATIONS);
      onCreate(db);      
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.