com.cyberlogix.mist6020biometrics.RosterController.java Source code

Java tutorial

Introduction

Here is the source code for com.cyberlogix.mist6020biometrics.RosterController.java

Source

package com.cyberlogix.mist6020biometrics;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import com.fasterxml.jackson.databind.ObjectMapper;
import entities.Student;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.json.JSONArray;
import org.json.JSONException;
import util.ServerConnect;
import util.ServerResponse;
import util.UtilHelper;
import javafx.scene.control.Dialogs;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableRow;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.json.JSONObject;
import util.BioMetrics;

/**
 * FXML Controller class
 *
 * @author Mathew
 */
public class RosterController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    private TableView tblRoster;

    @FXML
    private TableColumn colRegNo;

    @FXML
    private TableColumn colName;

    @FXML
    private TableColumn colSignedIn;

    @FXML
    private ImageView rosterImage;

    @FXML
    private ProgressBar rosterPbar;

    @FXML
    private ProgressIndicator rosterPin;

    @FXML
    private Label alertBox;

    List<Student> students;

    BioMetrics bio;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        bio = new BioMetrics();
        // TODO
        seedTable();
        printsListener();
    }

    public void seedTable() {
        System.out.println("Seeding Table");
        students = new ArrayList<>();
        ObjectMapper jackson = new ObjectMapper();
        try {
            String url = ServerConnect.STUDENTS_URL + "/all";
            ServerConnect sc = new ServerConnect();
            ServerResponse msg = sc.processRequest("", url, ServerConnect.METHOD_GET);
            if (msg.getStatusCode() == 200 || msg.getStatusCode() == 201) {
                JSONArray payload = msg.getResponse().getJSONArray("payload");
                System.err.println(payload);
                if (payload.length() > 0) {
                    for (int i = 0; i < payload.length(); i++) {
                        Student student = jackson.readValue(payload.getJSONObject(i).toString(), Student.class);
                        students.add(student);
                    }
                    ObservableList<Student> data = FXCollections.observableArrayList();
                    data.addAll(students);

                    //add Data to table
                    colRegNo.setCellValueFactory(new PropertyValueFactory<Student, String>("regNo"));
                    colName.setCellValueFactory(new PropertyValueFactory<Student, String>("name"));
                    colSignedIn.setCellValueFactory(new PropertyValueFactory<Student, String>("signedIn"));
                    colSignedIn.setStyle("-fx-text-fill: green; -fx-font-weight: bold;");
                    tblRoster.setItems(data);
                }
            } else {
                //                Dialogs.showErrorDialog(new Stage(), msg.getResponse().getString("message"), "Error " + msg.getStatusCode(), "Error");
                alertBox.setText(msg.getResponse().getString("message"));
                alertBox.setStyle("-fx-text-fill: red; -fx-font-weight: bold;");
            }
        } catch (JSONException | IOException e) {
            UtilHelper.debugTrace(e);
        }
    }

    public void printsListener() {

        long delay = 5 * 1000;
        final long period = 5 * 1000;
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        String b64Print = bio.getImage(rosterImage, rosterPbar, rosterPin, new Stage(), period);
                        System.out.println("Print: " + b64Print);
                        try {
                            if (!b64Print.equalsIgnoreCase("error")) {
                                String url = ServerConnect.STUDENTS_URL + "/verify";
                                ServerConnect sc = new ServerConnect();
                                JSONObject req = new JSONObject();
                                req.put("print", b64Print);
                                ServerResponse msg = sc.processRequest(req.toString(), url,
                                        ServerConnect.METHOD_POST);
                                if (msg.getStatusCode() == 200 || msg.getStatusCode() == 201) {
                                    ObjectMapper jackson = new ObjectMapper();
                                    Student student = jackson.readValue(
                                            msg.getResponse().getJSONObject("payload").toString(), Student.class);
                                    //update table
                                    int pos = -1;
                                    ObservableList<Student> students = tblRoster.getItems();
                                    for (int i = 0; i < students.size(); i++) {
                                        if (students.get(i).getId() == student.getId()) {
                                            System.out.println("Student Matched...");
                                            student.setSignedIn("Present");
                                            students.remove(students.get(i));
                                            pos = i;
                                        }
                                    }
                                    if (pos >= 0) {
                                        students.add(pos, student);
                                    }
                                    System.out.println("Students Size: " + students.size());
                                    tblRoster.setItems(students);

                                    //                                    Dialogs.showConfirmDialog(new Stage(), msg.getResponse().getString("message") + " " + student.getName());
                                    alertBox.setText(
                                            msg.getResponse().getString("message") + " " + student.getName());
                                    alertBox.setStyle("-fx-text-fill: green; -fx-font-weight: bold;");
                                } else {
                                    //                                    Dialogs.showErrorDialog(new Stage(), msg.getResponse().getString("message"), "Error " + msg.getStatusCode(), "Error");
                                    alertBox.setText(msg.getResponse().getString("message"));
                                    alertBox.setStyle("-fx-text-fill: red; -fx-font-weight: bold;");
                                }
                            }

                        } catch (JSONException | IOException e) {
                            UtilHelper.debugTrace(e);
                        }
                    }
                });
            }
        }, delay, period);

    }

}