示例#1
0
 public Discount getProductDiscount(int storeId, int productId)
 {
     try
     {
         //SqlConnection connection = Connector.getInstance().getSQLConnection();
         lock (connection)
         {
             connection.Open();
             var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE storeId=@storeId AND productId=@productId", new { storeId = storeId, productId = productId }).First();
             connection.Close();
             DiscountEntry     d   = (DiscountEntry)discountEntry;
             DiscountComponent dis = getDiscountByID(d.getId());
             if (dis != null)
             {
                 return((Discount)dis);
             }
             int discountId = d.getId();
             DiscountComponentEntry component = (DiscountComponentEntry)connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE id=@id", new { id = discountId }).First();
             bool isPartOfComplex             = false;
             if (component.getIsPartOfComplex() == 1)
             {
                 isPartOfComplex = true;
             }
             if (d.getType() == "Visible")
             {
                 VisibleDiscount v = new VisibleDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getVisibleType(), component.getStoreId());
                 Product         p = DBProduct.getInstance().getProductByID(d.getProductId());
                 v.setProduct(p);
                 discounts.AddFirst(v);
                 connection.Close();
                 return(v);
             }
             else
             {
                 int             productID = d.getProductId();
                 ReliantDiscount r         = null;
                 if (d.getReliantType() == "sameProduct")
                 {
                     Product p = DBProduct.getInstance().getProductByID(productID);
                     r = new ReliantDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getNumOfProducts(), p, component.getStoreId());
                     discounts.AddFirst(r);
                 }
                 return(r);
             }
         }
     }
     catch (Exception)
     {
         if (connection.State != ConnectionState.Closed)
         {
             connection.Close();
         }
         return(null);
     }
 }
示例#2
0
 public void removeProduct(Product product)
 {
     if (product.getStore() != store || !store.getProductList().Contains(product))
     {
         throw new StoreException("product "
                                  + product.getProductName() + " doesn't belong to store "
                                  + store.getStoreName());
     }
     store.removeProduct(product);
     DBProduct.getInstance().removeProduct(product);
 }
示例#3
0
 public void addProduct(Product product)
 {
     if (!permissions.editProduct())
     {
         throw new PermissionsException("Error:" + userName.getUsername() +
                                        " has no permissions to edit products in store " +
                                        store.getStoreName());
     }
     store.addProduct(product);
     DBProduct.getInstance().addProduct(product);
 }
示例#4
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();
            }
        }
示例#5
0
        public void closeStore(Store store)
        {
            List <StoreRole> roles = store.getRoles();

            foreach (StoreRole role in roles)
            {
                //SubscribedUser sub = role.getUser();
                //sub.removeStoreRole(role);
            }
            dbStore.removeStore(store);
            DBProduct.getInstance().removeAllProductsFromStore(store);
        }
示例#6
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());
 }
示例#7
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");
 }
示例#8
0
 public void removeProduct(Product product)
 {
     if (!permissions.editProduct())
     {
         throw new PermissionsException("Error:" + userName.getUsername() +
                                        " has no permissions to edit products in store " +
                                        store.getStoreName());
     }
     if (product.getStore() != store || !store.getProductList().Contains(product))
     {
         throw new StoreException("product " + product.getProductName() +
                                  " doesn't belong to store " + store.getStoreName());
     }
     store.removeProduct(product);
     DBProduct.getInstance().removeProduct(product);
 }
示例#9
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);
            }
        }
示例#10
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();
            }


            ///////////////////////////////////////////////////
        }
示例#11
0
 public void decQuantityLeft(int amount)
 {
     quantityLeft = quantityLeft - amount;
     DBProduct.getInstance().update(this);
 }
示例#12
0
 public void setQuantityLeft(int quantity)
 {
     this.quantityLeft = quantity;
     DBProduct.getInstance().update(this);
 }
示例#13
0
 public void addProduct(Product product)
 {
     store.addProduct(product);
     DBProduct.getInstance().addProduct(product);
 }