Skip to content

Commit

Permalink
Added function that runs periodically to send emails
Browse files Browse the repository at this point in the history
  • Loading branch information
BornIncompetence committed Dec 11, 2018
1 parent 9f6c78d commit 94f68b3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import javafx.scene.layout.GridPane
import javafx.scene.text.Font
import javafx.stage.Stage
import java.sql.Connection
import java.util.*

object GUIFont {
// val bold = Font.loadFont("file:resources/fonts/SF-Pro-Text-Bold.otf", 20.0)!!
Expand Down Expand Up @@ -40,6 +41,8 @@ lateinit var connection: Connection
// This is the account that is logged into the account
lateinit var account: Account

val timer = Timer()

// This is the gridPane that every window uses
fun grid(): GridPane {
val gridPane = GridPane()
Expand All @@ -63,6 +66,11 @@ class GUI : Application() {
window.title = "Vision"
window.show()
}

override fun stop() {
timer.cancel()
timer.purge()
}
}

fun main(args: Array<String>) {
Expand Down
6 changes: 4 additions & 2 deletions src/Squeal.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.time.LocalDateTime

// We will be using the user's locally installed database for the purposes
// of this assignment. The user should already have a MYSQL database
// created with the name "scheduler"
Expand Down Expand Up @@ -100,10 +102,10 @@ fun removeAppointment(appID: Int): String {
//Sends email to address
//Returns true for email sent
//Returns false for email not sent
fun sendEmail(emailAddress: String, appointmentName: String, startDate: String, username: String): Boolean{
fun sendEmail(emailAddress: String, appointmentName: String, startDate: LocalDateTime, username: String): Boolean{

val emailSent = emailAddress == ""//Set to true if email is sent
val emailMSG = " Hello, $username \nYou have a/an $appointmentName @ $startDate "//Message String
val emailMSG = " Hello, $username \nYou have a/an $appointmentName @ ${startDate.toLocalDate()} ${startDate.toLocalTime()} "//Message String
println(emailMSG)

//TODO actually send message emailMSG and update emailSent
Expand Down
39 changes: 31 additions & 8 deletions src/Welcome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import javafx.scene.text.Text
import javafx.stage.FileChooser
import javafx.stage.Modality
import javafx.stage.Stage
import java.time.Duration
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.concurrent.schedule

// Main menu
object Welcome {
Expand All @@ -15,7 +20,7 @@ object Welcome {

// Specifies different types of stages to appear on top of main window
private enum class StageType {
CREATE_ACCOUNT, CHANGE_USERNAME, CHANGE_PASSWORD, MODIFY_ACCOUNT, MAKE_APPOINTMENT, CANCEL_APPOINTMENT, CHANGE_APPOINTMENT, SET_COLOR, IMPORT_FILE
CREATE_ACCOUNT, CHANGE_USERNAME, CHANGE_PASSWORD, MODIFY_ACCOUNT, MAKE_APPOINTMENT, CHANGE_APPOINTMENT, SET_COLOR, IMPORT_FILE
}

//Class to hold type of Calendar
Expand Down Expand Up @@ -73,19 +78,19 @@ object Welcome {
menuAccount.items.addAll(createAccount, changeUsername, changePassword, modifyAccount)

//Open modifyAccount window on menu click
val createAppt = MenuItem("Create Appointment")
createAppt.setOnAction {
val createApt = MenuItem("Create Appointment")
createApt.setOnAction {
changeStage(StageType.MAKE_APPOINTMENT)
stage.showAndWait()
}
menuAppointment.items.addAll(createAppt)
menuAppointment.items.addAll(createApt)

val changeAppt = MenuItem("Change / Cancel Appointment")
changeAppt.setOnAction {
val changeApt = MenuItem("Change / Cancel Appointment")
changeApt.setOnAction {
changeStage(StageType.CHANGE_APPOINTMENT)
stage.showAndWait()
}
menuAppointment.items.addAll(changeAppt)
menuAppointment.items.addAll(changeApt)

//Create the calendar type menu to the view
val setCalendarType = Menu("Set calendar type")
Expand Down Expand Up @@ -148,6 +153,25 @@ object Welcome {
return Scene(vBox, 300.0, 200.0)
}

init {
timer.schedule(1000, 60000) {
val emailStatement = connection.createStatement()
val emailResult = emailStatement.executeQuery(getAppointments(account.username))
var remindersSet = false
while (emailResult.next()) {
val name = emailResult.getString("title")
val start = emailResult.getString("start_date")
val reminder = emailResult.getInt("reminder")
val startDate = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val now = LocalDateTime.now()
val minutes = Duration.between(startDate, now).toMinutes()
if (minutes < reminder && startDate > now) {
remindersSet = sendEmail(account.email, name, startDate, account.username)
}
}
}
}

//This function takes a stage type and executes its corresponding window
private fun changeStage(stageType: StageType) {
stage.scene = when (stageType) {
Expand All @@ -157,7 +181,6 @@ object Welcome {
Welcome.StageType.MODIFY_ACCOUNT -> ModifyData.scene
Welcome.StageType.MAKE_APPOINTMENT -> MakeAppointment.scene
Welcome.StageType.IMPORT_FILE -> FileExplorer.scene
Welcome.StageType.CANCEL_APPOINTMENT -> TODO()
Welcome.StageType.CHANGE_APPOINTMENT -> ChangeCancelAppointment.scene
Welcome.StageType.SET_COLOR -> TODO() // NOT NEEDED YET, Probably doesn't need to have its own stage
}
Expand Down
1 change: 1 addition & 0 deletions src/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CREATE TABLE Appointments (
title VARCHAR(40) NULL,
start_date DATETIME NOT NULL,
end_date DATETIME NOT NULL,
reminder INT NULL,
CONSTRAINT appointment_pk PRIMARY KEY (appointment_id)
);

Expand Down

0 comments on commit 94f68b3

Please sign in to comment.