// Path: api/src/main/java/ch/liip/timeforcoffee/api/models/Station.java
// public class Station {
// 
//     private final int id;
//     private final String name;
//     private final Location location;
//     private final float distance;
//     private boolean isFavorite;
// 
//     public Station(int id, String name, float distance, Location location, boolean isFavorite) {
//         this.id = id;
//         this.name = name;
//         this.location = location;
//         this.distance = distance;
//         this.isFavorite = isFavorite;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public Location getLocation() {
//         return location;
//     }
// 
//     public float getDistance() {
//         return distance;
//     }
// 
//     public boolean getIsFavorite() {
//         return isFavorite;
//     }
// 
//     public void setIsFavorite(boolean isFavorite) {
//         this.isFavorite = isFavorite;
//     }
// 
//     public String getDistanceForDisplay(Location userLocation) {
// 
//         if (userLocation != null) {
//             int directDistance = getDistanceInMeter(userLocation);
// 
//             if (directDistance > 5000) {
//                 int km = (int) (Math.round((double) (directDistance) / 1000));
//                 return km + " km";
//             } else {
//                 return directDistance + " m";
//             }
//         }
//         return null;
//     }
// 
//     private int getDistanceInMeter(Location userLocation) {
//         if (userLocation != null) {
//             return (int) userLocation.distanceTo(location);
//         }
//         return 0;
//     }
// 
//     @Override
//     public boolean equals(Object object) {
//         boolean sameName = false;
// 
//         if (object instanceof Station) {
//             sameName = this.name.equals(((Station) object).getName());
//         }
// 
//         return sameName;
//     }
// }


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;
import android.provider.BaseColumns;
import java.util.ArrayList;
import java.util.List;
import ch.liip.timeforcoffee.api.models.Station;
        db.execSQL(SQL_CREATE_LINE_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion == 1 && newVersion == 2) {
            // get old favorite stations
            List<Station> oldFavoriteStations = new ArrayList<>();

            Cursor cursor = db.query("favorites", new String[] {"id", "station_id", "name", "latitude", "longitude"}, null, null, null, null, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                String id = cursor.getString(cursor.getColumnIndexOrThrow("station_id"));
                String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                double latitude = cursor.getDouble(cursor.getColumnIndexOrThrow("latitude"));
                double longitude = cursor.getDouble(cursor.getColumnIndexOrThrow("longitude"));

                Location location = new Location("fav");
                location.setLatitude(latitude);
                location.setLongitude(longitude);

                oldFavoriteStations.add(new Station(Integer.parseInt(id), name, 0, location, true));
                cursor.moveToNext();
            }
            cursor.close();

            // remove old table and create the new ones
            db.execSQL(SQL_DELETE_OLD_STATION_TABLE);
            onCreate(db);

            // add old favorite stations in the new table
// Path: api/src/main/java/ch/liip/timeforcoffee/api/models/Station.java
// public class Station {
// 
//     private final int id;
//     private final String name;
//     private final Location location;
//     private final float distance;
//     private boolean isFavorite;
// 
//     public Station(int id, String name, float distance, Location location, boolean isFavorite) {
//         this.id = id;
//         this.name = name;
//         this.location = location;
//         this.distance = distance;
//         this.isFavorite = isFavorite;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public Location getLocation() {
//         return location;
//     }
// 
//     public float getDistance() {
//         return distance;
//     }
// 
//     public boolean getIsFavorite() {
//         return isFavorite;
//     }
// 
//     public void setIsFavorite(boolean isFavorite) {
//         this.isFavorite = isFavorite;
//     }
// 
//     public String getDistanceForDisplay(Location userLocation) {
// 
//         if (userLocation != null) {
//             int directDistance = getDistanceInMeter(userLocation);
// 
//             if (directDistance > 5000) {
//                 int km = (int) (Math.round((double) (directDistance) / 1000));
//                 return km + " km";
//             } else {
//                 return directDistance + " m";
//             }
//         }
//         return null;
//     }
// 
//     private int getDistanceInMeter(Location userLocation) {
//         if (userLocation != null) {
//             return (int) userLocation.distanceTo(location);
//         }
//         return 0;
//     }
// 
//     @Override
//     public boolean equals(Object object) {
//         boolean sameName = false;
// 
//         if (object instanceof Station) {
//             sameName = this.name.equals(((Station) object).getName());
//         }
// 
//         return sameName;
//     }
// }


// Path: mobile/src/main/java/ch/liip/timeforcoffee/helper/FavoritesDatabaseHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;
import android.provider.BaseColumns;
import java.util.ArrayList;
import java.util.List;
import ch.liip.timeforcoffee.api.models.Station;

        db.execSQL(SQL_CREATE_LINE_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion == 1 && newVersion == 2) {
            // get old favorite stations
            List<Station> oldFavoriteStations = new ArrayList<>();

            Cursor cursor = db.query("favorites", new String[] {"id", "station_id", "name", "latitude", "longitude"}, null, null, null, null, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                String id = cursor.getString(cursor.getColumnIndexOrThrow("station_id"));
                String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                double latitude = cursor.getDouble(cursor.getColumnIndexOrThrow("latitude"));
                double longitude = cursor.getDouble(cursor.getColumnIndexOrThrow("longitude"));

                Location location = new Location("fav");
                location.setLatitude(latitude);
                location.setLongitude(longitude);

                oldFavoriteStations.add(new Station(Integer.parseInt(id), name, 0, location, true));
                cursor.moveToNext();
            }
            cursor.close();

            // remove old table and create the new ones
            db.execSQL(SQL_DELETE_OLD_STATION_TABLE);
            onCreate(db);

            // add old favorite stations in the new table
            for(Station oldFavoriteStation : oldFavoriteStations) {