/// <summary>
        /// Update devices to database
        /// </summary>
        /// <param name="deviceMl"></param>
        /// <returns></returns>
        public ActionResult UpdateDevice(DeviceModel deviceMl)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null)
                {
                    var duplicateLogicalDeviceId = CheckForDuplicateId(deviceMl);
                    var duplicateSerial = CheckForDuplicateSerial(deviceMl);

                    if (duplicateLogicalDeviceId == 1 || duplicateSerial == 1)
                    {
                        Warning(JetstreamResource.DuplicateAddDeviceMessage, true);
                        return RedirectToAction("RedirectToEditDevice", new RouteValueDictionary(deviceMl));
                    }
                    else
                    {
                        if (ModelState.IsValid)
                        {
                            string oldLogicalDeviceId;
                            using (JetstreamClient objMainServiceClient = new JetstreamClient())
                            {
                                DeviceModel dm = objMainServiceClient.GetDeviceByDeviceId(deviceMl.DeviceId);
                                oldLogicalDeviceId = dm.LogicalDeviceId;
                            }

                            // construct a Jetstream service client
                            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                            RemoveLogicalDeviceRequest removeRequest = new RemoveLogicalDeviceRequest
                            {
                                LogicalDeviceId = oldLogicalDeviceId
                            };

                            //Remove device from jetstream
                            client.RemoveLogicalDevice(removeRequest);

                            using (JetstreamClient objMainServiceClient = new JetstreamClient())
                            {
                                objMainServiceClient.UpdateDevice(deviceMl);
                            }

                            AddLogicalDeviceRequest addRequest = new AddLogicalDeviceRequest
                            {
                                DeviceDefinitionId = deviceMl.DeviceGuid,
                                DeviceSerialNumber = deviceMl.SerialNumber,
                                LogicalDeviceId = deviceMl.LogicalDeviceId,
                                Region = (Regions)Enum.Parse(typeof(Regions), deviceMl.Region)
                            };

                            // call the Jetstream GetDeviceDefinitions ReST endpoint
                            AddLogicalDeviceResponse addResponse = client.AddLogicalDevice(addRequest);

                            Success(string.Format(JetstreamResource.DeviceSaveMessage, deviceMl.LogicalDeviceId, addResponse.Id), true);
                            return RedirectToAction("DeviceDetail", "Device");
                        }
                        else
                        {
                            return RedirectToAction("RedirectToEditDevice", new RouteValueDictionary(deviceMl));
                        }
                    }
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : UpdateDevice(DeviceModel deviceMl) :: DeviceController");
                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 : UpdateDevice(DeviceModel deviceMl) :: DeviceController");
                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 ActionResult EditDevice(DeviceModel deviceMl, string command, SortingPagingInfo info)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null && deviceMl != null)
                {
                    if (string.Equals(command, JetstreamResource.UpdateCommand))
                    {
                        return RedirectToAction("EditCurrentDevice", new { deviceId = deviceMl.DeviceId });
                    }
                    else if (string.Equals(command, JetstreamResource.CancelCommand))
                    {
                        return RedirectToAction("DeviceDetail", "Device");
                    }
                    else if (string.Equals(command, JetstreamResource.DeleteCommand))
                    {
                        return RedirectToAction("DeleteDevice", new { deviceId = deviceMl.DeviceId });
                    }
                    else
                    {
                        using (JetstreamClient objMainServiceClient = new JetstreamClient())
                        {
                            deviceMl = objMainServiceClient.GetDeviceByDeviceId(deviceMl.DeviceId);
                        }

                        deviceMl.Inventories = ApplySortingInventory(info, deviceMl.Inventories).Take(Constants.PagingPageSize).ToList();

                        ViewBag.SortingPagingInfo = info;
                        return View("EditDevice", deviceMl);
                    }
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : EditDevice(DeviceModel deviceMl, string command, SortingPagingInfo info) :: DeviceController");
                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 : EditDevice(DeviceModel deviceMl, string command, SortingPagingInfo info) :: DeviceController");
                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>
        /// Edit current Device
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public ActionResult EditCurrentDevice(int deviceId)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                DeviceModel deviceMl;
                using (JetstreamClient objMainServiceClient = new JetstreamClient())
                {
                    deviceMl = objMainServiceClient.GetDeviceByDeviceId(deviceId);
                }

                // construct a Jetstream service client
                JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                // create the GetDeviceDefinitions request
                GetDeviceDefinitionsRequest request = new GetDeviceDefinitionsRequest();

                // call the Jetstream GetDeviceDefinitions ReST endpoint
                GetDeviceDefinitionsResponse response = client.GetDeviceDefinitions(request);

                if (!string.IsNullOrEmpty(response.Body))
                {
                    //Get the device definations for binding to model dropdown
                    var modelList = response.DeviceDefinitionList.Select(model => new Models
                    {
                        ModelId = model.Id,
                        Name = model.Name
                    }).ToList();

                    ViewBag.ModelList = new SelectList(modelList.OrderBy(x => x.Name), "ModelId", "Name", deviceMl.DeviceGuid);
                }

                return View("EditCurrentDevice", deviceMl);
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : EditCurrentDevice(int deviceId) :: DeviceController");
                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 : EditCurrentDevice(int deviceId) :: DeviceController");
                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 ActionResult EditDevice(int deviceId)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null && deviceId != 0)
                {
                    DeviceModel deviceMl;
                    using (JetstreamClient objMainServiceClient = new JetstreamClient())
                    {
                        deviceMl = objMainServiceClient.GetDeviceByDeviceId(deviceId);
                    }
                    SortingPagingInfo info = new SortingPagingInfo
                   {
                       SortField = Enums.InventorySortField.Material.ToString(),
                       SortDirection = Constants.Ascending,
                       SearchText = string.Empty
                   };
                    deviceMl.Inventories = deviceMl.Inventories.OrderBy(x => x.Material).Take(Constants.PagingPageSize).ToList();
                    ViewBag.SortingPagingInfo = info;
                    return View("EditDevice", deviceMl);
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : EditDevice(int deviceId) :: DeviceController");
                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 : EditDevice(int deviceId) :: DeviceController");
                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");
            }
        }