Updated DBHelper for Deliverable 3
This commit is contained in:
parent
cac2532c5a
commit
91476b3ddc
3 changed files with 499 additions and 25 deletions
|
@ -6,6 +6,7 @@ import android.database.sqlite.SQLiteOpenHelper;
|
|||
import android.database.Cursor;
|
||||
import android.content.ContentValues;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -22,7 +23,7 @@ import java.util.List;
|
|||
public class DBHelper extends SQLiteOpenHelper {
|
||||
|
||||
//version of db used for update method
|
||||
private static final int DB_VERSION = 2;
|
||||
private static final int DB_VERSION = 3;
|
||||
//name of db in app data
|
||||
private static final String DB_NAME = "UsersDB.db";
|
||||
|
||||
|
@ -40,15 +41,44 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
private static final String COLUMN_FIRSTNAME = "firstName";
|
||||
private static final String COLUMN_LASTNAME = "lastName";
|
||||
private static final String COLUMN_USERTYPE = "userType";
|
||||
|
||||
// private static final String TABLE_SERVICEPROVIDERS = "serviceProviders";
|
||||
private static final String COLUMN_ADDRESS = "address";
|
||||
private static final String COLUMN_PHONE = "phone";
|
||||
private static final String COLUMN_COMPANY = "company";
|
||||
private static final String COLUMN_LICENSED = "licensed";
|
||||
|
||||
//name of table containing services and rates
|
||||
private static final String TABLE_SERVICES = "services";
|
||||
//columns of TABLE_LOGIN
|
||||
//columns of TABLE_SERVICES
|
||||
private static final String COLUMN_SERVICE = "service";
|
||||
private static final String COLUMN_RATE = "rate";
|
||||
|
||||
//name of table containing service provider information
|
||||
private static final String TABLE_SERVICEPROVIDERS = "serviceProviders";
|
||||
//columns of TABLE_SERVICEPROVIDERS
|
||||
private static final String COLUMN_SERVICEPROVIDERNAME = "username";
|
||||
private static final String COLUMN_SERVICEPROVIDERSERVICE = "service";
|
||||
|
||||
//name of table containing service provider availability
|
||||
//availability is stored as number of minutes from 00:00
|
||||
private static final String TABLE_AVAILABILITY = "availability";
|
||||
//columns of TABLE_AVAILABILITY
|
||||
private static final String COLUMN_AVAILABILITYNAME = "username";
|
||||
private static final String COLUMN_MONSTART = "mondaystart";
|
||||
private static final String COLUMN_MONEND = "mondayend";
|
||||
private static final String COLUMN_TUESTART = "tuesdaystart";
|
||||
private static final String COLUMN_TUEEND = "tuesdayend";
|
||||
private static final String COLUMN_WEDSTART = "wednesdaystart";
|
||||
private static final String COLUMN_WEDEND = "wednesdayend";
|
||||
private static final String COLUMN_THUSTART = "thursdaystart";
|
||||
private static final String COLUMN_THUEND = "thursdayend";
|
||||
private static final String COLUMN_FRISTART = "fridaystart";
|
||||
private static final String COLUMN_FRIEND = "fridayend";
|
||||
private static final String COLUMN_SATSTART = "saturdaystart";
|
||||
private static final String COLUMN_SATEND = "saturdayend";
|
||||
private static final String COLUMN_SUNSTART = "sundaystart";
|
||||
private static final String COLUMN_SUNEND = "sundayend";
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates an instance of DBHelper to allow activities to access and
|
||||
|
@ -71,10 +101,15 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
//making the table containing user login information
|
||||
String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "("
|
||||
+ COLUMN_USERNAME + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
|
||||
+ COLUMN_PASSWORD + " TEXT NOT NULL,"
|
||||
+ COLUMN_PASSWORD + " TEXT,"
|
||||
+ COLUMN_FIRSTNAME + " TEXT DEFAULT 'FirstName',"
|
||||
+ COLUMN_LASTNAME + " TEXT DEFAULT 'LastName',"
|
||||
+ COLUMN_USERTYPE + " TEXT NOT NULL" + ")";
|
||||
+ COLUMN_USERTYPE + " TEXT NOT NULL, "
|
||||
+ COLUMN_ADDRESS + " TEXT, "
|
||||
+ COLUMN_PHONE + " TEXT, "
|
||||
+ COLUMN_COMPANY + " TEXT, "
|
||||
+ COLUMN_LICENSED + " TEXT "
|
||||
+ ")";
|
||||
db.execSQL(CREATE_LOGIN_TABLE);
|
||||
|
||||
//making the table containing services and their rates
|
||||
|
@ -82,6 +117,38 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
+ COLUMN_SERVICE + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
|
||||
+ COLUMN_RATE + " REAL DEFAULT 0.0" + ")";
|
||||
db.execSQL(CREATE_SERVICES_TABLE);
|
||||
|
||||
//making the table containing service providers and offered services
|
||||
String CREATE_SERVICEPROVIDERS_TABLE = "CREATE TABLE "+ TABLE_SERVICEPROVIDERS + "("
|
||||
+ COLUMN_SERVICEPROVIDERNAME + " TEXT, "
|
||||
+ COLUMN_SERVICEPROVIDERSERVICE + " TEXT, "
|
||||
//service provider name is foreign key
|
||||
+ " FOREIGN KEY(" + COLUMN_SERVICEPROVIDERNAME
|
||||
+ ") REFERENCES " + TABLE_LOGIN + "(" + COLUMN_USERNAME +"), "
|
||||
//service is also foreign key
|
||||
+ " FOREIGN KEY(" + COLUMN_SERVICEPROVIDERSERVICE
|
||||
+ ") REFERENCES " + TABLE_SERVICES + "(" + COLUMN_SERVICE +") "
|
||||
+ ")";
|
||||
db.execSQL(CREATE_SERVICEPROVIDERS_TABLE);
|
||||
|
||||
//making the table containing services and their rates
|
||||
String CREATE_AVAILABILITY_TABLE = "CREATE TABLE "+ TABLE_AVAILABILITY + "("
|
||||
+ COLUMN_AVAILABILITYNAME + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK, "
|
||||
+ COLUMN_MONSTART + " REAL, "
|
||||
+ COLUMN_MONEND + " REAL, "
|
||||
+ COLUMN_TUESTART + " REAL, "
|
||||
+ COLUMN_TUEEND + " REAL, "
|
||||
+ COLUMN_WEDSTART + " REAL, "
|
||||
+ COLUMN_WEDEND + " REAL, "
|
||||
+ COLUMN_THUSTART + " REAL, "
|
||||
+ COLUMN_THUEND + " REAL, "
|
||||
+ COLUMN_FRISTART + " REAL, "
|
||||
+ COLUMN_FRIEND + " REAL, "
|
||||
+ COLUMN_SATSTART + " REAL, "
|
||||
+ COLUMN_SATEND + " REAL, "
|
||||
+ COLUMN_SUNSTART + " REAL, "
|
||||
+ COLUMN_SUNEND + " REAL)";
|
||||
db.execSQL(CREATE_AVAILABILITY_TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -97,6 +164,39 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
db.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE_SERVICES + "("
|
||||
+ COLUMN_SERVICE + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
|
||||
+ COLUMN_RATE + " REAL DEFAULT 0.0" + ")");
|
||||
|
||||
case 2: //going from db versions 1-2 to 3
|
||||
db.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE_SERVICEPROVIDERS + "("
|
||||
+ COLUMN_SERVICEPROVIDERNAME + " TEXT NOT NULL, "
|
||||
+ COLUMN_SERVICEPROVIDERSERVICE + " TEXT NOT NULL, "
|
||||
//service provider name is foreign key
|
||||
+ " FOREIGN KEY(" + COLUMN_SERVICEPROVIDERNAME
|
||||
+ ") REFERENCES " + TABLE_LOGIN + "(" + COLUMN_USERNAME +"), "
|
||||
//service is also foreign key
|
||||
+ " FOREIGN KEY(" + COLUMN_SERVICEPROVIDERSERVICE
|
||||
+ ") REFERENCES " + TABLE_SERVICES + "(" + COLUMN_SERVICE +") "
|
||||
+ ")");
|
||||
db.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE_AVAILABILITY + "("
|
||||
+ COLUMN_AVAILABILITYNAME + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK, "
|
||||
+ COLUMN_MONSTART + " REAL, "
|
||||
+ COLUMN_MONEND + " REAL, "
|
||||
+ COLUMN_TUESTART + " REAL, "
|
||||
+ COLUMN_TUEEND + " REAL, "
|
||||
+ COLUMN_WEDSTART + " REAL, "
|
||||
+ COLUMN_WEDEND + " REAL, "
|
||||
+ COLUMN_THUSTART + " REAL, "
|
||||
+ COLUMN_THUEND + " REAL, "
|
||||
+ COLUMN_FRISTART + " REAL, "
|
||||
+ COLUMN_FRIEND + " REAL, "
|
||||
+ COLUMN_SATSTART + " REAL, "
|
||||
+ COLUMN_SATEND + " REAL, "
|
||||
+ COLUMN_SUNSTART + " REAL, "
|
||||
+ COLUMN_SUNEND + " REAL)");
|
||||
|
||||
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_ADDRESS + " TEXT");
|
||||
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_PHONE + " TEXT");
|
||||
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_COMPANY + " TEXT");
|
||||
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_LICENSED + " TEXT");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,6 +217,7 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return whether adding user was successful
|
||||
*/
|
||||
public boolean addUser(UserType userType){
|
||||
if (userType == null) return false;
|
||||
//Check for duplicate username by querying login table
|
||||
Cursor cursor = writeDB.query(TABLE_LOGIN,
|
||||
new String[] {COLUMN_USERNAME},
|
||||
|
@ -137,10 +238,35 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
values.put(COLUMN_FIRSTNAME, userType.getFirstname());
|
||||
values.put(COLUMN_LASTNAME, userType.getLastname());
|
||||
values.put(COLUMN_USERTYPE, userType.getClass().getSimpleName());
|
||||
if (userType.getClass().getSimpleName().equals("Homeowner")){
|
||||
ServiceProvider serviceProvider = (ServiceProvider)userType;
|
||||
|
||||
String address = serviceProvider.getAddress();
|
||||
if (address != null){
|
||||
values.put(COLUMN_ADDRESS, address);
|
||||
}
|
||||
|
||||
String phone = serviceProvider.getPhonenumber();
|
||||
if (phone != null){
|
||||
values.put(COLUMN_PHONE, phone);
|
||||
}
|
||||
|
||||
String company = serviceProvider.getCompanyname();
|
||||
if (company != null){
|
||||
values.put(COLUMN_COMPANY, company);
|
||||
}
|
||||
|
||||
boolean licensed = serviceProvider.isLicensed();
|
||||
values.put(COLUMN_LICENSED, licensed);
|
||||
|
||||
}
|
||||
|
||||
writeDB.insert(TABLE_LOGIN, null, values);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Looks in database for user with requested username, and returns an
|
||||
* object of UserType corresponding to said user's role.
|
||||
|
@ -150,7 +276,8 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return object representing user found
|
||||
*/
|
||||
public UserType findUserByUsername(String username){
|
||||
UserType usertype;
|
||||
if (username == null) return null;
|
||||
UserType usertype = null;
|
||||
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN
|
||||
+ " WHERE " + COLUMN_USERNAME + " = ?",
|
||||
new String[]{username});
|
||||
|
@ -164,13 +291,19 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
usertype = new Admin();
|
||||
} else if (cursor.getString(4)
|
||||
.equals("ServiceProvider")){
|
||||
usertype = new ServiceProvider(username, password, firstname, lastname);
|
||||
ServiceProvider serviceProvider = new ServiceProvider(username, password, firstname, lastname);
|
||||
getAllServicesProvidedByUser(serviceProvider);
|
||||
getAvailabilities(serviceProvider);
|
||||
serviceProvider.setAddress(cursor.getString(5));
|
||||
serviceProvider.setPhonenumber(cursor.getString(6));
|
||||
serviceProvider.setCompanyname(cursor.getString(7));
|
||||
serviceProvider.setLicensed(Boolean.parseBoolean(cursor.getString(8)));
|
||||
usertype = serviceProvider;
|
||||
} else {
|
||||
usertype = new HomeOwner(username, password, firstname, lastname);
|
||||
}
|
||||
} else {
|
||||
usertype = null;
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
return usertype;
|
||||
}
|
||||
|
@ -189,14 +322,27 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return whether updating user information was successful
|
||||
*/
|
||||
public boolean updateUserInfo(String username, String password, String firstname, String lastname){
|
||||
return updateUserInfo(username, password, firstname, lastname,
|
||||
null, null, null, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean updateUserInfo(String username, String password, String firstname, String lastname,
|
||||
String address, String phonenumber, String companyname, Boolean licensed){
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(COLUMN_PASSWORD, password);
|
||||
values.put(COLUMN_FIRSTNAME, firstname);
|
||||
values.put(COLUMN_LASTNAME, lastname);
|
||||
if (password != null && !password.equals("")) values.put(COLUMN_PASSWORD, password);
|
||||
if (firstname != null && !firstname.equals("")) values.put(COLUMN_FIRSTNAME, firstname);
|
||||
if (lastname != null && !lastname.equals(""))values.put(COLUMN_LASTNAME, lastname);
|
||||
if (address != null && !address.equals(""))values.put(COLUMN_ADDRESS, address);
|
||||
if (phonenumber != null && !phonenumber.equals(""))values.put(COLUMN_PHONE, phonenumber);
|
||||
if (companyname != null && !companyname.equals(""))values.put(COLUMN_COMPANY, companyname);
|
||||
if (licensed != null)values.put(COLUMN_LICENSED, licensed.booleanValue());
|
||||
|
||||
|
||||
return writeDB.update(TABLE_LOGIN, values, COLUMN_USERNAME+" = ?",
|
||||
new String[]{username}) > 0;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -236,11 +382,12 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return whether adding service was successful
|
||||
*/
|
||||
public boolean addService(Service service){
|
||||
//Check for duplicate username by querying login table
|
||||
if (service == null) return false;
|
||||
//Check for duplicate username by querying services table
|
||||
Cursor cursor = writeDB.query(TABLE_SERVICES,
|
||||
new String[] {COLUMN_SERVICE},
|
||||
COLUMN_SERVICE + " = ?",
|
||||
new String[]{service.getName()},
|
||||
new String[]{service.getName().toLowerCase().trim()},
|
||||
null, null, null,
|
||||
"1");
|
||||
//If cursor has 1+ elements in it, username already exists in table
|
||||
|
@ -251,7 +398,7 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
cursor.close();
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(COLUMN_SERVICE, service.getName());
|
||||
values.put(COLUMN_SERVICE, service.getName().toLowerCase().trim());
|
||||
values.put(COLUMN_RATE, service.getRate());
|
||||
writeDB.insert(TABLE_SERVICES, null, values);
|
||||
return true;
|
||||
|
@ -266,7 +413,10 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return object representing service found
|
||||
*/
|
||||
public Service findService(String serviceName){
|
||||
if (serviceName == null) return null;
|
||||
|
||||
Service service;
|
||||
serviceName = serviceName.toLowerCase().trim();
|
||||
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_SERVICES
|
||||
+ " WHERE " + COLUMN_SERVICE + " = ?",
|
||||
new String[]{serviceName});
|
||||
|
@ -293,11 +443,12 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return whether updating service information was successful
|
||||
*/
|
||||
public boolean updateService(Service service){
|
||||
if (service == null) return false;
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(COLUMN_RATE, service.getRate());
|
||||
|
||||
return writeDB.update(TABLE_SERVICES, values, COLUMN_SERVICE+" = ?",
|
||||
new String[]{service.getName()}) > 0;
|
||||
new String[]{service.getName().toLowerCase().trim()}) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -312,6 +463,9 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return whether updating service information was successful
|
||||
*/
|
||||
public boolean updateService(String name, double rate){
|
||||
if (name == null) return false;
|
||||
|
||||
name = name.toLowerCase().trim();
|
||||
ContentValues values = new ContentValues();
|
||||
if (rate > 0)
|
||||
values.put(COLUMN_RATE, rate);
|
||||
|
@ -322,16 +476,38 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
|
||||
/**
|
||||
* Looks in database for a service, and deletes the corresponding
|
||||
* entry. Returns true if a user was deleted, false otherwise.
|
||||
* entry. Returns true if a service was deleted, false otherwise.
|
||||
*
|
||||
* @param service service of entry to delete
|
||||
* @return whether the service was deleted
|
||||
*/
|
||||
public boolean deleteService(String service) {
|
||||
return writeDB.delete(TABLE_SERVICES, COLUMN_SERVICE+" = ?",
|
||||
if (service == null) return false;
|
||||
|
||||
boolean deleted;
|
||||
String nullify = null;
|
||||
service = service.toLowerCase().trim();
|
||||
ContentValues contentValues = new ContentValues();
|
||||
contentValues.put(COLUMN_SERVICEPROVIDERSERVICE, nullify);
|
||||
writeDB.update(TABLE_SERVICEPROVIDERS, contentValues, COLUMN_SERVICEPROVIDERSERVICE+" = ?",
|
||||
new String[]{service});
|
||||
|
||||
deleted = writeDB.delete(TABLE_SERVICES, COLUMN_SERVICE+" = ?",
|
||||
new String[]{service}) > 0;
|
||||
|
||||
if (deleted) {
|
||||
writeDB.delete(TABLE_SERVICEPROVIDERS, COLUMN_SERVICEPROVIDERSERVICE+" = ?",
|
||||
new String[]{service});
|
||||
} else {
|
||||
ContentValues restoreContentValues = new ContentValues();
|
||||
restoreContentValues.put(COLUMN_SERVICEPROVIDERSERVICE, service);
|
||||
writeDB.update(TABLE_SERVICEPROVIDERS, restoreContentValues, COLUMN_SERVICEPROVIDERSERVICE+" = ?",
|
||||
null);
|
||||
}
|
||||
return deleted;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a list of String arrays containing the service categories,
|
||||
* names and hourly rates.
|
||||
|
@ -339,7 +515,167 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
* @return list of arrays of [service, rate]
|
||||
*/
|
||||
public List<String[]> getAllServices(){
|
||||
return getAll("SELECT * FROM " + TABLE_SERVICES);
|
||||
return getAll("SELECT * FROM " + TABLE_SERVICES + " ORDER BY " + COLUMN_SERVICE);
|
||||
}
|
||||
|
||||
public boolean addServiceProvidedByUser(ServiceProvider serviceProvider, Service service){
|
||||
if (serviceProvider == null || service == null) return false;
|
||||
return addServiceProvidedByUser(serviceProvider.getUsername(), service.getName());
|
||||
}
|
||||
|
||||
public boolean addServiceProvidedByUser(ServiceProvider serviceProvider, String serviceName){
|
||||
if (serviceProvider == null || serviceName == null) return false;
|
||||
return addServiceProvidedByUser(serviceProvider.getUsername(), serviceName);
|
||||
}
|
||||
|
||||
public boolean addServiceProvidedByUser(String serviceProviderUsername, String serviceName){
|
||||
if (serviceProviderUsername == null || serviceName == null) return false;
|
||||
|
||||
//TODO: Check if serviceProviderUsername and serviceName are in db before adding
|
||||
|
||||
serviceName = serviceName.toLowerCase().trim();
|
||||
|
||||
//Check for duplicate username/service combination by querying login table
|
||||
Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS,
|
||||
new String[] {COLUMN_SERVICEPROVIDERNAME},
|
||||
COLUMN_SERVICEPROVIDERNAME + " = ? AND "
|
||||
+ COLUMN_SERVICEPROVIDERSERVICE + " = ?",
|
||||
new String[]{serviceProviderUsername, serviceName},
|
||||
null, null, null,
|
||||
"1");
|
||||
//If cursor has 1+ elements in it, username already exists in table
|
||||
if (cursor != null && cursor.getCount() > 0){
|
||||
cursor.close();
|
||||
return false;
|
||||
}
|
||||
cursor.close();
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(COLUMN_SERVICEPROVIDERNAME, serviceProviderUsername);
|
||||
values.put(COLUMN_SERVICEPROVIDERSERVICE, serviceName);
|
||||
writeDB.insert(TABLE_SERVICEPROVIDERS, null, values);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean deleteServiceProvidedByUser(ServiceProvider serviceProvider, Service service){
|
||||
if (serviceProvider == null || service == null) return false;
|
||||
return deleteServiceProvidedByUser(serviceProvider.getUsername(), service.getName());
|
||||
}
|
||||
|
||||
public boolean deleteServiceProvidedByUser(ServiceProvider serviceProvider, String serviceName){
|
||||
if (serviceProvider == null || serviceName == null) return false;
|
||||
return deleteServiceProvidedByUser(serviceProvider.getUsername(), serviceName);
|
||||
}
|
||||
|
||||
public boolean deleteServiceProvidedByUser(String serviceProviderUsername, String serviceName){
|
||||
if (serviceProviderUsername == null || serviceName == null) return false;
|
||||
serviceName = serviceName.toLowerCase().trim();
|
||||
return writeDB.delete(TABLE_SERVICEPROVIDERS,
|
||||
COLUMN_SERVICEPROVIDERNAME + " = ? AND "
|
||||
+ COLUMN_SERVICEPROVIDERSERVICE + " = ?",
|
||||
new String[]{serviceProviderUsername, serviceName}) > 0;
|
||||
}
|
||||
|
||||
public List<String[]> getAllServicesProvidedByUser(ServiceProvider serviceProvider){
|
||||
if (serviceProvider == null) return new ArrayList<>();
|
||||
|
||||
return getAllServicesProvidedByUser(serviceProvider.getUsername());
|
||||
}
|
||||
|
||||
public List<String[]> getAllServicesProvidedByUser(String serviceProviderName){
|
||||
if (serviceProviderName == null) return new ArrayList<>();
|
||||
|
||||
return getAll("SELECT " + TABLE_SERVICES + "." + COLUMN_SERVICE + ", "
|
||||
+ TABLE_SERVICES + "." + COLUMN_RATE
|
||||
+ " FROM " + TABLE_SERVICES
|
||||
+ " JOIN " + TABLE_SERVICEPROVIDERS
|
||||
+ " ON " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERSERVICE + " = "
|
||||
+ TABLE_SERVICES + "." + COLUMN_SERVICE
|
||||
+ " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME
|
||||
+ "= '" + serviceProviderName + "'");
|
||||
}
|
||||
|
||||
public List<String[]> getAllProvidersByService(Service service){
|
||||
if (service == null) return new ArrayList<>();
|
||||
|
||||
return getAllProvidersByService(service.getName());
|
||||
}
|
||||
|
||||
public List<String[]> getAllProvidersByService(String serviceName){
|
||||
if (serviceName == null) return new ArrayList<>();
|
||||
|
||||
serviceName = serviceName.toLowerCase().trim();
|
||||
return getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME
|
||||
+ " FROM " + TABLE_SERVICEPROVIDERS
|
||||
+ " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '"
|
||||
+ serviceName + "'");
|
||||
}
|
||||
|
||||
public boolean updateAvailability(ServiceProvider serviceProvider){
|
||||
//availability is stored as number of minutes from 00:00
|
||||
if (serviceProvider == null) return false;
|
||||
int[][] availabilities = serviceProvider.getAvailabilities();
|
||||
if (availabilities == null) return false;
|
||||
|
||||
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_AVAILABILITY
|
||||
+ " WHERE " + COLUMN_AVAILABILITYNAME + " = ?",
|
||||
new String[]{serviceProvider.getUsername()});
|
||||
|
||||
ContentValues contentValues = new ContentValues();
|
||||
addAvailabilityToContentValues(contentValues, COLUMN_MONSTART, COLUMN_MONEND, availabilities[0]);
|
||||
addAvailabilityToContentValues(contentValues, COLUMN_TUESTART, COLUMN_TUEEND, availabilities[1]);
|
||||
addAvailabilityToContentValues(contentValues, COLUMN_WEDSTART, COLUMN_WEDEND, availabilities[2]);
|
||||
addAvailabilityToContentValues(contentValues, COLUMN_THUSTART, COLUMN_THUEND, availabilities[3]);
|
||||
addAvailabilityToContentValues(contentValues, COLUMN_FRISTART, COLUMN_FRIEND, availabilities[4]);
|
||||
addAvailabilityToContentValues(contentValues, COLUMN_SATSTART, COLUMN_SATEND, availabilities[5]);
|
||||
addAvailabilityToContentValues(contentValues, COLUMN_SUNSTART, COLUMN_SUNEND, availabilities[6]);
|
||||
if (!cursor.moveToFirst()){
|
||||
contentValues.put(COLUMN_AVAILABILITYNAME, serviceProvider.getUsername());
|
||||
writeDB.insert(TABLE_AVAILABILITY, null, contentValues);
|
||||
} else {
|
||||
writeDB.update(TABLE_AVAILABILITY, contentValues,
|
||||
COLUMN_AVAILABILITYNAME + " = ?", new String[]{serviceProvider.getUsername()});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addAvailabilityToContentValues(ContentValues contentValues,
|
||||
String startColumn, String endColumn,
|
||||
int[] startAndEndTimes){
|
||||
if (startAndEndTimes == null){
|
||||
contentValues.put(startColumn, 0);
|
||||
contentValues.put(endColumn, 0);
|
||||
} else {
|
||||
int startTime = startAndEndTimes[0]*60+startAndEndTimes[1];
|
||||
int endTime = startAndEndTimes[2]*60+startAndEndTimes[3];
|
||||
if (endTime - startTime <=0 || startTime > 1439 || startTime <= 0
|
||||
|| endTime > 1439 || endTime <= 0) {
|
||||
contentValues.put(startColumn, 0);
|
||||
contentValues.put(endColumn, 0);
|
||||
} else {
|
||||
contentValues.put(startColumn, startTime);
|
||||
contentValues.put(endColumn, endTime);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//note that this method overwrites serviceProvider's availability if it exists
|
||||
public int[][] getAvailabilities(ServiceProvider serviceProvider){
|
||||
if (serviceProvider==null) return new int[7][4];
|
||||
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_AVAILABILITY
|
||||
+ " WHERE " + COLUMN_AVAILABILITYNAME + " = ?",
|
||||
new String[]{serviceProvider.getUsername()});
|
||||
if (cursor.moveToFirst()){
|
||||
for (int i = 0; i < 7; i++) {
|
||||
int start = cursor.getInt(i*2+1);
|
||||
int end = cursor.getInt(i*2+2);
|
||||
serviceProvider.setAvailabilities(i, start/60, start%60,
|
||||
end/60, end%60);
|
||||
}
|
||||
}
|
||||
return serviceProvider.getAvailabilities();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -363,6 +699,11 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets all items in a table
|
||||
* @param rawQuery SELECT * query
|
||||
* @return list of array representing all items in raw query
|
||||
*/
|
||||
private List<String[]> getAll(String rawQuery){
|
||||
List<String[]> list = new LinkedList<>();
|
||||
String[] infoArray;
|
||||
|
@ -383,3 +724,4 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,8 +15,21 @@ public class ServiceProvider extends UserType {
|
|||
//Field for list of services that service provider offers.
|
||||
private List<Service> services;
|
||||
//Field for array of availabilities
|
||||
/*
|
||||
DO NOT CHANGE THIS ARRAY. The 2D array size is hard coded such that the first array has a size
|
||||
of 7 and the array inside has an array size of 4. The array is setup such that the index represent a day of the
|
||||
week. Monday is 0, Tuesday is 1, Wednesday is 2, Thursday is 3, Friday is 4, Saturday is 5,
|
||||
Sunday is 6. so, [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]. Inside each of
|
||||
the arrays there is a another array containing int values such that index 0,1,2,3 is startHour,
|
||||
startMin, endHour, endMin respectively.
|
||||
*/
|
||||
private int[][] availabilities;
|
||||
|
||||
private String address;
|
||||
private String phonenumber;
|
||||
private String companyname;
|
||||
private boolean licensed;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for the service object which takes the parameters to
|
||||
|
@ -78,5 +91,35 @@ public class ServiceProvider extends UserType {
|
|||
return availabilities;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhonenumber() {
|
||||
return phonenumber;
|
||||
}
|
||||
|
||||
public void setPhonenumber(String phonenumber) {
|
||||
this.phonenumber = phonenumber;
|
||||
}
|
||||
|
||||
public String getCompanyname() {
|
||||
return companyname;
|
||||
}
|
||||
|
||||
public void setCompanyname(String companyname) {
|
||||
this.companyname = companyname;
|
||||
}
|
||||
|
||||
public boolean isLicensed() {
|
||||
return licensed;
|
||||
}
|
||||
|
||||
public void setLicensed(boolean licensed) {
|
||||
this.licensed = licensed;
|
||||
}
|
||||
}
|
|
@ -140,7 +140,7 @@ public class DBIntegrationTest {
|
|||
addedOne = dbHelper.addService(originalService);
|
||||
dbService = dbHelper.findService("Exterminating flatworms");
|
||||
|
||||
assertEquals("Exterminating flatworms", dbService.getName());
|
||||
assertEquals("exterminating flatworms", dbService.getName());
|
||||
assertEquals(20.00, dbService.getRate(), 0.001);
|
||||
|
||||
|
||||
|
@ -148,7 +148,7 @@ public class DBIntegrationTest {
|
|||
addedTwo = dbHelper.addService(originalService);
|
||||
dbService = dbHelper.findService("Cleaning fishtanks");
|
||||
|
||||
assertEquals("Cleaning fishtanks", dbService.getName());
|
||||
assertEquals("cleaning fishtanks", dbService.getName());
|
||||
assertEquals(15.00, dbService.getRate(), 0.001);
|
||||
|
||||
if (addedOne) {
|
||||
|
@ -186,7 +186,7 @@ public class DBIntegrationTest {
|
|||
|
||||
service = dbHelper.findService("Exterminating flatworms");
|
||||
|
||||
assertEquals("Exterminating flatworms", service.getName());
|
||||
assertEquals("exterminating flatworms", service.getName());
|
||||
assertEquals(25.00, service.getRate(), 0.001);
|
||||
|
||||
dbHelper.deleteService("Exterminating flatworms");
|
||||
|
@ -210,5 +210,94 @@ public class DBIntegrationTest {
|
|||
dbHelper.deleteService("Exterminating flatworms");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAndDeleteServiceProvidedByUser(){
|
||||
dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null));
|
||||
dbHelper.addService(new Service("Hitman", 12358));
|
||||
boolean added = dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "hitman");
|
||||
assertTrue(added);
|
||||
boolean deleted = dbHelper.deleteServiceProvidedByUser("jbO4aBF4dC", "Hitman");
|
||||
assertTrue(deleted);
|
||||
dbHelper.deleteUser("jbO4aBF4dC");
|
||||
dbHelper.deleteService("hitman");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllServicesProvidedByUserAndDeleteService(){
|
||||
dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null));
|
||||
|
||||
dbHelper.addService(new Service("Hitman", 12358));
|
||||
dbHelper.addService(new Service("Exterminating flatworms", 392.457));
|
||||
|
||||
dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "hitman");
|
||||
dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms");
|
||||
|
||||
List<String[]> servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC");
|
||||
assertEquals(2, servicesProvidedByUser.size());
|
||||
assertEquals("hitman", servicesProvidedByUser.get(0)[0]);
|
||||
assertEquals(12358, Double.parseDouble(servicesProvidedByUser.get(0)[1]), 0.00001);
|
||||
assertEquals("exterminating flatworms", servicesProvidedByUser.get(1)[0]);
|
||||
assertEquals(392.457, Double.parseDouble(servicesProvidedByUser.get(1)[1]), 0.00001);
|
||||
|
||||
dbHelper.deleteService("hitman");
|
||||
servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC");
|
||||
assertEquals(1, servicesProvidedByUser.size());
|
||||
|
||||
dbHelper.deleteService("exterminating flatworms");
|
||||
servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC");
|
||||
assertEquals(0, servicesProvidedByUser.size());
|
||||
|
||||
dbHelper.deleteUser("jbO4aBF4dC");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllProvidersByService(){
|
||||
dbHelper.addService(new Service("Exterminating flatworms", 392.457));
|
||||
dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null));
|
||||
dbHelper.addUser(new ServiceProvider("7MuF1c59XP", null, null, null));
|
||||
|
||||
dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms");
|
||||
dbHelper.addServiceProvidedByUser("7MuF1c59XP", "exterminating flatworms");
|
||||
|
||||
List<String[]> providersByService = dbHelper.getAllProvidersByService("exterminating flatworms");
|
||||
|
||||
assertEquals(2, providersByService.size());
|
||||
assertEquals("jbO4aBF4dC", providersByService.get(0)[0]);
|
||||
assertEquals("7MuF1c59XP", providersByService.get(1)[0]);
|
||||
|
||||
dbHelper.deleteService("Exterminating flatworms");
|
||||
dbHelper.deleteUser("jbO4aBF4dC");
|
||||
dbHelper.deleteUser("7MuF1c59XP");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateAndGetAvailability(){
|
||||
ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null);
|
||||
serviceProvider.setAvailabilities(0, 4, 18, 19, 30);
|
||||
serviceProvider.setAvailabilities(3, 8, 12, 15, 14);
|
||||
|
||||
//TODO:Perhaps implement a deep clone function for UserType?
|
||||
ServiceProvider serviceProvider2 = new ServiceProvider("jbO4aBF4dC", null, null, null);
|
||||
serviceProvider2.setAvailabilities(0, 4, 18, 19, 30);
|
||||
serviceProvider2.setAvailabilities(3, 8, 12, 15, 14);
|
||||
|
||||
dbHelper.addUser(serviceProvider2);
|
||||
|
||||
boolean updated = dbHelper.updateAvailability(serviceProvider2);
|
||||
assertTrue(updated);
|
||||
|
||||
serviceProvider2.setAvailabilities(3, 8, 12, 15, 10);
|
||||
int[][] dbAvailabilities = dbHelper.getAvailabilities(serviceProvider2);
|
||||
int[][] availabilities = serviceProvider.getAvailabilities();
|
||||
|
||||
assertEquals(14, serviceProvider2.getAvailabilities()[3][3]);
|
||||
|
||||
for (int i = 0; i < 7; i++){
|
||||
for (int j = 0; j < 4; j++){
|
||||
assertEquals(availabilities[i][j], dbAvailabilities[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue