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,
35 WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW
39 final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
42 public WeatherLocation doQuery(final Cursor cursor) {
43 final int id = cursor.getInt(cursor.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation._ID));
44 final String city = cursor.getString(cursor.
45 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY));
46 final String country = cursor.getString(cursor.
47 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY));
48 final boolean isSelected = (cursor.getInt(cursor
49 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED)) != 0);
50 Date lastCurrentUIUpdate = null;
51 if (!cursor.isNull(cursor
52 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE))) {
53 final long javaTime = cursor.getLong(cursor
54 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE));
55 lastCurrentUIUpdate = new Date(javaTime);
57 Date lasForecastUIUpdate = null;
58 if (!cursor.isNull(cursor
59 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE))) {
60 final long javaTime = cursor.getLong(cursor
61 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE));
62 lasForecastUIUpdate = new Date(javaTime);
64 final double latitude = cursor.getDouble(cursor.
65 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE));
66 final double longitude = cursor.getDouble(cursor.
67 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE));
68 final boolean isNew = (cursor.getInt(cursor
69 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW)) != 0);
72 return new WeatherLocation()
76 .setIsSelected(isSelected)
77 .setLastCurrentUIUpdate(lastCurrentUIUpdate)
78 .setLastForecastUIUpdate(lasForecastUIUpdate)
79 .setLatitude(latitude)
80 .setLongitude(longitude)
85 return this.queryDataBase(
86 WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
87 selectionArgs, selection, doQuery);
90 public long insertIntoDataBase(final WeatherLocation weatherLocation) {
91 // Create a new map of values, where column names are the keys
92 final ContentValues values = new ContentValues();
93 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
94 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
95 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
96 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
97 if (javaTime != null) {
98 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
100 javaTime = weatherLocation.getLastForecastUIUpdate();
101 if (javaTime != null) {
102 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
104 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
105 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
106 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
108 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
111 public void updateDataBase(final WeatherLocation weatherLocation) {
112 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
113 final String[] selectionArgs = { "1" };
114 // Create a new map of values, where column names are the keys
115 final ContentValues values = new ContentValues();
116 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
117 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
118 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
119 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
120 if (javaTime != null) {
121 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
123 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
125 javaTime = weatherLocation.getLastForecastUIUpdate();
126 if (javaTime != null) {
127 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
129 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
131 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
132 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
133 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
135 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
138 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
139 private WeatherLocation queryDataBase(final String table,
140 final String[] projection, final String[] selectionArgs,
141 final String selection, final DoQuery doQuery) {
142 final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
144 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
146 if (!cursor.moveToFirst()) {
150 return doQuery.doQuery(cursor);
160 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
161 private long insertIntoDataBase(final String table, final ContentValues values) {
162 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
164 return db.insert(table, null, values);
170 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
171 private long updateDataBase(final String table, final String[] selectionArgs,
172 final String selection, final ContentValues values) {
173 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
175 return db.update(table, values, selection, selectionArgs);