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.Constants.*;
import static com.application.DB.Settings.*;
import static com.application.GUI.PopUpWindows.LoginPopup.getPasswordTextField;
Eilert Tunheim
committed
Eilert Tunheim
committed
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()) {
setPhoneNo(row.get("Phone_no").getStringValue());
}
if (!row.get("First_name").isNull()) {
setFirstName(row.get("First_name").getStringValue());
}
if (!row.get("Last_name").isNull()) {
setLastName(row.get("Last_name").getStringValue());
}
if (!row.get("Admin").isNull()) {
setIsAdmin(row.get("Admin").getBooleanValue());
}
Eilert Tunheim
committed
}
} else {
NotificationPopUp.displayNotificationWindow("Wrong username or password!");
getPasswordTextField().clear();
Eilert Tunheim
committed
}
}
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);
}
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);
}
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;
}
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;
}
}