Schedule-Creator

Java GUI to create schedules for UNCG students
git clone git://git.wrycode.com/wrycode/archive/Schedule-Creator.git
Log | Files | Refs | README

commit 23ba5d71f2de5c0340f1664c964b6f60406da153
parent 9ee4fdac952d5f53c4ab5de0d5475d48f02261b8
Author: Ilyass Sfar <42755577+cloud0x9@users.noreply.github.com>
Date:   Sun, 12 Apr 2020 16:02:52 -0400

Made email button fully functional (#60)

* add email validation

* add UI email validation dialogue

* add a failsafe dialogue connected to the email UI button

* Make the api ui button fully functional

* Style guide
Diffstat:
MScheduleCreator/API/EmailAPI.java | 74+++++++++++++++++++++++++++++++++++++++++++++++---------------------------
MScheduleCreator/API/EmailAdapter.java | 10+++++++++-
MScheduleCreator/controllers/CoursesController.java | 42++++++++++++++++++++++++++++--------------
AScheduleCreator/controllers/EmailPopupController.java | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DScheduleCreator/controllers/PopupController.java | 36------------------------------------
MScheduleCreator/resources/views/email_popup.fxml | 6+++---
MScheduleCreator/resources/views/primary.fxml | 9++-------
7 files changed, 150 insertions(+), 88 deletions(-)

diff --git a/ScheduleCreator/API/EmailAPI.java b/ScheduleCreator/API/EmailAPI.java @@ -5,7 +5,7 @@ package ScheduleCreator.API; * * @author Ilyass Sfar * - * Last Updated: 3/29/2020 + * Last Updated: 4/12/2020 */ import com.mailjet.client.errors.MailjetException; import com.mailjet.client.errors.MailjetSocketTimeoutException; @@ -14,6 +14,8 @@ import com.mailjet.client.MailjetRequest; import com.mailjet.client.MailjetResponse; import com.mailjet.client.ClientOptions; import com.mailjet.client.resource.Emailv31; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.json.JSONArray; import org.json.JSONObject; @@ -23,6 +25,7 @@ public class EmailAPI implements APIInterface { private static final String apiPrivateKey = "2ebe9765bf49eeac4c25fa0436edbcbf"; private static final String clientVersion = "v3.1"; private static final String sendEmail = "ScheduleCreatorUNCG@gmail.com"; + public static final Pattern validEmail = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE); /** * Send email's using an API. @@ -35,32 +38,49 @@ public class EmailAPI implements APIInterface { */ @Override public void sendEmail(String _email, String _message) throws MailjetException, MailjetSocketTimeoutException { - //Initalize the API requirements - MailjetClient client; - MailjetRequest request; - MailjetResponse response; - //Create a new client to send emails, using public and private api keys, and speify which version of the client is being used - client = new MailjetClient((apiPublicKey), (apiPrivateKey), new ClientOptions(clientVersion)); - request = new MailjetRequest(Emailv31.resource) - .property(Emailv31.MESSAGES, new JSONArray() - .put(new JSONObject() - //The email that is being used to send, this is controled from the mailjet webiste - .put(Emailv31.Message.FROM, new JSONObject() - .put("Email", sendEmail) - .put("Name", "Schedule Creator")) - //The email that is being send to - .put(Emailv31.Message.TO, new JSONArray() - .put(new JSONObject() - .put("Email", _email) - .put("Name", "Student"))) - //Subject line of email - .put(Emailv31.Message.SUBJECT, "Schedule Creator") - //Body of email - .put(Emailv31.Message.TEXTPART, _message))); - response = client.post(request); + //Check if the email given by user is valid, if so then the api call is made. + if (validate(_email)) { + //Initalize the API requirements + MailjetClient client; + MailjetRequest request; + MailjetResponse response; + //Create a new client to send emails, using public and private api keys, and speify which version of the client is being used + client = new MailjetClient((apiPublicKey), (apiPrivateKey), new ClientOptions(clientVersion)); + request = new MailjetRequest(Emailv31.resource) + .property(Emailv31.MESSAGES, new JSONArray() + .put(new JSONObject() + //The email that is being used to send, this is controled from the mailjet webiste + .put(Emailv31.Message.FROM, new JSONObject() + .put("Email", sendEmail) + .put("Name", "Schedule Creator")) + //The email that is being send to + .put(Emailv31.Message.TO, new JSONArray() + .put(new JSONObject() + .put("Email", _email) + .put("Name", "Student"))) + //Subject line of email + .put(Emailv31.Message.SUBJECT, "Schedule Creator") + //Body of email + .put(Emailv31.Message.TEXTPART, _message))); + response = client.post(request); - //Print out what email was just sent, it was sent or not, and any errors from the API - System.out.println(response.getStatus()); - System.out.println(response.getData()); + //Print out what email was just sent, it was sent or not, and any errors from the API + System.out.println(response.getStatus()); + System.out.println(response.getData()); + } else { + //if the email is INVALID then the api call is not made. + System.out.println("\"" + _email + "\"" + " is NOT a valid email....." + "\n" + "Email was not sent, the email given is invalid."); + + } + } + //Suporting method to "sendEmail", to validate emails before a api call is made. + + public static boolean validate(String _email) { + Matcher matcher = validEmail.matcher(_email); + if (matcher.find()) { + return true; + } else { + return false; + } } } diff --git a/ScheduleCreator/API/EmailAdapter.java b/ScheduleCreator/API/EmailAdapter.java @@ -5,7 +5,7 @@ package ScheduleCreator.API; * * @author Ilyass Sfar * - * Last Updated: 3/29/2020 + * Last Updated: 4/12/2020 */ import com.mailjet.client.errors.MailjetException; import com.mailjet.client.errors.MailjetSocketTimeoutException; @@ -28,4 +28,12 @@ public class EmailAdapter implements APIInterface { EmailAdapter.thisApi.sendEmail(_email, _message); } + public static boolean validate(String _email) { + if (EmailAPI.validate(_email)) { + return true; + } else { + return false; + } + } + } diff --git a/ScheduleCreator/controllers/CoursesController.java b/ScheduleCreator/controllers/CoursesController.java @@ -5,7 +5,7 @@ package ScheduleCreator.controllers; * * @author Jamison Valentine, Ilyass Sfar, Nick Econopouly, Nathan Tolodzieki * - * Last Updated: 4/6/2020 + * Last Updated: 4/12/2020 */ import ScheduleCreator.Adapter; import java.io.IOException; @@ -35,7 +35,9 @@ import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.geometry.VPos; import javafx.scene.Scene; +import javafx.scene.control.Alert; import javafx.scene.control.Button; +import javafx.scene.control.ButtonType; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; @@ -82,14 +84,14 @@ public class CoursesController implements Initializable { // List of courses for current semester. FilteredList<String> courseList; - protected Semester currentSemester; + protected static Semester currentSemester; protected Course focusedCourse; protected Course currentCourse; protected Adapter adapter = new Adapter(); protected int NUM_ROWS; protected int NUM_COLS; - protected int currentScheduleIndex; + protected static int currentScheduleIndex; BorderPane[][] grid; List<BorderPane> entries = new ArrayList(); @@ -627,17 +629,29 @@ public class CoursesController implements Initializable { //Calls popup fxml for the email api public void popupAction(ActionEvent event) { - try { - FXMLLoader fxmlLoader = new FXMLLoader(); - fxmlLoader.setLocation(getClass().getResource("/ScheduleCreator/resources/views/email_popup.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 450, 150); - Stage stage = new Stage(); - stage.setTitle("Email Course Information"); - stage.setScene(scene); - stage.show(); - } catch (IOException e) { - Logger logger = Logger.getLogger(getClass().getName()); - logger.log(Level.SEVERE, "Failed to create new Window.", e); + //if no courses are selected, and the email button is pressed then thier is nothing to email, an error box is thrown + if (this.currentSemester == null || this.currentSemester.getSelectedCourses().size() == 0) { + Alert alert = new Alert(Alert.AlertType.WARNING, "", ButtonType.OK); + alert.setTitle("Warning"); + alert.setHeaderText("You have not selected any courses yet"); + alert.setContentText("Select a semseter and courses and try again!"); + alert.showAndWait(); + System.out.println("No semster or courses choosen."); + + //if the user does have a course selected and the email button is pressed, it shows as normal + } else { + try { + FXMLLoader fxmlLoader = new FXMLLoader(); + fxmlLoader.setLocation(getClass().getResource("/ScheduleCreator/resources/views/email_popup.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 450, 150); + Stage stage = new Stage(); + stage.setTitle("Email Course Information"); + stage.setScene(scene); + stage.show(); + } catch (IOException e) { + Logger logger = Logger.getLogger(getClass().getName()); + logger.log(Level.SEVERE, "Failed to create new Window.", e); + } } } diff --git a/ScheduleCreator/controllers/EmailPopupController.java b/ScheduleCreator/controllers/EmailPopupController.java @@ -0,0 +1,61 @@ +package ScheduleCreator.controllers; + +/** + * Controller for email api popup + * + * @author Ilyass Sfar + * + * Last Updated: 4/12/2020 + */ +import ScheduleCreator.API.EmailAdapter; +import ScheduleCreator.models.Section; +import com.mailjet.client.errors.MailjetException; +import com.mailjet.client.errors.MailjetSocketTimeoutException; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.ButtonType; +import javafx.scene.control.TextField; +import javafx.stage.Stage; + +public class EmailPopupController { + + @FXML + private TextField emailTF; + + @FXML + private Button sendBtn; + + //Calls email api + @FXML + public void sendEmail(ActionEvent event) throws MailjetException, MailjetSocketTimeoutException { + if (EmailAdapter.validate(emailTF.getText())) { + EmailAdapter testAPI = new EmailAdapter(); + testAPI.sendEmail(emailTF.getText(), EmailPopupController.getCRNS()); + Stage stage = (Stage) sendBtn.getScene().getWindow(); + stage.close(); + } else { + Alert alert = new Alert(Alert.AlertType.ERROR, "", ButtonType.OK); + alert.setTitle("Invalid Email"); + alert.setHeaderText("Invalid Email"); + alert.setContentText("The given email " + "\"" + emailTF.getText() + "\"" + " is NOT valid."); + alert.showAndWait(); + } + } + + public static String getCRNS() { + //Check if the user has choosen any courses when pressing the email button, if not a error popup is shown notifying the user + if (CoursesController.currentSemester == null || CoursesController.currentSemester.getSelectedCourses().size() == 0) { + Alert alert = new Alert(Alert.AlertType.ERROR, "You have not selected a semseter or any courses", ButtonType.OK); + alert.showAndWait(); + } + //If the user has selected some course then the crns for the coruses are rietrived. + StringBuilder content = new StringBuilder(); + for (Section section : CoursesController.currentSemester.getSchedules().get(CoursesController.currentScheduleIndex).getAddedSections()) { + content.append(section.getID() + "-" + section.getSectionNumber() + " | " + section.getCRN() + "\n"); + } + return content.toString(); + } + +} diff --git a/ScheduleCreator/controllers/PopupController.java b/ScheduleCreator/controllers/PopupController.java @@ -1,36 +0,0 @@ -package ScheduleCreator.controllers; - -/** - * Controller for email api popup - * - * @author Ilyass Sfar - * - * Last Updated: 4/6/2020 - */ -import ScheduleCreator.API.EmailAdapter; -import com.mailjet.client.errors.MailjetException; -import com.mailjet.client.errors.MailjetSocketTimeoutException; -import javafx.event.ActionEvent; -import javafx.fxml.FXML; -import javafx.scene.control.Button; -import javafx.scene.control.TextField; -import javafx.stage.Stage; - -public class PopupController { - - @FXML - private TextField emailTF; - - @FXML - private Button sendBtn; - - //Calls email api - @FXML - public void sendEmail(ActionEvent event) throws MailjetException, MailjetSocketTimeoutException { - EmailAdapter testAPI = new EmailAdapter(); - testAPI.sendEmail(emailTF.getText(), "This is a temp UI API test call"); - Stage stage = (Stage) sendBtn.getScene().getWindow(); - stage.close(); - } - -} diff --git a/ScheduleCreator/resources/views/email_popup.fxml b/ScheduleCreator/resources/views/email_popup.fxml @@ -8,11 +8,11 @@ <?import javafx.scene.layout.VBox?> <?import javafx.scene.text.Font?> -<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ScheduleCreator.controllers.PopupController"> +<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ScheduleCreator.controllers.EmailPopupController"> <children> - <HBox alignment="CENTER" prefHeight="18.0" prefWidth="95.0" style="-fx-background-color: #66ccff;"> + <HBox alignment="CENTER" prefHeight="18.0" prefWidth="95.0" style="-fx-background-color: #C0C0C0;"> <children> - <Label text="Enter Your Email"> + <Label text="Enter Your Email" textFill="#616161"> <font> <Font size="14.0" /> </font> diff --git a/ScheduleCreator/resources/views/primary.fxml b/ScheduleCreator/resources/views/primary.fxml @@ -1,10 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<?import java.lang.*?> -<?import javafx.scene.control.*?> -<?import javafx.scene.image.*?> -<?import javafx.scene.layout.*?> -<?import javafx.scene.text.*?> <?import javafx.scene.control.Button?> <?import javafx.scene.image.Image?> <?import javafx.scene.image.ImageView?> @@ -12,7 +7,7 @@ <?import javafx.scene.text.Font?> <?import javafx.scene.text.Text?> -<AnchorPane id="AnchorPane" prefHeight="560" prefWidth="1090" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ScheduleCreator.controllers.PrimaryController"> +<AnchorPane id="AnchorPane" prefHeight="560" prefWidth="1090" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ScheduleCreator.controllers.PrimaryController"> <children> <Button layoutX="366.0" layoutY="256.0" onAction="#changeToSelectClasses" prefHeight="39.0" prefWidth="286.0" text="Schedule Generator"> <font> @@ -27,7 +22,7 @@ <Font name="System Italic" size="37.0" /> </font> </Text> - <ImageView fitHeight="181.0" fitWidth="113.0" layoutX="696.0" layoutY="252.0" pickOnBounds="true" preserveRatio="true"> + <ImageView fitHeight="114.0" fitWidth="66.0" layoutX="696.0" layoutY="252.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../uncg_emblem_3-color-174x300.png" /> </image>