| 1 | /* |
| 2 | * Copyright (C) 2010, 2011 Keith Kildare |
| 3 | * |
| 4 | * This file is part of SimplyDo. |
| 5 | * |
| 6 | * SimplyDo is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * SimplyDo is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with SimplyDo. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | package kdk.android.simplydo; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | import android.content.Context; |
| 26 | import android.database.Cursor; |
| 27 | import android.database.sqlite.SQLiteDatabase; |
| 28 | import android.database.sqlite.SQLiteOpenHelper; |
| 29 | import android.database.sqlite.SQLiteStatement; |
| 30 | import android.util.Log; |
| 31 | |
| 32 | public class DataManager |
| 33 | { |
| 34 | public static final String DATABASE_NAME = "simplydo.db"; |
| 35 | private static final int DATABASE_VERSION = 1; |
| 36 | |
| 37 | private static class DatabaseHelper extends SQLiteOpenHelper |
| 38 | { |
| 39 | |
| 40 | DatabaseHelper(Context context) |
| 41 | { |
| 42 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void onCreate(SQLiteDatabase db) |
| 47 | { |
| 48 | db.execSQL("CREATE TABLE lists (" |
| 49 | + "id INTEGER PRIMARY KEY," |
| 50 | + "label TEXT" |
| 51 | + ");"); |
| 52 | db.execSQL("CREATE TABLE items (" |
| 53 | + "id INTEGER PRIMARY KEY," |
| 54 | + "list_id INTEGER," |
| 55 | + "label TEXT," |
| 56 | + "active INTEGER," |
| 57 | + "star INTEGER" |
| 58 | + ");"); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) |
| 63 | { |
| 64 | Log.d(L.TAG, "Got callback to upgrading database from version " + |
| 65 | oldVersion + |
| 66 | " to " |
| 67 | + newVersion); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private DatabaseHelper helper; |
| 72 | |
| 73 | |
| 74 | public DataManager(Context ctx) |
| 75 | { |
| 76 | helper = new DatabaseHelper(ctx); |
| 77 | } |
| 78 | |
| 79 | public List<ListDesc> fetchLists() |
| 80 | { |
| 81 | List<ListDesc> rv = new ArrayList<ListDesc>(); |
| 82 | SQLiteDatabase db = helper.getReadableDatabase(); |
| 83 | Cursor cursor = db.query("lists", new String[] { "id", "label" }, |
| 84 | null, null, null, null, "id desc"); |
| 85 | |
| 86 | if (cursor.moveToFirst()) |
| 87 | { |
| 88 | do |
| 89 | { |
| 90 | ListDesc list = new ListDesc(cursor.getInt(0), cursor.getString(1), 0, 0); |
| 91 | list.setTotalItems(countItemsInList(db, list.getId())); |
| 92 | list.setActiveItems(countInactiveItemsInList(db, list.getId())); |
| 93 | rv.add(list); |
| 94 | } while (cursor.moveToNext()); |
| 95 | } |
| 96 | if (cursor != null && !cursor.isClosed()) { |
| 97 | cursor.close(); |
| 98 | } |
| 99 | |
| 100 | Log.d(L.TAG, "fetchLists returned " + rv.size() + " items"); |
| 101 | |
| 102 | db.close(); |
| 103 | return rv; |
| 104 | } |
| 105 | |
| 106 | public void fetchItems(int listId, List<ItemDesc> rv) |
| 107 | { |
| 108 | SQLiteDatabase db = helper.getReadableDatabase(); |
| 109 | Cursor cursor = db.query("items", new String[] { "id", "label", "active", "star" }, |
| 110 | "list_id=?", new String[]{"" + listId}, null, null, "id desc"); |
| 111 | |
| 112 | if (cursor.moveToFirst()) |
| 113 | { |
| 114 | do |
| 115 | { |
| 116 | ItemDesc list = new ItemDesc( |
| 117 | cursor.getInt(0), |
| 118 | cursor.getString(1), |
| 119 | cursor.getInt(2) != 0, |
| 120 | cursor.getInt(3) != 0); |
| 121 | rv.add(list); |
| 122 | } while (cursor.moveToNext()); |
| 123 | } |
| 124 | if (cursor != null && !cursor.isClosed()) |
| 125 | { |
| 126 | cursor.close(); |
| 127 | } |
| 128 | |
| 129 | Log.d(L.TAG, "fetchItems returned " + rv.size() + " items"); |
| 130 | |
| 131 | db.close(); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | public void updateItemActiveness(int itemId, boolean active) |
| 136 | { |
| 137 | Log.d(L.TAG, "Setting active property of " + itemId + " to " + active); |
| 138 | |
| 139 | //long t = System.currentTimeMillis(); |
| 140 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 141 | //long t2 = System.currentTimeMillis(); |
| 142 | //Log.d(TAG, "Open DB took " + ((t2-t)/1000.0)); |
| 143 | //t = t2; |
| 144 | SQLiteStatement stmt = db.compileStatement("update items set active=? where id=?"); |
| 145 | stmt.bindLong(1, active?1:0); |
| 146 | stmt.bindLong(2, itemId); |
| 147 | //t2 = System.currentTimeMillis(); |
| 148 | //Log.d(TAG, "stmt creation took " + ((t2-t)/1000.0)); |
| 149 | //t = t2; |
| 150 | stmt.execute(); |
| 151 | //t2 = System.currentTimeMillis(); |
| 152 | //Log.d(TAG, "execute() took " + ((t2-t)/1000.0)); |
| 153 | //t = t2; |
| 154 | stmt.close(); |
| 155 | db.close(); |
| 156 | //t2 = System.currentTimeMillis(); |
| 157 | //Log.d(TAG, "close() took " + ((t2-t)/1000.0)); |
| 158 | //t = t2; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | public void updateItemStarness(int itemId, boolean star) |
| 163 | { |
| 164 | Log.d(L.TAG, "Setting star property of " + itemId + " to " + star); |
| 165 | |
| 166 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 167 | SQLiteStatement stmt = db.compileStatement("update items set star=? where id=?"); |
| 168 | stmt.bindLong(1, star?1:0); |
| 169 | stmt.bindLong(2, itemId); |
| 170 | stmt.execute(); |
| 171 | stmt.close(); |
| 172 | db.close(); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | public void updateItemLabel(int itemId, String newLabel) |
| 177 | { |
| 178 | Log.d(L.TAG, "Updating label of item " + itemId + " to " + newLabel); |
| 179 | |
| 180 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 181 | SQLiteStatement stmt = db.compileStatement("update items set label=? where id=?"); |
| 182 | stmt.bindString(1, newLabel); |
| 183 | stmt.bindLong(2, itemId); |
| 184 | stmt.execute(); |
| 185 | stmt.close(); |
| 186 | db.close(); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | public void updateListLabel(int listId, String newLabel) |
| 191 | { |
| 192 | Log.d(L.TAG, "Updating label of list " + listId + " to " + newLabel); |
| 193 | |
| 194 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 195 | SQLiteStatement stmt = db.compileStatement("update lists set label=? where id=?"); |
| 196 | stmt.bindString(1, newLabel); |
| 197 | stmt.bindLong(2, listId); |
| 198 | stmt.execute(); |
| 199 | stmt.close(); |
| 200 | db.close(); |
| 201 | } |
| 202 | |
| 203 | public void moveItem(int itemId, int toListId) |
| 204 | { |
| 205 | Log.d(L.TAG, "Moving item " + itemId + " to list " + toListId); |
| 206 | |
| 207 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 208 | SQLiteStatement stmt = db.compileStatement("update items set list_id=? where id=?"); |
| 209 | stmt.bindLong(1, toListId); |
| 210 | stmt.bindLong(2, itemId); |
| 211 | stmt.execute(); |
| 212 | stmt.close(); |
| 213 | db.close(); |
| 214 | } |
| 215 | |
| 216 | public void createList(String label) |
| 217 | { |
| 218 | Log.d(L.TAG, "Insert list " + label); |
| 219 | |
| 220 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 221 | SQLiteStatement stmt = db.compileStatement("insert into lists (label) values (?)"); |
| 222 | stmt.bindString(1, label); |
| 223 | stmt.executeInsert(); |
| 224 | stmt.close(); |
| 225 | db.close(); |
| 226 | } |
| 227 | |
| 228 | public int createItem(int list_id, String label) |
| 229 | { |
| 230 | Log.v(L.TAG, "DataManager.createItem(): Insert item " + label); |
| 231 | |
| 232 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 233 | SQLiteStatement stmt = db.compileStatement("insert into items (list_id,label,active) values (?,?,?)"); |
| 234 | stmt.bindLong(1, list_id); |
| 235 | stmt.bindString(2, label); |
| 236 | stmt.bindLong(3, 1); |
| 237 | long id = stmt.executeInsert(); |
| 238 | stmt.close(); |
| 239 | db.close(); |
| 240 | |
| 241 | Log.d(L.TAG, "DataManager.createItem(): Inserted item and got id " + id); |
| 242 | if(id == -1) |
| 243 | { |
| 244 | Log.e(L.TAG, "DataManager.createItem(): Attempt to insert item failed. Got " + id + " from executeInsert()"); |
| 245 | } |
| 246 | return (int)id; |
| 247 | } |
| 248 | |
| 249 | public void deleteInactive(int list_id) |
| 250 | { |
| 251 | Log.d(L.TAG, "Deleting inactive items from list " + list_id); |
| 252 | |
| 253 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 254 | SQLiteStatement stmt = db.compileStatement("delete from items where list_id=? and active=0"); |
| 255 | stmt.bindLong(1, list_id); |
| 256 | stmt.execute(); |
| 257 | stmt.close(); |
| 258 | db.close(); |
| 259 | } |
| 260 | |
| 261 | public void deleteList(int list_id) |
| 262 | { |
| 263 | Log.d(L.TAG, "Deleting list " + list_id); |
| 264 | |
| 265 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 266 | |
| 267 | SQLiteStatement stmt = db.compileStatement("delete from items where list_id=?"); |
| 268 | stmt.bindLong(1, list_id); |
| 269 | stmt.execute(); |
| 270 | stmt.close(); |
| 271 | |
| 272 | SQLiteStatement stmt2 = db.compileStatement("delete from lists where id=?"); |
| 273 | stmt2.bindLong(1, list_id); |
| 274 | stmt2.execute(); |
| 275 | stmt2.close(); |
| 276 | db.close(); |
| 277 | } |
| 278 | |
| 279 | public void deleteItem(int itemId) |
| 280 | { |
| 281 | Log.d(L.TAG, "Deleting item " + itemId); |
| 282 | |
| 283 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 284 | |
| 285 | SQLiteStatement stmt = db.compileStatement("delete from items where id=?"); |
| 286 | stmt.bindLong(1, itemId); |
| 287 | stmt.execute(); |
| 288 | stmt.close(); |
| 289 | db.close(); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | private int countItemsInList(SQLiteDatabase db, int listId) |
| 294 | { |
| 295 | SQLiteStatement stmt = db.compileStatement("select count(*) from items where list_id=?"); |
| 296 | stmt.bindLong(1, listId); |
| 297 | int rv = (int)stmt.simpleQueryForLong(); |
| 298 | stmt.close(); |
| 299 | |
| 300 | return rv; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | private int countInactiveItemsInList(SQLiteDatabase db, int listId) |
| 305 | { |
| 306 | SQLiteStatement stmt = db.compileStatement("select count(*) from items where list_id=? and active=1"); |
| 307 | stmt.bindLong(1, listId); |
| 308 | int rv = (int)stmt.simpleQueryForLong(); |
| 309 | stmt.close(); |
| 310 | |
| 311 | return rv; |
| 312 | } |
| 313 | } |