a4f3f56ca03a4dce73c4728c3a5cd369323e1139
[JavaForFun] /
1 package de.example.exampletdd.model;
2
3 import java.util.Calendar;
4 import java.util.Date;
5
6 import android.content.ContentValues;
7 import android.database.Cursor;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.database.sqlite.SQLiteOpenHelper;
10
11 public class WeatherLocationDbQueries {
12         private final SQLiteOpenHelper mDbHelper;
13
14         public interface DoQuery {
15                 
16                 public WeatherLocation doQuery(final Cursor cursor);
17         }
18
19         public WeatherLocationDbQueries(final SQLiteOpenHelper dbHelper) {
20                 this.mDbHelper = dbHelper;
21         }
22         
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
36                     };
37         
38
39         final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
40
41                 @Override
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);
56                         }
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);
63                         }
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);
70                         
71                         
72                         return new WeatherLocation()
73                                         .setId(id)
74                                         .setCity(city)
75                                         .setCountry(country)
76                                         .setIsSelected(isSelected)
77                                         .setLastCurrentUIUpdate(lastCurrentUIUpdate)
78                                         .setLastForecastUIUpdate(lasForecastUIUpdate)
79                                         .setLatitude(latitude)
80                                         .setLongitude(longitude)
81                         .setIsNew(isNew);
82                 }
83         };
84
85         return this.queryDataBase(
86                         WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
87                         selectionArgs, selection, doQuery);
88     }
89         
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());
99                 }
100                 javaTime = weatherLocation.getLastForecastUIUpdate();
101                 if (javaTime != null) {
102                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
103                 }
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());
107                 
108                 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
109         }
110         
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());
122                 } else {
123                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
124                 }
125                 javaTime = weatherLocation.getLastForecastUIUpdate();
126                 if (javaTime != null) {
127                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
128                 } else {
129                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
130                 }
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());
134                 
135                 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
136         }
137         
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         // TODO: execute around idiom? I miss try/finally with resources
143         final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
144         try {
145                 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
146                 try {
147                         if (!cursor.moveToFirst()) {
148                         return null;
149                         }
150                         else {
151                                 return doQuery.doQuery(cursor);
152                         }
153                 } finally {
154                         cursor.close();
155                 }
156         } finally {
157                 db.close();
158         }
159     }
160         
161         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
162         private long insertIntoDataBase(final String table, final ContentValues values) {
163         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
164         try {
165                 return db.insert(table, null, values);
166         } finally {
167                 db.close();
168         }
169     }
170         
171         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
172         private long updateDataBase(final String table, final String[] selectionArgs,
173                         final String selection, final ContentValues values) {
174         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
175         try {
176                 return db.update(table, values, selection, selectionArgs);
177         } finally {
178                 db.close();
179         }
180     }
181         
182 }