1 package name.gumartinm.weather.information.model;
5 import android.content.ContentValues;
6 import android.database.Cursor;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
10 public class WeatherLocationDbQueries {
11 private final SQLiteOpenHelper mDbHelper;
13 public interface DoQuery {
15 public WeatherLocation doQuery(final Cursor cursor);
18 public WeatherLocationDbQueries(final SQLiteOpenHelper dbHelper) {
19 this.mDbHelper = dbHelper;
22 public WeatherLocation queryDataBase() {
23 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
24 final String[] selectionArgs = { "1" };
25 final String[] projection = {
26 WeatherLocationContract.WeatherLocation._ID,
27 WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY,
28 WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY,
29 WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED,
30 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE,
31 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE,
32 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE,
33 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE,
34 WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW
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);
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));
67 final boolean isNew = (cursor.getInt(cursor
68 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW)) != 0);
71 return new WeatherLocation()
75 .setIsSelected(isSelected)
76 .setLastCurrentUIUpdate(lastCurrentUIUpdate)
77 .setLastForecastUIUpdate(lasForecastUIUpdate)
78 .setLatitude(latitude)
79 .setLongitude(longitude)
84 return this.queryDataBase(
85 WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
86 selectionArgs, selection, doQuery);
89 public long insertIntoDataBase(final WeatherLocation weatherLocation) {
90 // Create a new map of values, where column names are the keys
91 final ContentValues values = new ContentValues();
92 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
93 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
94 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
95 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
96 if (javaTime != null) {
97 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
99 javaTime = weatherLocation.getLastForecastUIUpdate();
100 if (javaTime != null) {
101 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
103 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
104 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
105 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
107 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
110 public void updateDataBase(final WeatherLocation weatherLocation) {
111 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
112 final String[] selectionArgs = { "1" };
113 // Create a new map of values, where column names are the keys
114 final ContentValues values = new ContentValues();
115 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
116 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
117 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
118 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
119 if (javaTime != null) {
120 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
122 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
124 javaTime = weatherLocation.getLastForecastUIUpdate();
125 if (javaTime != null) {
126 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
128 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
130 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
131 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
132 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
134 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
137 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
138 private WeatherLocation queryDataBase(final String table,
139 final String[] projection, final String[] selectionArgs,
140 final String selection, final DoQuery doQuery) {
141 final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
143 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
145 if (!cursor.moveToFirst()) {
149 return doQuery.doQuery(cursor);
159 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
160 private long insertIntoDataBase(final String table, final ContentValues values) {
161 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
163 return db.insert(table, null, values);
169 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
170 private long updateDataBase(final String table, final String[] selectionArgs,
171 final String selection, final ContentValues values) {
172 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
174 return db.update(table, values, selection, selectionArgs);