Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite3 database implementation #8

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Model DAO Helper
ruibritopt committed May 26, 2015
commit 9e12c23e6c43e9a9ca970548cd6702de006a3f36
1 change: 1 addition & 0 deletions AndroidSvgNaviMap/res/values/scan_condition_values.xml
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>

<string-array name="devices_array">
104 changes: 104 additions & 0 deletions AndroidSvgNaviMap/src/de/tuhh/ti5/androidsvgnavimap/db/DBHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package de.tuhh.ti5.androidsvgnavimap.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by ruibrito on 14/05/15.
*/
public class DBHelper extends SQLiteOpenHelper {

// Logcat tag
private static final String TAG = "DBHelper";

// Database version
private static final int DATABASE_VERSION = 1;

// Database name
private static final String DATABASE_NAME = "fingerprintsDB";

// Table names
public static final String TABLE_FINGERPRINT = "fingerprint";
public static final String TABLE_LOCATION = "location";
public static final String TABLE_MAC = "mac";

// FINGERPRINT Table
public static final String FP_ID = "fp_ID";
public static final String FP_date = "date";
public static final String FP_weather = "weather";
public static final String FP_barrier = "barrier";
public static final String FP_devices = "devices";
public static final String FP_people = "people";
public static final String FP_location = "fp_Location";

// LOCATION Table
public static final String LOC_ID = "loc_ID";
public static final String LOC_vertex = "vertex";
public static final String LOC_floor = "floor";
public static final String LOC_building = "building";
public static final String LOC_alias = "alias";


// MAC Table
public static final String MAC_ID = "mac_ID";
public static final String MAC_mac = "mac";
public static final String MAC_strength = "strength";
public static final String MAC_channel = "channel";
public static final String MAC_fp = "mac_FP";


public static final String CREATE_LOCATION_TABLE =
"CREATE TABLE " + TABLE_LOCATION + "("
+ LOC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT"
+ LOC_vertex + " INTEGER"
+ LOC_floor + " INTEGER"
+ LOC_building + " TEXT"
+ LOC_alias + " TEXT";

public static final String CREATE_FINGERPRINT_TABLE =
"CREATE TABLE " + TABLE_FINGERPRINT + "("
+ FP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT"
+ FP_date + " TEXT"
+ FP_weather + " TEXT"
+ FP_barrier + " TEXT"
+ FP_devices + " TEXT"
+ FP_people + " TEXT"
+ FP_location + " TEXT"
+ "FOREIGN KEY(" + FP_location + ") REFERENCES "
+ TABLE_LOCATION + " (" + LOC_ID + ")";

// + FP_location + " TEXT " + "REFERENCES " + TABLE_LOCATION + " (" + LOC_ID + ")";

public static final String CREATE_MAC_TABLE =
"CREATE TABLE " + TABLE_MAC + "("
+ MAC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT"
+ MAC_mac + " TEXT"
+ MAC_strength + " TEXT"
+ MAC_channel + " INTEGER"
+ MAC_fp + " TEXT"
+ "FOREIGN KEY(" + MAC_fp + ") REFERENCES "
+ TABLE_FINGERPRINT + " (" + FP_ID + ")";


public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_LOCATION_TABLE);
db.execSQL(CREATE_FINGERPRINT_TABLE);
db.execSQL(CREATE_MAC_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package de.tuhh.ti5.androidsvgnavimap.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import de.tuhh.ti5.androidsvgnavimap.db.DBHelper;
import de.tuhh.ti5.androidsvgnavimap.db.model.Fingerprint;
import de.tuhh.ti5.androidsvgnavimap.db.model.Location;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
* Created by ruibrito on 15/05/15.
*/
public class FingerprintDAO {

public static final String TAG = "FingerprintDAO";

// DATABASE FIELDs
private SQLiteDatabase sqLiteDatabase;
private DBHelper dbHelper;
private Context context;

private String[] tableCollumns = {
DBHelper.FP_ID, DBHelper.FP_date,
DBHelper.FP_weather, DBHelper.FP_barrier,
DBHelper.FP_devices, DBHelper.FP_people, DBHelper.FP_location};

public FingerprintDAO(Context context) {
this.context = context;

dbHelper = new DBHelper(context);
// open the database
try {
open();
} catch (SQLException e) {
Log.e(TAG, "SQLException on openning database " + e.getMessage());
e.printStackTrace();
}
}

public void open() throws SQLException {
sqLiteDatabase = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public Fingerprint createFingerprint(String date, String weather, String barriers, String devices, String people, long locationID) {
ContentValues values = new ContentValues();

if (date == null) {
values.put(DBHelper.FP_date, getCurrentDate());
} else {
values.put(DBHelper.FP_date, date);
}
values.put(DBHelper.FP_weather, weather);
values.put(DBHelper.FP_barrier, barriers);
values.put(DBHelper.FP_devices, devices);
values.put(DBHelper.FP_people, people);
values.put(DBHelper.FP_location, locationID);

long insertedID = sqLiteDatabase.insert(DBHelper.TABLE_FINGERPRINT, null, values);

Cursor cursor = sqLiteDatabase.query(DBHelper.TABLE_FINGERPRINT, tableCollumns,
DBHelper.FP_ID + " = " + insertedID, null, null, null, null);

cursor.moveToFirst();

Fingerprint newFingerprint = cursorToFingerprint(cursor);

cursor.close();

return newFingerprint;
}

public Fingerprint getFingerprintByID(long id) {
Cursor cursor = sqLiteDatabase.query(
DBHelper.TABLE_LOCATION,
tableCollumns,
DBHelper.FP_ID + " = ?", new String[]{String.valueOf(id)},
null, null, null);

if (cursor != null) {
cursor.moveToFirst();
}

Fingerprint fingerprint = cursorToFingerprint(cursor);

return fingerprint;
}

private Fingerprint cursorToFingerprint(Cursor cursor) {

Fingerprint fp = new Fingerprint();

fp.setFingerPrintID(cursor.getLong(0));
fp.setDate(cursor.getString(1));
fp.setWeather(cursor.getString(2));
fp.setBarriers(cursor.getString(3));
fp.setElectricDevices(cursor.getString(4));
fp.setPeople(cursor.getString(5));

long locationID = cursor.getLong(6);

LocationDAO locationDAO = new LocationDAO(context);

Location location = locationDAO.getLocationByID(locationID);

if (location != null) {
fp.setLocationID(location);
}

return fp;
}

private String getCurrentDate() {
Calendar rightNow = Calendar.getInstance();

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = dateFormat.format(rightNow.getTime());

return formattedDate;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package de.tuhh.ti5.androidsvgnavimap.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import de.tuhh.ti5.androidsvgnavimap.db.DBHelper;
import de.tuhh.ti5.androidsvgnavimap.db.model.Location;

import java.sql.SQLException;

/**
* Created by ruibrito on 20/05/15.
*/
public class LocationDAO {

public static final String TAG = "LocationDAO";

// DATABASE FIELDs
private SQLiteDatabase sqLiteDatabase;
private DBHelper dbHelper;
private Context context;

private String[] tableColumns = {DBHelper.LOC_ID, DBHelper.LOC_vertex,
DBHelper.LOC_floor, DBHelper.LOC_building, DBHelper.LOC_alias};

public LocationDAO(Context context) {
dbHelper = new DBHelper(context);
this.context = context;
// open the database
try {
open();
} catch (SQLException e) {
Log.e(TAG, "SQLException on openning database " + e.getMessage());
e.printStackTrace();
}
}

public void open() throws SQLException {
sqLiteDatabase = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public Location createLocation(long vertex, int floor, String building, String alias) {
ContentValues values = new ContentValues();

values.put(DBHelper.LOC_vertex, vertex);
values.put(DBHelper.LOC_floor, floor);
values.put(DBHelper.LOC_building, building);
values.put(DBHelper.LOC_alias, alias);

long insertID = sqLiteDatabase.insert(DBHelper.TABLE_LOCATION, null, values);

Cursor cursor = sqLiteDatabase.query(
DBHelper.TABLE_LOCATION,
tableColumns,
DBHelper.LOC_ID + " = " + insertID, null, null, null, null);

cursor.moveToFirst();

Location newLocation = cursorToLocation(cursor);

cursor.close();

return newLocation;
}

public Location getLocationID(long vertex, int floor, String building) {

Cursor cursor = sqLiteDatabase.query(DBHelper.TABLE_LOCATION,
tableColumns,
DBHelper.LOC_vertex + " = ? AND " + DBHelper.LOC_floor + " = ? AND " + DBHelper.LOC_building + " = ?",
new String[]{String.valueOf(vertex), String.valueOf(floor), building}, null, null, null);

if (cursor.moveToFirst()) {
return cursorToLocation(cursor);
} else {
return createLocation(vertex, floor, building, null);
}
}

public Location getLocationID(long vertex, int floor, String building, String alias) {

Cursor cursor = sqLiteDatabase.query(
DBHelper.TABLE_LOCATION,
tableColumns,
DBHelper.LOC_vertex + " = ? AND " + DBHelper.LOC_floor + " = ? AND " + DBHelper.LOC_building + " = ?",
new String[]{String.valueOf(vertex), String.valueOf(floor), building}, null, null, null);

if (cursor.moveToFirst()) {
return cursorToLocation(cursor);
} else {
return createLocation(vertex, floor, building, alias);
}
}

public Location getLocationByID(long id) {
Cursor cursor = sqLiteDatabase.query(
DBHelper.TABLE_LOCATION,
tableColumns,
DBHelper.LOC_ID + " = ?", new String[]{String.valueOf(id)},
null, null, null);

if (cursor != null) {
cursor.moveToFirst();
}

Location location = cursorToLocation(cursor);

return location;
}

private Location cursorToLocation(Cursor cursor) {
Location loc = new Location();

loc.setLocationID(cursor.getLong(0));
loc.setVertex(cursor.getLong(1));
loc.setFloor(cursor.getInt(2));
loc.setBuilding(cursor.getString(3));
loc.setLocationName(cursor.getString(4));

return loc;
}
}
Loading