package com.mehmetakiftutuncu.eshotroid.model; import java.util.ArrayList; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.mehmetakiftutuncu.eshotroid.provider.EshotroidProvider; import android.content.ContentValues; import android.database.Cursor; /** A model class for a bus * * @author mehmetakiftutuncu */ public class Bus { private int number; private String source; private String destination; private String route; private boolean isFavorited; private ArrayList<BusTime> timesH; private ArrayList<BusTime> timesC; private ArrayList<BusTime> timesP; private boolean timesHExists; private boolean timesCExists; private boolean timesPExists; public Bus(int number, String source, String destination, String route, boolean isFavorited, ArrayList<BusTime> timesH, ArrayList<BusTime> timesC, ArrayList<BusTime> timesP, boolean timesHExists, boolean timesCExists, boolean timesPExists) { setNumber(number); setSource(source); setDestination(destination); setRoute(route); setFavorited(isFavorited); setTimesH(timesH); setTimesC(timesC); setTimesP(timesP); setTimesHExists(timesHExists); setTimesCExists(timesCExists); setTimesPExists(timesPExists); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public String getRoute() { return route; } public void setRoute(String route) { this.route = route; } public boolean isFavorited() { return isFavorited; } public void setFavorited(boolean isFavorited) { this.isFavorited = isFavorited; } public ArrayList<BusTime> getTimesH() { return timesH; } public void setTimesH(ArrayList<BusTime> timesH) { this.timesH = timesH; } public ArrayList<BusTime> getTimesC() { return timesC; } public void setTimesC(ArrayList<BusTime> timesC) { this.timesC = timesC; } public ArrayList<BusTime> getTimesP() { return timesP; } public void setTimesP(ArrayList<BusTime> timesP) { this.timesP = timesP; } public boolean timesHExists() { return timesHExists; } public void setTimesHExists(boolean timesHExists) { this.timesHExists = timesHExists; } public boolean timesCExists() { return timesCExists; } public void setTimesCExists(boolean timesCExists) { this.timesCExists = timesCExists; } public boolean timesPExists() { return timesPExists; } public void setTimesPExists(boolean timesPExists) { this.timesPExists = timesPExists; } /** * Converts this bus to a ContentValues package * * @return A ContentValues package having the values as attributes of this bus */ public ContentValues toContentValues() { ContentValues values = new ContentValues(); values.put(EshotroidProvider.Busses.COLUMN_NUMBER, getNumber()); values.put(EshotroidProvider.Busses.COLUMN_SOURCE, getSource()); values.put(EshotroidProvider.Busses.COLUMN_DESTINATION, getDestination()); values.put(EshotroidProvider.Busses.COLUMN_ROUTE, getRoute()); values.put(EshotroidProvider.Busses.COLUMN_ISFAVORITED, (isFavorited() ? 1 : 0)); values.put(EshotroidProvider.Busses.COLUMN_TIMESH, (getTimesH() != null ? new Gson().toJson(getTimesH()) : null)); values.put(EshotroidProvider.Busses.COLUMN_TIMESC, (getTimesC() != null ? new Gson().toJson(getTimesC()) : null)); values.put(EshotroidProvider.Busses.COLUMN_TIMESP, (getTimesP() != null ? new Gson().toJson(getTimesP()) : null)); values.put(EshotroidProvider.Busses.COLUMN_TIMESH_EXISTS, (timesHExists() ? 1 : 0)); values.put(EshotroidProvider.Busses.COLUMN_TIMESC_EXISTS, (timesCExists() ? 1 : 0)); values.put(EshotroidProvider.Busses.COLUMN_TIMESP_EXISTS, (timesPExists() ? 1 : 0)); return values; } /** * Converts given Cursor to a Bus * * @param cursor Cursor generated by a query * * @return Bus generated by the values read from the cursor */ public static Bus fromCursor(Cursor cursor) { if(cursor != null && cursor.getCount() > 0) { int number = cursor.getInt(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_NUMBER)); String source = cursor.getString(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_SOURCE)); String destination = cursor.getString(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_DESTINATION)); String route = cursor.getString(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_ROUTE)); boolean isFavorited = (cursor.getInt(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_ISFAVORITED)) == 1); ArrayList<BusTime> timesH = new Gson().fromJson(cursor.getString(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_TIMESH)), new TypeToken<ArrayList<BusTime>>(){}.getType()); ArrayList<BusTime> timesC = new Gson().fromJson(cursor.getString(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_TIMESC)), new TypeToken<ArrayList<BusTime>>(){}.getType()); ArrayList<BusTime> timesP = new Gson().fromJson(cursor.getString(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_TIMESP)), new TypeToken<ArrayList<BusTime>>(){}.getType()); boolean timesHExists = (cursor.getInt(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_TIMESH_EXISTS)) == 1); boolean timesCExists = (cursor.getInt(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_TIMESC_EXISTS)) == 1); boolean timesPExists = (cursor.getInt(cursor.getColumnIndex(EshotroidProvider.Busses.COLUMN_TIMESP_EXISTS)) == 1); return new Bus(number, source, destination, route, isFavorited, timesH, timesC, timesP, timesHExists, timesCExists, timesPExists); } return null; } @Override public String toString() { return number + ": " + source + " - " + destination; } }