/// <summary>
        /// Validate or publish all inspection reports
        /// </summary>
        /// <param name="collection">Form collection</param>
        /// <returns>PartialViewResult</returns>
        public PartialViewResult PublishValidateSelectedInspectionReports(string selectedInspectionReports,FormCollection collection)
        {
            string inspectionReportName = Session["InspectionReportName"].ToString();
            string selectedOption = string.Empty;
            Guid serviceOrderReportId = new Guid(Session["serviceOrderReportId"].ToString());
            List<Guid?> selectedInspectionReportIds = new List<Guid?>();
            string[] selectedIds = selectedInspectionReports.Split(new string[] { "&&&" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in selectedIds)
            {
                selectedInspectionReportIds.Add(new Guid(item));
            }

            if (collection.AllKeys.Contains("inspectionReportItemId"))
                collection.Remove("inspectionReportItemId");

            if (collection.AllKeys.Contains("selectedOption"))
            {
                selectedOption = collection["selectedOption"].ToString();
                collection.Remove("selectedOption");
            }

            bool isPublish = selectedOption == "publish";

            ParameterPublishValidateInspectionReports parameters = new ParameterPublishValidateInspectionReports
            {
                InspectionReportName = inspectionReportName,
                ServiceOrderId = serviceOrderReportId,
                RolesForUser = Roles.GetRolesForUser(UserName).ToList(),
                UserName = UserName,
                IsPublish = isPublish,
                SelectedIds = selectedInspectionReportIds
            };

            InspectionReportBusiness.PublishValidateSelected(parameters);

            return SearchInspectionReport(null, inspectionReportName, collection);
        }
        /// <summary>
        /// Validate or publish all inspection report items
        /// </summary>
        /// <param name="parameters">Parameters of PublishValidateSelected method</param>
        public static void PublishValidateSelected(ParameterPublishValidateInspectionReports parameters)
        {
            Guid inspectionReportId = GetInspectionReportByName(parameters.InspectionReportName, parameters.ServiceOrderId).InspectionReportId;
            List<ApprovalItem> approvalItems = null;
            int approvalLevel = 0;
            Guid inspectionReportItemId = Guid.Empty;
            using (VestalisEntities context = new VestalisEntities())
            {
                approvalItems = GetApprovalItems(context, parameters.RolesForUser,parameters.SelectedIds).Where(data => data.ApprovalStatus == (int)ApprovalStatus.Ready
                    && data.CanPublish == parameters.IsPublish).ToList();
                foreach (ApprovalItem approvalItem in approvalItems)
                {
                    approvalLevel = 0;
                    approvalLevel = approvalItem.ApprovalLevel;
                    approvalItem.ApprovalStatus = (int)ApprovalStatus.Completed;
                    approvalItem.ModificationBy = parameters.UserName;
                    approvalItem.ModificationDate = DateTime.UtcNow;

                    inspectionReportItemId = approvalItem.InspectionReportItemId.GetValueOrDefault();

                    //Get the next approval item
                    ApprovalItem approvalItem2 = (from appItem in context.ApprovalItems
                                                  where appItem.InspectionReportItemId == inspectionReportItemId
                                                         && appItem.IsDeleted == false
                                                         && appItem.ApprovalStatus == (int)ApprovalStatus.Waiting
                                                         && appItem.ApprovalLevel == (approvalLevel + 1)
                                                  select appItem).FirstOrDefault();

                    //if exist, update the status
                    if (approvalItem2 != null)
                    {
                        approvalItem2.ApprovalStatus = (int)ApprovalStatus.Ready;
                        approvalItem2.ModificationBy = parameters.UserName;
                        approvalItem2.ModificationDate = DateTime.UtcNow;
                    }

                    //update the inspection report item with the current completed level
                    InspectionReportItem currentInspectionReportItem = (from inspectionReportItem in context.InspectionReportItems
                                                                        where inspectionReportItem.InspectionReportItemId == inspectionReportItemId
                                                                        select inspectionReportItem).FirstOrDefault();

                    currentInspectionReportItem.CurrentCompletedLevel = approvalLevel;
                    currentInspectionReportItem.ModificationBy = parameters.UserName;
                    currentInspectionReportItem.ModificationDate = DateTime.UtcNow;
                    if (approvalItem.CanPublish.GetValueOrDefault())
                    {
                        currentInspectionReportItem.PublicationDate = DateTime.UtcNow;
                    }

                }
                //save all changes
                context.SaveChanges();
            }
        }