Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Date;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- public class Context {
- /*
- * This Singleton object keeps track of all the data for the application
- * Could be better but no time to optimize and shit
- */
- private final static Context instance = new Context();
- private static ObservableList<Patient> patientData = FXCollections.observableArrayList();
- private static ObservableList<Measurement> measurementData = FXCollections.observableArrayList();
- private static ObservableList<Report> reportData = FXCollections.observableArrayList();
- private static ObservableList<Garment> garmentData = FXCollections.observableArrayList();
- public static Context getInstance(){
- return instance;
- }
- public static ObservableList<Patient> getPatientData(){
- return patientData;
- }
- public static ObservableList<Report> getReportData(){
- return reportData;
- }
- public static ObservableList<Measurement> getMeasurementData(){
- return measurementData;
- }
- public static ObservableList<Garment> getGarmentData(){
- return garmentData;
- }
- public static void setPatientData(ObservableList<Patient> incoming){
- patientData = incoming;
- }
- public static void setMeasurementData(ObservableList<Measurement> incoming){
- measurementData = incoming;
- }
- public static void setReportData(ObservableList<Report> incoming){
- reportData = incoming;
- }
- public static void setGarmentData(ObservableList<Garment> incoming){
- garmentData = incoming;
- }
- /*
- * Helper method to add new patients to the database and update the application
- */
- public static void addPatients(String firstName, String lastName, String dob, String gender, String number, String email){
- PreparedStatement preparedStatement = null;
- Connection connection = null;
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- preparedStatement = connection.prepareStatement(
- "INSERT INTO Patient (registered, first_name, last_name, dob, gender, number, email) "
- + "VALUES (now(),?,?,?,?,?,?)");
- preparedStatement.setString(1, firstName);
- preparedStatement.setString(2, lastName);
- preparedStatement.setString(3, dob);
- preparedStatement.setString(4, gender);
- preparedStatement.setString(5, number);
- preparedStatement.setString(6, email);
- preparedStatement.executeUpdate();
- patientData.clear();
- loadPatients();
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- } finally {
- try {
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(connection != null){
- connection.close();
- }
- } catch (SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- }
- public static void addMeasurement(String type, String side, String bioimpedance, String m1, String m2, String m3,
- String m4, String m5, int patient_id){
- PreparedStatement preparedStatement = null;
- Connection connection = null;
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- preparedStatement = connection.prepareStatement(
- "INSERT INTO Measurement (date, type, side, bioimpedance, first, second, third, fourth, fifth, patient_id) "
- + "VALUES (now(),?,?,?,?,?,?,?,?,?)");
- preparedStatement.setString(1, type);
- preparedStatement.setString(2, side);
- preparedStatement.setString(3, bioimpedance);
- preparedStatement.setString(4, m1);
- preparedStatement.setString(5, m2);
- preparedStatement.setString(6, m3);
- preparedStatement.setString(7, m4);
- preparedStatement.setString(8, m5);
- preparedStatement.setInt(9, patient_id);
- preparedStatement.executeUpdate();
- measurementData.clear();
- loadMeasurements();
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- } finally {
- try {
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(connection != null){
- connection.close();
- }
- } catch (SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- }
- public static void addGarment(String side, String a, String b, String c, String d,
- String e, String f, String g, int patient_id){
- PreparedStatement preparedStatement = null;
- Connection connection = null;
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- preparedStatement = connection.prepareStatement(
- "INSERT INTO Garment (date, side, a, b, c, d, e, f, g, patient_id) "
- + "VALUES (now(),?,?,?,?,?,?,?,?,?)");
- preparedStatement.setString(1, side);
- preparedStatement.setString(2, a);
- preparedStatement.setString(3, b);
- preparedStatement.setString(4, c);
- preparedStatement.setString(5, d);
- preparedStatement.setString(6, e);
- preparedStatement.setString(7, f);
- preparedStatement.setString(8, g);
- preparedStatement.setInt(9, patient_id);
- preparedStatement.executeUpdate();
- garmentData.clear();
- loadGarments();
- } catch (SQLException x) {
- System.out.println("SQLException: " + x.getMessage());
- System.out.println("SQLState: " + x.getSQLState());
- } finally {
- try {
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(connection != null){
- connection.close();
- }
- } catch (SQLException x){
- System.out.println("SQLException: " + x.getMessage());
- System.out.println("SQLState: " + x.getSQLState());
- }
- }
- }
- /*
- * Helper method to commit updates of patient data to the database and update the application
- */
- public static void commitPatients(String toCommit, int patient_id, int position){
- PreparedStatement preparedStatement = null;
- Connection connection = null;
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- switch(position){
- case 3: // first name
- preparedStatement = connection.prepareStatement("UPDATE Patient SET first_name = ? WHERE id = ?");
- for(Patient p : patientData){
- if(p.getIDProperty().get() == patient_id){
- p.getFirstNameProperty().set(toCommit);
- }
- }
- break;
- case 4: // last name
- preparedStatement = connection.prepareStatement("UPDATE Patient SET first_name = ? WHERE id = ?");
- for(Patient p : patientData){
- if(p.getIDProperty().get() == patient_id){
- p.getLastNameProperty().set(toCommit);
- }
- }
- break;
- case 5: // dob
- preparedStatement = connection.prepareStatement("UPDATE Patient SET dob = ? WHERE id = ?");
- for(Patient p : patientData){
- if(p.getIDProperty().get() == patient_id){
- p.getDOBProperty().set(toCommit);
- }
- }
- break;
- case 6: // gender
- preparedStatement = connection.prepareStatement("UPDATE Patient SET gender = ? WHERE id = ?");
- for(Patient p : patientData){
- if(p.getIDProperty().get() == patient_id){
- p.getGenderProperty().set(toCommit);
- }
- }
- break;
- case 7: // number
- preparedStatement = connection.prepareStatement("UPDATE Patient SET number = ? WHERE id = ?");
- for(Patient p : patientData){
- if(p.getIDProperty().get() == patient_id){
- p.getNumberProperty().set(toCommit);
- }
- }
- break;
- case 8: // email
- preparedStatement = connection.prepareStatement("UPDATE Patient SET email = ? WHERE id = ?");
- for(Patient p : patientData){
- if(p.getIDProperty().get() == patient_id){
- p.getEmailProperty().set(toCommit);
- }
- }
- break;
- }
- preparedStatement.setString(1, toCommit);
- preparedStatement.setInt(2, patient_id);
- preparedStatement.executeUpdate();
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- } finally {
- try {
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(connection != null){
- connection.close();
- }
- } catch (SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- }
- public static void commitMeasurements(String toCommit, int measurement_id, int position){
- PreparedStatement preparedStatement = null;
- Connection connection = null;
- // probably a better way to do this.
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- switch(position){
- case 3: // type
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET type = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getTypeProperty().set(toCommit);
- }
- }
- preparedStatement.setString(1, toCommit);
- break;
- case 4: // side
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET side = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getSideProperty().set(toCommit);
- }
- }
- preparedStatement.setString(1, toCommit);
- break;
- case 5: // bioimpedance
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET bioimpedance = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getBioimpedanceProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 6: // first
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET first = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getFirstProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 7: // second
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET second = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getSecondProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 8: // third
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET third = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getThirdProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 9: // fourth
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET fourth = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getFourthProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 10: // fifth
- preparedStatement = connection.prepareStatement("UPDATE Measurement SET fifth = ? WHERE id = ?");
- for(Measurement m : measurementData){
- if(m.getIDProperty().get() == measurement_id){
- m.getFifthProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- }
- preparedStatement.setInt(2, measurement_id);
- preparedStatement.executeUpdate();
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- } finally {
- try {
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(connection != null){
- connection.close();
- }
- } catch (SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- }
- public static void commitGarments(String toCommit, int garment_id, int position){
- PreparedStatement preparedStatement = null;
- Connection connection = null;
- // probably a better way to do this.
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- switch(position){
- case 3: // side
- preparedStatement = connection.prepareStatement("UPDATE Garment SET side = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getSideProperty().set(toCommit);
- }
- }
- preparedStatement.setString(1, toCommit);
- break;
- case 4: // a
- preparedStatement = connection.prepareStatement("UPDATE Garment SET a = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getAProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 5: // b
- preparedStatement = connection.prepareStatement("UPDATE Garment SET b = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getBProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 6: // c
- preparedStatement = connection.prepareStatement("UPDATE Garment SET c = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getCProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 7: // d
- preparedStatement = connection.prepareStatement("UPDATE Garment SET d = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getDProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 8: // e
- preparedStatement = connection.prepareStatement("UPDATE Garment SET e = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getEProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 9: // f
- preparedStatement = connection.prepareStatement("UPDATE Garment SET f = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getFProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- case 10: // g
- preparedStatement = connection.prepareStatement("UPDATE Garment SET g = ? WHERE id = ?");
- for(Garment g : garmentData){
- if(g.getIDProperty().get() == garment_id){
- g.getGProperty().set(Double.parseDouble(toCommit));
- }
- }
- preparedStatement.setDouble(1, Double.parseDouble(toCommit));
- break;
- }
- preparedStatement.setInt(2, garment_id);
- preparedStatement.executeUpdate();
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- } finally {
- try {
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(connection != null){
- connection.close();
- }
- } catch (SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- }
- public static void commitReport(String title, String content, int report_id){
- PreparedStatement preparedStatement = null;
- Connection connection = null;
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- preparedStatement = connection.prepareStatement("UPDATE Report SET title = ?, content = ? WHERE id = ?");
- for(Report r : reportData){
- if(r.getIDProperty().get() == report_id){
- r.getTitleProperty().set(title);
- r.getContentProperty().set(content);
- }
- }
- preparedStatement.setString(1, title);
- preparedStatement.setString(2, content);
- preparedStatement.setInt(3, report_id);
- preparedStatement.executeUpdate();
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- } finally {
- try {
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(connection != null){
- connection.close();
- }
- } catch (SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- }
- public static void loadPatients(){
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- Connection connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Patient");
- ResultSet rs = preparedStatement.executeQuery();
- ObservableList<Patient> incomingData = FXCollections.observableArrayList();
- while (rs.next()) {
- int id = rs.getInt(1);
- Date date = rs.getDate(2);
- String firstName = rs.getString(3);
- String lastName = rs.getString(4);
- String dob = rs.getString(5);
- String gender = rs.getString(6);
- String number = rs.getString(7);
- String email = rs.getString(8);
- incomingData.add(new Patient(id, date, firstName, lastName, dob, gender, number, email));
- }
- Context.getInstance();
- Context.setPatientData(incomingData);
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- public static void loadMeasurements(){
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- Connection connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Measurement");
- ResultSet rs = preparedStatement.executeQuery();
- ObservableList<Measurement> incomingData = FXCollections.observableArrayList();
- while (rs.next()) {
- int id = rs.getInt(1);
- Date date = rs.getDate(2);
- String type = rs.getString(3);
- String side = rs.getString(4);
- double bioimpedance = rs.getDouble(5);
- double first = rs.getDouble(6);
- double second = rs.getDouble(7);
- double third = rs.getDouble(8);
- double fourth = rs.getDouble(9);
- double fifth = rs.getDouble(10);
- int patient_id = rs.getInt(11);
- incomingData.add(new Measurement(id, date, type, side, bioimpedance, first, second, third, fourth, fifth, patient_id));
- }
- Context.getInstance();
- Context.setMeasurementData(incomingData);
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- public static void loadReports(){
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- Connection connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Report");
- ResultSet rs = preparedStatement.executeQuery();
- ObservableList<Report> incomingData = FXCollections.observableArrayList();
- while (rs.next()) {
- int id = rs.getInt(1);
- Date date = rs.getDate(2);
- String title = rs.getString(3);
- String content = rs.getString(4);
- int patient_id = rs.getInt(5);
- incomingData.add(new Report(id, date, title, content, patient_id));
- }
- Context.getInstance();
- Context.setReportData(incomingData);
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- public static void loadGarments(){
- try {
- String connectionString = "jdbc:mysql://188.166.215.160:3306/isys3400?useSSL=false";
- Connection connection = DriverManager.getConnection(connectionString, "application", "@ISYS3400project");
- PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Garment");
- ResultSet rs = preparedStatement.executeQuery();
- ObservableList<Garment> incomingData = FXCollections.observableArrayList();
- while (rs.next()) {
- int id = rs.getInt(1);
- Date date = rs.getDate(2);
- String side = rs.getString(3);
- double a = rs.getDouble(4);
- double b = rs.getDouble(5);
- double c = rs.getDouble(6);
- double d = rs.getDouble(7);
- double e = rs.getDouble(8);
- double f = rs.getDouble(9);
- double g = rs.getDouble(10);
- int patient_id = rs.getInt(11);
- incomingData.add(new Garment(id, date, side, a, b, c, d, e, f, g, patient_id));
- }
- Context.getInstance();
- Context.setGarmentData(incomingData);
- } catch (SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement