From da469fe2662eb65a398642df4999ad809bc415c1 Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Tue, 20 Nov 2018 13:03:26 -0500 Subject: [PATCH 1/6] Added some DB functionalities - not done yet --- .../olympus/olympusservices/DBHelper.java | 419 +++++++++++++++++- .../olympusservices/ServiceProvider.java | 11 +- 2 files changed, 412 insertions(+), 18 deletions(-) diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java index 6735a7f..98acd0e 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -7,8 +7,12 @@ import android.database.Cursor; import android.content.ContentValues; import java.util.ArrayList; +import java.util.Calendar; import java.util.LinkedList; import java.util.List; +import java.util.GregorianCalendar; + +import com.uottawa.olympus.olympusservices.Booking.Status; /** * The class DBHelper allows the Android application to access and perform @@ -23,7 +27,7 @@ import java.util.List; public class DBHelper extends SQLiteOpenHelper { //version of db used for update method - private static final int DB_VERSION = 4; + private static final int DB_VERSION = 5; //name of db in app data private static final String DB_NAME = "UsersDB.db"; @@ -59,6 +63,9 @@ public class DBHelper extends SQLiteOpenHelper { //columns of TABLE_SERVICEPROVIDERS private static final String COLUMN_SERVICEPROVIDERNAME = "username"; private static final String COLUMN_SERVICEPROVIDERSERVICE = "service"; + private static final String COLUMN_AVERAGERATING = "averageRating"; + private static final String COLUMN_RATERS = "raters"; + private static final String COLUMN_ACTIVE = "active"; //name of table containing service provider availability //availability is stored as number of minutes from 00:00 @@ -80,7 +87,19 @@ public class DBHelper extends SQLiteOpenHelper { private static final String COLUMN_SUNSTART = "sundaystart"; private static final String COLUMN_SUNEND = "sundayend"; - + //name of table containing booking + private static final String TABLE_BOOKINGS = "bookings"; + //columns of TABLE_BOOKING + private static final String COLUMN_BOOKINGSERVICEPROVIDER = "serviceProvider"; + private static final String COLUMN_BOOKINGHOMEOWNER = "homeOwner"; + private static final String COLUMN_BOOKINGSERVICE = "service"; + private static final String COLUMN_BOOKINGYEAR = "year"; + private static final String COLUMN_BOOKINGMONTH = "month"; + private static final String COLUMN_BOOKINGDATE = "day"; + private static final String COLUMN_BOOKINGSTART = "starttime"; + private static final String COLUMN_BOOKINGEND = "endtime"; + private static final String COLUMN_BOOKINGSTATUS = "status"; + private static final String COLUMN_RATING = "rating"; /** * Creates an instance of DBHelper to allow activities to access and @@ -130,6 +149,9 @@ public class DBHelper extends SQLiteOpenHelper { String CREATE_SERVICEPROVIDERS_TABLE = "CREATE TABLE "+ TABLE_SERVICEPROVIDERS + "(" + COLUMN_SERVICEPROVIDERNAME + " TEXT, " + COLUMN_SERVICEPROVIDERSERVICE + " TEXT, " + + COLUMN_AVERAGERATING + " REAL DEFAULT 0, " + + COLUMN_RATERS + " REAL DEFAULT 0, " + + COLUMN_ACTIVE + " TEXT DEFAULT 'active', " //service provider name is foreign key + " FOREIGN KEY(" + COLUMN_SERVICEPROVIDERNAME + ") REFERENCES " + TABLE_LOGIN + "(" + COLUMN_USERNAME +"), " @@ -157,6 +179,21 @@ public class DBHelper extends SQLiteOpenHelper { + COLUMN_SUNSTART + " REAL, " + COLUMN_SUNEND + " REAL)"; db.execSQL(CREATE_AVAILABILITY_TABLE); + + //making the table containing bookings + String CREATE_BOOKING_TABLE = "CREATE TABLE "+ TABLE_BOOKINGS + "(" + + COLUMN_BOOKINGSERVICEPROVIDER + " TEXT," + + COLUMN_BOOKINGHOMEOWNER + " TEXT," + + COLUMN_BOOKINGSERVICE + " TEXT, " + + COLUMN_BOOKINGYEAR + " REAL," + + COLUMN_BOOKINGMONTH + " REAL, " + + COLUMN_BOOKINGDATE + " REAL, " + + COLUMN_BOOKINGSTART + " REAL, " + + COLUMN_BOOKINGEND + " REAL, " + + COLUMN_BOOKINGSTATUS + " TEXT, " + + COLUMN_RATING + " REAL DEFAULT 0 " + + ")"; + db.execSQL(CREATE_BOOKING_TABLE); } @Override @@ -208,7 +245,22 @@ public class DBHelper extends SQLiteOpenHelper { case 3: db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_DESCRIPTION + " TEXT"); db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_SALT + " TEXT"); - + case 4: + db.execSQL("ALTER TABLE " + TABLE_SERVICEPROVIDERS + " ADD COLUMN " + COLUMN_AVERAGERATING + " REAL DEFAULT 0"); + db.execSQL("ALTER TABLE " + TABLE_SERVICEPROVIDERS + " ADD COLUMN " + COLUMN_RATERS + " REAL DEFAULT 0"); + db.execSQL("ALTER TABLE " + TABLE_SERVICEPROVIDERS + " ADD COLUMN " + COLUMN_ACTIVE + " TEXT DEFAULT 'active'"); + db.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE_BOOKINGS + "(" + + COLUMN_BOOKINGSERVICEPROVIDER + " TEXT," + + COLUMN_BOOKINGHOMEOWNER + " TEXT," + + COLUMN_BOOKINGSERVICE + " TEXT, " + + COLUMN_BOOKINGYEAR + " REAL," + + COLUMN_BOOKINGMONTH + " REAL, " + + COLUMN_BOOKINGDATE + " REAL, " + + COLUMN_BOOKINGSTART + " REAL, " + + COLUMN_BOOKINGEND + " REAL, " + + COLUMN_BOOKINGSTATUS + " TEXT, " + + COLUMN_RATING + " REAL DEFAULT 0 " + + ")"); } } @@ -571,7 +623,7 @@ public class DBHelper extends SQLiteOpenHelper { //Check for duplicate username/service combination by querying login table Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS, - new String[] {COLUMN_SERVICEPROVIDERNAME}, + new String[] {COLUMN_ACTIVE}, COLUMN_SERVICEPROVIDERNAME + " = ? AND " + COLUMN_SERVICEPROVIDERSERVICE + " = ?", new String[]{serviceProviderUsername, serviceName}, @@ -579,8 +631,18 @@ public class DBHelper extends SQLiteOpenHelper { "1"); //If cursor has 1+ elements in it, username/service combination already exists in table if (cursor != null && cursor.getCount() > 0){ - cursor.close(); - return false; + if (cursor.getString(0).equals("active")){ + cursor.close(); + return false; + } else { + ContentValues values = new ContentValues(); + values.put(COLUMN_ACTIVE, "active"); + return writeDB.update(TABLE_SERVICEPROVIDERS, values, + COLUMN_SERVICEPROVIDERNAME + " = ? AND " + + COLUMN_SERVICEPROVIDERSERVICE + " = ?", + new String[]{serviceProviderUsername, serviceName})>0; + } + } cursor.close(); @@ -604,7 +666,11 @@ public class DBHelper extends SQLiteOpenHelper { public boolean deleteServiceProvidedByUser(String serviceProviderUsername, String serviceName){ if (serviceProviderUsername == null || serviceName == null) return false; serviceName = serviceName.toLowerCase().trim(); - return writeDB.delete(TABLE_SERVICEPROVIDERS, + + ContentValues values = new ContentValues(); + values.put(COLUMN_ACTIVE, "inactive"); + + return writeDB.update(TABLE_SERVICEPROVIDERS, values, COLUMN_SERVICEPROVIDERNAME + " = ? AND " + COLUMN_SERVICEPROVIDERSERVICE + " = ?", new String[]{serviceProviderUsername, serviceName}) > 0; @@ -626,7 +692,8 @@ public class DBHelper extends SQLiteOpenHelper { + " ON " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERSERVICE + " = " + TABLE_SERVICES + "." + COLUMN_SERVICE + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME - + "= '" + serviceProviderName + "'"); + + "= '" + serviceProviderName + "'" + + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); } public List getAllProvidersByService(Service service){ @@ -642,7 +709,8 @@ public class DBHelper extends SQLiteOpenHelper { return getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME + " FROM " + TABLE_SERVICEPROVIDERS + " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '" - + serviceName + "'"); + + serviceName + "'" + + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); } public boolean updateAvailability(ServiceProvider serviceProvider){ @@ -732,6 +800,339 @@ public class DBHelper extends SQLiteOpenHelper { return availabilities; } + + public boolean addBooking(Booking booking){ + if (booking == null) return false; + + GregorianCalendar current = new GregorianCalendar(); + current.setTimeInMillis(System.currentTimeMillis()); + GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), + booking.getDay(), booking.getStarth(), booking.getStartmin()); + + //check if time of booking is after this time + if (current.compareTo(bookDate) > 0) return false; + + + ContentValues contentValues = new ContentValues(); + contentValues.put(COLUMN_BOOKINGSTART, booking.getStarth()*60 + booking.getStartmin()); + contentValues.put(COLUMN_BOOKINGEND, booking.getEndh()*60 + booking.getEndmin()); + contentValues.put(COLUMN_BOOKINGDATE, booking.getDay()); + contentValues.put(COLUMN_BOOKINGMONTH, booking.getMonth()); + contentValues.put(COLUMN_BOOKINGYEAR, booking.getYear()); + contentValues.put(COLUMN_BOOKINGSERVICEPROVIDER, booking.getServiceprovider().getUsername()); + contentValues.put(COLUMN_BOOKINGHOMEOWNER, booking.getHomeowner().getUsername()); + contentValues.put(COLUMN_BOOKINGSERVICE, booking.getService().getName()); + contentValues.put(COLUMN_BOOKINGSTATUS, Status.PENDING.toString()); + contentValues.put(COLUMN_RATING, 0); + + writeDB.insert(TABLE_BOOKINGS, null, contentValues); + + return true; + } + + public List findBookings(String username){ + List bookingList = new ArrayList<>(); + ServiceProvider serviceProvider = null; + HomeOwner homeOwner = null; + Cursor cursor = writeDB.rawQuery("SELECT * FROM " + TABLE_LOGIN + " WHERE " + + COLUMN_BOOKINGSERVICEPROVIDER + " = ?", + new String[] {username}); + if (!cursor.moveToFirst() || cursor.getString(5).equals("Admin")) return bookingList; + + if (cursor.getString(5).equals("ServiceProvider")) { + serviceProvider = (ServiceProvider)findUserByUsername(username); + cursor = writeDB.rawQuery("SELECT * FROM " + TABLE_BOOKINGS + " WHERE " + + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " + + COLUMN_BOOKINGSTATUS + " != ?", new String[]{username, Status.CANCELLED.toString()}); + } else { + homeOwner = (HomeOwner)findUserByUsername(username); + cursor = writeDB.rawQuery("SELECT * FROM " + TABLE_BOOKINGS + " WHERE " + + COLUMN_BOOKINGHOMEOWNER + " = ? AND " + + COLUMN_BOOKINGSTATUS + " != ?", new String[]{username, Status.CANCELLED.toString()}); + } + + if (cursor.moveToFirst()){ + for (int i=0; i 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}, +// COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " +// + COLUMN_BOOKINGYEAR + " = ? AND " +// + COLUMN_BOOKINGMONTH + " = ? AND " +// + COLUMN_BOOKINGDATE + " = ? AND " +// + COLUMN_BOOKINGSTATUS + " != ?", +// new String[] {serviceProvider, String.valueOf(year), +// String.valueOf(month), String.valueOf(day), Status.CANCELLED.toString()}, +// null, null, COLUMN_BOOKINGSTART, null); +// boolean found = cursor.moveToFirst(); +// cursor.close(); +// return found; +// } + + + + public boolean confirmBooking(Booking booking){ + if (booking == null) return false; + + ContentValues contentValues = new ContentValues(); + contentValues.put(COLUMN_BOOKINGSTATUS, Status.CONFIRMED.toString()); + + GregorianCalendar current = new GregorianCalendar(); + current.setTimeInMillis(System.currentTimeMillis()); + GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), + booking.getDay(), booking.getStarth(), booking.getStartmin()); + + //check if time of booking is before this time + if (current.compareTo(bookDate) > 0) return false; + + return writeDB.update(TABLE_BOOKINGS, contentValues, + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " + + COLUMN_BOOKINGHOMEOWNER + " = ? AND " + + COLUMN_BOOKINGYEAR + " = ? AND " + + COLUMN_BOOKINGMONTH + " = ? AND " + + COLUMN_BOOKINGDATE + " = ? AND " + + COLUMN_BOOKINGSTART + " = ? AND " + + COLUMN_BOOKINGSTATUS + " = ?)", + new String[] {booking.getServiceprovider().getUsername(), + booking.getHomeowner().getUsername(), + String.valueOf(booking.getYear()), + String.valueOf(booking.getMonth()), + String.valueOf(booking.getDay()), + String.valueOf(booking.getStarth()*60 + booking.getStartmin()), + Status.PENDING.toString()}) > 0; + } + + public boolean cancelBooking(Booking booking){ + if (booking == null) return false; + + ContentValues contentValues = new ContentValues(); + contentValues.put(COLUMN_BOOKINGSTATUS, Status.CANCELLED.toString()); + + GregorianCalendar current = new GregorianCalendar(); + current.setTimeInMillis(System.currentTimeMillis()); + GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), + booking.getDay(), booking.getStarth(), booking.getStartmin()); + + //check if time of booking has passed + if (current.compareTo(bookDate) > 0) return false; + + return writeDB.update(TABLE_BOOKINGS, contentValues, + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " + + COLUMN_BOOKINGHOMEOWNER + " = ? AND " + + COLUMN_BOOKINGYEAR + " = ? AND " + + COLUMN_BOOKINGMONTH + " = ? AND " + + COLUMN_BOOKINGDATE + " = ? AND " + + COLUMN_BOOKINGSTART + " = ?)", + new String[] {booking.getServiceprovider().getUsername(), + booking.getHomeowner().getUsername(), + String.valueOf(booking.getYear()), + String.valueOf(booking.getMonth()), + String.valueOf(booking.getDay()), + String.valueOf(booking.getStarth()*60 + booking.getStartmin())}) > 0; + } + + public boolean addRating(Booking booking, double rating){ + if (booking == null) return false; + + GregorianCalendar current = new GregorianCalendar(); + current.setTimeInMillis(System.currentTimeMillis()); + GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), + booking.getDay(), booking.getEndh(), booking.getEndmin()); + + //check if time of booking is before this time + if (current.compareTo(bookDate) < 0) return false; + + ContentValues contentValues = new ContentValues(); + contentValues.put(COLUMN_RATING, rating); + + + boolean updated = writeDB.update(TABLE_BOOKINGS, contentValues, + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " + + COLUMN_BOOKINGHOMEOWNER + " = ? AND " + + COLUMN_BOOKINGYEAR + " = ? AND " + + COLUMN_BOOKINGMONTH + " = ? AND " + + COLUMN_BOOKINGDATE + " = ? AND " + + COLUMN_BOOKINGSTART + " = ?)", + new String[] {booking.getServiceprovider().getUsername(), + booking.getHomeowner().getUsername(), + String.valueOf(booking.getYear()), + String.valueOf(booking.getMonth()), + String.valueOf(booking.getDay()), + String.valueOf(booking.getStarth()*60 + booking.getStartmin())}) > 0; + + if (updated) { + Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS, new String[]{COLUMN_AVERAGERATING, COLUMN_RATERS}, + COLUMN_SERVICEPROVIDERNAME + " = ? AND " + + COLUMN_SERVICEPROVIDERSERVICE + " = ?", + new String[] {booking.getServiceprovider().getUsername(), booking.getService().getName()}, + null, null, null, null); + cursor.moveToFirst(); + + int currentRaters = cursor.getInt(1) + 1; + double currentAverageRatings = cursor.getDouble(0) + rating; + cursor.close(); + + contentValues = new ContentValues(); + contentValues.put(COLUMN_AVERAGERATING, currentAverageRatings/(double)currentRaters); + contentValues.put(COLUMN_RATERS, currentRaters); + + writeDB.update(TABLE_BOOKINGS, contentValues, + COLUMN_SERVICEPROVIDERNAME + " = ? AND " + + COLUMN_SERVICEPROVIDERSERVICE + " = ?", + new String[] {booking.getServiceprovider().getUsername(), + booking.getService().getName()}); + } + return updated; + + } + + public double getRatings(ServiceProvider serviceProvider, Service service){ + return getRatings(serviceProvider.getUsername(), service.getName()); + } + + public double getRatings(String serviceProviderName, String serviceName){ + Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS, new String[]{COLUMN_AVERAGERATING}, + COLUMN_SERVICEPROVIDERNAME + " = ? AND " + + COLUMN_SERVICEPROVIDERSERVICE + " = ?", + new String[] {serviceProviderName, serviceName}, + null, null, null, null); + cursor.moveToFirst(); + cursor.close(); + + return cursor.getDouble(0); + } + + public int getRaters(ServiceProvider serviceProvider, Service service){ + return getRaters(serviceProvider.getUsername(), service.getName()); + + } + + public int getRaters(String serviceProviderName, String serviceName){ + Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS, new String[]{COLUMN_RATERS}, + COLUMN_SERVICEPROVIDERNAME + " = ? AND " + + COLUMN_SERVICEPROVIDERSERVICE + " = ?", + new String[] {serviceProviderName, serviceName}, + null, null, null, null); + cursor.moveToFirst(); + cursor.close(); + + return cursor.getInt(0); + } + + + + /** * Prints all entries of table. One row is printed per line. Columns are * separated by spaces. diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/ServiceProvider.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/ServiceProvider.java index 35839c8..d2bf955 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/ServiceProvider.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/ServiceProvider.java @@ -46,15 +46,7 @@ public class ServiceProvider extends UserType { */ ServiceProvider(String username, String password, String firstname, String lastname, String address, String phonenumber, String companyname, boolean licensed){ - super(username, password, firstname, lastname); - services = new ArrayList<>(); - availabilities = new int[7][4]; - this.address = address; - this.phonenumber = phonenumber; - this.companyname = companyname; - this.licensed = licensed; - this.description = ""; - this.rating = 0; + this(username, password, firstname, lastname, address, phonenumber, companyname, licensed, ""); } ServiceProvider(String username, String password, String firstname, String lastname, String address, @@ -67,6 +59,7 @@ public class ServiceProvider extends UserType { this.companyname = companyname; this.licensed = licensed; this.description = description; + this.rating = 0; } /** From 3cf1f74dc4f668f0dee75e692aafc88ac9dfb414 Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Tue, 20 Nov 2018 23:43:16 -0500 Subject: [PATCH 2/6] Completed booking and lookup by time available --- .../olympusservices/DBIntegrationTest.java | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java index 64b4972..32981c0 100644 --- a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java @@ -238,7 +238,7 @@ public class DBIntegrationTest { added = dbHelper.addService(new Service("extermiNating fLatworms", 25.00)); assertTrue(!added); - dbHelper.deleteService("Exterminating flatworms"); + dbHelper.deleteAll(); } @Test @@ -255,7 +255,7 @@ public class DBIntegrationTest { assertEquals("exterminating flatworms", service.getName()); assertEquals(25.00, service.getRate(), 0.001); - dbHelper.deleteService("Exterminating flatworms"); + dbHelper.deleteAll(); } @Test @@ -273,7 +273,7 @@ public class DBIntegrationTest { assertEquals(dbService.getRate(), Double.parseDouble(service[1]), 0.001); } - dbHelper.deleteService("Exterminating flatworms"); + dbHelper.deleteAll(); } @Test @@ -285,8 +285,8 @@ public class DBIntegrationTest { assertTrue(added); boolean deleted = dbHelper.deleteServiceProvidedByUser("jbO4aBF4dC", "Hitman"); assertTrue(deleted); - dbHelper.deleteUser("jbO4aBF4dC"); - dbHelper.deleteService("hitman"); + + dbHelper.deleteAll(); } @Test @@ -318,7 +318,7 @@ public class DBIntegrationTest { servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC"); assertEquals(0, servicesProvidedByUser.size()); - dbHelper.deleteUser("jbO4aBF4dC"); + dbHelper.deleteAll(); } @Test @@ -338,9 +338,7 @@ public class DBIntegrationTest { assertEquals("jbO4aBF4dC", providersByService.get(0)[0]); assertEquals("7MuF1c59XP", providersByService.get(1)[0]); - dbHelper.deleteService("Exterminating flatworms"); - dbHelper.deleteUser("jbO4aBF4dC"); - dbHelper.deleteUser("7MuF1c59XP"); + dbHelper.deleteAll(); } @@ -369,7 +367,7 @@ public class DBIntegrationTest { servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC"); assertEquals(0, servicesProvidedByUser.size()); - dbHelper.deleteUser("jbO4aBF4dC"); + dbHelper.deleteAll(); } @@ -407,7 +405,7 @@ public class DBIntegrationTest { assertEquals(availabilities[i][j], dbAvailabilities[i][j]); } } - dbHelper.deleteUser("jbO4aBF4dC"); + dbHelper.deleteAll(); } @@ -432,7 +430,37 @@ public class DBIntegrationTest { assertEquals(0, dbAvailabilities[i][j]); } } - dbHelper.deleteUser("jbO4aBF4dC"); + dbHelper.deleteAll(); + } + + @Test + public void testAddBooking(){ + ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, + "testaddress", "8888888888", "companydotcom", true); + serviceProvider.setAvailabilities(0, 4, 18, 19, 30); + serviceProvider.setAvailabilities(1, 5, 20, 21, 11); + serviceProvider.setAvailabilities(3, 7, 12, 15, 14); + serviceProvider.setAvailabilities(4, 0, 0, 23, 29); + + dbHelper.addUser(serviceProvider); + dbHelper.updateAvailability(serviceProvider); + + dbHelper.addUser(new HomeOwner("7MuF1c59XP", null, null, null)); + + dbHelper.addService(new Service("Hitman", 12358)); + dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "Hitman"); + + boolean added = dbHelper.addBooking("jbO4aBF4dC", "7MuF1c59XP", "Hitman", + 2018, 11, 20, 8, 12, 10, 0); + assertTrue(added); + added = dbHelper.addBooking("jbO4aBF4dC", "7MuF1c59XP", "Hitman", + 2018, 11, 20, 9, 12, 12, 0); + assertTrue(!added); + added = dbHelper.addBooking("jbO4aBF4dC", "7MuF1c59XP", "Hitman", + 2018, 11, 22, 6, 12, 7, 30); + assertTrue(!added); + + dbHelper.deleteAll(); } } From 4914779451fd459016b1f5ad8f04211184fc70cc Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Tue, 20 Nov 2018 23:54:03 -0500 Subject: [PATCH 3/6] Trying again to fix DB things --- .../olympus/olympusservices/DBHelper.java | 315 +++++++++++------- .../olympusservices/DBIntegrationTest.java | 8 +- 2 files changed, 204 insertions(+), 119 deletions(-) diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java index 98acd0e..8bc9996 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -296,12 +296,15 @@ public class DBHelper extends SQLiteOpenHelper { } cursor.close(); + //Put values of UserType into columns ContentValues values = new ContentValues(); values.put(COLUMN_USERNAME, userType.getUsername()); values.put(COLUMN_PASSWORD, userType.getPassword()); values.put(COLUMN_FIRSTNAME, userType.getFirstname()); values.put(COLUMN_LASTNAME, userType.getLastname()); values.put(COLUMN_USERTYPE, userType.getClass().getSimpleName()); + + //special case for ServiceProvider if (userType instanceof ServiceProvider){ ServiceProvider serviceProvider = (ServiceProvider)userType; @@ -696,21 +699,27 @@ public class DBHelper extends SQLiteOpenHelper { + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); } - public List getAllProvidersByService(Service service){ + public List getAllProvidersByService(Service service){ if (service == null) return new ArrayList<>(); return getAllProvidersByService(service.getName()); } - public List getAllProvidersByService(String serviceName){ + public List getAllProvidersByService(String serviceName){ if (serviceName == null) return new ArrayList<>(); serviceName = serviceName.toLowerCase().trim(); - return getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME + List providers = getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME + " FROM " + TABLE_SERVICEPROVIDERS + " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '" + serviceName + "'" + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); + + List providerList = new ArrayList<>(); + for (String[] provider : providers){ + providerList.add(provider[0]); + } + return providerList; } public boolean updateAvailability(ServiceProvider serviceProvider){ @@ -804,30 +813,53 @@ public class DBHelper extends SQLiteOpenHelper { public boolean addBooking(Booking booking){ 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(); current.setTimeInMillis(System.currentTimeMillis()); - GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), - booking.getDay(), booking.getStarth(), booking.getStartmin()); + GregorianCalendar bookDate = new GregorianCalendar(year, month, day, starth, startmin); //check if time of booking is after this time if (current.compareTo(bookDate) > 0) return false; + if (isProviderAvailable(serviceProvider, year, month, day, starth, startmin, + endh, endmin)){ - ContentValues contentValues = new ContentValues(); - contentValues.put(COLUMN_BOOKINGSTART, booking.getStarth()*60 + booking.getStartmin()); - contentValues.put(COLUMN_BOOKINGEND, booking.getEndh()*60 + booking.getEndmin()); - contentValues.put(COLUMN_BOOKINGDATE, booking.getDay()); - contentValues.put(COLUMN_BOOKINGMONTH, booking.getMonth()); - contentValues.put(COLUMN_BOOKINGYEAR, booking.getYear()); - contentValues.put(COLUMN_BOOKINGSERVICEPROVIDER, booking.getServiceprovider().getUsername()); - contentValues.put(COLUMN_BOOKINGHOMEOWNER, booking.getHomeowner().getUsername()); - contentValues.put(COLUMN_BOOKINGSERVICE, booking.getService().getName()); - contentValues.put(COLUMN_BOOKINGSTATUS, Status.PENDING.toString()); - contentValues.put(COLUMN_RATING, 0); + ContentValues contentValues = new ContentValues(); + contentValues.put(COLUMN_BOOKINGSTART, starth * 60 + startmin); + contentValues.put(COLUMN_BOOKINGEND, endh * 60 + endmin); + contentValues.put(COLUMN_BOOKINGDATE, day); + contentValues.put(COLUMN_BOOKINGMONTH, month); + contentValues.put(COLUMN_BOOKINGYEAR, year); + contentValues.put(COLUMN_BOOKINGSERVICEPROVIDER, serviceProvider); + contentValues.put(COLUMN_BOOKINGHOMEOWNER, homeOwner); + contentValues.put(COLUMN_BOOKINGSERVICE, service); + contentValues.put(COLUMN_BOOKINGSTATUS, Status.PENDING.toString()); + 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 findBookings(String username){ @@ -881,104 +913,116 @@ public class DBHelper extends SQLiteOpenHelper { } -// public boolean isProviderAvailable(String serviceProvider, String monthDayYear, -// int starth, int startmin, int endh, int endmin) { -// String[] date = monthDayYear.split("/"); -// return isProviderAvailable(serviceProvider, Integer.parseInt(date[2]), -// Integer.parseInt(date[0]), Integer.parseInt(date[1]), -// starth, startmin, endh, endmin); -// } + public boolean isProviderAvailable(String serviceProvider, String monthDayYear, + int starth, int startmin, int endh, int endmin) { + String[] date = monthDayYear.split("/"); + return isProviderAvailable(serviceProvider, Integer.parseInt(date[2]), + Integer.parseInt(date[0]), Integer.parseInt(date[1]), + starth, startmin, endh, endmin); + } -// /** -// * Returns false if end time before start time, or service provider not available on that day -// * -// * @param serviceProvider -// * @param year -// * @param month -// * @param day -// * @param starth -// * @param startmin -// * @param endh -// * @param endmin -// * @return -// */ -// public boolean isProviderAvailable(String serviceProvider, int year, int month, int day, -// int starth, int startmin, int endh, int endmin){ -// -// int bookingStart = starth*60 + startmin; -// int bookingEnd = endh*60 + endmin; -// -// if (bookingEnd < bookingStart) return false; -// -// GregorianCalendar start = new GregorianCalendar(year, month-1, day); -// //Calendar.DAY_OF_WEEK starts with 1 for Sunday, and onwards -// int dayOfWeek = start.get(Calendar.DAY_OF_WEEK)-1; -// Cursor cursor = null; -// int availabilityStart, availabilityEnd; -// -// //Check availabilities on day of week -// switch (dayOfWeek){ -// case 1: //Sunday -// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SUNSTART, COLUMN_SUNEND}, -// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, -// null, null, null); -// break; -// case 2: //Monday -// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_MONSTART, COLUMN_MONEND}, -// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, -// null, null, null); -// break; -// case 3: //Tuesday -// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_TUESTART, COLUMN_TUEEND}, -// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, -// null, null, null); -// break; -// case 4: //Wednesday -// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_WEDSTART, COLUMN_WEDEND}, -// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, -// null, null, null); -// break; -// case 5: //Thursday -// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_THUSTART, COLUMN_THUEND}, -// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, -// null, null, null); -// break; -// case 6: //Friday -// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_FRISTART, COLUMN_FRIEND}, -// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, -// null, null, null); -// break; -// case 7: //Saturday -// cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SATSTART, COLUMN_SATEND}, -// COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, -// null, null, null); -// break; -// } -// -// cursor.moveToFirst(); -// availabilityStart = cursor.getInt(0); -// availabilityEnd = cursor.getInt(1); -// -// //service provider not available if availability end is 0, if availability starts after booking start, -// // or if availability ends before booking end -// 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}, -// COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " -// + COLUMN_BOOKINGYEAR + " = ? AND " -// + COLUMN_BOOKINGMONTH + " = ? AND " -// + COLUMN_BOOKINGDATE + " = ? AND " -// + COLUMN_BOOKINGSTATUS + " != ?", -// new String[] {serviceProvider, String.valueOf(year), -// String.valueOf(month), String.valueOf(day), Status.CANCELLED.toString()}, -// null, null, COLUMN_BOOKINGSTART, null); -// boolean found = cursor.moveToFirst(); -// cursor.close(); -// return found; -// } + /** + * Returns false if end time before start time, or service provider not available on that day + * + * @param serviceProvider + * @param year + * @param month + * @param day + * @param starth + * @param startmin + * @param endh + * @param endmin + * @return + */ + public boolean isProviderAvailable(String serviceProvider, int year, int month, int day, + int starth, int startmin, int endh, int endmin){ + + int bookingStart = starth*60 + startmin; + int bookingEnd = endh*60 + endmin; + + if (bookingEnd < bookingStart) return false; + + GregorianCalendar start = new GregorianCalendar(year, month-1, day); + //Calendar.DAY_OF_WEEK starts with 1 for Sunday, and onwards + int dayOfWeek = start.get(Calendar.DAY_OF_WEEK); + Cursor cursor = null; + int availabilityStart, availabilityEnd; + + //Check availabilities on day of week + switch (dayOfWeek){ + case 1: //Sunday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SUNSTART, COLUMN_SUNEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 2: //Monday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_MONSTART, COLUMN_MONEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 3: //Tuesday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_TUESTART, COLUMN_TUEEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 4: //Wednesday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_WEDSTART, COLUMN_WEDEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 5: //Thursday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_THUSTART, COLUMN_THUEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 6: //Friday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_FRISTART, COLUMN_FRIEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 7: //Saturday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SATSTART, COLUMN_SATEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + } + + cursor.moveToFirst(); + availabilityStart = cursor.getInt(0); + availabilityEnd = cursor.getInt(1); + + //service provider not available if availability end is 0, if availability starts after booking start, + // or if availability ends before booking end + 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}, + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " + + COLUMN_BOOKINGYEAR + " = ? AND " + + COLUMN_BOOKINGMONTH + " = ? AND " + + COLUMN_BOOKINGDATE + " = ? AND " + + COLUMN_BOOKINGSTATUS + " != ?", + new String[] {serviceProvider, String.valueOf(year), + String.valueOf(month), String.valueOf(day), Status.CANCELLED.toString()}, + null, null, COLUMN_BOOKINGSTART, null); + if (cursor.moveToFirst()){ + for (int i = 0; i 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); } + public List getProvidersAboveRating(Service service, double rating){ + return getProvidersAboveRating(service.getName(), rating); + } + + public List getProvidersAboveRating(String serviceName, double rating){ + List providers = getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME + " FROM " + TABLE_SERVICEPROVIDERS + + " WHERE " + COLUMN_SERVICE + " = " + serviceName + " AND " + + COLUMN_AVERAGERATING + " >= " + rating); + List providerList = new ArrayList<>(); + for (String[] provider : providers){ + providerList.add(provider[0]); + } + return providerList; + } + + public List 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 getProvidersByTime(String serviceName, int year, int month, int day, + int starth, int startmin, int endh, int endmin){ + List 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; } + 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); + } + } diff --git a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java index 32981c0..aca0a49 100644 --- a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java @@ -121,7 +121,7 @@ public class DBIntegrationTest { dbHelper.deleteUser("jbO4aBF4dC"); - List providersList = dbHelper.getAllProvidersByService("hitman"); + List providersList = dbHelper.getAllProvidersByService("hitman"); assertEquals(0, providersList.size()); providersList = dbHelper.getAllProvidersByService("hitman"); assertEquals(0, providersList.size()); @@ -332,11 +332,11 @@ public class DBIntegrationTest { dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms"); dbHelper.addServiceProvidedByUser("7MuF1c59XP", "exterminating flatworms"); - List providersByService = dbHelper.getAllProvidersByService("exterminating flatworms"); + List providersByService = dbHelper.getAllProvidersByService("exterminating flatworms"); assertEquals(2, providersByService.size()); - assertEquals("jbO4aBF4dC", providersByService.get(0)[0]); - assertEquals("7MuF1c59XP", providersByService.get(1)[0]); + assertEquals("jbO4aBF4dC", providersByService.get(0)); + assertEquals("7MuF1c59XP", providersByService.get(1)); dbHelper.deleteAll(); } From 44a2fe402e47b35c58a28bf18c5731a18fbc2b90 Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Wed, 21 Nov 2018 22:58:35 -0500 Subject: [PATCH 4/6] wow I closed a cursor before finishing using it --- .../olympus/olympusservices/DBHelper.java | 79 +++++++++++++++++-- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java index 8bc9996..bd369d6 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -400,10 +400,28 @@ public class DBHelper extends SQLiteOpenHelper { } - + /** + * This method only needs to be called for service providers. Updates user login and + * profile information with requested username. Returns true if entry was found and updated. + * Returns false if nobody was found with said username. + * + * @param username username of entry to update + * @param password new password + * @param firstname new first name + * @param lastname new last name + * @param address new address + * @param phonenumber new phone number + * @param companyname new company name + * @param licensed licensing status + * @param description new description + * + * @return whether updating user information was successful + */ public boolean updateUserInfo(String username, String password, String firstname, String lastname, String address, String phonenumber, String companyname, Boolean licensed, String description){ + if (username == null) return false; + ContentValues values = new ContentValues(); if (password != null && !password.equals("")) values.put(COLUMN_PASSWORD, password); if (firstname != null && !firstname.equals("")) values.put(COLUMN_FIRSTNAME, firstname); @@ -477,7 +495,7 @@ public class DBHelper extends SQLiteOpenHelper { * Returns true if successful in adding service to database. * * @param service service to be added - * @return whether adding service was successful + * @return true if adding service was successful */ public boolean addService(Service service){ if (service == null) return false; @@ -538,7 +556,7 @@ public class DBHelper extends SQLiteOpenHelper { * * @param service service object containing updated values * - * @return whether updating service information was successful + * @return true if updating service information was successful */ public boolean updateService(Service service){ if (service == null) return false; @@ -558,7 +576,7 @@ public class DBHelper extends SQLiteOpenHelper { * @param name name of service * @param rate rate of service * - * @return whether updating service information was successful + * @return true if updating service information was successful */ public boolean updateService(String name, double rate){ if (name == null) return false; @@ -577,7 +595,7 @@ public class DBHelper extends SQLiteOpenHelper { * entry. Returns true if a service was deleted, false otherwise. * * @param service service of entry to delete - * @return whether the service was deleted + * @return true if the service was deleted */ public boolean deleteService(String service) { if (service == null) return false; @@ -607,16 +625,40 @@ public class DBHelper extends SQLiteOpenHelper { return getAll("SELECT * FROM " + TABLE_SERVICES + " ORDER BY " + COLUMN_SERVICE); } + /** + * Adds a service to a service provider. The service provider now offers the service. + * + * @param serviceProvider service provider who offers service + * @param service service offered + * + * @return true if adding service to provider was successful + */ public boolean addServiceProvidedByUser(ServiceProvider serviceProvider, Service service){ if (serviceProvider == null || service == null) return false; return addServiceProvidedByUser(serviceProvider.getUsername(), service.getName()); } + /** + * Adds a service to a service provider. The service provider now offers the service. + * + * @param serviceProvider service provider who offers service + * @param serviceName service offered + * + * @return true if adding service to provider was successful + */ public boolean addServiceProvidedByUser(ServiceProvider serviceProvider, String serviceName){ if (serviceProvider == null || serviceName == null) return false; return addServiceProvidedByUser(serviceProvider.getUsername(), serviceName); } + /** + * Adds a service to a service provider. The service provider now offers the service. + * + * @param serviceProviderUsername service provider who offers service + * @param serviceName service offered + * + * @return true if adding service to provider was successful + */ public boolean addServiceProvidedByUser(String serviceProviderUsername, String serviceName){ if (serviceProviderUsername == null || serviceName == null) return false; @@ -656,6 +698,14 @@ public class DBHelper extends SQLiteOpenHelper { return true; } + /** + * Removes service from service provider. Provider no longer offers this service. + * + * @param serviceProvider service provider who once offered service + * @param service service to be removed + * + * @return true if service provider's status for this service is now inactive + */ public boolean deleteServiceProvidedByUser(ServiceProvider serviceProvider, Service service){ if (serviceProvider == null || service == null) return false; return deleteServiceProvidedByUser(serviceProvider.getUsername(), service.getName()); @@ -1152,9 +1202,10 @@ public class DBHelper extends SQLiteOpenHelper { new String[] {serviceProviderName, serviceName}, null, null, null, null); cursor.moveToFirst(); + double rating = cursor.getDouble(0); cursor.close(); - return cursor.getDouble(0); + return rating; } public int getRaters(ServiceProvider serviceProvider, Service service){ @@ -1207,6 +1258,18 @@ public class DBHelper extends SQLiteOpenHelper { return providers; } + public List getProvidersByTimeAndRating(String serviceName, double rating, int year, int month, int day, + int starth, int startmin, int endh, int endmin){ + List providers = getProvidersByTime(serviceName, year, month, day, starth, startmin, endh, endmin); + for (int i = 0; i < providers.size(); i++){ + String provider = providers.get(i); + if (getRatings(provider, serviceName) Date: Sat, 24 Nov 2018 14:23:17 -0500 Subject: [PATCH 5/6] Changed fetch service providers to include first and last name --- .../olympus/olympusservices/DBHelper.java | 754 ++++++++++++------ .../olympusservices/DBIntegrationTest.java | 161 ++-- 2 files changed, 612 insertions(+), 303 deletions(-) diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java index bd369d6..2423793 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -675,7 +675,7 @@ public class DBHelper extends SQLiteOpenHelper { null, null, null, "1"); //If cursor has 1+ elements in it, username/service combination already exists in table - if (cursor != null && cursor.getCount() > 0){ + if (cursor.moveToFirst()){ if (cursor.getString(0).equals("active")){ cursor.close(); return false; @@ -711,11 +711,27 @@ public class DBHelper extends SQLiteOpenHelper { return deleteServiceProvidedByUser(serviceProvider.getUsername(), service.getName()); } + /** + * Removes service from service provider. Provider no longer offers this service. + * + * @param serviceProvider service provider who once offered service + * @param serviceName service to be removed + * + * @return true if service provider's status for this service is now inactive + */ public boolean deleteServiceProvidedByUser(ServiceProvider serviceProvider, String serviceName){ if (serviceProvider == null || serviceName == null) return false; return deleteServiceProvidedByUser(serviceProvider.getUsername(), serviceName); } + /** + * Removes service from service provider. Provider no longer offers this service. + * + * @param serviceProviderUsername service provider who once offered service + * @param serviceName service to be removed + * + * @return true if service provider's status for this service is now inactive + */ public boolean deleteServiceProvidedByUser(String serviceProviderUsername, String serviceName){ if (serviceProviderUsername == null || serviceName == null) return false; serviceName = serviceName.toLowerCase().trim(); @@ -729,19 +745,30 @@ public class DBHelper extends SQLiteOpenHelper { new String[]{serviceProviderUsername, serviceName}) > 0; } + /** + * Fetches all services provided by a service provider + * + * @param serviceProvider + * @return a list of [service, rate] + */ public List getAllServicesProvidedByUser(ServiceProvider serviceProvider){ if (serviceProvider == null) return new ArrayList<>(); return getAllServicesProvidedByUser(serviceProvider.getUsername()); } + /** + * Fetches all services provided by a service provider + * + * @param serviceProviderName + * @return a list of [service, rate] + */ public List 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 + + " FROM " + TABLE_SERVICES + " JOIN " + TABLE_SERVICEPROVIDERS + " ON " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERSERVICE + " = " + TABLE_SERVICES + "." + COLUMN_SERVICE + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME @@ -749,29 +776,47 @@ public class DBHelper extends SQLiteOpenHelper { + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); } - public List getAllProvidersByService(Service service){ + /** + * Fetches all service providers who provide a specified service + * + * @param service name of service + * @return a list of [username, first name, last name, average rating] sorted by rating + */ + public List getAllProvidersByService(Service service){ if (service == null) return new ArrayList<>(); return getAllProvidersByService(service.getName()); } - public List getAllProvidersByService(String serviceName){ + /** + * Fetches all service providers who provide a specified service + * + * @param serviceName name of service + * @return a list of [username, first name, last name, average rating] sorted by rating + */ + public List getAllProvidersByService(String serviceName){ if (serviceName == null) return new ArrayList<>(); serviceName = serviceName.toLowerCase().trim(); - List providers = getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME - + " FROM " + TABLE_SERVICEPROVIDERS - + " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '" - + serviceName + "'" - + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'"); - - List providerList = new ArrayList<>(); - for (String[] provider : providers){ - providerList.add(provider[0]); - } - return providerList; + return getAll( + "SELECT " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME + ", " + + TABLE_LOGIN + "." + COLUMN_FIRSTNAME + ", " + + TABLE_LOGIN + "." + COLUMN_LASTNAME + ", " + + TABLE_SERVICEPROVIDERS + "." + COLUMN_AVERAGERATING + + " FROM " + TABLE_SERVICEPROVIDERS + " JOIN " + TABLE_LOGIN + + " ON " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME + " = " + + TABLE_LOGIN + "." + COLUMN_USERNAME + + " AND " + COLUMN_SERVICEPROVIDERSERVICE + " = '" + serviceName + "'" + + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'" + + " ORDER BY " + TABLE_SERVICEPROVIDERS + "." + COLUMN_AVERAGERATING + " DESC"); } + /** + * Updates a service provider's availabilities based on ServiceProvider object provided + * + * @param serviceProvider + * @return true if availability was updated successfully + */ public boolean updateAvailability(ServiceProvider serviceProvider){ //availability is stored as number of minutes from 00:00 if (serviceProvider == null) return false; @@ -801,27 +846,6 @@ public class DBHelper extends SQLiteOpenHelper { 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){ @@ -860,6 +884,53 @@ public class DBHelper extends SQLiteOpenHelper { } + public List getAvailabilityByDate(ServiceProvider serviceProvider, int year, int month, int day){ + return getAvailabilityByDate(serviceProvider.getUsername(), year, month, day); + } + + public List getAvailabilityByDate(String serviceProvider, int year, int month, int day){ + + List availabilities = new ArrayList<>(); + + Cursor bookings = writeDB.query(TABLE_BOOKINGS, new String[] {COLUMN_BOOKINGSTART, COLUMN_BOOKINGEND}, + COLUMN_BOOKINGSERVICEPROVIDER+ " = ?" + + " AND " + COLUMN_BOOKINGYEAR + " = ?" + + " AND " + COLUMN_BOOKINGMONTH + " = ?" + + " AND " + COLUMN_BOOKINGDATE + " = ?", + new String[]{serviceProvider, String.valueOf(year), + String.valueOf(month),String.valueOf(day)}, + null, null, COLUMN_BOOKINGSTART, null); + + Cursor availability = getAvailabilityByDayOfWeek(serviceProvider, year, month, day); + if (availability.moveToFirst()) { + int previousEnd = availability.getInt(0); + int currentStart; + int currentEnd; + if (bookings.moveToFirst()) { + for (int i = 0; i < bookings.getCount(); i++) { + currentStart = bookings.getInt(0); + if (currentStart - previousEnd >= 30){ + availabilities.add(new int[]{previousEnd/60, previousEnd%60, + currentStart/60, currentStart%60}); + } + previousEnd = bookings.getInt(1); + bookings.moveToNext(); + } + currentStart = availability.getInt(1); + if (currentStart - previousEnd >= 30){ + availabilities.add(new int[]{previousEnd/60, previousEnd%60, + currentStart/60, currentStart%60}); + } + } else { + currentStart = availability.getInt(0); + currentEnd = availability.getInt(1); + availabilities.add(new int[]{currentStart/60, currentStart%60, + currentEnd/60, currentEnd%60}); + } + } + return availabilities; + } + public boolean addBooking(Booking booking){ if (booking == null) return false; @@ -879,17 +950,44 @@ public class DBHelper extends SQLiteOpenHelper { starth, startmin, endh, endmin); } + /** + * + * @param serviceProvider + * @param homeOwner + * @param service + * @param year + * @param month + * @param day + * @param starth + * @param startmin + * @param endh + * @param endmin + * @return true if booking was successfully added + */ 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(); current.setTimeInMillis(System.currentTimeMillis()); - GregorianCalendar bookDate = new GregorianCalendar(year, month, day, starth, startmin); + GregorianCalendar bookDate = new GregorianCalendar(year, month-1, day, starth, startmin); //check if time of booking is after this time if (current.compareTo(bookDate) > 0) return false; + service = service.trim().toLowerCase(); + + Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS, new String[]{COLUMN_SERVICEPROVIDERNAME}, + COLUMN_SERVICEPROVIDERNAME + " = ? AND " + + COLUMN_SERVICEPROVIDERSERVICE + " = ?", + new String[] {serviceProvider, service}, + null, null, null, "1"); + if (!cursor.moveToFirst()) { + cursor.close(); + return false; + } + cursor.close(); + if (isProviderAvailable(serviceProvider, year, month, day, starth, startmin, endh, endmin)){ @@ -912,6 +1010,11 @@ public class DBHelper extends SQLiteOpenHelper { return false; } + /** + * + * @param username + * @return list of booking objects related to specified user + */ public List findBookings(String username){ List bookingList = new ArrayList<>(); ServiceProvider serviceProvider = null; @@ -924,13 +1027,13 @@ public class DBHelper extends SQLiteOpenHelper { if (cursor.getString(5).equals("ServiceProvider")) { serviceProvider = (ServiceProvider)findUserByUsername(username); cursor = writeDB.rawQuery("SELECT * FROM " + TABLE_BOOKINGS + " WHERE " - + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " - + COLUMN_BOOKINGSTATUS + " != ?", new String[]{username, Status.CANCELLED.toString()}); + + COLUMN_BOOKINGSERVICEPROVIDER + " = ?", + new String[]{username}); } else { homeOwner = (HomeOwner)findUserByUsername(username); cursor = writeDB.rawQuery("SELECT * FROM " + TABLE_BOOKINGS + " WHERE " - + COLUMN_BOOKINGHOMEOWNER + " = ? AND " - + COLUMN_BOOKINGSTATUS + " != ?", new String[]{username, Status.CANCELLED.toString()}); + + COLUMN_BOOKINGHOMEOWNER + " = ?", + new String[]{username}); } if (cursor.moveToFirst()){ @@ -945,7 +1048,8 @@ public class DBHelper extends SQLiteOpenHelper { int month = cursor.getInt(4); int year = cursor.getInt(3); String stat = cursor.getString(8); - Status status = (stat.equals("Pending")? Status.PENDING : Status.CONFIRMED); + Status status = (stat.equals("Pending")? Status.PENDING : + stat.equals("Confirmed")? Status.CONFIRMED : Status.CANCELLED); ServiceProvider serviceprovider = (serviceProvider == null? (ServiceProvider)findUserByUsername(cursor.getString(0)):serviceProvider); HomeOwner homeowner = (homeOwner == null? @@ -963,185 +1067,44 @@ public class DBHelper extends SQLiteOpenHelper { } - public boolean isProviderAvailable(String serviceProvider, String monthDayYear, - int starth, int startmin, int endh, int endmin) { - String[] date = monthDayYear.split("/"); - return isProviderAvailable(serviceProvider, Integer.parseInt(date[2]), - Integer.parseInt(date[0]), Integer.parseInt(date[1]), - starth, startmin, endh, endmin); + /** + * Sets status of specified booking to cancelled. Returns false if booking + * does not exist in database, or date of booking has already passed. + * + * @param booking + * @return true if booking was successfully cancelled + */ + public boolean confirmBooking(Booking booking){ + return modifyBookingStatus(booking, Status.CONFIRMED); } /** - * Returns false if end time before start time, or service provider not available on that day + * Sets status of specified booking to cancelled. Returns false if booking was already cancelled, + * booking does not exist in database, or date of booking has already passed. * - * @param serviceProvider - * @param year - * @param month - * @param day - * @param starth - * @param startmin - * @param endh - * @param endmin - * @return + * @param booking + * @return true if booking was successfully cancelled */ - public boolean isProviderAvailable(String serviceProvider, int year, int month, int day, - int starth, int startmin, int endh, int endmin){ - - int bookingStart = starth*60 + startmin; - int bookingEnd = endh*60 + endmin; - - if (bookingEnd < bookingStart) return false; - - GregorianCalendar start = new GregorianCalendar(year, month-1, day); - //Calendar.DAY_OF_WEEK starts with 1 for Sunday, and onwards - int dayOfWeek = start.get(Calendar.DAY_OF_WEEK); - Cursor cursor = null; - int availabilityStart, availabilityEnd; - - //Check availabilities on day of week - switch (dayOfWeek){ - case 1: //Sunday - cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SUNSTART, COLUMN_SUNEND}, - COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, - null, null, null); - break; - case 2: //Monday - cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_MONSTART, COLUMN_MONEND}, - COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, - null, null, null); - break; - case 3: //Tuesday - cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_TUESTART, COLUMN_TUEEND}, - COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, - null, null, null); - break; - case 4: //Wednesday - cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_WEDSTART, COLUMN_WEDEND}, - COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, - null, null, null); - break; - case 5: //Thursday - cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_THUSTART, COLUMN_THUEND}, - COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, - null, null, null); - break; - case 6: //Friday - cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_FRISTART, COLUMN_FRIEND}, - COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, - null, null, null); - break; - case 7: //Saturday - cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SATSTART, COLUMN_SATEND}, - COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, - null, null, null); - break; - } - - cursor.moveToFirst(); - availabilityStart = cursor.getInt(0); - availabilityEnd = cursor.getInt(1); - - //service provider not available if availability end is 0, if availability starts after booking start, - // or if availability ends before booking end - 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}, - COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " - + COLUMN_BOOKINGYEAR + " = ? AND " - + COLUMN_BOOKINGMONTH + " = ? AND " - + COLUMN_BOOKINGDATE + " = ? AND " - + COLUMN_BOOKINGSTATUS + " != ?", - new String[] {serviceProvider, String.valueOf(year), - String.valueOf(month), String.valueOf(day), Status.CANCELLED.toString()}, - null, null, COLUMN_BOOKINGSTART, null); - if (cursor.moveToFirst()){ - for (int i = 0; i bookingStart)|| - (availabilityStart < bookingEnd && availabilityEnd > bookingEnd) || - (availabilityStart > bookingStart && availabilityEnd < bookingEnd)) return false; - - cursor.moveToNext(); - } - } - return true; - } - - - - public boolean confirmBooking(Booking booking){ - if (booking == null) return false; - - ContentValues contentValues = new ContentValues(); - contentValues.put(COLUMN_BOOKINGSTATUS, Status.CONFIRMED.toString()); - - GregorianCalendar current = new GregorianCalendar(); - current.setTimeInMillis(System.currentTimeMillis()); - GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), - booking.getDay(), booking.getStarth(), booking.getStartmin()); - - //check if time of booking is before this time - if (current.compareTo(bookDate) > 0) return false; - - return writeDB.update(TABLE_BOOKINGS, contentValues, - COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " - + COLUMN_BOOKINGHOMEOWNER + " = ? AND " - + COLUMN_BOOKINGYEAR + " = ? AND " - + COLUMN_BOOKINGMONTH + " = ? AND " - + COLUMN_BOOKINGDATE + " = ? AND " - + COLUMN_BOOKINGSTART + " = ? AND " - + COLUMN_BOOKINGSTATUS + " = ?)", - new String[] {booking.getServiceprovider().getUsername(), - booking.getHomeowner().getUsername(), - String.valueOf(booking.getYear()), - String.valueOf(booking.getMonth()), - String.valueOf(booking.getDay()), - String.valueOf(booking.getStarth()*60 + booking.getStartmin()), - Status.PENDING.toString()}) > 0; - } - public boolean cancelBooking(Booking booking){ - if (booking == null) return false; - - ContentValues contentValues = new ContentValues(); - contentValues.put(COLUMN_BOOKINGSTATUS, Status.CANCELLED.toString()); - - GregorianCalendar current = new GregorianCalendar(); - current.setTimeInMillis(System.currentTimeMillis()); - GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), - booking.getDay(), booking.getStarth(), booking.getStartmin()); - - //check if time of booking has passed - if (current.compareTo(bookDate) > 0) return false; - - return writeDB.update(TABLE_BOOKINGS, contentValues, - COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " - + COLUMN_BOOKINGHOMEOWNER + " = ? AND " - + COLUMN_BOOKINGYEAR + " = ? AND " - + COLUMN_BOOKINGMONTH + " = ? AND " - + COLUMN_BOOKINGDATE + " = ? AND " - + COLUMN_BOOKINGSTART + " = ?)", - new String[] {booking.getServiceprovider().getUsername(), - booking.getHomeowner().getUsername(), - String.valueOf(booking.getYear()), - String.valueOf(booking.getMonth()), - String.valueOf(booking.getDay()), - String.valueOf(booking.getStarth()*60 + booking.getStartmin())}) > 0; + return modifyBookingStatus(booking, Status.CANCELLED); } + + /** + * Add a rating for a specific booking, and updates the average rating for the service provider + * and service combination. The booking must have passed before a rating can be added. + * Note that the rating is final. + * + * @param booking + * @param rating + * @return true if adding the rating was successful + */ public boolean addRating(Booking booking, double rating){ if (booking == null) return false; GregorianCalendar current = new GregorianCalendar(); current.setTimeInMillis(System.currentTimeMillis()); - GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth(), + GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth()-1, booking.getDay(), booking.getEndh(), booking.getEndmin()); //check if time of booking is before this time @@ -1191,11 +1154,26 @@ public class DBHelper extends SQLiteOpenHelper { } + /** + * Returns the average rating for a service provider's service + * + * @param serviceProvider + * @param service + * @return average rating of specified service provider and service combination + */ public double getRatings(ServiceProvider serviceProvider, Service service){ return getRatings(serviceProvider.getUsername(), service.getName()); } + /** + * Returns the average rating for a service provider's service + * + * @param serviceProviderName name of service provider + * @param serviceName name of service + * @return average rating of specified service provider and service combination + */ public double getRatings(String serviceProviderName, String serviceName){ + serviceName = serviceName.trim().toLowerCase(); Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS, new String[]{COLUMN_AVERAGERATING}, COLUMN_SERVICEPROVIDERNAME + " = ? AND " + COLUMN_SERVICEPROVIDERSERVICE + " = ?", @@ -1208,12 +1186,27 @@ public class DBHelper extends SQLiteOpenHelper { return rating; } + /** + * Returns the number of people who rated a service provider's service + * + * @param serviceProvider name of service provider + * @param service name of service + * @return number of raters for specified service provider and service combination + */ public int getRaters(ServiceProvider serviceProvider, Service service){ return getRaters(serviceProvider.getUsername(), service.getName()); } + /** + * Returns the number of people who rated a service provider's service + * + * @param serviceProviderName name of service provider + * @param serviceName name of service + * @return number of raters for specified service provider and service combination + */ public int getRaters(String serviceProviderName, String serviceName){ + serviceName = serviceName.trim().toLowerCase(); Cursor cursor = writeDB.query(TABLE_SERVICEPROVIDERS, new String[]{COLUMN_RATERS}, COLUMN_SERVICEPROVIDERNAME + " = ? AND " + COLUMN_SERVICEPROVIDERSERVICE + " = ?", @@ -1225,54 +1218,150 @@ public class DBHelper extends SQLiteOpenHelper { return cursor.getInt(0); } - public List getProvidersAboveRating(Service service, double rating){ + /** + * Returns list of [username, first name, last name, rating] of service providers that + * have a rating equal to or above the specified rating + * + * @param service + * @param rating minimum rating + * @return a List of [username, first name, last name, rating] of service providers + */ + public List getProvidersAboveRating(Service service, double rating){ return getProvidersAboveRating(service.getName(), rating); } - public List getProvidersAboveRating(String serviceName, double rating){ - List providers = getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME + " FROM " + TABLE_SERVICEPROVIDERS - + " WHERE " + COLUMN_SERVICE + " = " + serviceName + " AND " - + COLUMN_AVERAGERATING + " >= " + rating); - List providerList = new ArrayList<>(); - for (String[] provider : providers){ - providerList.add(provider[0]); - } - return providerList; + /** + * Returns list of [username, first name, last name, rating] of service providers that + * have a rating equal to or above the specified rating + * + * @param serviceName name of service + * @param rating minimum rating + * @return a List of [username, first name, last name, rating] of service providers + */ + public List getProvidersAboveRating(String serviceName, double rating){ + serviceName = serviceName.trim().toLowerCase(); + return getAll("SELECT " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME + ", " + + TABLE_LOGIN + "." + COLUMN_FIRSTNAME + ", " + + TABLE_LOGIN + "." + COLUMN_LASTNAME + ", " + + TABLE_SERVICEPROVIDERS + "." + COLUMN_AVERAGERATING + + " FROM " + TABLE_SERVICEPROVIDERS + " JOIN " + TABLE_LOGIN + + " ON " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME + " = " + + TABLE_LOGIN + "." + COLUMN_USERNAME + + " AND " + COLUMN_SERVICEPROVIDERSERVICE + " = '" + serviceName + "'" + + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'" + + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_AVERAGERATING + " >= " + rating + + " ORDER BY " + TABLE_SERVICEPROVIDERS + "." + COLUMN_AVERAGERATING + " DESC"); } - public List getProvidersByTime(Service service, int year, int month, int day, + /** + * Returns list of [username, first name, last name, rating] of service providers + * available at the specified time + * + * @param service + * @param year + * @param month + * @param day + * @param starth starting hour + * @param startmin starting minute + * @param endh ending hour + * @param endmin ending minute + * @return a List of [username, first name, last name, rating] of service providers + */ + public List 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 getProvidersByTime(String serviceName, int year, int month, int day, + /** + * Returns list of [username, first name, last name, rating] of service providers + * available at the specified time + * + * @param serviceName name of service + * @param year + * @param month + * @param day + * @param starth starting hour + * @param startmin starting minute + * @param endh ending hour + * @param endmin ending minute + * @return a List of [username, first name, last name, rating] of service providers + */ + public List getProvidersByTime(String serviceName, int year, int month, int day, int starth, int startmin, int endh, int endmin){ - List providers = getAllProvidersByService(serviceName); - + serviceName = serviceName.trim().toLowerCase(); + List providers = getAll("SELECT " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME + ", " + + TABLE_LOGIN + "." + COLUMN_FIRSTNAME + ", " + + TABLE_LOGIN + "." + COLUMN_LASTNAME + ", " + + TABLE_SERVICEPROVIDERS + "." + COLUMN_AVERAGERATING + ", " + + " FROM " + TABLE_SERVICEPROVIDERS + " JOIN " + TABLE_LOGIN + + " ON " + TABLE_SERVICEPROVIDERS + "." + COLUMN_SERVICEPROVIDERNAME + " = " + + TABLE_LOGIN + "." + COLUMN_USERNAME + + " AND " + COLUMN_SERVICEPROVIDERSERVICE + " = '" + serviceName + "'" + + " AND " + TABLE_SERVICEPROVIDERS + "." + COLUMN_ACTIVE + " = 'active'" + + " ORDER BY " + TABLE_SERVICEPROVIDERS + "." + COLUMN_AVERAGERATING + " DESC"); for (int i = 0; i < providers.size(); i++){ - String username = providers.get(i); - if (!isProviderAvailable(username, year, month, day, starth, startmin, endh, endmin)){ + if (!isProviderAvailable(providers.get(i)[0], year, month, day, + starth, startmin, endh, endmin)){ providers.remove(i); } } return providers; } - public List getProvidersByTimeAndRating(String serviceName, double rating, int year, int month, int day, + /** + * Returns list of [username, first name, last name, rating] of service providers + * available at the specified time and have a rating equal to or above the specified rating + * + * @param service + * @param rating minimum rating of filter + * @param year + * @param month + * @param day + * @param starth starting hour + * @param startmin starting minute + * @param endh ending hour + * @param endmin ending minute + * @return List of [username, first name, last name, rating] of service providers + */ + public List getProvidersByTimeAndRating(Service service, double rating, int year, int month, int day, + int starth, int startmin, int endh, int endmin){ + return getProvidersByTimeAndRating(service.getName(), rating, year, month, day, + starth, startmin, endh, endmin); + } + + /** + * Returns list of [username, first name, last name, rating] of service providers + * available at the specified time and have a rating equal to or above the specified rating + * + * @param serviceName name of service + * @param rating minimum rating of filter + * @param year + * @param month + * @param day + * @param starth starting hour + * @param startmin starting minute + * @param endh ending hour + * @param endmin ending minute + * @return List of [username, first name, last name, rating] of service providers + */ + public List getProvidersByTimeAndRating(String serviceName, double rating, int year, int month, int day, int starth, int startmin, int endh, int endmin){ - List providers = getProvidersByTime(serviceName, year, month, day, starth, startmin, endh, endmin); - for (int i = 0; i < providers.size(); i++){ - String provider = providers.get(i); - if (getRatings(provider, serviceName) providers = getProvidersByTime(serviceName, year, month, day, starth, startmin, endh, endmin); + + int i = providers.size()-1; + boolean allLowRatingsRemoved = false; + while (i>-1 && !allLowRatingsRemoved){ + if (getRatings(providers.get(i)[0], serviceName)>=rating){ + allLowRatingsRemoved = true; + } else { + providers.remove(i--); } } return providers; } - - /** * Prints all entries of table. One row is printed per line. Columns are * separated by spaces. @@ -1293,6 +1382,17 @@ public class DBHelper extends SQLiteOpenHelper { cursor.close(); } + /** + * Deletes all data from database. It's used for testing when + * DB needs to be cleared. Please don't call this anywhere else. + */ + 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); + } /** * Gets all items in a table @@ -1319,15 +1419,187 @@ public class DBHelper extends SQLiteOpenHelper { } /** - * Deletes all data from database. It's used for testing when - * DB needs to be cleared. Please don't call this anywhere else. + * Adds a service provider's availabilities for a specific day to content values + * @param contentValues + * @param startColumn + * @param endColumn + * @param startAndEndTimes */ - 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); + 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); + } + } + + } + + /** + * Helper method to get a service provider's availability by day of week + * + * @param serviceProvider username of service provider + * @param year + * @param month + * @param day + * @return Cursor containing the start and end of the service provider's availabilities + * on selected day of the week + */ + private Cursor getAvailabilityByDayOfWeek(String serviceProvider, int year, int month, int day){ + GregorianCalendar start = new GregorianCalendar(year, month-1, day); + //Calendar.DAY_OF_WEEK starts with 1 for Sunday, and onwards + int dayOfWeek = start.get(Calendar.DAY_OF_WEEK); + Cursor cursor = null; + + //Check availabilities on day of week + switch (dayOfWeek){ + case 1: //Sunday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SUNSTART, COLUMN_SUNEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 2: //Monday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_MONSTART, COLUMN_MONEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 3: //Tuesday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_TUESTART, COLUMN_TUEEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 4: //Wednesday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_WEDSTART, COLUMN_WEDEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 5: //Thursday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_THUSTART, COLUMN_THUEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 6: //Friday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_FRISTART, COLUMN_FRIEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + case 7: //Saturday + cursor = writeDB.query(TABLE_AVAILABILITY, new String[] {COLUMN_SATSTART, COLUMN_SATEND}, + COLUMN_AVAILABILITYNAME + " = ?", new String[] {serviceProvider}, null, + null, null, null); + break; + } + + return cursor; + } + + /** + * Helper method to change booking status + * @param booking + * @param status status to update + * @return true if modification was successful + */ + private boolean modifyBookingStatus(Booking booking, Status status){ + if (booking == null) return false; + + GregorianCalendar current = new GregorianCalendar(); + current.setTimeInMillis(System.currentTimeMillis()); + GregorianCalendar bookDate = new GregorianCalendar(booking.getYear(), booking.getMonth()-1, + booking.getDay(), booking.getStarth(), booking.getStartmin()); + + //check if time of booking has passed + if (current.compareTo(bookDate) > 0) return false; + + ContentValues contentValues = new ContentValues(); + contentValues.put(COLUMN_BOOKINGSTATUS, status.toString()); + + return writeDB.update(TABLE_BOOKINGS, contentValues, + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " + + COLUMN_BOOKINGHOMEOWNER + " = ? AND " + + COLUMN_BOOKINGYEAR + " = ? AND " + + COLUMN_BOOKINGMONTH + " = ? AND " + + COLUMN_BOOKINGDATE + " = ? AND " + + COLUMN_BOOKINGSTART + " = ? AND " + + COLUMN_BOOKINGSTATUS + " != " + Status.CANCELLED.toString() + ")", + new String[] {booking.getServiceprovider().getUsername(), + booking.getHomeowner().getUsername(), + String.valueOf(booking.getYear()), + String.valueOf(booking.getMonth()), + String.valueOf(booking.getDay()), + String.valueOf(booking.getStarth()*60 + booking.getStartmin())}) > 0; + } + + /** + * Helper method to determine whether service provider is available at specified date and time. + * Returns false if end time before start time, or service provider not available + * + * @param serviceProvider + * @param year + * @param month + * @param day + * @param starth + * @param startmin + * @param endh + * @param endmin + * @return true if service provider is available for specified time slot + */ + private boolean isProviderAvailable(String serviceProvider, int year, int month, int day, + int starth, int startmin, int endh, int endmin){ + + int bookingStart = starth*60 + startmin; + int bookingEnd = endh*60 + endmin; + int availabilityStart, availabilityEnd; + + if (bookingEnd < bookingStart) return false; + + Cursor cursor = getAvailabilityByDayOfWeek(serviceProvider, year, month, day); + + cursor.moveToFirst(); + availabilityStart = cursor.getInt(0); + availabilityEnd = cursor.getInt(1); + + //service provider not available if availability end is 0, if availability starts after booking start, + // or if availability ends before booking end + 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}, + COLUMN_BOOKINGSERVICEPROVIDER + " = ? AND " + + COLUMN_BOOKINGYEAR + " = ? AND " + + COLUMN_BOOKINGMONTH + " = ? AND " + + COLUMN_BOOKINGDATE + " = ? AND " + + COLUMN_BOOKINGSTATUS + " != ?", + new String[] {serviceProvider, String.valueOf(year), + String.valueOf(month), String.valueOf(day), Status.CANCELLED.toString()}, + null, null, COLUMN_BOOKINGSTART, null); + if (cursor.moveToFirst()){ + for (int i = 0; i bookingStart)|| + (availabilityStart < bookingEnd && availabilityEnd > bookingEnd) || + (availabilityStart > bookingStart && availabilityEnd < bookingEnd)) return false; + + cursor.moveToNext(); + } + } + return true; } } diff --git a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java index aca0a49..0e27671 100644 --- a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java @@ -18,6 +18,15 @@ import static org.junit.Assert.*; @RunWith(RobolectricTestRunner.class) @Config(packageName = "com.uottawa.olympus.olympusservices") public class DBIntegrationTest { + + /** + * Enum for setting up before a test + */ + private enum TestAfter{ + USER, SERVICE, LINK, AVAILABILITY, BOOKING, RATING; + } + + private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application); //testing user login table @@ -121,7 +130,7 @@ public class DBIntegrationTest { dbHelper.deleteUser("jbO4aBF4dC"); - List providersList = dbHelper.getAllProvidersByService("hitman"); + List providersList = dbHelper.getAllProvidersByService("hitman"); assertEquals(0, providersList.size()); providersList = dbHelper.getAllProvidersByService("hitman"); assertEquals(0, providersList.size()); @@ -174,9 +183,10 @@ public class DBIntegrationTest { @Test public void testGetAllUsers(){ - dbHelper.addUser(new HomeOwner("jbO4aBF4dC", "soccer", "Miguel", "Garzon")); + setUp(TestAfter.USER); List allUsers = dbHelper.getAllUsers(); + assertEquals(allUsers.size(), 5); for (String[] user : allUsers){ /* for (String s : user){ @@ -260,15 +270,12 @@ public class DBIntegrationTest { @Test public void testGetAllServices(){ - dbHelper.addService(new Service("Exterminating flatworms", 20.00)); + setUp(TestAfter.SERVICE); List allServices = dbHelper.getAllServices(); + assertTrue(allServices.size() == 3); for (String[] service : allServices){ -/* for (String s : user){ - System.out.print(s + " "); - } - System.out.println();*/ Service dbService = dbHelper.findService(service[0]); assertEquals(dbService.getRate(), Double.parseDouble(service[1]), 0.001); } @@ -278,9 +285,8 @@ public class DBIntegrationTest { @Test public void testAddAndDeleteServiceProvidedByUser(){ - dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null, - "testaddress", "8888888888", "companydotcom", true)); - dbHelper.addService(new Service("Hitman", 12358)); + setUp(TestAfter.SERVICE); + boolean added = dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "hitman"); assertTrue(added); boolean deleted = dbHelper.deleteServiceProvidedByUser("jbO4aBF4dC", "Hitman"); @@ -291,19 +297,9 @@ public class DBIntegrationTest { @Test public void testGetAllServicesProvidedByUserAndDeleteService(){ - ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, - "testaddress", "8888888888", "companydotcom", true); - dbHelper.addUser(serviceProvider); + setUp(TestAfter.LINK); - Service service1 = new Service("Hitman", 12358); - Service service2 = new Service("Exterminating flatworms", 392.457); - dbHelper.addService(service1); - dbHelper.addService(service2); - - dbHelper.addServiceProvidedByUser(serviceProvider, service1); - dbHelper.addServiceProvidedByUser(serviceProvider, service2); - - List servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser(serviceProvider); + List servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC"); assertEquals(2, servicesProvidedByUser.size()); assertEquals("hitman", servicesProvidedByUser.get(0)[0]); assertEquals(12358, Double.parseDouble(servicesProvidedByUser.get(0)[1]), 0.00001); @@ -323,20 +319,13 @@ public class DBIntegrationTest { @Test public void testGetAllProvidersByService(){ - dbHelper.addService(new Service("Exterminating flatworms", 392.457)); - dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null, - "testaddress", "8888888888", "companydotcom", true)); - dbHelper.addUser(new ServiceProvider("7MuF1c59XP", null, null, null, - "testaddress", "8888888888", "companydotcom", true)); + setUp(TestAfter.LINK); - dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms"); - dbHelper.addServiceProvidedByUser("7MuF1c59XP", "exterminating flatworms"); - - List providersByService = dbHelper.getAllProvidersByService("exterminating flatworms"); + List providersByService = dbHelper.getAllProvidersByService("exterminating flatworms"); assertEquals(2, providersByService.size()); - assertEquals("jbO4aBF4dC", providersByService.get(0)); - assertEquals("7MuF1c59XP", providersByService.get(1)); + assertEquals("jbO4aBF4dC", providersByService.get(0)[0]); + assertEquals("DW44FkUsX7", providersByService.get(1)[0]); dbHelper.deleteAll(); } @@ -344,19 +333,9 @@ public class DBIntegrationTest { @Test public void testDeleteServiceProvidedByUser(){ - ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, - "testaddress", "8888888888", "companydotcom", true); - dbHelper.addUser(serviceProvider); + setUp(TestAfter.LINK); - Service service1 = new Service("Hitman", 12358); - Service service2 = new Service("Exterminating flatworms", 392.457); - dbHelper.addService(service1); - dbHelper.addService(service2); - - dbHelper.addServiceProvidedByUser(serviceProvider, service1); - dbHelper.addServiceProvidedByUser(serviceProvider, service2); - - List servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser(serviceProvider); + List servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC"); assertEquals(2, servicesProvidedByUser.size()); dbHelper.deleteServiceProvidedByUser("jbO4aBF4dC","hitman"); @@ -435,32 +414,90 @@ public class DBIntegrationTest { @Test public void testAddBooking(){ - ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, - "testaddress", "8888888888", "companydotcom", true); - serviceProvider.setAvailabilities(0, 4, 18, 19, 30); - serviceProvider.setAvailabilities(1, 5, 20, 21, 11); - serviceProvider.setAvailabilities(3, 7, 12, 15, 14); - serviceProvider.setAvailabilities(4, 0, 0, 23, 29); - - dbHelper.addUser(serviceProvider); - dbHelper.updateAvailability(serviceProvider); - - dbHelper.addUser(new HomeOwner("7MuF1c59XP", null, null, null)); - - dbHelper.addService(new Service("Hitman", 12358)); - dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "Hitman"); + setUp(TestAfter.BOOKING); + //December 1, 2020 is a Tuesday. Provider is available from 5:20 to 21:11 boolean added = dbHelper.addBooking("jbO4aBF4dC", "7MuF1c59XP", "Hitman", - 2018, 11, 20, 8, 12, 10, 0); + 2020, 12, 1, 8, 12, 10, 0); assertTrue(added); + //Provider is available from 5:20 to 21:11, but has a booking from 8:12 to 10:00 added = dbHelper.addBooking("jbO4aBF4dC", "7MuF1c59XP", "Hitman", - 2018, 11, 20, 9, 12, 12, 0); + 2020, 12, 1, 9, 12, 12, 0); assertTrue(!added); + + //December 3, 2020 is was a Thursday. Provider is available from 7:12 to 15:14 added = dbHelper.addBooking("jbO4aBF4dC", "7MuF1c59XP", "Hitman", - 2018, 11, 22, 6, 12, 7, 30); + 2020, 12, 3, 6, 12, 7, 30); + assertTrue(!added); + + //November 20, 2018 is in the past. Should not be able to add booking + added = dbHelper.addBooking("jbO4aBF4dC", "7MuF1c59XP", "Hitman", + 2018, 11, 20, 8, 12, 10, 0); assertTrue(!added); dbHelper.deleteAll(); } + +// @Test +// public void testConfirmBooking(){ +// setUp(TestAfter.RATING); +// +// } + + // Ever gotten tired of adding things at the start of a test just to delete it all again? + // I have. + // This is a work in progress + private void setUp(TestAfter testAfter){ + dbHelper.deleteAll(); + + dbHelper.addUser(new Admin()); + + ServiceProvider serviceProvider1 = new ServiceProvider("jbO4aBF4dC", null, "Jack", "Black", + "testaddress", "8888888888", "companydotcom", true); + dbHelper.addUser(serviceProvider1); + + ServiceProvider serviceProvider2 = new ServiceProvider("DW44FkUsX7", null, "Dwayne", "Johnson", + "testaddress", "1248921742", "companydotcom", false); + dbHelper.addUser(serviceProvider2); + + HomeOwner homeOwner1 = new HomeOwner("7MuF1c59XP", null, "Mufasa", "Died"); + dbHelper.addUser(homeOwner1); + + HomeOwner homeOwner2 = new HomeOwner("wRV3phzpl5", null, "Wren", "Phillips"); + dbHelper.addUser(homeOwner2); + + Service service1 = new Service("Hitman", 12358); + dbHelper.addService(service1); + + Service service2 = new Service("Exterminating flatworms", 392.457); + dbHelper.addService(service2); + + Service service3 = new Service("Petting cats", 0); + dbHelper.addService(service3); + + if (!(testAfter.equals(TestAfter.USER) || testAfter.equals(TestAfter.SERVICE))){ + dbHelper.addServiceProvidedByUser(serviceProvider1, service1); + dbHelper.addServiceProvidedByUser(serviceProvider1, service2); + + dbHelper.addServiceProvidedByUser(serviceProvider2, service3); + dbHelper.addServiceProvidedByUser(serviceProvider2, service2); + + if (!testAfter.equals(TestAfter.LINK)){ + serviceProvider1.setAvailabilities(0, 4, 18, 19, 30); + serviceProvider1.setAvailabilities(1, 5, 20, 21, 11); + serviceProvider1.setAvailabilities(3, 7, 12, 15, 14); + serviceProvider1.setAvailabilities(4, 0, 0, 23, 29); + + dbHelper.updateAvailability(serviceProvider1); + + if (!testAfter.equals(TestAfter.AVAILABILITY)){ + Booking booking1 = new Booking(8, 12, 10, 0,1, + 12, 2020, serviceProvider1, homeOwner1, service1); + dbHelper.addBooking(booking1); + } + } + } + } + } From 8d5f01325cd73e3b45b077870ecc4f6d1487e82c Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Sat, 24 Nov 2018 15:17:40 -0500 Subject: [PATCH 6/6] Added a small clarification thing --- .../olympus/olympusservices/DBHelper.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java index 2423793..8448b52 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -884,10 +884,26 @@ public class DBHelper extends SQLiteOpenHelper { } + /** + * Returns a list of unbooked time slots that are at least 30 minutes + * @param serviceProvider + * @param year + * @param month + * @param day + * @return list of [start hour, start minute, end hour, end minute] of available timeslots + */ public List getAvailabilityByDate(ServiceProvider serviceProvider, int year, int month, int day){ return getAvailabilityByDate(serviceProvider.getUsername(), year, month, day); } + /** + * Returns a list of unbooked time slots that are at least 30 minutes + * @param serviceProvider + * @param year + * @param month + * @param day + * @return list of [start hour, start minute, end hour, end minute] of available timeslots + */ public List getAvailabilityByDate(String serviceProvider, int year, int month, int day){ List availabilities = new ArrayList<>();