1 package de.example.exampletdd.model;
3 import java.util.Calendar;
6 import android.content.ContentValues;
7 import android.database.Cursor;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.database.sqlite.SQLiteOpenHelper;
11 public class WeatherLocationDbQueries {
12 private final SQLiteOpenHelper mDbHelper;
14 public interface DoQuery {
16 public WeatherLocation doQuery(final Cursor cursor);
19 public WeatherLocationDbQueries(final SQLiteOpenHelper dbHelper) {
20 this.mDbHelper = dbHelper;
23 public WeatherLocation queryDataBase() {
24 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
25 final String[] selectionArgs = { "1" };
26 final String[] projection = {
27 WeatherLocationContract.WeatherLocation._ID,
28 WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY,
29 WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY,
30 WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED,
31 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE,
32 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE,
33 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE,
34 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE
38 final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
41 public WeatherLocation doQuery(final Cursor cursor) {
42 final int id = cursor.getInt(cursor.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation._ID));
43 final String city = cursor.getString(cursor.
44 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY));
45 final String country = cursor.getString(cursor.
46 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY));
47 final boolean isSelected = (cursor.getInt(cursor
48 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED)) == 0) ? false : true;
49 Date lastCurrentUIUpdate = null;
50 if (!cursor.isNull(cursor
51 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE))) {
52 final long javaTime = cursor.getLong(cursor
53 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE));
54 lastCurrentUIUpdate = new Date(javaTime);
56 Date lasForecastUIUpdate = null;
57 if (!cursor.isNull(cursor
58 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE))) {
59 final long javaTime = cursor.getLong(cursor
60 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE));
61 lasForecastUIUpdate = new Date(javaTime);
63 final double latitude = cursor.getDouble(cursor.
64 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE));
65 final double longitude = cursor.getDouble(cursor.
66 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE));
69 return new WeatherLocation()
73 .setIsSelected(isSelected)
74 .setLastCurrentUIUpdate(lastCurrentUIUpdate)
75 .setLastForecastUIUpdate(lasForecastUIUpdate)
76 .setLatitude(latitude)
77 .setLongitude(longitude);
81 return this.queryDataBase(
82 WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
83 selectionArgs, selection, doQuery);
86 public long insertIntoDataBase(final WeatherLocation weatherLocation) {
87 // Create a new map of values, where column names are the keys
88 final ContentValues values = new ContentValues();
89 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
90 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
91 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
92 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
93 if (javaTime != null) {
94 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
96 javaTime = weatherLocation.getLastForecastUIUpdate();
97 if (javaTime != null) {
98 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
100 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
101 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
103 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
106 public void updateDataBase(final WeatherLocation weatherLocation) {
107 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
108 final String[] selectionArgs = { "1" };
109 // Create a new map of values, where column names are the keys
110 final ContentValues values = new ContentValues();
111 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
112 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
113 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
114 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
115 if (javaTime != null) {
116 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
118 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
120 javaTime = weatherLocation.getLastForecastUIUpdate();
121 if (javaTime != null) {
122 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
124 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
126 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
127 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
129 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
132 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
133 private WeatherLocation queryDataBase(final String table,
134 final String[] projection, final String[] selectionArgs,
135 final String selection, final DoQuery doQuery) {
136 // TODO: execute around idiom? I miss try/finally with resources
137 final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
139 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
141 if (!cursor.moveToFirst()) {
145 return doQuery.doQuery(cursor);
155 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
156 private long insertIntoDataBase(final String table, final ContentValues values) {
157 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
159 return db.insert(table, null, values);
165 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
166 private long updateDataBase(final String table, final String[] selectionArgs,
167 final String selection, final ContentValues values) {
168 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
170 return db.update(table, values, selection, selectionArgs);