fa97ca29b0baab15cee94412acc000e43d706b20
[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                     };
36         
37
38         final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
39
40                 @Override
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) ? false : true;  
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);
55                         }
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);
62                         }
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                         
68                         
69                         return new WeatherLocation()
70                                         .setId(id)
71                                         .setCity(city)
72                                         .setCountry(country)
73                                         .setIsSelected(isSelected)
74                                         .setLastCurrentUIUpdate(lastCurrentUIUpdate)
75                                         .setLastForecastUIUpdate(lasForecastUIUpdate)
76                                         .setLatitude(latitude)
77                                         .setLongitude(longitude);
78                 }
79         };
80
81         return this.queryDataBase(
82                         WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
83                         selectionArgs, selection, doQuery);
84     }
85         
86         public long insertIntoDataBase(final WeatherLocation weatherLocation) {
87                 // Create a new map of values, where column names are the keys
88                 final ContentValues values = new ContentValues();
89                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
90                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
91                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
92                 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
93                 if (javaTime != null) {
94                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
95                 }
96                 javaTime = weatherLocation.getLastForecastUIUpdate();
97                 if (javaTime != null) {
98                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
99                 }
100                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
101                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
102                 
103                 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
104         }
105         
106         public void updateDataBase(final WeatherLocation weatherLocation) {
107                 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
108         final String[] selectionArgs = { "1" };
109                 // Create a new map of values, where column names are the keys
110                 final ContentValues values = new ContentValues();
111                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
112                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
113                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
114                 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
115                 if (javaTime != null) {
116                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
117                 } else {
118                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
119                 }
120                 javaTime = weatherLocation.getLastForecastUIUpdate();
121                 if (javaTime != null) {
122                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
123                 } else {
124                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
125                 }
126                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
127                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
128                 
129                 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
130         }
131         
132         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
133         private WeatherLocation queryDataBase(final String table,
134                         final String[] projection, final String[] selectionArgs,
135                         final String selection, final DoQuery doQuery) {
136         // TODO: execute around idiom? I miss try/finally with resources
137         final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
138         try {
139                 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
140                 try {
141                         if (!cursor.moveToFirst()) {
142                         return null;
143                         }
144                         else {
145                                 return doQuery.doQuery(cursor);
146                         }
147                 } finally {
148                         cursor.close();
149                 }
150         } finally {
151                 db.close();
152         }
153     }
154         
155         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
156         private long insertIntoDataBase(final String table, final ContentValues values) {
157         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
158         try {
159                 return db.insert(table, null, values);
160         } finally {
161                 db.close();
162         }
163     }
164         
165         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
166         private long updateDataBase(final String table, final String[] selectionArgs,
167                         final String selection, final ContentValues values) {
168         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
169         try {
170                 return db.update(table, values, selection, selectionArgs);
171         } finally {
172                 db.close();
173         }
174     }
175         
176 }