133 lines
3.2 KiB
Java
133 lines
3.2 KiB
Java
package fishrungames.bashgid.core.db;
|
|
|
|
import fishrungames.bashgid.core.PhotoManager.PhotoRecordData;
|
|
|
|
import android.content.ContentValues;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.util.Log;
|
|
|
|
public class PhotoDataSource
|
|
{
|
|
|
|
private BashgidSqliteHelper dbHelper = null;
|
|
|
|
public PhotoDataSource(Context context, BashgidSqliteHelper dbHelper)
|
|
{
|
|
|
|
this.dbHelper = dbHelper;
|
|
|
|
}
|
|
|
|
public void AddOrReplacePhoto(PhotoRecordData recordData)
|
|
{
|
|
SQLiteDatabase database = dbHelper.getWritableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
try
|
|
{
|
|
ContentValues values = new ContentValues();
|
|
|
|
// values.put(BashgidSqliteHelper.COLUMN_ID, 1);
|
|
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
|
|
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
|
|
values.put(BashgidSqliteHelper.COLUMN_IMAGE_URL, recordData.imageUrl);
|
|
values.put(BashgidSqliteHelper.COLUMN_IMAGE_HASH, recordData.imageHash);
|
|
values.put(BashgidSqliteHelper.COLUMN_GEOLAT, recordData.geoLat);
|
|
values.put(BashgidSqliteHelper.COLUMN_GEOLON, recordData.geoLon);
|
|
|
|
boolean r = isPhotoAlreadyExist(recordData.imageUrl, database);
|
|
if (!r)
|
|
{
|
|
Log.e("a", "aaa1");
|
|
database.insert(BashgidSqliteHelper.TABLE_PHOTO, null, values);
|
|
Log.e("a", "aaa2");
|
|
} else
|
|
{
|
|
// Replace
|
|
Log.e("a", "aaa3");
|
|
}
|
|
|
|
Log.e("a", "aaa4");
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public PhotoRecordData GetPhotoByImageUrl(String imageUrl)
|
|
{
|
|
PhotoRecordData result = null;
|
|
|
|
SQLiteDatabase database = dbHelper.getWritableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
try
|
|
{
|
|
result = InnerGetPhotoByImageUrl(imageUrl, database);
|
|
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public PhotoRecordData InnerGetPhotoByImageUrl(String imageUrl, SQLiteDatabase database)
|
|
{
|
|
|
|
PhotoRecordData result = null;
|
|
|
|
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO, new String[] { BashgidSqliteHelper.COLUMN_TITLE,
|
|
BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_IMAGE_URL, BashgidSqliteHelper.COLUMN_IMAGE_HASH,
|
|
BashgidSqliteHelper.COLUMN_GEOLAT, BashgidSqliteHelper.COLUMN_GEOLON }, BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?", new String[] { imageUrl },
|
|
null, null, null, null);
|
|
|
|
if (cursor != null)
|
|
{
|
|
if (cursor.moveToFirst())
|
|
{
|
|
|
|
|
|
result = new PhotoRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3),
|
|
cursor.getDouble(4), cursor.getDouble(5));
|
|
}
|
|
|
|
cursor.close();
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
private boolean isPhotoAlreadyExist(String imageUrl, SQLiteDatabase database)
|
|
{
|
|
boolean result = false;
|
|
|
|
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL },
|
|
BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?", new String[] { imageUrl }, null, null, null, null);
|
|
|
|
if (cursor != null)
|
|
{
|
|
if (cursor.getCount() > 0)
|
|
{
|
|
result = true;
|
|
}
|
|
|
|
cursor.close();
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|