/// <summary>
        /// Method to get item by itemid
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public ItemModel GetItemByItemId(int itemId)
        {
            // Creating instance of the string builder class to hold the tracing data
            StringBuilder objStringBuilderTrace = new StringBuilder();
            ItemModel itemMl = new ItemModel();

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("Start method : GetItemByItemId(int itemId) :: JetstreamLaunchpad.DAL.Item Class : DBItem.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            // Clear StringBuilder object
            objStringBuilderTrace.Clear();
            var item = ObjTersoDataContext.items.FirstOrDefault(x => x.ItemId == itemId);
            if (item != null)
            {
                itemMl.ItemId = item.ItemId;
                itemMl.Material = item.Material;
                itemMl.Batch = item.Batch;
                itemMl.ExpirationDate = item.ExpirationDate;
                itemMl.ExpirationDateString = item.ExpirationDate != null ? String.Format("{0:MM/dd/yyyy}", item.ExpirationDate.Value) : string.Empty;
                itemMl.EPC = item.EPC;
            }

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("End method : GetItemByItemId(int itemId) :: JetstreamLaunchpad.DAL.Item Class : DBItem.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            objStringBuilderTrace.Clear();
            return itemMl;
        }
        /// <summary>
        /// Method to save item
        /// </summary>
        /// <param name="itemMl"></param>
        /// <returns></returns>
        public int SaveItem(ItemModel itemMl)
        {
            // Creating instance of the string builder class to hold the tracing data
            StringBuilder objStringBuilderTrace = new StringBuilder();
            item objItem = new item();

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("Start method : SaveItem(ItemModel itemML) :: JetstreamLaunchpad.DAL.Item class : DBItem.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            // Clear StringBuilder object
            objStringBuilderTrace.Clear();

            objItem.Material = itemMl.Material;
            objItem.Batch = itemMl.Batch;
            objItem.ExpirationDate = itemMl.ExpirationDate;
            objItem.EPC = itemMl.EPC;

            ObjTersoDataContext.items.Add(objItem);
            ObjTersoDataContext.SaveChanges();
            var itemId = itemMl.ItemId;

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("End method : SaveItem(ItemModel itemML) :: JetstreamLaunchpad.DAL.Item class : DBItem.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            objStringBuilderTrace.Clear();
            return itemId;
        }
        public void AddItem_OnPost_WithSessionNotNullAndAddCommand_ExpectOk()
        {
            //Arrange
            String expectedAction = "ItemDetail";
            _controllerContext.Setup(cc => cc.HttpContext.Session["UserName"]).Returns(string.Format("{0} {1}", "FName", "LName"));
            _itemController.ControllerContext = _controllerContext.Object;
            Random rNo = new Random();
            string randomNumber = rNo.Next().ToString();
            ItemModel itemML = new ItemModel
            {
                Material = "TestMaterial",
                Batch = "TestBatch",
                ExpirationDate = DateTime.Now,
                EPC = string.Format("TestEPC{0}", randomNumber),
            };

            //Act
            var redirectToRouteResult = _itemController.AddItem(itemML, "Add") as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(redirectToRouteResult, "Not a redirect result");
            Assert.IsFalse(redirectToRouteResult.Permanent); // Or IsTrue if you use RedirectToActionPermanent
            Assert.AreEqual(expectedAction, redirectToRouteResult.RouteValues["Action"]);
        }
 /// <summary>
 /// Get ITem with EPC tag
 /// </summary>
 /// <param name="itemMl"></param>
 /// <returns></returns>
 public int GetItemModelByEpc(ItemModel itemMl)
 {
     ServiceData serviceData = new ServiceData();
     try
     {
         int result;
         using (DBItem objDbItem = new DBItem())
         {
             result = objDbItem.GetItemModelByEpc(itemMl);
         }
         return result;
     }
     catch (Exception ex)
     {
         serviceData.Result = false;
         serviceData.ErrorMessage = ex.Message;
         serviceData.ErrorDetails = ex.ToString();
         throw new FaultException<ServiceData>(serviceData);
     }
 }
        /// <summary>
        /// Method to update item 
        /// </summary>
        /// <param name="itemMl"></param>
        /// <returns></returns>
        public int UpdateItem(ItemModel itemMl)
        {
            // Creating instance of the string builder class to hold the tracing data
            StringBuilder objStringBuilderTrace = new StringBuilder();
            int itemId = 0;

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("Start method : UpdateItem(ItemModel itemML) :: JetstreamLaunchpad.DAL.Item class : DBItem.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            // Clear StringBuilder object
            objStringBuilderTrace.Clear();

            var objItem = ObjTersoDataContext.items.FirstOrDefault(x => x.ItemId == itemMl.ItemId);
            if (objItem != null)
            {
                objItem.ItemId = itemMl.ItemId;
                objItem.Material = itemMl.Material;
                objItem.Batch = itemMl.Batch;
                objItem.ExpirationDate = itemMl.ExpirationDate;
                objItem.EPC = itemMl.EPC;
            }

            //objTersoDataContext.Entry(objUserDetail).State = System.Data.Entity.EntityState.Modified;
            ObjTersoDataContext.SaveChanges();
            if (objItem != null) itemId = objItem.ItemId;

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("End method : UpdateItem(ItemModel itemML) :: JetstreamLaunchpad.DAL.Item class : DBItem.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            objStringBuilderTrace.Clear();
            return itemId;
        }
        /// <summary>
        /// Get Item by EPC
        /// </summary>
        /// <param name="itemMl"></param>
        /// <returns></returns>
        public int GetItemModelByEpc(ItemModel itemMl)
        {
            // Creating instance of the string builder class to hold the tracing data
            StringBuilder objStringBuilderTrace = new StringBuilder();

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("Start method : GetItemModelByEPC(ItemModel itemML) :: JetstreamLaunchpad.DAL.Login class : DBLogin.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            // Clear StringBuilder object
            objStringBuilderTrace.Clear();

            var item =
                ObjTersoDataContext.items.Where(x => x.EPC.Equals(itemMl.EPC) && x.ItemId != itemMl.ItemId).ToList();
            var result = item.Count > 0 ? item.Count : 0;

            // Append tracing into string builder
            objStringBuilderTrace.AppendLine("End method : GetItemModelByEPC(ItemModel itemML) :: JetstreamLaunchpad.DAL.Login class : DBLogin.cs");
            // Writing trace log into the log file 
            SaveLogger.SaveLoggerTrace(objStringBuilderTrace.ToString());
            objStringBuilderTrace.Clear();
            return result;
        }
        public ActionResult AddItem(ItemModel itemMl, string command)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null)
                {
                    if (string.Equals(command, JetstreamResource.AddCommand))
                    {
                        var duplicateItem = CheckForDuplicate(itemMl);
                        if (duplicateItem > 0)
                        {
                            if (duplicateItem == 1)
                            {
                                Warning(JetstreamResource.DuplicateItemMessage, true);
                            }
                            return RedirectToAction("RedirectToAddItem", new RouteValueDictionary(itemMl));
                        }
                        else
                        {
                            if (ModelState.IsValid)
                            {
                                itemMl.ExpirationDate = itemMl.ExpirationDate + DateTime.Now.TimeOfDay;

                                using (JetstreamClient objMainServiceClient = new JetstreamClient())
                                {
                                    objMainServiceClient.SaveItem(itemMl);
                                }
                                Success(string.Format(JetstreamResource.ItemSaveMessage, itemMl.EPC), true);
                                return RedirectToAction("ItemDetail", "Item");
                            }
                            else
                            {
                                return RedirectToAction("RedirectToAddItem", new RouteValueDictionary(itemMl));
                            }
                        }
                    }
                    else
                    {
                        return RedirectToAction("ItemDetail", "Item");
                    }
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : AddItem(ItemModel itemMl, string command) :: ItemController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", fex.Detail.ErrorMessage, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, fex.Detail.ErrorDetails);

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : AddItem(ItemModel itemMl, string command) :: ItemController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
        }
 /// <summary>
 /// Action to redirect to edit view with filled object
 /// </summary>
 /// <param name="itemMl"></param>
 /// <returns></returns>
 public ActionResult RedirectToEditItem(ItemModel itemMl)
 {
     ViewBag.Name = itemMl.Material;
     return View("EditItem", itemMl);
 }
 /// <summary>
 /// Action to redirect to add view with filled object
 /// </summary>
 /// <param name="itemMl"></param>
 /// <returns></returns>
 public ActionResult RedirectToAddItem(ItemModel itemMl)
 {
     return View("AddItem", itemMl);
 }
        /// <summary>
        /// Check for duplicate Item
        /// </summary>
        /// <param name="itemMl"></param>
        /// <returns></returns>
        static int CheckForDuplicate(ItemModel itemMl)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            int duplicateItem = 0;       //Cannot remove initialization of variable to zero.giving error "Use of unassigned local variable 'duplicateItem'"
            try
            {
                using (JetstreamClient objMainServiceClient = new JetstreamClient())
                {
                    if (objMainServiceClient.GetItemModelByEpc(itemMl) > 0)
                    {
                        duplicateItem = 1;
                    }
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : CheckForDuplicate(ItemModel itemMl) :: ItemController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", fex.Detail.ErrorMessage, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, fex.Detail.ErrorDetails);
                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : CheckForDuplicate(ItemModel itemMl) :: ItemController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());                
            }
            return duplicateItem;
        }
        public ActionResult EditItem(ItemModel itemMl, string command)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null && itemMl != null)
                {
                    if (string.Equals(command, JetstreamResource.UpdateCommand))
                    {
                        return RedirectToAction("UpdateItem", new RouteValueDictionary(itemMl));
                    }
                    else if (string.Equals(command, JetstreamResource.CancelCommand))
                    {
                        return RedirectToAction("ItemDetail", "Item");
                    }
                    else if (string.Equals(command, JetstreamResource.DeleteCommand))
                    {
                        return RedirectToAction("DeleteItem", new { itemId = itemMl.ItemId });
                    }
                    else
                    {
                        return RedirectToAction("ItemDetail", "Item");
                    }
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : EditItem(ItemModel itemMl, string command) :: ItemController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", fex.Detail.ErrorMessage, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, fex.Detail.ErrorDetails);
                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : EditItem(ItemModel itemMl, string command) :: ItemController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
        }
        public void RedirectToEditItem_WithItemModel_ExpectOk()
        {
            //Arrange
            String expectedView = "EditItem";
            ItemModel itemML = new ItemModel
            {
                ItemId = 52,
                Material = "TestMaterial",
                Batch = "TestBatch",
                ExpirationDate = DateTime.Now,
                EPC = "DSN505EPC1",
            };

            //Act
            var actionResult = _itemController.RedirectToEditItem(itemML) as ViewResult;

            //Assert
            Assert.IsNotNull(actionResult, "RedirectToEditItem action result should not be null");
            Assert.AreEqual(actionResult.ViewName, expectedView, "View name should be EditItem");
        }
        public void UpdateItem_OnPost_WithSessionIsNull_UserModelAndCommand_ExpectUserLogin()
        {
            //Arrange
            String expectedController = "Login";
            String expectedAction = "UserLogin";
            _controllerContext.Setup(cc => cc.HttpContext.Session["UserName"]).Returns(null);
            _itemController.ControllerContext = _controllerContext.Object;
            ItemModel itemML = new ItemModel
            {
                ItemId = 52,
                Material = "TestMaterial",
                Batch = "TestBatch",
                ExpirationDate = DateTime.Now,
                EPC = "TestItemOne",
            };

            //Act
            var redirectToRouteResult = _itemController.UpdateItem(itemML) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(redirectToRouteResult, "Not a redirect result");
            Assert.IsFalse(redirectToRouteResult.Permanent); // Or IsTrue if you use RedirectToActionPermanent
            Assert.AreEqual(expectedAction, redirectToRouteResult.RouteValues["Action"]);
            Assert.AreEqual(expectedController, redirectToRouteResult.RouteValues["Controller"]);
        }
        public void UpdateItem_WithSessionNotNullAndItemModel__ExpectOk()
        {
            //Arrange
            String expectedAction = "ItemDetail";
            _controllerContext.Setup(cc => cc.HttpContext.Session["UserName"]).Returns(string.Format("{0} {1}", "FName", "LName"));
            _itemController.ControllerContext = _controllerContext.Object;
            ItemModel itemML = new ItemModel
            {
                ItemId = 52,
                Material = "TestMaterial",
                Batch = "TestBatch",
                ExpirationDate = DateTime.Now,
                EPC = "TestItemOne",
            };

            //Act
            var redirectToRouteResult = _itemController.UpdateItem(itemML) as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(redirectToRouteResult, "Not a redirect result");
            Assert.IsFalse(redirectToRouteResult.Permanent); // Or IsTrue if you use RedirectToActionPermanent
            Assert.AreEqual(expectedAction, redirectToRouteResult.RouteValues["Action"]);
        }