Beispiel #1
0
 public Admin()
 {
     dbStore          = DBStore.getInstance();
     dbSubscribedUser = DBSubscribedUser.getInstance();
     dbSession        = DBSession.getInstance();
     dbComplaint      = DBComplaint.getInstance();
 }
Beispiel #2
0
        public void removeFromCart(int productId)
        {
            DBSubscribedUser dbuser = DBSubscribedUser.getInstance();

            foreach (KeyValuePair <int, ShoppingCart> cart in shoppingCarts)
            {
                Product p = cart.Value.cartContainsProduct(productId);
                if (p != null)
                {
                    cart.Value.removeFromCart(p);
                    if (username != null)
                    {
                        dbuser.removeProductFromCartProductTable(username, cart.Value.getStoreID(), productId);
                    }

                    if (cart.Value.CartIsEmpty())
                    {
                        deleteCart(cart.Value);
                    }

                    return;
                }
            }
            throw new DoesntExistException("Product cannot be removed, it does not exist in cart");
        }
Beispiel #3
0
 public void init()
 {
     if (instance == null)
     {
         instance = new DBSubscribedUser();
     }
 }
Beispiel #4
0
        public void login(String username, String password, Session session)
        {
            String         encrypted = DBSubscribedUser.getInstance().encryptPassword(password);
            SubscribedUser sub       = DBSubscribedUser.getInstance().getSubscribedUser(username);

            if (sub == null)
            {
                throw new LoginException("Username does not exist");
            }
            SubscribedUser loggedIn = DBSubscribedUser.getInstance().getloggedInUser(username);

            if (loggedIn != null)
            {
                throw new LoginException("Username already logged in");
            }
            if (!Equals(sub.getPassword(), encrypted))
            {
                throw new LoginException("Incorrect password");
            }
            session.setSubscribedUser(sub);
            if (Equals(username, "admin"))
            {
                session.setState(new Admin());
            }
            else
            {
                session.setState(new LoggedIn());
            }
            session.setShoppingBasket(sub.getShoppingBasket());
            DBSubscribedUser.getInstance().login(sub);
        }
Beispiel #5
0
        public void addToCart(Product product, int amount)
        {
            int          storeID = product.getStore().getStoreID();
            bool         found   = false;
            ShoppingCart sc      = null;

            foreach (ShoppingCart s in shoppingCarts.Values)
            {
                if (s.getStoreID() == storeID)
                {
                    sc    = s;
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                sc = new ShoppingCart(storeID);
                if (username != null)
                {
                    DBSubscribedUser.getInstance().addCartToBasketCartTable(username, storeID);
                }
                shoppingCarts.Add(storeID, sc);
            }
            if (username != null)
            {
                DBSubscribedUser.getInstance().addProductToCartProductTable(username, storeID, product.getProductID(), amount);
            }
            sc.addToCart(product, amount);
        }
Beispiel #6
0
 public static DBSubscribedUser getInstance()
 {
     if (instance == null)
     {
         instance = new DBSubscribedUser();
     }
     return(instance);
 }
Beispiel #7
0
        private void init()
        {
            SubscribedUser admin = new SubscribedUser("Admin", "1234", new ShoppingBasket());

            DBSubscribedUser.getInstance().register(admin);
            PaymentService.getInstance().connectToSystem();
            DeliveryService.getInstance().connectToSystem();
            ConsistencySystem.getInstance().connectToSystem();
        }
Beispiel #8
0
        internal void changeQuantityOfProduct(int storeID, Product p, int newAmount)
        {
            ShoppingCart sc = getShoppingCartByID(storeID);

            sc.changeQuantityOfProduct(p, newAmount);
            if (username != null)
            {
                DBSubscribedUser.getInstance().updateAmountOnCartProductTable(username, storeID, p.getProductID(), newAmount);
            }
        }
Beispiel #9
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);
        }
Beispiel #10
0
        public void register(string username, string password, Session session)
        {
            String         encrypted = DBSubscribedUser.getInstance().encryptPassword(password);
            SubscribedUser s         = dbSubscribedUser.getSubscribedUser(username);

            if (s != null)
            {
                throw new RegisterException("username already exists");
            }
            SubscribedUser sub = new SubscribedUser(username, encrypted, session.getShoppingBasket());

            session.setSubscribedUser(sub);
            DBSubscribedUser.getInstance().register(sub);
        }
Beispiel #11
0
        public void register(string username, string password, Session session)
        {
            SubscribedUser s = dbSubscribedUser.getSubscribedUser(username);

            if (s != null)
            {
                throw new RegisterException("Error: Username already exists");
            }
            session.getShoppingBasket().setUsername(username);
            SubscribedUser sub = new SubscribedUser(username, password, session.getShoppingBasket());

            //session.setSubscribedUser(sub);
            DBSubscribedUser.getInstance().register(sub);
        }
Beispiel #12
0
 public static void initWitOutRead()
 {
     DBProduct.getInstance().init();
     DBSession.getInstance().init();
     DBDiscount.getInstance().init();
     DBSubscribedUser.getInstance().init();
     DBStore.getInstance().init();
     DBSubscribedUser.getInstance().updateShoppingBasket();
     DBNotifications.getInstance().init();
     PaymentService.getInstance().connectToSystem();
     DeliveryService.getInstance().connectToSystem();
     ConsistencySystem.getInstance().connectToSystem();
     NotificationsBridge.getInstance().setObserver(DomainBridge.getInstance());
 }
Beispiel #13
0
 public static void initTestWitOutRead()
 {
     testsMode = true;
     SystemLogger.configureLogs();
     DBProduct.getInstance().initTests();
     DBSession.getInstance().initTests();
     DBDiscount.getInstance().initTests();
     DBSubscribedUser.getInstance().initTests();
     DBStore.getInstance().initTests();
     DBNotifications.getInstance().initTests();
     PaymentService.getInstance().connectToSystem();
     DeliveryService.getInstance().connectToSystem();
     ConsistencySystem.getInstance().connectToSystem();
     NotificationsBridge.getInstance().setObserver(DomainBridge.getInstance());
     DomainBridge.getInstance().addAdmin("u1", "123");
 }
Beispiel #14
0
        private void deleteCart(ShoppingCart sc)
        {
            foreach (KeyValuePair <int, ShoppingCart> cart in shoppingCarts)
            {
                if (cart.Value.getStoreID() == sc.getStoreID())
                {
                    shoppingCarts.Remove(cart.Key);

                    if (username != null)
                    {
                        DBSubscribedUser.getInstance().deleteCartFromBasketCartTable(username, cart.Value.getStoreID());
                    }
                    return;
                }
            }
        }
Beispiel #15
0
 public void initTests()
 {
     try
     {
         //SqlConnection connection = Connector.getInstance().getSQLConnection();
         lock (connection)
         {
             connection.Open();
             connection.Execute("DELETE FROM Register");
             connection.Execute("DELETE FROM BasketCart");
             connection.Execute("DELETE FROM CartProduct");
             instance = new DBSubscribedUser();
             connection.Close();
         }
     }
     catch (Exception e)
     {
         connection.Close();
     }
 }
Beispiel #16
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);
        }
Beispiel #17
0
        public void loginAfterRegister(String username, String password, Session session)
        {
            String         encrypted = password;
            SubscribedUser sub       = DBSubscribedUser.getInstance().getSubscribedUser(username);

            if (sub == null)
            {
                throw new LoginException("Error: Username does not exist");
            }
            DBSubscribedUser.getInstance().updateStoreRole(sub);
            //SubscribedUser loggedIn = DBSubscribedUser.getInstance().getloggedInUser(username);
            //if( loggedIn != null)
            //    throw new LoginException("Error: Username already logged in");
            if (!Equals(sub.getPassword(), encrypted))
            {
                throw new LoginException("Error: Incorrect password");
            }
            ////////////erase

            // Store st = new Store("bb", "cc");
            //DBStore.getInstance().addStore(st);



            ////////erase
            session.setSubscribedUser(sub);

            if (Equals(username, "u1"))
            {
                session.setState(new Admin());
            }
            else
            {
                session.setState(new LoggedIn());
            }
            session.setShoppingBasket(new ShoppingBasket(sub.getUsername()));
            session.setShoppingBasket(sub.getShoppingBasket());
            DBSubscribedUser.getInstance().login(sub);
        }
Beispiel #18
0
        public Store getStore(int storeId)
        {
            foreach (Store s in stores)
            {
                if (s.getStoreID() == storeId)
                {
                    return(s);
                }
            }
            /////////////////////////////////////////////////////
            try
            {
                // SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    LinkedList <Store> newStores = new LinkedList <Store>();
                    var StoreResult     = connection.Query <StoreEntry>("SELECT * FROM [dbo].[Stores] WHERE storeId=@storeId ", new { storeId = storeId });
                    var StoreRoleResult = connection.Query <StoreRoleEntry>("SELECT * FROM [dbo].[StoreRoles] WHERE storeId=@storeId ", new { storeId = storeId });
                    var ContractResult  = connection.Query <Contract>("SELECT * FROM [dbo].[Contracts] WHERE storeId = @storeId", new { storeId = storeId });
                    var pendingResult   = connection.Query <string>("SELECT userName FROM [dbo].[PendingOwners] WHERE storeId = @storeId", new { storeId = storeId }).AsList();
                    var policyEntries   = connection.Query <PolicyEntry>("SELECT * FROM [dbo].[PurchasePolicy] WHERE storeID=@storeId", new { storeID = storeId });
                    connection.Close();

                    StoreEntry se = StoreResult.ElementAt(0);
                    Store      s  = new Store(se.getStoreId(), se.getName(), se.getDescription());
                    foreach (Contract c in ContractResult)
                    {
                        s.getContracts().AddFirst(c);
                    }
                    foreach (string pending in pendingResult)
                    {
                        s.getPending().AddFirst(pending);
                    }

                    LinkedList <Product> lst = DBProduct.getInstance().getAllProducts();
                    foreach (Product p in lst)
                    {
                        if (p.getStoreID() == s.getStoreID())
                        {
                            s.addProduct(p);
                        }
                    }
                    s.setPolicyList(parsePolicy(policyEntries));
                    foreach (StoreRoleEntry element in StoreRoleResult)
                    {
                        if (element.getStoreId() == s.getStoreID() && element.getIsOwner() == 1)
                        {
                            SubscribedUser appointedBy = null;
                            try
                            {
                                appointedBy = DBSubscribedUser.getInstance().getSubscribedUserForInitStore(element.getAppointedBy());
                            }
                            catch (Exception) { }
                            SubscribedUser user = DBSubscribedUser.getInstance().getSubscribedUserForInitStore(element.getUserName());
                            StoreOwner     so   = new StoreOwner(appointedBy, user, s);
                            s.addStoreRoleFromInitOwner(so);
                            storeRole.AddLast(so);
                        }
                        else if (element.getStoreId() == s.getStoreID() && element.getIsOwner() == 0)
                        {
                            SubscribedUser appointedBy = DBSubscribedUser.getInstance().getSubscribedUserForInitStore(element.getAppointedBy());
                            SubscribedUser user        = DBSubscribedUser.getInstance().getSubscribedUserForInitStore(element.getUserName());
                            Permissions    p           = new Permissions(false, false, false);
                            if (element.getEditDiscount() == 1)
                            {
                                p.setEditDiscount(true);
                            }
                            if (element.getEditPolicy() == 1)
                            {
                                p.setEditPolicy(true);
                            }
                            if (element.getEditProduct() == 1)
                            {
                                p.setEditProduct(true);
                            }
                            StoreManager sm = new StoreManager(appointedBy, s, user, p);
                            s.addStoreRoleFromInitManager(sm);
                            storeRole.AddLast(sm);
                        }
                    }
                    stores.AddLast(s);
                    LinkedList <DiscountComponent> discountList = DBDiscount.getInstance().getStoreDiscountsList(storeId);
                    s.setDiscountList(discountList);
                    return(s);
                }
            }
            catch (Exception e)
            {
                connection.Close();
                SystemLogger.getErrorLog().Error("Connection error in function getStore in DB Store store id " + storeId);
                throw new ConnectionException();
            }


            ///////////////////////////////////////////////////
        }
Beispiel #19
0
 public LoggedIn()
 {
     dbSubscribedUser = DBSubscribedUser.getInstance();
     dbComplaint      = DBComplaint.getInstance();
 }
Beispiel #20
0
 public Guest()
 {
     dbSubscribedUser = DBSubscribedUser.getInstance();
 }
Beispiel #21
0
        //public void addCoupon(string coupon, int storeID)
        //{
        //    ShoppingCart sc = getShoppingCartByID(storeID);
        //    if (sc != null)
        //        sc.addStoreCoupon(coupon);
        //    else
        //        throw new DoesntExistException("no such store ID in Shopping basket");
        //}

        //public void removeCoupon(int storeID)
        //{
        //    ShoppingCart sc = getShoppingCartByID(storeID);
        //    if (sc != null)
        //        sc.removeCoupon();
        //    else
        //        throw new DoesntExistException("no such store ID in Shopping basket");
        //}

        public int purchaseBasket(string address, string creditcard, string month, string year, string holder, string cvv)
        {
            //    foreach (KeyValuePair<int, ShoppingCart> pair1 in shoppingCarts)
            //    {
            //        ShoppingCart cart = pair1.Value;
            //        Dictionary<Product, int> productsInCart = cart.getProductsInCarts();
            //        foreach (KeyValuePair<Product, int> pair2 in productsInCart)
            //        {
            //            Product product = pair2.Key;
            //            int amount = pair2.Value;
            //            if (product.getQuantityLeft() < amount)
            //            {

            //                throw new IllegalAmountException("Error: Cannot complete purchase- " + product.getProductName() + " does not have enough quantity left");
            //            }
            //            product.decQuantityLeft(amount);
            //        }

            //    }

            int resultPay     = PaymentService.getInstance().checkOut(address, creditcard, month, year, holder, cvv);
            int resultDeliver = -1;


            if (resultPay != -1)
            {
                resultDeliver = DeliveryService.getInstance().sendToUser(address, creditcard, month, year, holder, cvv);

                if (resultDeliver == -1)
                {
                    int res2 = PaymentService.getInstance().cancelPayment(resultPay + "");

                    throw new CartException("Delivery FAILED");
                }
                if (username != null)
                {
                    foreach (KeyValuePair <int, ShoppingCart> pair1 in shoppingCarts)
                    {
                        ShoppingCart cart = pair1.Value;
                        Dictionary <Product, int> productsInCart = cart.getProductsInCarts();
                        foreach (KeyValuePair <Product, int> pair2 in productsInCart)
                        {
                            Product product = pair2.Key;
                            int     amount  = pair2.Value;
                            if (product.getQuantityLeft() < amount)
                            {
                                throw new IllegalAmountException("Error: Cannot complete purchase- " + product.getProductName() + " does not have enough quantity left");
                            }
                            product.decQuantityLeft(amount);
                        }
                    }
                    DBSubscribedUser.getInstance().updateTablesAfterPurchase(username, shoppingCarts);
                }
            }
            else
            {
                throw new CartException("Payment FAILED");
            }
            //throw new SuccessPaymentExeption("OK");
            return(resultDeliver);
        }