<repo_name>rockking1379/ItemTracker<file_sep>/src/org/itemtracker/common/objects/Loan.java
package org.itemtracker.common.objects;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Represents a loan
 * Created by james on 5/29/15.
 */
public class Loan
{
    private int loanId;
    private Loanable loanedItem;
    private Loanee loanee;
    private long checkOut;
    private long checkIn;
    private String returnNotes;

    public Loan(int loanId, Loanable loanedItem, Loanee loanee, long checkOut, long checkIn, String returnNotes)
    {
        this.loanId = loanId;
        this.loanedItem = loanedItem;
        this.loanee = loanee;
        this.checkOut = checkOut;
        this.checkIn = checkIn;
        this.returnNotes = returnNotes;
    }

    public int getLoanId()
    {
        return loanId;
    }

    public Loanable getLoanedItem()
    {
        return loanedItem;
    }

    public Loanee getLoanee()
    {
        return loanee;
    }

    public String getLoanDuration()
    {
        if (Long.valueOf(checkIn) != null)
        {
            Date duration = new Date(checkIn - checkOut);
            DateFormat format = new SimpleDateFormat("DD:HH:mm");
            String output = format.format(duration);
            String[] breakDown = output.split(":");

            return format.format(duration);
        }
        else
        {
            return "not checked in";
        }
    }

    public String getCheckOut()
    {
        Date d = new Date(checkOut);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(d);
    }

    public String getCheckIn()
    {
        Date d = new Date(checkIn);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(d);
    }

    public String getReturnNotes()
    {
        return returnNotes;
    }
}
<file_sep>/src/org/itemtracker/common/objects/Loanee.java
package org.itemtracker.common.objects;

/**
 * Represents a student who may loan out a loanable item
 * Created by james on 5/29/15.
 */
public class Loanee
{
    private int loaneeId;
    private String firstName;
    private String lastName;
    private String barcodeId;
    private String emailAddress;

    public Loanee(int loaneeId, String firstName, String lastName, String barcodeId, String emailAddress)
    {
        this.loaneeId = loaneeId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.barcodeId = barcodeId;
        this.emailAddress = emailAddress;
    }

    public int getLoaneeId()
    {
        return loaneeId;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public String getBarcodeId()
    {
        return barcodeId;
    }

    public String getEmailAddress()
    {
        return emailAddress;
    }

    @Override
    public String toString()
    {
        return firstName + " " + lastName;
    }
}
<file_sep>/src/org/itemtracker/common/utils/Logger.java
package org.itemtracker.common.utils;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Custom Logger Class for logging Errors and Exceptions <br>
 * Will put them into a database located in logs
 *
 * @author James <EMAIL>
 */
public class Logger
{
    /**
     * Create Statement for Error File <br>
     * Could make a single database file but this seems better to have one per
     * day
     */
    private final static String createError = "CREATE TABLE IF NOT EXISTS Error(error_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,error_time DATE NOT NULL, error_message VARCHAR(100) NOT NULL,error_stacktrace TEXT NOT NULL)";
    private final static String createEvent = "CREATE TABLE IF NOT EXISTS Event(event_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, event_time DATE NOT NULL, event_message VARCHAR(100) NOT NULL, event_user_name VARCHAR(100) NOT NULL)";

    /**
     * Logs data from Exception
     *
     * @param ex Exception caught
     * @return status of logging success
     */
    public static boolean logException(Exception ex)
    {
        boolean retVal;

        if (logExists())
        {
            Date d = new Date();
            String sDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
            String fileName = "./Logs/" + sDate + ".db";

            try
            {
                Connection con = DriverManager.getConnection("jdbc:sqlite:"
                        + fileName);
                java.sql.PreparedStatement stmnt = con
                        .prepareStatement("INSERT INTO Error(error_time, error_message, error_stacktrace) VALUES(?,?,?)");

                stmnt.setString(1, String.valueOf(d.getTime()));
                stmnt.setString(2, ex.getMessage());
                Writer stackTrace = new StringWriter();
                PrintWriter pWriter = new PrintWriter(stackTrace);
                ex.printStackTrace(pWriter);
                stmnt.setString(3, stackTrace.toString());

                retVal = stmnt.execute();
                pWriter.close();
                stackTrace.close();
                stmnt.close();
                con.close();
            }
            catch (SQLException e)
            {
                System.err.println("Logging Error");
                e.printStackTrace();
                retVal = false;
            }
            catch (IOException e)
            {
                System.err.println("Logging Error");
                e.printStackTrace();
                retVal = false;
            }
        }
        else
        {
            if (createLog())
            {
                return logException(ex);
            }
            else
            {
                retVal = false;
            }
        }

        return retVal;
    }

    /**
     * Logs System Event
     * Used for general purpose logging
     *
     * @param eventMessage Message of Event
     * @param userName User Name of User logging event. Can and often will be 'SYSTEM'
     * @return status of logging success
     */
    public static boolean logEvent(String eventMessage, String userName)
    {
        boolean retVal;

        if (logExists())
        {
            Date d = new Date();
            String sDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
            String fileName = "./Logs/" + sDate + ".db";

            try
            {
                Connection con = DriverManager.getConnection("jdbc:sqlite:"
                        + fileName);
                java.sql.PreparedStatement stmnt = con
                        .prepareStatement("INSERT INTO Event(event_time, event_message, event_user_name) VALUES(?,?,?)");

                stmnt.setString(1, String.valueOf(d.getTime()));
                stmnt.setString(2, eventMessage);
                stmnt.setString(3, userName);

                retVal = stmnt.execute();
                stmnt.close();
                con.close();
            }
            catch (SQLException e)
            {
                System.err.println("Logging Error");
                e.printStackTrace();
                retVal = false;
            }
        }
        else
        {
            if (createLog())
            {
                return logEvent(eventMessage, userName);
            }
            else
            {
                retVal = false;
            }
        }

        return retVal;
    }

    private static boolean logExists()
    {
        File dir = new File("./Logs");
        Date d = new Date();
        String sDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
        String fileName = "./Logs/" + sDate + ".db";
        File f = new File(fileName);

        return (dir.exists() && f.exists());
    }

    private static boolean createLog()
    {
        File dir = new File("./Logs");
        boolean retVal = true;

        if (!dir.exists())
        {
            retVal = dir.mkdir();
        }

        Date d = new Date();
        String sDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
        String fileName = "./Logs/" + sDate + ".db";
        File f = new File(fileName);

        try
        {
            Class.forName("org.sqlite.JDBC");
        }
        catch (ClassNotFoundException e)
        {
            System.err.println("Logging Error");
            e.printStackTrace();
            retVal = false;
        }

        if (!f.exists())
        {
            try
            {
                retVal = f.createNewFile();

                Connection con = DriverManager.getConnection("jdbc:sqlite:"
                        + fileName);
                Statement stmnt = con.createStatement();

                stmnt.execute(createError);
                stmnt.execute(createEvent);

                stmnt.close();
                con.close();
            }
            catch (IOException e)
            {
                System.err.println("Logging Error");
                e.printStackTrace();
                retVal = false;
            }
            catch (SQLException e)
            {
                System.err.println("Logging Error");
                e.printStackTrace();

                retVal = false;
            }
        }

        return retVal;
    }
}
<file_sep>/src/org/itemtracker/testgui/MainPageController.java
package org.itemtracker.testgui;

import com.panemu.tiwulfx.table.TextColumn;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import org.itemtracker.common.objects.Loan;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * controls MainPage fxml file
 * Created by james on 5/30/15.
 */
public class MainPageController implements Initializable
{
    @FXML
    private Button btnLoanOut;
    @FXML
    private Button btnReturnLoan;
    @FXML
    private Button btnLoanableHistory;
    @FXML
    private Button btnNewLoanee;
    @FXML
    private Button btnNewLoanable;
    @FXML
    private Button btnLoaneeHistory;
    @FXML
    private TableView<Loan> tblViewActiveLoans;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
        TextColumn<Loan> clmnLoanee = new TextColumn<>("loanee");
        TextColumn<Loan> clmnLoanable = new TextColumn<>("loanedItem");
        TextColumn<Loan> clmnCheckOut = new TextColumn<>("checkOut");

        clmnLoanable.setResizable(false);
        clmnLoanee.setResizable(false);
        clmnCheckOut.setResizable(false);

        clmnLoanable.setText("Loaned Item");
        clmnLoanee.setText("Loeaned To");
        clmnCheckOut.setText("Checked Out");

        clmnLoanable.setPrefWidth(150);
        clmnLoanee.setPrefWidth(150);
        clmnCheckOut.setPrefWidth(150);

        tblViewActiveLoans.getColumns().clear(); //just to be safe
        tblViewActiveLoans.getColumns().add(clmnLoanee);
        tblViewActiveLoans.getColumns().add(clmnLoanable);
        tblViewActiveLoans.getColumns().add(clmnCheckOut);

        List<Loan> loans = MainFrame.dbManager.getLoans();

        tblViewActiveLoans.getItems().addAll(loans);
    }
}
