protected void Page_Load(object sender, EventArgs e)
 {
     account = (Account)this.Session["Account"];
     if (account != null)
     {
         if (!IsPostBack)
         {
             switch (account.Role)
             {
                 case AccountType.Admin:
                     {
                         lbxAccounts.DataSource = am.Accounts;
                         lbxAccounts.DataBind();
                         ddlRole.Enabled = true;
                         ddlRole.DataSource = Enum.GetNames(typeof(AccountType));
                         ddlRole.DataBind();
                         break;
                     }
                 case AccountType.User:
                     {
                         List<Account> accounts = new List<Account>();
                         accounts.Add(account);
                         lbxAccounts.DataSource = accounts;
                         lbxAccounts.DataBind();
                         ddlRole.Enabled = false;
                         break;
                     }
             }
         }
     }
     else
     {
         Response.Redirect("/Index.aspx");
     }
 }
        protected void OnRegister(object sender, LoginCancelEventArgs e)
        {
            if (this.IsValid)
            {
                try
                {
                   Account account = new Account(tbxEmail.Text, tbxName.Text, AccountType.User);
                    if(accountManagement.CreateAccount(account, tbxPassword.Text))
                    {
                        Response.Redirect("/Login.aspx");
                    }
                    else
                    {
                        FailureText.Text = "Email is al bezet.";
                        e.Cancel = true;
                    }
                }
                catch
                {
                    FailureText.Text = "Account kon niet worden aan gemaakt.";
                    e.Cancel = true;
                }

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            account = (Account)this.Session["Account"];
            if (account != null)
            {
                if (!this.IsPostBack)
                {
                    pm = new ProductManagement();
                    lbxProducts.DataSource = pm.Products;
                    lbxProducts.DataBind();

                    switch (account.Role)
                    {
                        case AccountType.Admin:
                            {
                                btnDeleteProduct.Enabled = true;
                                break;
                            }
                        case AccountType.User:
                            {
                                btnDeleteProduct.Enabled = false;
                                break;
                            }
                    }
                }
            }
            else
            {
                Response.Redirect("/Index.aspx");

            }
        }
 public Product(int id, string name, double price, string imageURL, DateTime lastModified, Account modifiedBy)
 {
     this.ID = id;
     this.Name = name;
     this.ImageURL = imageURL;
     this.Price = price;
     this.LastModified = lastModified;
     this.ModifiedBy = modifiedBy;
 }
        public double CostsAccount(Account account)
        {
            totalCost = 0;
            List<Product> Products = DatabaseManager.GetCostsAccounts(account);
            foreach (Product product in Products)
            {
                totalCost += product.Price;
            }

            return totalCost;
        }
 public bool UpdateProduct(Product product, Account account)
 {
     if(DatabaseManager.ModifyProduct(product, account))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
 public bool RemoveAccount(Account account)
 {
     if (DatabaseManager.DeleteAccount(account))
     {
         this.Accounts = RequestAccounts();
         return true;
     }
     else
     {
         return false;
     }
 }
 public List<Group> OwnerGroups(Account account)
 {
     List<Group> tempGroups = new List<Group>();
     foreach(Group g in Groups)
     {
         if(g.Owner == account)
         {
             tempGroups.Add(g);
         }
     }
     return tempGroups;
 }
 public bool CreateProduct(Product product, Account account)
 {
     Product tempProduct = DatabaseManager.CreateProduct(product, account);
     if (tempProduct != null)
     {
         this.Products = RequestProducts();
         return true;
     }
     else
     {
         return false;
     }
 }
 public bool CreateAccount(Account account, string password)
 {
     Account tempAccount = DatabaseManager.CreateAccount(account, password);
     if(tempAccount != null)
     {
         this.Accounts = RequestAccounts();
         return true;
     }
     else
     {
         return false;
     }
 }
        public static bool AddGroupUser(Group group, Account account)
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, "INSERT INTO GROEP_GEBRUIKERS(GEBRUIKER_ID, GROEP_ID) VALUES (:userID, :groupID);");
                    command.Parameters.Add(":userID", account.ID);
                    command.Parameters.Add(":groupID", group.ID);

                    return ExecuteNonQuery(command);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
 protected void btnRegister_Click(object sender, EventArgs e)
 {
     try
         {
             Account account = new Account(tbxEmail.Text, tbxName.Text, AccountType.User);
             if (accountManagement.CreateAccount(account, tbxPassword.Text))
             {
                 Response.Redirect("/Login.aspx");
             }
             else
             {
                 FailureText.Text = "Email is al bezet.";
             }
         }
         catch
         {
             FailureText.Text = "Account kon niet worden aan gemaakt.";
         }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            account = (Account)this.Session["Account"];
            if(account != null)
            {
                if(!this.IsPostBack)
                {
                    gm = new GroupManagement();
                    switch(account.Role)
                    {
                        case AccountType.Admin:
                            {
                                lbxGroups.DataSource = gm.Groups;
                                lbxGroups.DataBind();
                                lbxAccounts.DataSource = gm.Groups[lbxGroups.SelectedIndex].Users;
                                lbxAccounts.DataBind();
                                break;
                            }
                        case AccountType.User:
                            {
                                ownerGroups = gm.OwnerGroups(account);
                                lbxGroups.DataSource = ownerGroups;
                                lbxGroups.DataBind();
                                lbxAccounts.DataSource = ownerGroups[lbxAccounts.SelectedIndex];
                                lbxAccounts.DataBind();
                                break;
                            }
                    }
                }
            }
            else
            {
                Response.Redirect("/Index.aspx");

            }
        }
        public static Product AddProductToGroceryList(Product product, Account account, GroceryList groceries)
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, "Insert INTO BOODSCHAPPENLIJST_PRODUCTEN(BOODSCHAPPENLIJST_ID, PRODUCT_ID, GEBRUIKER_ID) VALUES(:groceriesID, :productID, :accountID");
                    command.Parameters.Add(":groceriesID", groceries.ID);
                    command.Parameters.Add(":productID", product.ID);
                    command.Parameters.Add(":accountID", account.ID);

                    command.ExecuteNonQuery();
                    return product;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        public static bool RemoveGroupUser(Group group, Account account)
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, "DELETE FROM GROEP_GEBRUIKERS WHERE GEBRUIKER_ID = :userID AND GROEP_ID = :groupID;");
                    command.Parameters.Add(":userID", account.ID);
                    command.Parameters.Add(":groupID", group.ID);

                    return ExecuteNonQuery(command);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        public static bool ModifyProduct(Product product, Account account)
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, "UPDATE PRODUCT SET naam = :name, prijs = :price, imgurl = :imageURL, laastgewijzigd = :lastModified");
                    command.Parameters.Add(":name", product.Name);
                    command.Parameters.Add(":price", product.Price);
                    command.Parameters.Add("imageURL", product.ImageURL);
                    command.Parameters.Add(":lastModified", product.LastModified);

                    bool isAdded = ExecuteNonQuery(command);
                    if (!isAdded)
                    {
                        throw new Exception("The product could not be updated in the database.");
                    }

                    OracleCommand modCommand = CreateOracleCommand(connection, "INSERT INTO WIJZIGINGEN(ID, PRODUCT_ID, GEBRUIKER_ID, WIJZING, DATUM) VALUES(SEQ_WIJZIGINGID.NEXTVAL, :productID, :accountID, :description, :date");
                    modCommand.Parameters.Add(":productID", product.ID);
                    modCommand.Parameters.Add(":accountID", account.ID);
                    modCommand.Parameters.Add(":change", null);
                    modCommand.Parameters.Add(":date", product.LastModified);

                    return ExecuteNonQuery(modCommand);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        public static bool ModifyAccount(Account account)
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, "UPDATE GEBRUIKER SET email = :email, naam = :name, rol = :role WHERE ID = :AccountID");
                    command.Parameters.Add(":email", account.Email);
                    command.Parameters.Add(":naam", account.Name);
                    command.Parameters.Add(":role", account.Role.ToString());
                    command.Parameters.Add(":AccountID", account.ID);

                    return ExecuteNonQuery(command);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        public static List<Group> GetGroups()
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, @"select g.id as GroepID, g.naam as GroepNaam, a.ID as GebruikerID, a.Email, a.naam as GebruikerNaam, a.status, a.ROL from groep g, gebruiker a, groep_eigenaar e where e.groep_ID = g.ID AND e.gebruiker_ID = a.ID");

                    OracleDataReader reader = ExecuteQuery(command);
                    List<Group> Groups = new List<Group>();
                    while (reader.Read())
                    {
                        try
                        {
                            string groupid = reader["GroepID"].ToString();
                            string groupname = reader["GroepNaam"].ToString();
                            string email = reader["Email"].ToString();
                            string accountname = reader["Gebruikernaam"].ToString();
                            AccountType role = (AccountType)Enum.Parse(typeof(AccountType), reader["rol"].ToString());
                            Account owner = new Account(email, accountname, role);
                            Groups.Add(new Group(groupid, groupname, owner));
                        }
                        catch (OracleException exc)
                        {
                            Debug.WriteLine(exc.Message);
                            continue;
                        }
                    }
                    connection.Close();
                    foreach (Group group in Groups)
                    {
                        group.Users = GetGroupAccounts(group);
                    }
                    return Groups;
                }
                finally
                {
                    connection.Close();
                }
            }
        }
 public static List<Product> GetCostsAccounts(Account account)
 {
     using (OracleConnection connection = Connection)
     {
         try
         {
             OracleCommand command = CreateOracleCommand(connection, "");
             return null;
         }
         catch
         {
             return null;
         }
     }
 }
        public static bool DeleteAccount(Account account)
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, "UPDATE GEBRUIKER SET status = '0' WHERE ID = :AccountID");

                    command.Parameters.Add(":AccountID", account.ID);

                    return ExecuteNonQuery(command);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        public static Account CreateAccount(Account account, string password)
        {
            using (OracleConnection connection = Connection)
            {
                try
                {
                    OracleCommand command = CreateOracleCommand(connection, "INSERT INTO GEBRUIKER(EMAIL, WACHTWOORD, NAAM) VALUES (:email, :password, :name)");

                    command.Parameters.Add(":email", account.Email);
                    command.Parameters.Add(":password", password);
                    command.Parameters.Add(":name", account.Name);

                    bool isAdded = ExecuteNonQuery(command);

                    if (!isAdded)
                    {
                        throw new Exception("The account could not be added to the database.");
                    }

                    // Fetch database ID of account
                    OracleCommand commandID = CreateOracleCommand(connection, "SELECT ID FROM GEBRUIKER WHERE Email = :email");
                    commandID.Parameters.Add(":email", account.Email);

                    OracleDataReader reader = ExecuteQuery(commandID);

                    reader.Read();

                    int id = Convert.ToInt32(reader["ID"].ToString());

                    account.ID = id;

                    return account;
                }
                catch (OracleException exc)
                {

                    Debug.WriteLine(exc.Message);
                    return null;
                }
                finally
                {
                    connection.Close();
                }
            }
        }
 public bool UpdateAccount(Account account)
 {
     if (DatabaseManager.ModifyAccount(account))
     {
         this.Accounts = RequestAccounts();
         return true;
     }
     else
     {
         return false;
     }
 }
 public Group(string id, string name, Account owner)
 {
     this.ID = id;
     this.Name = name;
     this.Owner = owner;
 }