package app.android.gambit.local;



import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import app.android.gambit.InternetDateTime;


class LastUpdateDateTimeHandler
{
	private final SQLiteDatabase database;

	public LastUpdateDateTimeHandler() {
		this.database = DatabaseProvider.getInstance().getDatabase();
	}

	public void setCurrentDateTimeAsLastUpdated() {
		database.beginTransaction();
		try {
			trySetCurrentDateTimeAsLastUpdated();
			database.setTransactionSuccessful();
		}
		finally {
			database.endTransaction();
		}
	}

	private void trySetCurrentDateTimeAsLastUpdated() {
		if (!recordExists()) {
			insertEmptyRecord();
		}

		updateRecord(new InternetDateTime());
	}

	private boolean recordExists() {
		Cursor cursor = database.rawQuery(buildRecordsCountSelectingQuery(), null);
		cursor.moveToFirst();

		int recordsCount = cursor.getInt(0);

		return recordsCount > 0;
	}

	private void insertEmptyRecord() {
		database.execSQL(buildEmptyRecordInsertionQuery());
	}

	private String buildEmptyRecordInsertionQuery() {
		StringBuilder builder = new StringBuilder();

		builder.append(String.format("insert into %s ", DbTableNames.DB_LAST_UPDATE_TIME));
		builder.append(String.format("(%s) ", DbFieldNames.DB_LAST_UPDATE_TIME));
		builder.append(String.format("values (%s)", "''"));

		return builder.toString();
	}

	private void updateRecord(InternetDateTime dateTime) {
		database.execSQL(buildRecordUpdatingQuery(dateTime));
	}

	private String buildRecordUpdatingQuery(InternetDateTime dateTime) {
		StringBuilder builder = new StringBuilder();

		builder.append(String.format("update %s ", DbTableNames.DB_LAST_UPDATE_TIME));
		builder.append(String.format("set %s='%s' ", DbFieldNames.DB_LAST_UPDATE_TIME,
			dateTime.toString()));

		return builder.toString();
	}

	public InternetDateTime getLastUpdatedDateTime() {
		database.beginTransaction();
		try {
			InternetDateTime lastUpdatedDateTime = tryGetLastUpdatedDateTime();
			database.setTransactionSuccessful();
			return lastUpdatedDateTime;
		}
		finally {
			database.endTransaction();
		}
	}

	private InternetDateTime tryGetLastUpdatedDateTime() {
		ensureRecordExists();

		Cursor cursor = database.rawQuery(buildRecordSelectingQuery(), null);
		cursor.moveToFirst();

		String dateTimeAsString = cursor.getString(0);

		return new InternetDateTime(dateTimeAsString);
	}

	private void ensureRecordExists() {
		if (!recordExists()) {
			setCurrentDateTimeAsLastUpdated();
		}
	}

	private String buildRecordSelectingQuery() {
		StringBuilder builder = new StringBuilder();

		builder.append(String.format("select %s ", DbFieldNames.DB_LAST_UPDATE_TIME));
		builder.append(String.format("from %s", DbTableNames.DB_LAST_UPDATE_TIME));

		return builder.toString();
	}

	private String buildRecordsCountSelectingQuery() {
		return String.format("select count(*) from %s", DbTableNames.DB_LAST_UPDATE_TIME);
	}
}

@@ -29,17 +29,16 @@
 ocal;%0A%0A%0A
-%0A
 import a
@@ -913,24 +913,165 @@
 nt %3E 0;%0A%09%7D%0A%0A
+%09private String buildRecordsCountSelectingQuery() %7B%0A%09%09return String.format(%22select count(*) from %25s%22, DbTableNames.DB_LAST_UPDATE_TIME);%0A%09%7D%0A%0A
 %09private voi
@@ -2922,148 +2922,7 @@
 ;%0A%09%7D
-%0A%0A%09private String buildRecordsCountSelectingQuery() %7B%0A%09%09return String.format(%22select count(*) from %25s%22, DbTableNames.DB_LAST_UPDATE_TIME);%0A%09%7D
 %0A%7D%0A

Fix vertical indentation and method position in LastUpdateDateTimeHandler.