· 6 years ago · May 14, 2019, 12:08 PM
1package rtrk.pnrs.weatherforecast.MyLittleHelpers;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.provider.BaseColumns;
9import android.util.Log;
10
11public class DBWeatherHelper extends SQLiteOpenHelper implements BaseColumns {
12
13 private static final String DATABASE_NAME = "weather.db";
14 private static final int DATABASE_VERSION = 1;
15
16 private static final String TABLE_NAME = "weather";
17
18 private static final String COLUMN_CITY = "city";
19 private static final String COLUMN_DATE = "date";
20 private static final String COLUMN_WEEKDAY = "weekday";
21 private static final String COLUMN_TEMPERATURE = "temperature";
22 private static final String COLUMN_PRESSURE = "pressure";
23 private static final String COLUMN_HUMIDITY = "humidity";
24 private static final String COLUMN_SUNRISE = "sunrise";
25 private static final String COLUMN_SUNSET = "sunset";
26 private static final String COLUMN_WIND_SPEED = "wind_speed";
27 private static final String COLUMN_WIND_DIR = "wind_direction";
28
29
30 public DBWeatherHelper(Context context) {
31 super(context, DATABASE_NAME, null, DATABASE_VERSION);
32 }
33
34 @Override
35 public void onCreate(SQLiteDatabase db) {
36 final String SQL_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
37 _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
38 COLUMN_CITY + " TEXT NOT NULL," +
39 COLUMN_DATE + " TEXT NOT NULL," +
40 COLUMN_WEEKDAY + " TEXT NOT NULL," +
41 COLUMN_TEMPERATURE + " DOUBLE NOT NULL," +
42 COLUMN_PRESSURE + " DOUBLE NOT NULL," +
43 COLUMN_HUMIDITY + " DOUBLE NOT NULL," +
44 COLUMN_SUNRISE + " TEXT NOT NULL," +
45 COLUMN_SUNSET + " TEXT NOT NULL," +
46 COLUMN_WIND_SPEED + " DOUBLE NOT NULL," +
47 COLUMN_WIND_DIR + " TEXT NOT NULL" +
48 ");";
49 db.execSQL(SQL_TABLE);
50 }
51
52 @Override
53 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
54 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
55 onCreate(db);
56 }
57
58 //Add a new row to the database
59 public boolean insert(Forecast forecast) {
60 SQLiteDatabase dbWrite = this.getWritableDatabase();
61 ContentValues contentValues = new ContentValues();
62
63 Forecast f = check(forecast.getCity(), forecast.getDate());
64
65 contentValues.put(COLUMN_CITY, forecast.getCity());
66 contentValues.put(COLUMN_DATE, forecast.getDate());
67 contentValues.put(COLUMN_WEEKDAY, forecast.getWeekDay());
68 contentValues.put(COLUMN_TEMPERATURE, forecast.getTemperature());
69 contentValues.put(COLUMN_PRESSURE, forecast.getPressure());
70 contentValues.put(COLUMN_HUMIDITY, forecast.getHumidity());
71 contentValues.put(COLUMN_SUNRISE, forecast.getSunrise());
72 contentValues.put(COLUMN_SUNSET, forecast.getSunset());
73 contentValues.put(COLUMN_WIND_SPEED, forecast.getWindSpeed());
74 contentValues.put(COLUMN_WIND_DIR, forecast.getWindDirection());
75
76 if (f == null) {
77
78 Log.d("USAO SAM DA UBACIM", "MEEEETNEEEEEM");
79 if (dbWrite.insert(TABLE_NAME, null, contentValues) == -1) {
80 dbWrite.close();
81 return false;
82 } else {
83 dbWrite.close();
84 return true;
85 }
86
87 } else {
88 Log.d("USAO SAM DA UPDATE", "UPDAAAAAAATEEEEEEEE");
89 dbWrite.update(TABLE_NAME, contentValues, COLUMN_CITY + " =? AND " + COLUMN_DATE + " =? ", new String[]{forecast.getCity(), forecast.getDate()});
90 dbWrite.close();
91
92 return true;
93 }
94 }
95
96 private Forecast check(String city, String date) {
97 SQLiteDatabase db = this.getReadableDatabase();
98
99 Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + date + "\" ;", null, null);
100
101 if (cursor.getCount() <= 0) {
102 return null;
103 }
104
105 cursor.moveToFirst();
106
107 return createForecastItem(cursor);
108 }
109
110 //Delete data from the database
111 public boolean remove(String city) {
112 SQLiteDatabase db = this.getWritableDatabase();
113
114 if (db.delete(TABLE_NAME, COLUMN_CITY + "=?", new String[]{city}) == -1) {
115 db.close();
116 return false;
117 } else {
118 db.close();
119 return true;
120 }
121 }
122
123 public Forecast[] getItems(String gotham, int batman) {
124 SQLiteDatabase db = this.getReadableDatabase();
125
126 Cursor cursor = null;
127
128 if (batman == 0)
129 cursor = db.query(TABLE_NAME, null, COLUMN_CITY + "=?", new String[]{gotham}, null, null, COLUMN_TEMPERATURE, null);
130 else if (batman == 1)
131 cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + gotham + "\" and " + COLUMN_TEMPERATURE + " = " +
132 "(SELECT MIN(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + gotham + "\");", null, null);
133 else if (batman == 2)
134 cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + gotham + "\" and " + COLUMN_TEMPERATURE + " = " +
135 "(SELECT MAX(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + gotham + "\");", null, null);
136
137 assert cursor != null;
138 if (cursor.getCount() <= 0)
139 return null;
140
141 Forecast[] forecasts = new Forecast[cursor.getCount()];
142
143 int i = 0;
144 for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()) {
145 forecasts[i++] = createForecastItem(cursor);
146 }
147
148 db.close();
149
150 return forecasts;
151 }
152
153 public String[] getCities() {
154 SQLiteDatabase db = this.getReadableDatabase();
155 Cursor cursor = db.rawQuery("SELECT " + COLUMN_CITY + " FROM " + TABLE_NAME + " GROUP BY " + COLUMN_CITY + " ;", null, null);
156
157 if (cursor.getCount() <= 0)
158 return null;
159
160 String[] cities = new String[cursor.getCount()];
161
162 int i = 0;
163 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
164 cities[i++] = cursor.getString(0);
165
166 db.close();
167 cursor.close();
168
169 return cities;
170 }
171
172 public Forecast getItem(String city) {
173 SQLiteDatabase db = getReadableDatabase();
174 Cursor cursor = db.query(TABLE_NAME, null, COLUMN_CITY + "=?", new String[]{city}, null, null, null, null);
175
176 if (cursor.getCount() <= 0)
177 return null;
178
179 cursor.moveToLast();
180 Forecast forecast = createForecastItem(cursor);
181
182 cursor.close();
183 db.close();
184
185 return forecast;
186 }
187
188 public Forecast getItemByWeekDay(String city, String weekday, int x) {
189 SQLiteDatabase db = getReadableDatabase();
190 Cursor cursor = null;
191
192 if (x == 0)
193 cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_WEEKDAY + " = \"" + weekday + "\" ;", null, null);
194 if (x == 1)
195 cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_WEEKDAY + " = \"" + weekday + "\" AND " + COLUMN_TEMPERATURE + " >= 10 ;", null, null);
196 if (x == 2)
197 cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_WEEKDAY + " = \"" + weekday + "\" AND " + COLUMN_TEMPERATURE + " < 10 ;", null, null);
198
199 assert cursor != null;
200 if (cursor.getCount() <= 0)
201 return null;
202
203 cursor.moveToLast();
204 Forecast forecast = createForecastItem(cursor);
205
206 cursor.close();
207 db.close();
208
209 return forecast;
210 }
211
212 private Forecast createForecastItem(Cursor cursor) {
213 String city = cursor.getString(cursor.getColumnIndex(COLUMN_CITY));
214 String date = cursor.getString(cursor.getColumnIndex(COLUMN_DATE));
215 String weekday = cursor.getString(cursor.getColumnIndex(COLUMN_WEEKDAY));
216 double temperature = cursor.getDouble(cursor.getColumnIndex(COLUMN_TEMPERATURE));
217 double humidity = cursor.getDouble(cursor.getColumnIndex(COLUMN_HUMIDITY));
218 double pressure = cursor.getDouble(cursor.getColumnIndex(COLUMN_PRESSURE));
219 String sunrise = cursor.getString(cursor.getColumnIndex(COLUMN_SUNRISE));
220 String sunset = cursor.getString(cursor.getColumnIndex(COLUMN_SUNSET));
221 double wind_speed = cursor.getDouble(cursor.getColumnIndex(COLUMN_WIND_SPEED));
222 String wind_direction = cursor.getString(cursor.getColumnIndex(COLUMN_WIND_DIR));
223
224 return new Forecast(city, date, weekday, temperature, humidity, pressure, sunrise, sunset, wind_speed, wind_direction);
225 }
226}