From da469fe2662eb65a398642df4999ad809bc415c1 Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Tue, 20 Nov 2018 13:03:26 -0500 Subject: [PATCH] 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; } /**