Newer
Older
Eilert Tunheim
committed
package com.application.DB;
import com.application.GUI.Panes.CreateLogoBar;
import com.application.GUI.PopUpWindows.NotificationPopUp;
Eilert Tunheim
committed
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;
Eilert Tunheim
committed
/**
* This class handles the login system and all related functionality
*
* @author Eilert Tunheim, Karin Pettersen, Mads Arnesen
* @version 1.0.0
*/
Eilert Tunheim
committed
/**
* 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 {
Eilert Tunheim
committed
TableResult result = logIn(username,password);
Eilert Tunheim
committed
if(result.getTotalRows() != 0) {
for (FieldValueList row : result.iterateAll()) {
Eilert Tunheim
committed
if (row.get("Username").getValue().equals(username)) {
CreateLogoBar.getLogin().setText(username);
Eilert Tunheim
committed
Eilert Tunheim
committed
if (!row.get("Phone_no").isNull()) {
HelpingFunctions.setPhoneNo(row.get("Phone_no").getStringValue());
Eilert Tunheim
committed
}
if (!row.get("First_name").isNull()) {
HelpingFunctions.setFirstName(row.get("First_name").getStringValue());
Eilert Tunheim
committed
}
if (!row.get("Last_name").isNull()) {
HelpingFunctions.setLastName(row.get("Last_name").getStringValue());
Eilert Tunheim
committed
}
if (!row.get("Admin").isNull()) {
HelpingFunctions.setIsAdmin(row.get("Admin").getBooleanValue());
Eilert Tunheim
committed
}
Eilert Tunheim
committed
}
} else {
NotificationPopUp.displayNotificationWindow("Wrong username or password!");
getPasswordTextField().clear();
Eilert Tunheim
committed
}
}
/**
* 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 + ") ";
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;
}
}