Exemple #1
0
        public void removeUser(String user)
        {
            if (Equals(user, "u1"))
            {
                throw new UserException("admin cannot be removed");
            }
            SubscribedUser subscribedUser = DBSubscribedUser.getInstance().getSubscribedUser(user);

            if (subscribedUser == null)
            {
                throw new UserException("user to be removed does not exist");
            }
            try
            {
                Session session = dbSession.getSessionOfSubscribedUser(subscribedUser);
                if (session != null)
                {
                    if (session.getState() is LoggedIn)
                    {
                        session.logout();
                        session.setSubscribedUser(null);
                    }
                }
            }
            catch (DoesntExistException) { }
            LinkedList <StoreRole> toDelete      = new LinkedList <StoreRole>();
            LinkedList <Store>     toDeleteStore = new LinkedList <Store>();

            foreach (StoreRole role in subscribedUser.getStoreRoles())
            {
                role.removeAllAppointedBy();
                Store          store = role.getStore();
                SubscribedUser appointedBySubscribedUser = role.getAppointedBy();
                toDelete.AddFirst(role);
                if (appointedBySubscribedUser != null)
                {
                    StoreRole appointedByStoreRole = store.getStoreRole(role.getAppointedBy());
                    store.removeStoreRole(role);
                    appointedByStoreRole.removeRoleAppointedByMe(role);
                }
                if (role is StoreOwner && role.getStore().getNumberOfOwners() == 1)
                {
                    closeStore(role.getStore());
                }
                //DBStore.getInstance().removeStoreRole(role);
            }
            foreach (StoreRole sr in toDelete)
            {
                DBStore.getInstance().removeStoreRole(sr);
                subscribedUser.removeStoreRole(sr);
                sr.getStore().removeStoreRole(sr);
            }

            dbSubscribedUser.remove(subscribedUser);
        }
Exemple #2
0
        public void updateShoppingBasket()
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    foreach (KeyValuePair <string, SubscribedUser> pair in users)
                    {
                        string         username = pair.Key;
                        SubscribedUser su       = pair.Value;

                        string         sql = "SELECT * FROM BasketCart WHERE username=@username;";
                        var            c2  = connection.Query <BasketCartEntry>(sql, new { username = username });
                        ShoppingBasket sb  = su.getShoppingBasket();
                        if (Enumerable.Count(c2) > 0)
                        {
                            for (int i = 0; i < Enumerable.Count(c2); i++)
                            {
                                BasketCartEntry bc      = c2.ElementAt(i);
                                int             storeID = bc.getStoreID();
                                sql = "SELECT * FROM CartProduct WHERE storeID=@storeID AND username=@username;";
                                var c3 = connection.Query <CartProductEntry>(sql, new { storeID, username });

                                for (int j = 0; j < Enumerable.Count(c3); j++)
                                {
                                    CartProductEntry cp = c3.ElementAt(j);
                                    int     productID   = cp.getProductID();
                                    int     amount      = cp.getAmount();
                                    Product p           = DBProduct.getInstance().getProductByID(productID);
                                    sb.addToCartNoDBUpdate(p, amount, storeID);
                                }
                            }
                        }
                        List <StoreRole> storeRoles = su.getStoreRoles();

                        foreach (StoreRole sr in DBStore.getInstance().getAllStoreRoles(username))
                        {
                            if (sr.getUser().getUsername() == username)
                            {
                                storeRoles.Add(sr);
                            }
                        }
                    }
                    connection.Close();
                }
            }
            catch (Exception e)
            {
                connection.Close();
            }
        }
Exemple #3
0
        public void removeUser(String user)
        {
            if (Equals(user, "admin"))
            {
                throw new UserException("admin cannot be removed");
            }
            SubscribedUser subscribedUser = DBSubscribedUser.getInstance().getSubscribedUser(user);

            if (subscribedUser == null)
            {
                throw new UserException("user to be removed does not exist");
            }
            Session session = dbSession.getSessionOfSubscribedUser(subscribedUser);

            if (session != null)
            {
                if (session.getState() is LoggedIn)
                {
                    session.logout();
                }
            }
            foreach (StoreRole role in subscribedUser.getStoreRoles())
            {
                role.removeAllAppointedBy();
                Store          store = role.getStore();
                SubscribedUser appointedBySubscribedUser = role.getAppointedBy();
                if (appointedBySubscribedUser != null)
                {
                    StoreRole appointedByStoreRole = store.getStoreRole(role.getAppointedBy());
                    store.removeStoreRole(appointedByStoreRole);
                    appointedByStoreRole.removeRoleAppointedByMe(role);
                }
                if (role is StoreOwner && role.getStore().getNumberOfOwners() == 0)
                {
                    closeStore(role.getStore());
                }
                DBStore.getInstance().removeStoreRole(role);
            }
            session.setSubscribedUser(null);
            dbSubscribedUser.remove(subscribedUser);
        }
Exemple #4
0
        public SubscribedUser getSubscribedUser(string username)
        {
            if (users.ContainsKey(username))
            {
                return(users[username]);
            }
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    var c1 = connection.Query <RegisterEntry>("SELECT username, password FROM [dbo].[Register] WHERE username=@username ", new { username = username });

                    if (Enumerable.Count(c1) == 1)
                    {
                        RegisterEntry  re       = c1.ElementAt(0);
                        string         password = re.getPassword();
                        string         sql      = "SELECT * FROM BasketCart WHERE username=@username;";
                        var            c2       = connection.Query <BasketCartEntry>(sql, new { username = username });
                        ShoppingBasket sb       = new ShoppingBasket(username);

                        SubscribedUser   su         = new SubscribedUser(username, password, sb);
                        List <StoreRole> storeRoles = su.getStoreRoles();
                        users.Add(username, su);
                        DBStore.getInstance().getAllStoreRoles(username);

                        if (Enumerable.Count(c2) > 0)
                        {
                            for (int i = 0; i < Enumerable.Count(c2); i++)
                            {
                                BasketCartEntry bc      = c2.ElementAt(i);
                                int             storeID = bc.getStoreID();
                                sql = "SELECT * FROM CartProduct WHERE storeID=@storeID AND username=@username;";

                                var c3 = connection.Query <CartProductEntry>(sql, new { storeID, username });
                                for (int j = 0; j < Enumerable.Count(c3); j++)
                                {
                                    CartProductEntry cp = c3.ElementAt(j);
                                    int     productID   = cp.getProductID();
                                    int     amount      = cp.getAmount();
                                    Product p           = DBProduct.getInstance().getProductByID(productID);
                                    sb.addToCartNoDBUpdate(p, amount, storeID);
                                }
                            }
                        }

                        //foreach (StoreRole sr in DBStore.getInstance().getAllStoreRoles(username))
                        //{
                        //    if(sr.getUser().getUsername()==username)
                        //    {
                        //        storeRoles.Add(sr);
                        //    }
                        //}
                        //users.Add(username, su);
                        connection.Close();
                        return(su);
                    }

                    else
                    {
                        connection.Close();
                        return(null);
                    }
                }
            }
            catch (Exception e)
            {
                connection.Close();
                return(null);
            }
        }