d4e8622f3ea44b92dee1dabe1a13039d4e287821
[JavaForFun] /
1 package name.gumartinm.weather.information.model;
2
3 import java.util.Date;
4
5 import android.content.ContentValues;
6 import android.database.Cursor;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
9
10 public class WeatherLocationDbQueries {
11         private final SQLiteOpenHelper mDbHelper;
12
13         public interface DoQuery {
14                 
15                 public WeatherLocation doQuery(final Cursor cursor);
16         }
17
18         public WeatherLocationDbQueries(final SQLiteOpenHelper dbHelper) {
19                 this.mDbHelper = dbHelper;
20         }
21         
22         public WeatherLocation queryDataBase() {
23         final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
24         final String[] selectionArgs = { "1" };
25         final String[] projection = {
26                         WeatherLocationContract.WeatherLocation._ID,
27                         WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY,
28                         WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY,
29                         WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED,
30                         WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE,
31                         WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE,
32                         WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE,
33                         WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE,
34                 WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW
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);
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                 final boolean isNew = (cursor.getInt(cursor
68                         .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW)) != 0);
69                         
70                         
71                         return new WeatherLocation()
72                                         .setId(id)
73                                         .setCity(city)
74                                         .setCountry(country)
75                                         .setIsSelected(isSelected)
76                                         .setLastCurrentUIUpdate(lastCurrentUIUpdate)
77                                         .setLastForecastUIUpdate(lasForecastUIUpdate)
78                                         .setLatitude(latitude)
79                                         .setLongitude(longitude)
80                         .setIsNew(isNew);
81                 }
82         };
83
84         return this.queryDataBase(
85                         WeatherLocationContract.WeatherLocation.TABLE_NAME, projection,
86                         selectionArgs, selection, doQuery);
87     }
88         
89         public long insertIntoDataBase(final WeatherLocation weatherLocation) {
90                 // Create a new map of values, where column names are the keys
91                 final ContentValues values = new ContentValues();
92                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
93                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
94                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
95                 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
96                 if (javaTime != null) {
97                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
98                 }
99                 javaTime = weatherLocation.getLastForecastUIUpdate();
100                 if (javaTime != null) {
101                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
102                 }
103                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
104                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
105         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
106                 
107                 return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
108         }
109         
110         public void updateDataBase(final WeatherLocation weatherLocation) {
111                 final String selection = WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED + " = ?";
112         final String[] selectionArgs = { "1" };
113                 // Create a new map of values, where column names are the keys
114                 final ContentValues values = new ContentValues();
115                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
116                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
117                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
118                 Date javaTime = weatherLocation.getLastCurrentUIUpdate();
119                 if (javaTime != null) {
120                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
121                 } else {
122                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
123                 }
124                 javaTime = weatherLocation.getLastForecastUIUpdate();
125                 if (javaTime != null) {
126                         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
127                 } else {
128                         values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
129                 }
130                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
131                 values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
132         values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
133                 
134                 this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
135         }
136         
137         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
138         private WeatherLocation queryDataBase(final String table,
139                         final String[] projection, final String[] selectionArgs,
140                         final String selection, final DoQuery doQuery) {
141         final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
142         try {
143                 final Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, null);
144                 try {
145                         if (!cursor.moveToFirst()) {
146                         return null;
147                         }
148                         else {
149                                 return doQuery.doQuery(cursor);
150                         }
151                 } finally {
152                         cursor.close();
153                 }
154         } finally {
155                 db.close();
156         }
157     }
158         
159         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
160         private long insertIntoDataBase(final String table, final ContentValues values) {
161         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
162         try {
163                 return db.insert(table, null, values);
164         } finally {
165                 db.close();
166         }
167     }
168         
169         // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
170         private long updateDataBase(final String table, final String[] selectionArgs,
171                         final String selection, final ContentValues values) {
172         final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
173         try {
174                 return db.update(table, values, selection, selectionArgs);
175         } finally {
176                 db.close();
177         }
178     }
179         
180 }