355 lines
8.9 KiB
Java
355 lines
8.9 KiB
Java
package fishrungames.bashgid.core.db;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import fishrungames.bashgid.BashgidApplication;
|
|
import fishrungames.bashgid.MainActivity;
|
|
import fishrungames.bashgid.core.AlbumManager;
|
|
import fishrungames.bashgid.core.DatabaseDownloadTask;
|
|
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
|
|
import fishrungames.bashgid.core.AlbumManager.AlbumRecordData;
|
|
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;
|
|
|
|
import android.content.ContentValues;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
public class AlbumDataSource
|
|
{
|
|
|
|
private BashgidSqliteHelper dbHelper = null;
|
|
|
|
public AlbumDataSource(Context context, BashgidSqliteHelper dbHelper)
|
|
{
|
|
this.dbHelper = dbHelper;
|
|
|
|
}
|
|
|
|
public void CreateNewAlbum(AlbumManager.AlbumRecordData recordData)
|
|
{
|
|
SQLiteDatabase database = dbHelper.getWritableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
|
|
try
|
|
{
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(BashgidSqliteHelper.COLUMN_NAME, recordData.name);
|
|
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
|
|
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
|
|
values.put(BashgidSqliteHelper.COLUMN_HIDDEN, recordData.hidden);
|
|
|
|
if (!innerIsAlbumAlreadyExist(recordData.name, database))
|
|
{
|
|
database.insert(BashgidSqliteHelper.TABLE_ALBUM, null, values);
|
|
} else
|
|
{
|
|
database.update(BashgidSqliteHelper.TABLE_ALBUM, values, BashgidSqliteHelper.COLUMN_NAME + " = " +"'" + DatabaseDownloadTask.sqlEscapeString_withquotes(recordData.name) +"'", null);
|
|
}
|
|
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public boolean IsAlbumAlreadyExist(String name)
|
|
{
|
|
boolean result = false;
|
|
|
|
SQLiteDatabase database = dbHelper.getReadableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
|
|
try
|
|
{
|
|
result = innerIsAlbumAlreadyExist(name, database);
|
|
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
public void AddPhotoToAlbum(String imageUrl, String albumName)
|
|
{
|
|
SQLiteDatabase database = dbHelper.getWritableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
|
|
try
|
|
{
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(BashgidSqliteHelper.COLUMN_IMAGE_URL, imageUrl);
|
|
values.put(BashgidSqliteHelper.COLUMN_NAME, albumName);
|
|
|
|
if (!innerPhotoAlbumRelationAlreadyExist(imageUrl, albumName, database))
|
|
{
|
|
database.insert(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, null, values);
|
|
}
|
|
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public AlbumFullData GetAlbumFullData(String name)
|
|
{
|
|
AlbumFullData result = AlbumManager.CreateEmptyAlbum();
|
|
|
|
SQLiteDatabase database = dbHelper.getReadableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
try
|
|
{
|
|
|
|
result = InnerGetAlbumFullData(name, database);
|
|
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public AlbumFullData InnerGetAlbumFullData(String name, SQLiteDatabase database)
|
|
{
|
|
|
|
AlbumFullData result = AlbumManager.CreateEmptyAlbum();
|
|
|
|
AlbumRecordData recordData = innerGetAlbumByName(name, database);
|
|
|
|
if (recordData != null)
|
|
{
|
|
|
|
result = new AlbumFullData(recordData);
|
|
|
|
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);
|
|
|
|
for (String imageUrl : imageUrlArr)
|
|
{
|
|
result.photoRecordArr.add(BashgidApplication.photoDataSource.InnerGetPhotoByImageUrl(imageUrl, database));
|
|
}
|
|
|
|
result.title = BashgidApplication.translationDataSource.TryTranslateText(result.title, database);
|
|
result.description = BashgidApplication.translationDataSource.TryTranslateText(result.description, database);
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public AlbumShortData GetAlbumShortData(String name)
|
|
{
|
|
AlbumShortData result = new AlbumShortData(new AlbumRecordData("", "", "", 1));
|
|
|
|
SQLiteDatabase database = dbHelper.getReadableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
try
|
|
{
|
|
|
|
AlbumRecordData recordData = innerGetAlbumByName(name, database);
|
|
|
|
if (recordData != null)
|
|
{
|
|
|
|
result = new AlbumShortData(recordData);
|
|
|
|
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);
|
|
|
|
result.photoCount = imageUrlArr.size();
|
|
|
|
if (imageUrlArr.size() > 0)
|
|
{
|
|
result.firstImageUrl = imageUrlArr.get(0);
|
|
}
|
|
|
|
result.title = BashgidApplication.translationDataSource.TryTranslateText(result.title, database);
|
|
result.description = BashgidApplication.translationDataSource.TryTranslateText(result.description, database);
|
|
|
|
}
|
|
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public ArrayList<AlbumShortData> GetAllAlbumShortDataExceptHidden()
|
|
{
|
|
|
|
ArrayList<AlbumShortData> result = new ArrayList<AlbumShortData>();
|
|
|
|
SQLiteDatabase database = dbHelper.getReadableDatabase();
|
|
|
|
if (database != null)
|
|
{
|
|
try
|
|
{
|
|
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME,
|
|
BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_HIDDEN }, BashgidSqliteHelper.COLUMN_HIDDEN + "=0", null, null, null, null, null);
|
|
|
|
if (cursor != null)
|
|
{
|
|
if (cursor.moveToFirst())
|
|
{
|
|
do
|
|
{
|
|
|
|
AlbumShortData shortData = new AlbumShortData(new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3)));
|
|
|
|
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(shortData.name, database);
|
|
|
|
shortData.photoCount = imageUrlArr.size();
|
|
|
|
if (imageUrlArr.size() > 0)
|
|
{
|
|
shortData.firstImageUrl = imageUrlArr.get(0);
|
|
}
|
|
else
|
|
{
|
|
shortData.firstImageUrl = "R.drawable.no_picture";
|
|
}
|
|
|
|
shortData.title = BashgidApplication.translationDataSource.TryTranslateText(shortData.title, database);
|
|
shortData.description = BashgidApplication.translationDataSource.TryTranslateText(shortData.description, database);
|
|
|
|
result.add(shortData);
|
|
} while (cursor.moveToNext());
|
|
|
|
}
|
|
|
|
cursor.close();
|
|
cursor = null;
|
|
}
|
|
|
|
} finally
|
|
{
|
|
dbHelper.close();
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private boolean innerIsAlbumAlreadyExist(String name, SQLiteDatabase database)
|
|
{
|
|
boolean result = false;
|
|
|
|
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME }, BashgidSqliteHelper.COLUMN_NAME
|
|
+ "=?", new String[] { DatabaseDownloadTask.sqlEscapeString(name) }, null, null, null, null);
|
|
|
|
if (cursor != null)
|
|
{
|
|
if (cursor.getCount() > 0)
|
|
{
|
|
result = true;
|
|
}
|
|
|
|
cursor.close();
|
|
cursor = null;
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
private boolean innerPhotoAlbumRelationAlreadyExist(String imageUrl, String name, SQLiteDatabase database)
|
|
{
|
|
boolean result = false;
|
|
|
|
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL,
|
|
BashgidSqliteHelper.COLUMN_NAME }, BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?" + " AND " + BashgidSqliteHelper.COLUMN_NAME + "=?",
|
|
new String[] { DatabaseDownloadTask.sqlEscapeString(imageUrl), DatabaseDownloadTask.sqlEscapeString(name) }, null, null, null, null);
|
|
|
|
if (cursor != null)
|
|
{
|
|
if (cursor.getCount() > 0)
|
|
{
|
|
result = true;
|
|
}
|
|
|
|
cursor.close();
|
|
cursor = null;
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
private ArrayList<String> innerGetImageUrlArrInAlbum(String name, SQLiteDatabase database)
|
|
{
|
|
ArrayList<String> result = new ArrayList<String>();
|
|
|
|
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL },
|
|
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { DatabaseDownloadTask.sqlEscapeString(name) }, null, null, null, null);
|
|
|
|
if (cursor != null)
|
|
{
|
|
if (cursor.moveToFirst())
|
|
{
|
|
do
|
|
{
|
|
result.add(cursor.getString(0));
|
|
} while (cursor.moveToNext());
|
|
}
|
|
|
|
cursor.close();
|
|
cursor = null;
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
public AlbumRecordData innerGetAlbumByName(String name, SQLiteDatabase database)
|
|
{
|
|
|
|
AlbumRecordData result = new AlbumRecordData("", "", "", 1);
|
|
|
|
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME, BashgidSqliteHelper.COLUMN_TITLE,
|
|
BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_HIDDEN }, BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { DatabaseDownloadTask.sqlEscapeString(name) }, null, null, null, null);
|
|
|
|
if (cursor != null)
|
|
{
|
|
if (cursor.moveToFirst())
|
|
{
|
|
result = new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3));
|
|
}
|
|
|
|
cursor.close();
|
|
cursor = null;
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|