Trying again to fix DB things

This commit is contained in:
Mary Tran 2018-11-20 23:54:03 -05:00
parent e6c383f1f9
commit 5a3b59b1d9
2 changed files with 204 additions and 119 deletions

View file

@ -296,12 +296,15 @@ public class DBHelper extends SQLiteOpenHelper {
} }
cursor.close(); cursor.close();
//Put values of UserType into columns
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, userType.getUsername()); values.put(COLUMN_USERNAME, userType.getUsername());
values.put(COLUMN_PASSWORD, userType.getPassword()); values.put(COLUMN_PASSWORD, userType.getPassword());
values.put(COLUMN_FIRSTNAME, userType.getFirstname()); values.put(COLUMN_FIRSTNAME, userType.getFirstname());
values.put(COLUMN_LASTNAME, userType.getLastname()); values.put(COLUMN_LASTNAME, userType.getLastname());
values.put(COLUMN_USERTYPE, userType.getClass().getSimpleName()); values.put(COLUMN_USERTYPE, userType.getClass().getSimpleName());
//special case for ServiceProvider
if (userType instanceof ServiceProvider){ if (userType instanceof ServiceProvider){
ServiceProvider serviceProvider = (ServiceProvider)userType; ServiceProvider serviceProvider = (ServiceProvider)userType;
@ -696,21 +699,27 @@ public class DBHelper extends SQLiteOpenHelper {
+ " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'");
} }
public List<String[]> getAllProvidersByService(Service service){ public List<String> getAllProvidersByService(Service service){
if (service == null) return new ArrayList<>(); if (service == null) return new ArrayList<>();
return getAllProvidersByService(service.getName()); return getAllProvidersByService(service.getName());
} }
public List<String[]> getAllProvidersByService(String serviceName){ public List<String> getAllProvidersByService(String serviceName){
if (serviceName == null) return new ArrayList<>(); if (serviceName == null) return new ArrayList<>();
serviceName = serviceName.toLowerCase().trim(); serviceName = serviceName.toLowerCase().trim();
return getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME List<String[]> providers = getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME
+ " FROM " + TABLE_SERVICEPROVIDERS + " FROM " + TABLE_SERVICEPROVIDERS
+ " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '" + " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '"
+ serviceName + "'" + serviceName + "'"
+ " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'");
List<String> providerList = new ArrayList<>();
for (String[] provider : providers){
providerList.add(provider[0]);
}
return providerList;
} }
public boolean updateAvailability(ServiceProvider serviceProvider){ public boolean updateAvailability(ServiceProvider serviceProvider){
@ -804,30 +813,53 @@ public class DBHelper extends SQLiteOpenHelper {
public boolean addBooking(Booking booking){ public boolean addBooking(Booking booking){
if (booking == null) return false; if (booking == null) return false;
return addBooking(booking.getServiceprovider().getUsername(),
booking.getHomeowner().getUsername(), booking.getService().getName(),
booking.getYear(), booking.getMonth(), booking.getDay(),
booking.getStarth(), booking.getStartmin(),
booking.getEndh(), booking.getEndmin());
}
public boolean addBooking(String serviceProvider, String homeOwner, String service,
String monthDayYear, int starth, int startmin, int endh, int endmin){
String[] date = monthDayYear.split("/");
return addBooking(serviceProvider, homeOwner, service,
Integer.parseInt(date[2]), Integer.parseInt(date[0]), Integer.parseInt(date[1]),
starth, startmin, endh, endmin);
}
public boolean addBooking(String serviceProvider, String homeOwner, String service,
int year, int month, int day,
int starth, int startmin, int endh, int endmin){
GregorianCalendar current = new GregorianCalendar(); GregorianCalendar current = new GregorianCalendar();
current.setTimeInMillis(System.currentTimeMillis()); current.setTimeInMillis(System.currentTimeMillis());
GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), GregorianCalendar bookDate = new GregorianCalendar(year, month, day, starth, startmin);
booking.getDay(), booking.getStarth(), booking.getStartmin());
//check if time of booking is after this time //check if time of booking is after this time
if (current.compareTo(bookDate) > 0) return false; if (current.compareTo(bookDate) > 0) return false;
if (isProviderAvailable(serviceProvider, year, month, day, starth, startmin,
endh, endmin)){
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_BOOKINGSTART, booking.getStarth()*60 + booking.getStartmin()); contentValues.put(COLUMN_BOOKINGSTART, starth * 60 + startmin);
contentValues.put(COLUMN_BOOKINGEND, booking.getEndh()*60 + booking.getEndmin()); contentValues.put(COLUMN_BOOKINGEND, endh * 60 + endmin);
contentValues.put(COLUMN_BOOKINGDATE, booking.getDay()); contentValues.put(COLUMN_BOOKINGDATE, day);
contentValues.put(COLUMN_BOOKINGMONTH, booking.getMonth()); contentValues.put(COLUMN_BOOKINGMONTH, month);
contentValues.put(COLUMN_BOOKINGYEAR, booking.getYear()); contentValues.put(COLUMN_BOOKINGYEAR, year);
contentValues.put(COLUMN_BOOKINGSERVICEPROVIDER, booking.getServiceprovider().getUsername()); contentValues.put(COLUMN_BOOKINGSERVICEPROVIDER, serviceProvider);
contentValues.put(COLUMN_BOOKINGHOMEOWNER, booking.getHomeowner().getUsername()); contentValues.put(COLUMN_BOOKINGHOMEOWNER, homeOwner);
contentValues.put(COLUMN_BOOKINGSERVICE, booking.getService().getName()); contentValues.put(COLUMN_BOOKINGSERVICE, service);
contentValues.put(COLUMN_BOOKINGSTATUS, Status.PENDING.toString()); contentValues.put(COLUMN_BOOKINGSTATUS, Status.PENDING.toString());
contentValues.put(COLUMN_RATING, 0); contentValues.put(COLUMN_RATING, 0);
writeDB.insert(TABLE_BOOKINGS, null, contentValues); writeDB.insert(TABLE_BOOKINGS, null, contentValues);
return true; return true;
}
return false;
} }
public List<Booking> findBookings(String username){ public List<Booking> findBookings(String username){
@ -881,104 +913,116 @@ public class DBHelper extends SQLiteOpenHelper {
} }
// public boolean isProviderAvailable(String serviceProvider, String monthDayYear, public boolean isProviderAvailable(String serviceProvider, String monthDayYear,
// int starth, int startmin, int endh, int endmin) { int starth, int startmin, int endh, int endmin) {
// String[] date = monthDayYear.split("/"); String[] date = monthDayYear.split("/");
// return isProviderAvailable(serviceProvider, Integer.parseInt(date[2]), return isProviderAvailable(serviceProvider, Integer.parseInt(date[2]),
// Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[0]), Integer.parseInt(date[1]),
// starth, startmin, endh, endmin); starth, startmin, endh, endmin);
// } }
// /** /**
// * Returns false if end time before start time, or service provider not available on that day * Returns false if end time before start time, or service provider not available on that day
// * *
// * @param serviceProvider * @param serviceProvider
// * @param year * @param year
// * @param month * @param month
// * @param day * @param day
// * @param starth * @param starth
// * @param startmin * @param startmin
// * @param endh * @param endh
// * @param endmin * @param endmin
// * @return * @return
// */ */
// public boolean isProviderAvailable(String serviceProvider, int year, int month, int day, public boolean isProviderAvailable(String serviceProvider, int year, int month, int day,
// int starth, int startmin, int endh, int endmin){ int starth, int startmin, int endh, int endmin){
//
// int bookingStart = starth*60 + startmin; int bookingStart = starth*60 + startmin;
// int bookingEnd = endh*60 + endmin; int bookingEnd = endh*60 + endmin;
//
// if (bookingEnd < bookingStart) return false; if (bookingEnd < bookingStart) return false;
//
// GregorianCalendar start = new GregorianCalendar(year, month-1, day); GregorianCalendar start = new GregorianCalendar(year, month-1, day);
// //Calendar.DAY_OF_WEEK starts with 1 for Sunday, and onwards //Calendar.DAY_OF_WEEK starts with 1 for Sunday, and onwards
// int dayOfWeek = start.get(Calendar.DAY_OF_WEEK)-1; int dayOfWeek = start.get(Calendar.DAY_OF_WEEK);
// Cursor cursor = null; Cursor cursor = null;
// int availabilityStart, availabilityEnd; int availabilityStart, availabilityEnd;
//
// //Check availabilities on day of week //Check availabilities on day of week
// switch (dayOfWeek){ switch (dayOfWeek){
// case 1: //Sunday case 1: //Sunday
// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SUNSTART, COLUMN_SUNEND}, cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SUNSTART, COLUMN_SUNEND},
// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null,
// null, null, null); null, null, null);
// break; break;
// case 2: //Monday case 2: //Monday
// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_MONSTART, COLUMN_MONEND}, cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_MONSTART, COLUMN_MONEND},
// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null,
// null, null, null); null, null, null);
// break; break;
// case 3: //Tuesday case 3: //Tuesday
// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_TUESTART, COLUMN_TUEEND}, cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_TUESTART, COLUMN_TUEEND},
// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null,
// null, null, null); null, null, null);
// break; break;
// case 4: //Wednesday case 4: //Wednesday
// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_WEDSTART, COLUMN_WEDEND}, cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_WEDSTART, COLUMN_WEDEND},
// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null,
// null, null, null); null, null, null);
// break; break;
// case 5: //Thursday case 5: //Thursday
// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_THUSTART, COLUMN_THUEND}, cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_THUSTART, COLUMN_THUEND},
// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null,
// null, null, null); null, null, null);
// break; break;
// case 6: //Friday case 6: //Friday
// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_FRISTART, COLUMN_FRIEND}, cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_FRISTART, COLUMN_FRIEND},
// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null,
// null, null, null); null, null, null);
// break; break;
// case 7: //Saturday case 7: //Saturday
// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SATSTART, COLUMN_SATEND}, cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SATSTART, COLUMN_SATEND},
// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null,
// null, null, null); null, null, null);
// break; break;
// } }
//
// cursor.moveToFirst(); cursor.moveToFirst();
// availabilityStart = cursor.getInt(0); availabilityStart = cursor.getInt(0);
// availabilityEnd = cursor.getInt(1); availabilityEnd = cursor.getInt(1);
//
// //service provider not available if availability end is 0, if availability starts after booking start, //service provider not available if availability end is 0, if availability starts after booking start,
// // or if availability ends before booking end // or if availability ends before booking end
// if (availabilityEnd == 0 || availabilityStart > bookingStart || availabilityEnd < bookingEnd) return false; if (availabilityEnd == 0 || availabilityStart > bookingStart || availabilityEnd < bookingEnd) {
// return false;
// }
// //now we know for sure that the service provider is available on said day of the week
// //we check to see if any of the bookings overlap on this time slot
// cursor = writeDB.query(TABLE_BOOKINGS, new String[] {COLUMN_BOOKINGSTART, COLUMN_BOOKINGEND}, //now we know for sure that the service provider is available on said day of the week
// COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " //we check to see if any of the bookings overlap on this time slot
// + COLUMN_BOOKINGYEAR + " = ? AND " cursor = writeDB.query(TABLE_BOOKINGS, new String[] {COLUMN_BOOKINGSTART, COLUMN_BOOKINGEND},
// + COLUMN_BOOKINGMONTH + " = ? AND " COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND "
// + COLUMN_BOOKINGDATE + " = ? AND " + COLUMN_BOOKINGYEAR + " = ? AND "
// + COLUMN_BOOKINGSTATUS + " != ?", + COLUMN_BOOKINGMONTH + " = ? AND "
// new String[] {serviceProvider, String.valueOf(year), + COLUMN_BOOKINGDATE + " = ? AND "
// String.valueOf(month), String.valueOf(day), Status.CANCELLED.toString()}, + COLUMN_BOOKINGSTATUS + " != ?",
// null, null, COLUMN_BOOKINGSTART, null); new String[] {serviceProvider, String.valueOf(year),
// boolean found = cursor.moveToFirst(); String.valueOf(month), String.valueOf(day), Status.CANCELLED.toString()},
// cursor.close(); null, null, COLUMN_BOOKINGSTART, null);
// return found; if (cursor.moveToFirst()){
// } for (int i = 0; i<cursor.getCount(); i++){
availabilityStart = cursor.getInt(0);
availabilityEnd = cursor.getInt(1);
if ((availabilityStart < bookingStart && availabilityEnd > bookingStart)||
(availabilityStart < bookingEnd && availabilityEnd > bookingEnd) ||
(availabilityStart > bookingStart && availabilityEnd < bookingEnd)) return false;
cursor.moveToNext();
}
}
return true;
}
@ -1130,6 +1174,39 @@ public class DBHelper extends SQLiteOpenHelper {
return cursor.getInt(0); return cursor.getInt(0);
} }
public List<String> getProvidersAboveRating(Service service, double rating){
return getProvidersAboveRating(service.getName(), rating);
}
public List<String> getProvidersAboveRating(String serviceName, double rating){
List<String[]> providers = getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME + " FROM " + TABLE_SERVICEPROVIDERS
+ " WHERE " + COLUMN_SERVICE + " = " + serviceName + " AND "
+ COLUMN_AVERAGERATING + " >= " + rating);
List<String> providerList = new ArrayList<>();
for (String[] provider : providers){
providerList.add(provider[0]);
}
return providerList;
}
public List<String> getProvidersByTime(Service service, int year, int month, int day,
int starth, int startmin, int endh, int endmin){
return getProvidersByTime(service.getName(), year, month, day, starth, startmin, endh, endmin);
}
public List<String> getProvidersByTime(String serviceName, int year, int month, int day,
int starth, int startmin, int endh, int endmin){
List<String> providers = getAllProvidersByService(serviceName);
for (int i = 0; i < providers.size(); i++){
String username = providers.get(i);
if (!isProviderAvailable(username, year, month, day, starth, startmin, endh, endmin)){
providers.remove(i);
}
}
return providers;
}
@ -1178,5 +1255,13 @@ public class DBHelper extends SQLiteOpenHelper {
return list; return list;
} }
void deleteAll(){
writeDB.execSQL("DELETE FROM " + TABLE_LOGIN);
writeDB.execSQL("DELETE FROM " + TABLE_SERVICES);
writeDB.execSQL("DELETE FROM " + TABLE_SERVICEPROVIDERS);
writeDB.execSQL("DELETE FROM " + TABLE_AVAILABILITY);
writeDB.execSQL("DELETE FROM " + TABLE_BOOKINGS);
}
} }

View file

@ -121,7 +121,7 @@ public class DBIntegrationTest {
dbHelper.deleteUser("jbO4aBF4dC"); dbHelper.deleteUser("jbO4aBF4dC");
List<String[]> providersList = dbHelper.getAllProvidersByService("hitman"); List<String> providersList = dbHelper.getAllProvidersByService("hitman");
assertEquals(0, providersList.size()); assertEquals(0, providersList.size());
providersList = dbHelper.getAllProvidersByService("hitman"); providersList = dbHelper.getAllProvidersByService("hitman");
assertEquals(0, providersList.size()); assertEquals(0, providersList.size());
@ -332,11 +332,11 @@ public class DBIntegrationTest {
dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms"); dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms");
dbHelper.addServiceProvidedByUser("7MuF1c59XP", "exterminating flatworms"); dbHelper.addServiceProvidedByUser("7MuF1c59XP", "exterminating flatworms");
List<String[]> providersByService = dbHelper.getAllProvidersByService("exterminating flatworms"); List<String> providersByService = dbHelper.getAllProvidersByService("exterminating flatworms");
assertEquals(2, providersByService.size()); assertEquals(2, providersByService.size());
assertEquals("jbO4aBF4dC", providersByService.get(0)[0]); assertEquals("jbO4aBF4dC", providersByService.get(0));
assertEquals("7MuF1c59XP", providersByService.get(1)[0]); assertEquals("7MuF1c59XP", providersByService.get(1));
dbHelper.deleteAll(); dbHelper.deleteAll();
} }