Added docs and ability to get a list of all users from database

This commit is contained in:
Mary Tran 2018-10-08 18:00:21 -04:00
parent 5a382baab4
commit a80c6d37dc
3 changed files with 114 additions and 22 deletions

View file

@ -3,6 +3,6 @@ package com.uottawa.olympus.olympusservices;
public class Admin extends UserType {
Admin(){
super("admin", "admin", "admin", "admin");
super("admin", "admin", "Admin", "Admin");
}
}

View file

@ -6,24 +6,35 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.ContentValues;
import java.util.LinkedList;
import java.util.List;
/**
* The class DBHelper allows the Android application to access and perform
* CRUD operations on the tables of the SQLite database.
* There is currently a table of all users' login information.
* CRUD (Create, Read, Update, Delete) operations on the tables of the SQLite database.
* There is currently one table of all users' login information and names.
* Table of service providers and services to come soon.
*
* To use, create an object of this class with the current activity as context.
*
*/
public class DBHelper extends SQLiteOpenHelper {
//version of db used for update method
private static final int DB_VERSION = 1;
//name of db in app data
private static final String DB_NAME = "UsersDB.db";
private static String DB_PATH = "";
//SQLiteDatabase for reading
private static SQLiteDatabase readDB;
//SQLiteDatabase for writing
private static SQLiteDatabase writeDB;
//name of table containing user login information and names
private static final String TABLE_LOGIN = "userInfo";
//columns of TABLE_LOGIN
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
private static final String COLUMN_FIRSTNAME = "firstName";
@ -36,6 +47,12 @@ public class DBHelper extends SQLiteOpenHelper {
// private static final String TABLE_SERVICES = "services";
/**
* Creates an instance of DBHelper to allow activities to access and
* perform CRUD operations on the database via DBHelper's methods
*
* @param context current activity calling DBHelper
*/
public DBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
readDB = this.getReadableDatabase();
@ -68,6 +85,14 @@ public class DBHelper extends SQLiteOpenHelper {
}
/**
* Adds a user to the database. Returns false if there is a user already
* existing in the database with the same username. Returns true if
* successful in adding user to database.
*
* @param userType user to be added
* @return whether adding user was successful
*/
public boolean addUser(UserType userType){
//Check for duplicate username by querying login table
Cursor cursor = writeDB.query(TABLE_LOGIN,
@ -91,7 +116,14 @@ public class DBHelper extends SQLiteOpenHelper {
return true;
}
/**
* Looks in database for user with requested username, and returns an
* object of UserType corresponding to said user's role.
* Returns null if no such user found.
*
* @param username username to look up
* @return object representing user found
*/
public UserType findUserByUsername(String username){
UserType usertype;
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN
@ -118,8 +150,20 @@ public class DBHelper extends SQLiteOpenHelper {
return usertype;
}
public boolean updateUser(String username, String password, String firstname, String lastname){
/**
* Updates user login information and name for user with requested username.
* Returns true if a user of said username was found and entry updated.
* Returns false if no user was found of said username.
*
*
* @param username username of entry to update
* @param password new password
* @param firstname new first name
* @param lastname new last name
*
* @return whether updating user information was successful
*/
public boolean updateUserInfo(String username, String password, String firstname, String lastname){
ContentValues values = new ContentValues();
values.put(COLUMN_PASSWORD, password);
values.put(COLUMN_FIRSTNAME, firstname);
@ -130,12 +174,24 @@ public class DBHelper extends SQLiteOpenHelper {
}
/**
* Looks in database for user with requested username, and deletes the corresponding
* entry. Returns true if a user was deleted, false otherwise.
*
* @param username username of entry to delete
* @return whether a user was deleted
*/
public boolean deleteUser(String username) {
return writeDB.delete(TABLE_LOGIN, COLUMN_USERNAME+" = ?",
new String[]{username}) > 0;
}
/**
* Prints all entries of table. One row is printed per line. Columns are
* separated by spaces.
*
* @param tableName name of table to print
*/
public void printTable(String tableName){
Cursor cursor = readDB.rawQuery("SELECT * FROM "+tableName, null);
cursor.moveToFirst();
@ -150,4 +206,29 @@ public class DBHelper extends SQLiteOpenHelper {
cursor.close();
}
/**
* Returns a list of String arrays containing the username, first name,
* last name, and user type of every user in TABLE_LOGIN.
*
* @return list of arrays of [username, first name, last name, user type]
*/
public List<String[]> getAllUsers(){
List<String[]> listOfUsers = new LinkedList<>();
String[] user;
Cursor cursor = readDB.rawQuery("SELECT ? , ?, ?, ? FROM "+TABLE_LOGIN,
new String[]{COLUMN_USERNAME, COLUMN_FIRSTNAME,
COLUMN_LASTNAME, COLUMN_USERTYPE});
if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
user = new String[4];
for (int j = 0; j < cursor.getColumnNames().length; j++) {
user[j] = cursor.getString(j);
}
listOfUsers.add(user);
}
}
cursor.close();
return listOfUsers;
}
}

View file

@ -23,16 +23,6 @@ public class DBUnitTest {
UserType originalUser, dbUser;
boolean deleted;
originalUser = new Admin();
dbHelper.addUser(originalUser);
dbUser = dbHelper.findUserByUsername("admin");
assertEquals("Admin", dbUser.getClass().getSimpleName());
assertEquals("admin", dbUser.getUsername());
assertEquals("admin", dbUser.getPassword());
assertEquals("admin", dbUser.getFirstname());
assertEquals("admin", dbUser.getLastname());
originalUser = new User("mgarzon", "soccer", "Miguel", "Garzon");
dbHelper.addUser(originalUser);
@ -56,9 +46,6 @@ public class DBUnitTest {
assertEquals("Guzman", dbUser.getLastname());
deleted = dbHelper.deleteUser("admin");
assertTrue(deleted);
deleted = dbHelper.deleteUser("mgarzon");
assertTrue(deleted);
@ -74,10 +61,34 @@ public class DBUnitTest {
added = dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon"));
assertTrue(added);
added = dbHelper.addUser(new User("mgarzon", "seg2105", "Miguel", "Garzon"));
assertTrue(!added);
added = dbHelper.addUser(new ServiceProvider("mgarzon", "seg2105", "Juan", "Guzman"));
assertTrue(!added);
dbHelper.deleteUser("mgarzon");
}
@Test
public void testUpdateUserLogin(){
boolean updated;
UserType dbUser;
dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon"));
updated = dbHelper.updateUserInfo("mgarzon", "soccer", "Juan", "Guzman");
assertTrue(updated);
dbUser = dbHelper.findUserByUsername("mgarzon");
assertEquals("mgarzon", dbUser.getUsername());
assertEquals("soccer", dbUser.getPassword());
assertEquals("Juan", dbUser.getFirstname());
assertEquals("Guzman", dbUser.getLastname());
updated = dbHelper.updateUserInfo("jguzman", "seg2105", "Juan", "Guzman");
assertTrue(!updated);
dbHelper.deleteUser("mgarzon");
}
}