package org.smartpaws.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import org.smartpaws.MainActivity;
import org.smartpaws.net.DataMan;
import org.smartpaws.objects.Convention;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.SQLDataException;
public class DBMS extends SQLiteOpenHelper {
public DBMS(Context context) {
super(context, "cached_data.db", null, 3);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE sp_cached_json (id INTEGER PRIMARY KEY AUTOINCREMENT, convention_name TEXT UNIQUE NOT NULL, json TEXT NOT NULL)");
db.execSQL("CREATE TABLE sp_cached_img (id INTEGER PRIMARY KEY AUTOINCREMENT, convention_name TEXT UNIQUE NOT NULL, img_res TEXT UNIQUE NOT NULL, data BLOB NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 1 || oldVersion == 2) {
db.execSQL("CREATE TABLE sp_cached_img (id INTEGER PRIMARY KEY AUTOINCREMENT, convention_name TEXT UNIQUE NOT NULL, img_res TEXT UNIQUE NOT NULL, data BLOB NOT NULL)");
}
}
public Convention getConvention(String conventionName) {
try {
Cursor cursor = getReadableDatabase().query("sp_cached_json", new String[] { "convention_name", "json" }, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
try {
String name = cursor.getString(0);
if (!name.equalsIgnoreCase(conventionName)) continue;
String json = cursor.getString(1);
Log.e(MainActivity.APP_NAME, json);
if (json != null) return DataMan.GSON.fromJson(json, Convention.class);
} catch (Exception ex) {
ex.printStackTrace();
}
cursor.moveToNext();
}
}
if (cursor != null && !cursor.isClosed()) cursor.close();
} catch (SQLiteException ex) {
Log.e(MainActivity.APP_NAME, "getConvention call for " + conventionName + " in SQLite database returned an exception: ", ex);
}
return null;
}
public void setConvention(String conventionName, Convention convention) {
try {
final String json = DataMan.GSON.toJson(convention);
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("convention_name", conventionName);
values.put("json", json);
db.delete("sp_cached_json", "convention_name = ?", new String[] { conventionName });
db.insert("sp_cached_json", null, values);
} catch (SQLiteException ex) {
Log.e(MainActivity.APP_NAME, "setConvention call for " + conventionName + " in SQLite database returned an exception: ", ex);
}
}
public Bitmap getImage(String imageRes) {
try {
Cursor cursor = getReadableDatabase().query("sp_cached_img", new String[] { "img_res", "data" }, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
try {
String imageResDb = cursor.getString(0);
if (!imageResDb.equalsIgnoreCase(imageRes)) continue;
byte[] data = cursor.getBlob(1);
Log.e(MainActivity.APP_NAME, imageResDb);
if (data != null && data.length > 0) return BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (Exception ex) {
ex.printStackTrace();
}
cursor.moveToNext();
}
}
if (cursor != null && !cursor.isClosed()) cursor.close();
} catch (SQLiteException ex) {
Log.e(MainActivity.APP_NAME, "getImage call for \"" + imageRes + "\" in SQLite database returned an exception: ", ex);
}
return null;
}
public void setImage(String imageRes, Bitmap image) {
try {
ByteArrayOutputStream imgOut = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 0, imgOut);
imgOut.flush();
byte[] data = imgOut.toByteArray();
imgOut.close();
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("img_res", imageRes);
values.put("data", data);
db.delete("sp_cached_img", "img_res = ?", new String[]{imageRes});
db.insert("sp_cached_img", null, values);
} catch (Exception ex) {
Log.e(MainActivity.APP_NAME, "setImage call for \"" + imageRes + "\" in SQLite database returned an exception: ", ex);
}
}
}