e35dc183652f503ff035d98d643c0b10397a157e
[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         final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
143         try {
144                 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
145                 try {
146                         if (!cursor.moveToFirst()) {
147                         return null;
148                         }
149                         else {
150                                 return doQuery.doQuery(cursor);
151                         }
152                 } finally {
153                         cursor.close();
154                 }
155         } finally {
156                 db.close();
157         }
158     }
159         
160         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
161         private long insertIntoDataBase(final String table, final ContentValues values) {
162         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
163         try {
164                 return db.insert(table, null, values);
165         } finally {
166                 db.close();
167         }
168     }
169         
170         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
171         private long updateDataBase(final String table, final String[] selectionArgs,
172                         final String selection, final ContentValues values) {
173         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
174         try {
175                 return db.update(table, values, selection, selectionArgs);
176         } finally {
177                 db.close();
178         }
179     }
180         
181 }