From e28c09e7042dcba3f19b8f29d444ee7d98d56f65 Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Sat, 20 Mar 2010 17:52:42 +0100 Subject: [PATCH] Add/Edit server: server title should be unique --- src/org/yaaic/db/Database.java | 40 ++++++++++++++++++++--- src/org/yaaic/view/AddServerActivity.java | 7 ++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/org/yaaic/db/Database.java b/src/org/yaaic/db/Database.java index e56d75f..a7ea5be 100644 --- a/src/org/yaaic/db/Database.java +++ b/src/org/yaaic/db/Database.java @@ -97,11 +97,14 @@ public class Database extends SQLiteOpenHelper { // XXX: We delete the database currently, in future versions we want to // migrate the database to the new version (add or remove rows..) - db.execSQL("DROP TABLE IF EXISTS " + ServerConstants.TABLE_NAME + ";"); - db.execSQL("DROP TABLE IF EXISTS " + ChannelConstants.TABLE_NAME + ";"); - db.execSQL("DROP TABLE IF EXISTS " + IdentityConstants.TABLE_NAME + ";"); - onCreate(db); + // XXX: Do not delete databases (release version) + + // db.execSQL("DROP TABLE IF EXISTS " + ServerConstants.TABLE_NAME + ";"); + // db.execSQL("DROP TABLE IF EXISTS " + ChannelConstants.TABLE_NAME + ";"); + // db.execSQL("DROP TABLE IF EXISTS " + IdentityConstants.TABLE_NAME + ";"); + + // onCreate(db); } /** @@ -230,6 +233,35 @@ public class Database extends SQLiteOpenHelper return server; } + /** + * Check if the given server title is currently used + * + * @param title The server title + * @return true if there's a server with this title, false otherwise + */ + public boolean isTitleUsed(String title) + { + boolean isTitleUsed = false; + + Cursor cursor = this.getReadableDatabase().query( + ServerConstants.TABLE_NAME, + ServerConstants.ALL, + ServerConstants.TITLE + " = '" + title + "'", + null, + null, + null, + null + ); + + if (cursor.moveToNext()) { + isTitleUsed = true; + } + + cursor.close(); + + return isTitleUsed; + } + /** * Populate a server object from the given database cursor * @param cursor diff --git a/src/org/yaaic/view/AddServerActivity.java b/src/org/yaaic/view/AddServerActivity.java index f8bcb1b..ee85975 100644 --- a/src/org/yaaic/view/AddServerActivity.java +++ b/src/org/yaaic/view/AddServerActivity.java @@ -263,6 +263,13 @@ public class AddServerActivity extends Activity implements OnClickListener } catch (NumberFormatException e) { throw new ValidationException("Enter a numeric port"); } + + Database db = new Database(this); + if (db.isTitleUsed(title) && (server == null || !server.getTitle().equals(title))) { + db.close(); + throw new ValidationException("There is already a server with this title"); + } + db.close(); } /**