/// <summary>
 /// Create a new ApprovalItem object.
 /// </summary>
 /// <param name="approvalItemId">Initial value of the ApprovalItemId property.</param>
 /// <param name="roleName">Initial value of the RoleName property.</param>
 /// <param name="approvalLevel">Initial value of the ApprovalLevel property.</param>
 /// <param name="creationBy">Initial value of the CreationBy property.</param>
 /// <param name="creationDate">Initial value of the CreationDate property.</param>
 /// <param name="isDeleted">Initial value of the IsDeleted property.</param>
 public static ApprovalItem CreateApprovalItem(global::System.Guid approvalItemId, global::System.String roleName, global::System.Int32 approvalLevel, global::System.String creationBy, global::System.DateTime creationDate, global::System.Boolean isDeleted)
 {
     ApprovalItem approvalItem = new ApprovalItem();
     approvalItem.ApprovalItemId = approvalItemId;
     approvalItem.RoleName = roleName;
     approvalItem.ApprovalLevel = approvalLevel;
     approvalItem.CreationBy = creationBy;
     approvalItem.CreationDate = creationDate;
     approvalItem.IsDeleted = isDeleted;
     return approvalItem;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the ApprovalItems EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToApprovalItems(ApprovalItem approvalItem)
 {
     base.AddObject("ApprovalItems", approvalItem);
 }
        /// <summary>
        /// Add approval items
        /// </summary>
        /// <param name="parameterSaveInspectionReport">parameterSaveInspectionReport</param>
        /// <param name="inspectionReportItemId">inspectionReportItemId</param>
        /// <param name="context">Data base context</param>
        /// <param name="inspectionReportItem">inspectionReportItem</param>
        private static void AddApprovalItem(ParameterSaveInspectionReport parameterSaveInspectionReport, Guid inspectionReportItemId, VestalisEntities context, InspectionReportItem inspectionReportItem)
        {
            ApprovalItem approvalItem = null;

            List<ValidationRole> roles = (from role in context.ValidationRoles
                                          where role.BusinessApplicationId == parameterSaveInspectionReport.BusinessApplicationId
                                          && role.IsDeleted == false
                                          select role).ToList();

            foreach (ValidationRole role in roles)
            {
                approvalItem = new ApprovalItem
                {
                    ApprovalLevel = role.RoleLevel,
                    CanPublish = role.CanPublish,
                    CreationBy = parameterSaveInspectionReport.UserName,
                    CreationDate = DateTime.UtcNow,
                    InspectionReportItemId = inspectionReportItemId,
                    IsReadOnly = role.IsReadOnly,
                    RoleName = role.RoleName,
                    ApprovalStatus = (int)ApprovalStatus.Waiting
                };
                if (role.RoleLevel == 1)
                {
                    if (!role.CanPublish.GetValueOrDefault())
                    {
                        approvalItem.ApprovalStatus = (int)ApprovalStatus.Completed;
                        inspectionReportItem.CurrentCompletedLevel = 1;
                    }
                    else
                    {
                        approvalItem.ApprovalStatus = (int)ApprovalStatus.Ready;
                    }
                }
                if (role.RoleLevel == 2)
                {
                    approvalItem.ApprovalStatus = (int)ApprovalStatus.Ready;
                }
                context.ApprovalItems.AddObject(approvalItem);
            }
        }
        private static void EditApprovalItem(ParameterSaveInspectionReport parameters)
        {
            using (VestalisEntities context = new VestalisEntities())
            {
                InspectionReportItem inspectionReportItem = context.InspectionReportItems.FirstOrDefault(data => data.InspectionReportItemId == parameters.InspectionReportItemId);
                List<ValidationRole> roles = null;
                ValidationRole userRole = null;
                List<ApprovalItem> approvalItems = null;
                ApprovalItem approvalItem = null;
                bool isRole1Completed = false;

                if (inspectionReportItem != null)
                {
                    int currentApprovalLevel = inspectionReportItem.CurrentCompletedLevel.GetValueOrDefault();

                    userRole = (from role in context.ValidationRoles
                                where role.BusinessApplicationId == parameters.BusinessApplicationId
                                && role.IsDeleted == false && parameters.RolesForUser.Contains(role.RoleName)
                                select role).FirstOrDefault();

                    if (userRole.RoleLevel == 1)
                    {

                        roles = (from role in context.ValidationRoles
                                 where role.BusinessApplicationId == parameters.BusinessApplicationId
                                 && role.IsDeleted == false
                                 orderby role.RoleLevel
                                 select role).ToList();

                        approvalItems = (from appItems in context.ApprovalItems
                                         where appItems.InspectionReportItemId == parameters.InspectionReportItemId
                                         && appItems.IsDeleted == false
                                         select appItems).ToList();

                    }
                    else
                    {
                        roles = (from role in context.ValidationRoles
                                 where role.BusinessApplicationId == parameters.BusinessApplicationId
                                 && role.IsDeleted == false && role.RoleLevel >= currentApprovalLevel
                                 orderby role.RoleLevel
                                 select role).ToList();

                        approvalItems = (from appItems in context.ApprovalItems
                                         where appItems.InspectionReportItemId == parameters.InspectionReportItemId
                                         && appItems.IsDeleted == false && appItems.ApprovalLevel >= currentApprovalLevel
                                         select appItems).ToList();

                    }

                    foreach (ApprovalItem appItem in approvalItems)
                    {
                        appItem.IsDeleted = true;
                        appItem.ModificationBy = parameters.UserName;
                        appItem.ModificationDate = DateTime.UtcNow;
                    }

                    foreach (ValidationRole role in roles)
                    {
                        approvalItem = new ApprovalItem
                        {
                            ApprovalLevel = role.RoleLevel,
                            CanPublish = role.CanPublish,
                            CreationBy = parameters.UserName,
                            CreationDate = DateTime.UtcNow,
                            InspectionReportItemId = parameters.InspectionReportItemId,
                            IsReadOnly = role.IsReadOnly,
                            RoleName = role.RoleName,
                            ApprovalStatus = (int)ApprovalStatus.Waiting
                        };
                        if (role.RoleLevel == 1)
                        {
                            if (!role.CanPublish.GetValueOrDefault())
                            {
                                approvalItem.ApprovalStatus = (int)ApprovalStatus.Completed;
                                inspectionReportItem.CurrentCompletedLevel = 1;
                                inspectionReportItem.ModificationBy = parameters.UserName;
                                inspectionReportItem.ModificationDate = DateTime.UtcNow;
                                isRole1Completed = true;
                            }
                            else
                            {
                                approvalItem.ApprovalStatus = (int)ApprovalStatus.Ready;
                                isRole1Completed = false;
                            }

                        }
                        else if (isRole1Completed && role.RoleLevel == 2)
                        {
                            approvalItem.ApprovalStatus = (int)ApprovalStatus.Ready;
                        }
                        else if (role.RoleLevel == currentApprovalLevel)
                        {
                            approvalItem.ApprovalStatus = (int)ApprovalStatus.Ready;
                            inspectionReportItem.CurrentCompletedLevel = (currentApprovalLevel - 1);
                            inspectionReportItem.ModificationBy = parameters.UserName;
                            inspectionReportItem.ModificationDate = DateTime.UtcNow;
                        }
                        context.ApprovalItems.AddObject(approvalItem);
                    }
                }
                context.SaveChanges();
            }
        }
        public static void UnPublishInspectionReport(Guid inspectionReportItemId, string userName, List<string> userRoles)
        {
            using (VestalisEntities context = new VestalisEntities())
            {

                //Get approval item that have can publish equals true and approval status completed for the roles of the logged user
                ApprovalItem currentAppItem = (from appItem in context.ApprovalItems
                                               where
                                                   appItem.IsDeleted == false &&
                                                   userRoles.Contains(appItem.RoleName)
                                                   && appItem.InspectionReportItemId == inspectionReportItemId
                                                   && appItem.CanPublish == true &&
                                                   appItem.ApprovalStatus == (int)ApprovalStatus.Completed
                                               select appItem).FirstOrDefault();

                if (currentAppItem != null)
                {
                    //if the current inspection report item was published && and get the approval status completed, the system continue with the process
                    if (currentAppItem.InspectionReportItem.PublicationDate != null &&
                        currentAppItem.ApprovalStatus == (int)ApprovalStatus.Completed)
                    {
                        currentAppItem.IsDeleted = true;
                        currentAppItem.ModificationBy = userName;
                        currentAppItem.ModificationDate = DateTime.UtcNow;

                        ApprovalItem newAppItem = new ApprovalItem
                                                      {
                                                          InspectionReportItemId = inspectionReportItemId,
                                                          RoleName = currentAppItem.RoleName,
                                                          ApprovalLevel = currentAppItem.ApprovalLevel,
                                                          CanPublish = currentAppItem.CanPublish,
                                                          IsReadOnly = currentAppItem.IsReadOnly,
                                                          ApprovalStatus = (int)ApprovalStatus.Ready,
                                                          CreationBy = userName,
                                                          CreationDate = DateTime.UtcNow
                                                      };
                        context.ApprovalItems.AddObject(newAppItem);

                        InspectionReportItem currentInspectionReportItem =
                            (from inspectionReportItem in context.InspectionReportItems
                             where inspectionReportItem.InspectionReportItemId == inspectionReportItemId
                             select inspectionReportItem).FirstOrDefault();

                        if (currentInspectionReportItem != null)
                        {
                            currentInspectionReportItem.ModificationBy = userName;
                            currentInspectionReportItem.ModificationDate = DateTime.UtcNow;
                            currentInspectionReportItem.PublicationDate = null;
                            currentInspectionReportItem.CurrentCompletedLevel -= 1;
                        }

                    }
                }
                context.SaveChanges();
            }
        }