1c0ee4df8f2c96fbcc6f0daf6bea33fa3c531e82
[JavaForFun] /
1 /**
2  * Copyright 2014 Gustavo Martin Morcuende
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package name.gumartinm.weather.information.model;
17
18 import java.util.Date;
19
20 import android.content.ContentValues;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.database.sqlite.SQLiteOpenHelper;
24
25 public class WeatherLocationDbQueries {
26         private final SQLiteOpenHelper mDbHelper;
27
28         public interface DoQuery {
29                 
30                 public WeatherLocation doQuery(final Cursor cursor);
31         }
32
33         public WeatherLocationDbQueries(final SQLiteOpenHelper dbHelper) {
34                 this.mDbHelper = dbHelper;
35         }
36         
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
50                     };
51         
52
53         final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
54
55                 @Override
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);
70                         }
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);
77                         }
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);
84                         
85                         
86                         return new WeatherLocation()
87                                         .setId(id)
88                                         .setCity(city)
89                                         .setCountry(country)
90                                         .setIsSelected(isSelected)
91                                         .setLastCurrentUIUpdate(lastCurrentUIUpdate)
92                                         .setLastForecastUIUpdate(lasForecastUIUpdate)
93                                         .setLatitude(latitude)
94                                         .setLongitude(longitude)
95                         .setIsNew(isNew);
96                 }
97         };
98
99         return this.queryDataBase(
100                         WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
101                         selectionArgs, selection, doQuery);
102     }
103         
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());
113                 }
114                 javaTime = weatherLocation.getLastForecastUIUpdate();
115                 if (javaTime != null) {
116                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
117                 }
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());
121                 
122                 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
123         }
124         
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());
136                 } else {
137                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
138                 }
139                 javaTime = weatherLocation.getLastForecastUIUpdate();
140                 if (javaTime != null) {
141                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
142                 } else {
143                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
144                 }
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());
148                 
149                 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
150         }
151         
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();
157         try {
158                 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
159                 try {
160                         if (!cursor.moveToFirst()) {
161                         return null;
162                         }
163                         else {
164                                 return doQuery.doQuery(cursor);
165                         }
166                 } finally {
167                         cursor.close();
168                 }
169         } finally {
170                 db.close();
171         }
172     }
173         
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();
177         try {
178                 return db.insert(table, null, values);
179         } finally {
180                 db.close();
181         }
182     }
183         
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();
188         try {
189                 return db.update(table, values, selection, selectionArgs);
190         } finally {
191                 db.close();
192         }
193     }
194         
195 }