/**
 * Created by Eduardo veras on 19-Jun-16.
 * Edited by Siclait on 19-Juu-16
 */

import Entity.*;
import Service.*;

import javax.jws.soap.SOAPBinding;
import java.util.List;

public class DatabaseManager {

    // Singleton Constructor
    private DatabaseManager(){

    }

    public static void BootDataBase(){

        List<User> users = UserORMService.GetInstance().FindAll();

        if(users.size() == 0)
        {
            System.out.println("\n\nCreating Admins ...");

            UserORMService.GetInstance().Create(new User("admin", "Administrator", "The", "admin", true));
            UserORMService.GetInstance().Create(new User("Wardo", "Eduardo", "Veras", "1234", true));
            UserORMService.GetInstance().Create(new User("EmmJ", "Emmanuel", "Jaquez", "1234", true));
            UserORMService.GetInstance().Create(new User("Djsiclait", "Djidjelly", "Siclait", "1234", true));

            System.out.println("Admins created successfully!\n");
        }
        else
            System.out.println("\n\nDatabase already created!\n");

    }

    /*
     * Database root Commands : Security/Authentification
     * Admin and Server only
     */
    public static boolean CheckUserCredentials(String username, String password){

        User user = UserORMService.GetInstance().Find(username);

        if(user == null) // User does not exist
            return false;
        else if(user.getPassword().equals(password)) // Password Correct
            return true;
        else // Password Incorrect
            return false;
    }

    public static boolean CheckUserCredentials(String username){

        User user = UserORMService.GetInstance().Find(username);

        if(user == null) // User does not exist
            return false;
        else
            return user.isAdmin();
    }

    // Exclusive to Admin
    public static void MakeAdmin(String username){

        if(!CheckUserCredentials(username)) {
            User user = UserORMService.GetInstance().Find(username);

            user.setAdmin(true);

            UserORMService.GetInstance().Edit(user);
        }
        else
            System.out.println("\n\nUser, " + username + ", is already an Administrator!\n");
    }

    // Exclusive to Admin
    public static List<User> FetchAllUsers(){

        return UserORMService.GetInstance().FindAll();
    }

    // User Related Functions
    public static void CreateNewUser(String username, String firstName, String lastName, String password){

        UserORMService.GetInstance().Create(new User(username, firstName, lastName, password, false));
    }

    // Used for general user
    public static void DeleteUserAccount(String username){

        UserORMService.GetInstance().Delete(UserORMService.GetInstance().Find(username));
    }

    // TODO: Add change user password function

}

@@ -68,17 +68,17 @@
 on 19-Ju
-u
+n
 -16%0A */%0A
@@ -131,28 +131,40 @@
 vax.
-jws.soap.SOAPBinding
+persistence.PersistenceException
 ;%0Aim
@@ -356,44 +356,21 @@
 s = 
-UserORMService.GetInstance().FindAll
+FetchAllUsers
 ();%0A
@@ -1036,20 +1036,23 @@
 lready c
-reat
+onfigur
 ed!%5Cn%22);
@@ -1936,16 +1936,78 @@
 ame)) %7B%0A
+%0A            System.out.println(%22%5Cn%5CnMaking new Admin ...%22);%0A%0A
         
@@ -2154,16 +2154,82 @@
 t(user);
+%0A%0A            System.out.println(%22Admin created successfully!%5Cn%22);
 %0A       
@@ -2528,20 +2528,23 @@
  static 
-void
+boolean
  CreateN
@@ -2613,32 +2613,51 @@
 ing password)%7B%0A%0A
+        try %7B%0A%0A    
         UserORMS
@@ -2739,24 +2739,206 @@
 d, false));%0A
+            return true;%0A        %7D catch (PersistenceException exp)%7B%0A            System.out.println(%22User, %22 + username + %22, already exist%5Cn%5Cn%22);%0A            return false;%0A        %7D%0A
     %7D%0A%0A    /
@@ -2979,20 +2979,23 @@
  static 
-void
+boolean
  DeleteU
@@ -3016,32 +3016,50 @@
 ing username)%7B%0A%0A
+        try%7B%0A%0A    
         UserORMS
@@ -3128,24 +3128,182 @@
 username));%0A
+            return true;%0A        %7D catch (Exception exp)%7B%0A            System.out.println(%22This user does not exist%5Cn%5Cn%22);%0A            return false;%0A        %7D%0A
     %7D%0A%0A    /

Test FetchAllUsers(): Success!