Browse Source

fixed dbh; clean some code

Julien Clauter 9 years ago
parent
commit
3d2c1b1d19

+ 54 - 40
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -72,10 +72,10 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
 		attackSQLBuilder.append(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK).append(" INTEGER,");
 		attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
-		attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE,", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
-				NetworkEntry.COLUMN_NAME_BSSID));
         attackSQLBuilder.append(AttackEntry.COLUMN_NAME_SYNC_ID).append(" INTEGER,");
         attackSQLBuilder.append(AttackEntry.COLUMN_NAME_DEVICE).append(" TEXT,");
+		attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE,", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
+				NetworkEntry.COLUMN_NAME_BSSID));
         attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE", AttackEntry.COLUMN_NAME_DEVICE, SyncDeviceEntry.TABLE_NAME,
                 SyncDeviceEntry.COLUMN_NAME_DEVICE_ID));
 		attackSQLBuilder.append(")");
@@ -148,22 +148,22 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 
 	@Override
 	public void onCreate(SQLiteDatabase db) {
-		db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
+        db.execSQL(SQL_CREATE_SYNC_DEVICES_ENTRIES);
+        db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
 		db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
 		db.execSQL(SQL_CREATE_PACKET_ENTRIES);
 		db.execSQL(SQL_CREATE_PROFILE_ENTRIES);
-		db.execSQL(SQL_CREATE_SYNC_DEVICES_ENTRIES);
 		db.execSQL(SQL_CREATE_SYNC_INFO_ENTRIES);
 	}
 
 	@Override
 	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-		db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
+        db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
+        db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
 		db.execSQL(SQL_DELETE_PACKET_ENTRIES);
 		db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
 		db.execSQL(SQL_DELETE_PROFILE_ENTRIES);
 		db.execSQL(SQL_DELETE_NETWORK_ENTRIES);
-		db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
 		onCreate(db);
 	}
 
@@ -255,36 +255,37 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		
 		SQLiteDatabase db = this.getWritableDatabase();
 
-		String mac = HelperUtils.getMacAdress(context);
-		ContentValues syncDeviceValues = new ContentValues();
-		syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, mac);
+		//String mac = HelperUtils.getMacAdress(context);
+        SyncDevice currentDevice = SyncDevice.currentDevice();
+        ContentValues syncDeviceValues = new ContentValues();
+		syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, currentDevice.getDeviceID());
 		syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
+		syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, record.getAttack_id());
+		//String query = "SELECT  * FROM " + SyncInfoEntry.TABLE_NAME +
+		//			  " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
+		//			   "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
+		//Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
+		//long attackCount = 0;
+		//long portscanCount = 0;
+		//if (cursor.moveToFirst()){
+		//	attackCount = cursor.getLong(2);
+		//	portscanCount = cursor.getLong(3);
+		//}
 		
-		String query = "SELECT  * FROM " + SyncInfoEntry.TABLE_NAME + 
-					  " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
-					   "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
-		Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
-		long attackCount = 0;
-		long portscanCount = 0;
-		if (cursor.moveToFirst()){
-			attackCount = cursor.getLong(2);
-			portscanCount = cursor.getLong(3);
-		}			
-		
-		if("PORTSCAN".equals(record.getProtocol())){
-			portscanCount++;
-		}else { attackCount++; }
+		//if("PORTSCAN".equals(record.getProtocol())){
+		//	portscanCount++;
+		//}else { attackCount++; }
 			
-		Log.i("DBHelper", "Update number of attack: " + attackCount);
+		//Log.i("DBHelper", "Update number of attack: " + attackCount);
 		
-		ContentValues synInfoValues = new ContentValues();
-		synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
-		synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
-		synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
-		synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
+		//ContentValues synInfoValues = new ContentValues();
+		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
+		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
+		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
+		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
 
 		// Inserting Rows
-		db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
+		//db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
 		db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
 		db.close(); // Closing database connection
 	}
@@ -783,13 +784,13 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	
 	/**
 	 * Updates the the timestamp of a single device id
-	 * @param devices The Device id
+	 * @param deviceID The Device id
 	 * @param timestamp The synchronization timestamp
 	 */
-	public void updateSyncDevice(String devices, long timestamp){
+	public void updateTimestampOfSyncDevice(String deviceID, long timestamp){
 		SQLiteDatabase db = this.getReadableDatabase();		
 		ContentValues deviceValues = new ContentValues();
-		deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, devices);
+		deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, deviceID);
 		deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, timestamp);
 		db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
 		db.close();
@@ -1643,6 +1644,10 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 
     }
 
+    /**
+     * Inserts the given devices in the database with save.
+     * @param devices list of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
+     */
     public void insertSyncDevices(List<SyncDevice> devices){
         SQLiteDatabase db = this.getWritableDatabase();
 
@@ -1650,13 +1655,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 
         try {
             for (SyncDevice device : devices){
-                ContentValues recordValues = new ContentValues();
-                recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
-                recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
-                recordValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
-
-                // Inserting Rows
-                db.insert(SyncDeviceEntry.TABLE_NAME, null, recordValues);
+                insertSyncDevice(device, db);
             }
             db.setTransactionSuccessful();
         } finally {
@@ -1666,6 +1665,21 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         db.close(); // Closing database connection
     }
 
+    /**
+     * Inserts the given dives in the given SQLite Database without save.
+     * @param device {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
+     * @param db  {@link android.database.sqlite.SQLiteDatabase}
+     */
+    private void insertSyncDevice(SyncDevice device, SQLiteDatabase db){
+        ContentValues recordValues = new ContentValues();
+        recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
+        recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
+        recordValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
+
+        // Inserting Rows
+        db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, recordValues, SQLiteDatabase.CONFLICT_REPLACE);
+    }
+
 
     /** Returns the color for the given index
      * @return int color*/