Skip to content
Snippets Groups Projects
AccountHandler.java 5.91 KiB
Newer Older
import com.application.GUI.Panes.CreateLogoBar;
Eilert Tunheim's avatar
Eilert Tunheim committed
import com.application.GUI.PopUpWindows.NotificationPopUp;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.TableResult;

import static com.application.DB.Settings.*;
import static com.application.GUI.PopUpWindows.LoginPopup.getPasswordTextField;
/**
 * This class handles the login system and all related functionality
 *
 * @author Eilert Tunheim, Karin Pettersen, Mads Arnesen
 * @version 1.0.0
 */
public class AccountHandler {
    /**
     * This function iterates through the results and sets the information
     *
     * @param username input parameter for the username
     * @param password input parameter for the password
     * @throws Exception throws exception if anything goes wrong
     */
    public static void getAccountInformation(String username, String password) throws Exception {
        TableResult result = logIn(username,password);
Eilert Tunheim's avatar
Eilert Tunheim committed
        if(result.getTotalRows() != 0) {
            for (FieldValueList row : result.iterateAll()) {
Eilert Tunheim's avatar
Eilert Tunheim committed
                if (row.get("Username").getValue().equals(username)) {
                    CreateLogoBar.getLogin().setText(username);
                    HelpingFunctions.setUserName(username);
                        HelpingFunctions.setPhoneNo(row.get("Phone_no").getStringValue());
                        HelpingFunctions.setFirstName(row.get("First_name").getStringValue());
                        HelpingFunctions.setLastName(row.get("Last_name").getStringValue());
                        HelpingFunctions.setIsAdmin(row.get("Admin").getBooleanValue());
Eilert Tunheim's avatar
Eilert Tunheim committed
        } else {
            NotificationPopUp.displayNotificationWindow("Wrong username or password!");
            getPasswordTextField().clear();
    /**
     * This function creates the login sql statement and returns a TableResult to be iterated through
     *
     * @param username input parameter for the username
     * @param password input parameter for the password
     * @return a TableResult that can be iterated through
     * @throws Exception throws exception if anything goes wrong
     */
    public static TableResult logIn(String username, String password) throws Exception {

        // Sqlstatement
        final String sqlStatement = "SELECT Username, Admin, Phone_no, First_name, Last_name " +
                "FROM " + PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME + " " +
                "WHERE Username = " + '"' + username + '"' + " " +
                "AND Password = " + '"' + password+ '"';

        System.out.println(sqlStatement);

        // Retrieves the results from the queryjob
        return HelpingFunctions.createQueryJob(sqlStatement);
    }

    /**
     * Retrieves information regarding an account based on the username input
     *
     * @param username input parameter for the username
     * @return a TableResult that can be iterated through
     * @throws Exception throws exception if anything goes wrong
     */
    public static TableResult getAccount(String username) throws Exception {

        // Sqlstatement
        final String sqlStatement = "SELECT Username " +
                "FROM " + PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME + " " +
                "WHERE Username = " + '"' + username + '"';

        System.out.println(sqlStatement);

        // Retrieves the results from the queryjob
        return HelpingFunctions.createQueryJob(sqlStatement);
    }

    /**
     * This function takes a number of parameters as input and creates a user in the database
     *
     * @param firstName input parameter for firstname
     * @param lastName input parameter for lastName
     * @param phoneNo input parameter for phoneNo
     * @param username input parameter for username
     * @param password input parameter for password
     * @param isAdmin input parameter from the checkbox is the account is admin or not
     * @return a boolean if the user was added or not
     * @throws Exception throws exception if anything goes wrong
     */
    public static boolean addUser(String firstName, String lastName, String phoneNo, String username, String password, boolean isAdmin) throws Exception {

        if(getAccount(username).getTotalRows() == 0) {
            // Sqlstatement
            final String sqlStatement =
                    "INSERT INTO " + PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME + "(First_name, Last_name, Phone_no, Username, Password, Admin) " +
                            "VALUES(" + '"' + firstName + '"' + "," + '"' + lastName + '"' + "," + '"' + phoneNo + '"' + "," + '"' + username + '"' + "," + '"' + password + '"' + "," + isAdmin + ") ";
            System.out.println(sqlStatement);
            HelpingFunctions.createQueryJob(sqlStatement);
            return true;
        } else {
            return false;
        }
    /**
     * This function deletes an account based on the username input parameter
     *
     * @param username input parameter for username
     * @return a boolean if the user was deleted or not
     * @throws Exception throws exception if anything goes wrong
     */
    public static boolean deleteUser(String username) throws Exception {

        if(getAccount(username).getTotalRows() != 0){
            // Sqlstatement
            final String sqlStatement = "DELETE FROM "+ PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME +" WHERE Username = "+'"'+username+'"';

            System.out.println(sqlStatement);

            HelpingFunctions.createQueryJob(sqlStatement);

            return true;
        } else {
            return false;
        }
    }