Example #1
0
        private void removeChildren(DiscountComposite d)
        {
            foreach (DiscountComponent child in d.getChildren())
            {
                if (child is DiscountComposite)
                {
                    removeChildren((DiscountComposite)child);
                }
                else
                {
                    if (child is ReliantDiscount)
                    {
                        ReliantDiscount r = (ReliantDiscount)child;

                        if (r.getProduct() != null)
                        {
                            r.getProduct().removeReliantDiscount();
                        }
                    }
                    if (child is VisibleDiscount)
                    {
                        VisibleDiscount v = (VisibleDiscount)child;

                        if (v.getProduct() != null)
                        {
                            v.getProduct().removeDiscount();
                        }
                    }
                }
            }
        }
Example #2
0
 /* public void addReliantDiscount(double percentage, String condition, String duration)
  * {
  *   discount = new ReliantDiscount(condition, duration, percentage: percentage);
  * }
  *
  * public void addVisibleDiscount(double percentage, String duration)
  * {
  *   discount = new VisibleDiscount(percentage, duration);
  * }
  * public DiscountComponent getDiscount()
  * {
  *   return this.discount;
  * }
  * public void addReliantDiscount(double percentage, String condition, String duration)
  * {
  *   discount = new ReliantDiscount(percentage, condition, duration);
  * }
  *
  * public void addVisibleDiscount(double percentage, String duration)
  * {
  *   discount = new VisibleDiscount(percentage, duration);
  * }*/
 public void addDiscount(DiscountComponent d)
 {
     if (discount == null)
     {
         this.discount = d;
     }
     else
     {
         List <DiscountComponent> list = new List <DiscountComponent>();
         list.Add(this.discount);
         list.Add(d);
         DiscountComposite composite = new DiscountComposite(list, "or");
         this.discount = composite;
     }
 }
Example #3
0
        public void addComplexDiscount(List <DiscountComponent> list, string type, double percentage, string duration)
        {
            DiscountComposite composite = new DiscountComposite(list, type, percentage, duration, store.getStoreID());

            foreach (DiscountComponent d in list)
            {
                store.removeDiscoutFromList(d.getId());
                DBDiscount.getInstance().setIsPartOfComplex(d.getId(), true);
                d.setIsPartOfComplex(true);
            }
            DBDiscount.getInstance().addDiscount(composite);
            store.addDiscount(composite);

            //DBDiscount.getInstance().addDiscount(composite);
        }
Example #4
0
 public void removeDiscount(DiscountComponent d)
 {
     try
     {
         //SqlConnection connection = Connector.getInstance().getSQLConnection();
         lock (connection)
         {
             connection.Open();
             connection.Execute("DELETE FROM DiscountComponent WHERE id=@id ", new { id = d.getId() });
             if (d is Discount)
             {
                 connection.Execute("DELETE FROM Discount WHERE id=@id ", new { id = d.getId() });
                 connection.Close();
             }
             else
             {
                 connection.Execute("DELETE FROM DiscountComposite WHERE id=@id ", new { id = d.getId() });
                 DiscountComposite composite = (DiscountComposite)d;
                 connection.Close();
                 foreach (DiscountComponent component in composite.getChildren())
                 {
                     removeDiscount(component);
                 }
             }
             discounts.Remove(d);
         }
     }
     catch (Exception e)
     {
         if (connection.State == ConnectionState.Open)
         {
             connection.Close();
         }
         throw e;
     }
 }
Example #5
0
        public void addDiscount(DiscountComponent d)
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    string sql = "INSERT INTO [dbo].[DiscountComponent] (id, percentage, duration, type, storeId, isPartOfComplex)" +
                                 " VALUES (@id,@percentage, @duration, @type, @storeId, @isPartOfComplex)";
                    int isPartOfComplex;
                    if (d.getIsPartOfComplex())
                    {
                        isPartOfComplex = 1;
                    }
                    else
                    {
                        isPartOfComplex = 0;
                    }
                    if (d is Discount)
                    {
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Discount",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        if (d is VisibleDiscount)
                        {
                            VisibleDiscount v = (VisibleDiscount)d;
                            addVisibleDiscount(v);
                        }
                        if (d is ReliantDiscount)
                        {
                            ReliantDiscount r = (ReliantDiscount)d;
                            addReliantDiscount(r);
                        }
                    }
                    if (d is DiscountComposite)
                    {
                        DiscountComposite composite = (DiscountComposite)d;
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Composite",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        foreach (DiscountComponent child in composite.getChildren())
                        {
                            string sql2 = "INSERT INTO [dbo].[DiscountComposite] (id, childid, type)" +
                                          " VALUES (@id, @childid, @type)";

                            connection.Execute(sql2, new
                            {
                                id      = d.getId(),
                                childid = child.getId(),
                                type    = composite.getType()
                            });
                        }
                    }
                    connection.Close();
                    discounts.AddFirst(d);
                }
            }

            catch (Exception e)
            {
                connection.Close();
                throw e;
            }
        }
Example #6
0
        public LinkedList <DiscountComponent> getStoreDiscountsList(int storeId)
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                LinkedList <DiscountComponent> storeDiscounts = new LinkedList <DiscountComponent>();
                lock (connection)
                {
                    connection.Open();
                    var c = connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE storeId=@storeId AND type=@type", new { storeId = storeId, type = "Discount" }).ToList <DiscountComponentEntry>();
                    connection.Close();
                    List <DiscountComponentEntry> discountList = (List <DiscountComponentEntry>)c;
                    foreach (DiscountComponentEntry d in discountList)
                    {
                        connection.Open();
                        var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE id=@id", new { id = d.getId() }).First();
                        connection.Close();

                        DiscountEntry de = (DiscountEntry)discountEntry;


                        if (de.getProductId() != -1)//productDiscount
                        {
                            Discount dis = getProductDiscount(d.getStoreId(), de.getProductId());

                            if (dis.getIsPartOfComplex() == false) //add to store discounts only if it is not part of complex discount
                            {
                                storeDiscounts.AddFirst(dis);
                            }
                        }
                        else//StoreDiscount
                        {
                            Discount dis = getStoreDiscount(d.getId(), d.getStoreId());
                            if (dis.getIsPartOfComplex() == false)
                            {
                                storeDiscounts.AddFirst(dis);
                            }
                            discounts.AddFirst(dis);
                        }
                    }
                    connection.Open();
                    var compositeEntryList = connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE storeId=@storeId AND type=@type", new { storeId = storeId, type = "Composite" }).ToList <DiscountComponentEntry>();
                    List <DiscountComponentEntry> compositeEntryL = (List <DiscountComponentEntry>)compositeEntryList;
                    int i = 0;
                    while (compositeEntryL.Count != 0)
                    {
                        DiscountComponentEntry   di       = compositeEntryL.ElementAt(i);
                        List <DiscountComponent> children = new List <DiscountComponent>();
                        var discountChildList             = connection.Query <DiscountCompositeEntry>("SELECT * FROM [dbo].[DiscountComposite] WHERE id=@id", new { id = di.getId() }).ToList <DiscountCompositeEntry>();
                        connection.Close();
                        List <DiscountCompositeEntry> de = (List <DiscountCompositeEntry>)discountChildList;
                        string type = de.ElementAt(0).getType();
                        bool   childrenPulledFromDB = true;
                        foreach (DiscountCompositeEntry en in de)
                        {
                            if (getDiscountByID(en.getchildid()) == null)
                            {
                                childrenPulledFromDB = false;
                                break;
                            }
                        }
                        if (childrenPulledFromDB)
                        {
                            foreach (DiscountCompositeEntry en in de)
                            {
                                DiscountComponent disc = getDiscountByID(en.getchildid());
                                children.Add(disc);
                            }
                            bool isPartOfComplex = false;
                            if (di.getIsPartOfComplex() == 1)
                            {
                                isPartOfComplex = true;
                            }
                            DiscountComposite compos = new DiscountComposite(di.getId(), children, type, di.getPercentage(), di.getDuration(), di.getStoreId(), isPartOfComplex);
                            discounts.AddFirst(compos);
                            storeDiscounts.AddFirst(compos);
                            compositeEntryL.Remove(di);
                        }
                        if (compositeEntryL.Count != 0)
                        {
                            i = (i + 1) % compositeEntryL.Count;
                        }
                    }
                    connection.Close();
                    return(storeDiscounts);
                }
            }
            catch (Exception)
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
                return(new LinkedList <DiscountComponent>());
            }
        }
Example #7
0
        public void addComplexDiscount(List <DiscountComponent> list, string type)
        {
            DiscountComposite composite = new DiscountComposite(list, type);

            store.addDiscount(composite);
        }