2 * Copyright 2014 Gustavo Martin Morcuende
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package name.gumartinm.weather.information.model;
18 import java.util.Date;
20 import android.content.ContentValues;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.database.sqlite.SQLiteOpenHelper;
25 public class WeatherLocationDbQueries {
26 private final SQLiteOpenHelper mDbHelper;
28 public interface DoQuery {
30 public WeatherLocation doQuery(final Cursor cursor);
33 public WeatherLocationDbQueries(final SQLiteOpenHelper dbHelper) {
34 this.mDbHelper = dbHelper;
37 public WeatherLocation queryDataBase() {
38 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
39 final String[] selectionArgs = { "1" };
40 final String[] projection = {
41 WeatherLocationContract.WeatherLocation._ID,
42 WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY,
43 WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY,
44 WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED,
45 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE,
46 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE,
47 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE,
48 WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE,
49 WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW
53 final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
56 public WeatherLocation doQuery(final Cursor cursor) {
57 final int id = cursor.getInt(cursor.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation._ID));
58 final String city = cursor.getString(cursor.
59 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY));
60 final String country = cursor.getString(cursor.
61 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY));
62 final boolean isSelected = (cursor.getInt(cursor
63 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED)) != 0);
64 Date lastCurrentUIUpdate = null;
65 if (!cursor.isNull(cursor
66 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE))) {
67 final long javaTime = cursor.getLong(cursor
68 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE));
69 lastCurrentUIUpdate = new Date(javaTime);
71 Date lasForecastUIUpdate = null;
72 if (!cursor.isNull(cursor
73 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE))) {
74 final long javaTime = cursor.getLong(cursor
75 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE));
76 lasForecastUIUpdate = new Date(javaTime);
78 final double latitude = cursor.getDouble(cursor.
79 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE));
80 final double longitude = cursor.getDouble(cursor.
81 getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE));
82 final boolean isNew = (cursor.getInt(cursor
83 .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW)) != 0);
86 return new WeatherLocation()
90 .setIsSelected(isSelected)
91 .setLastCurrentUIUpdate(lastCurrentUIUpdate)
92 .setLastForecastUIUpdate(lasForecastUIUpdate)
93 .setLatitude(latitude)
94 .setLongitude(longitude)
99 return this.queryDataBase(
100 WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
101 selectionArgs, selection, doQuery);
104 public long insertIntoDataBase(final WeatherLocation weatherLocation) {
105 // Create a new map of values, where column names are the keys
106 final ContentValues values = new ContentValues();
107 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
108 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
109 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
110 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
111 if (javaTime != null) {
112 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
114 javaTime = weatherLocation.getLastForecastUIUpdate();
115 if (javaTime != null) {
116 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
118 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
119 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
120 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
122 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
125 public void updateDataBase(final WeatherLocation weatherLocation) {
126 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
127 final String[] selectionArgs = { "1" };
128 // Create a new map of values, where column names are the keys
129 final ContentValues values = new ContentValues();
130 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
131 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
132 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
133 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
134 if (javaTime != null) {
135 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
137 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
139 javaTime = weatherLocation.getLastForecastUIUpdate();
140 if (javaTime != null) {
141 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
143 values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
145 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
146 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
147 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
149 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
152 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
153 private WeatherLocation queryDataBase(final String table,
154 final String[] projection, final String[] selectionArgs,
155 final String selection, final DoQuery doQuery) {
156 final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
158 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
160 if (!cursor.moveToFirst()) {
164 return doQuery.doQuery(cursor);
174 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
175 private long insertIntoDataBase(final String table, final ContentValues values) {
176 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
178 return db.insert(table, null, values);
184 // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
185 private long updateDataBase(final String table, final String[] selectionArgs,
186 final String selection, final ContentValues values) {
187 final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
189 return db.update(table, values, selection, selectionArgs);