/*
  * This method creates a new PartListin the system and in the db
  * This method can return:
  * null = the PartList was not created
  * a PartList = the PartList was created succesfully
  */
 public PartList CreatePartList(Item i, int shoppinglistNumber, int amount)
 {
     PartList psl = new PartList(amount, false, i);
     if(sDB.CreatePartList(psl, shoppinglistNumber) != 1)
     {
         psl = null;
     }
     else
     {
         psl.Id = sDB.FindPartListID(shoppinglistNumber, i.Id);
     }
     
     return psl;
 }
 public void AddPartList(ShoppingList sl, PartList psl)
 {
     sCtrl.AddPartList(sl, psl);
 }
        /*
         * This method is used to create a PartList in the DB
         * This method return the number of lines this method changed in the DB.
         */
        public int CreatePartList(PartList psl, int shoppinglistNumber)
        {
            conn.Open();
            DbTransaction dbTrans = conn.GetDbCon().BeginTransaction();
            try
            {
                DbCommand comm = conn.GetDbCon().CreateCommand();
                comm.Transaction = dbTrans;

                string sql = "INSERT INTO dbo.partlist values(@amount, @bought, @item_id, @shoppinglist_id)";
                comm.CommandText = sql;

                DbParameter dbParamAmount = conn.GetDbFactory().CreateParameter();
                dbParamAmount.DbType = DbType.Int32;
                dbParamAmount.Value = psl.Amount;
                dbParamAmount.ParameterName = "amount";

                DbParameter dbParamBought = conn.GetDbFactory().CreateParameter();
                dbParamBought.DbType = DbType.Boolean; //HERHERHERHER
                dbParamBought.Value = psl.Bought;
                dbParamBought.ParameterName = "bought";

                DbParameter dbParamItemID = conn.GetDbFactory().CreateParameter();
                dbParamItemID.DbType = DbType.Int32;
                dbParamItemID.Value = psl.Item.Id;
                dbParamItemID.ParameterName = "item_id";

                DbParameter dbParamShoppingListID = conn.GetDbFactory().CreateParameter();
                dbParamShoppingListID.DbType = DbType.Int32;
                dbParamShoppingListID.Value = shoppinglistNumber;
                dbParamShoppingListID.ParameterName = "shoppinglist_id";

                comm.Parameters.Add(dbParamAmount);
                comm.Parameters.Add(dbParamBought);
                comm.Parameters.Add(dbParamItemID);
                comm.Parameters.Add(dbParamShoppingListID);
                int result = comm.ExecuteNonQuery();
                dbTrans.Commit();
                conn.GetDbCon().Close();
                return result;
            }
            catch (SqlException)
            {
                dbTrans.Rollback();
                conn.GetDbCon().Close();
                return 0;
            }
        }
        /*
         * This method is used to get all PartLists associated with the specified ShoppingList
         * This method returns a collection of all PartLists associated with this ShopppingList
         */

        private List<PartList> GetPartListsSLNumber(int ShoppingListNumber)
        {
            List<PartList> pls = new List<PartList>();

            conn.Open();
            DbCommand comm = conn.GetDbCon().CreateCommand();
            string sql =
                "SELECT id, amount, bought, item_id from dbo.partlist where shoppinglist_id = @ShoppingListNumber";
            comm.CommandText = sql;
            DbParameter dbParamShoppingListNumber = conn.GetDbFactory().CreateParameter();
            dbParamShoppingListNumber.DbType = DbType.Int32;
            dbParamShoppingListNumber.Value = ShoppingListNumber;
            dbParamShoppingListNumber.ParameterName = "ShoppingListNumber";
            comm.Parameters.Add(dbParamShoppingListNumber);
            List<PartList> PartLists = new List<PartList>();

            DbDataReader dr = comm.ExecuteReader();
            while (dr.Read())
            {
                PartList psl = new PartList();
                psl.Id = dr.GetInt32(0);
                int i = dr.GetInt32(1);
                psl.Amount = i;
                if (dr.GetBoolean(2) == true) // HERHERHERHERH
                {
                    psl.Bought = true;
                }
                else
                {
                    psl.Bought = false;
                }
                int ItemID = 0;
                ItemID = dr.GetInt32(3);
                psl.Item = iDb.GetItem(ItemID);
                PartLists.Add(psl);
            }
            dr.Close();
            pls = PartLists;
            return pls;

        }
 /*
  * This method adds a PartList to the specified ShoppingList
  */
 public void AddPartList(ShoppingList sl, PartList psl)
 {
     sl.PartLists.Add(psl);
 }