/// <summary>
        /// Create a new Inspection report item
        /// </summary>
        /// <param name="parameters">Parameters to save the information</param>
        public static Guid AddInspectionReport(ParameterSaveInspectionReport parameters)
        {
            Guid inspectionReportId = GetInspectionReportByName(parameters.InspectionReportName, parameters.ServiceOrderId).InspectionReportId;
            Fields fieldBusinessApplication = CacheHandler.Get(String.Format("Field{0}", parameters.BusinessApplicationId),
                                            () => DynamicFormEngine.GetFields(parameters.BusinessApplicationId));

            string fieldName = string.Empty;
            string valData = string.Empty;
            Guid inspectionReportItemId = Guid.NewGuid();
            InspectionReportItem inspectionReportItem = null;
            using (VestalisEntities context = new VestalisEntities())
            {
                inspectionReportItem = new InspectionReportItem
                {
                    InspectionReportId = inspectionReportId,
                    StatusCode = ConstantApplication.InspectionReportPendingPublish,
                    CreationBy = parameters.UserName,
                    CreationDate = DateTime.UtcNow,
                    InspectionReportItemId = inspectionReportItemId
                };
                context.InspectionReportItems.AddObject(inspectionReportItem);

                AddApprovalItem(parameters, inspectionReportItemId, context, inspectionReportItem);

                foreach (var formCollectionValue in parameters.FormCollection.Keys)
                {
                    fieldName = formCollectionValue.ToString();
                    ValueProviderResult val = parameters.FormCollection.GetValue(fieldName);
                    if (val != null)
                    {
                        //Get the form value for the specific field
                        valData = val.AttemptedValue.ToString();
                        if (!String.IsNullOrEmpty(valData))
                        {
                            //Set the form value
                            FormValue formValue = SetFormValueToInspectionReport(parameters, inspectionReportItemId, fieldName, valData,
                                                                          fieldBusinessApplication);
                            //Add the form value to the context
                            context.FormValues.AddObject(formValue);
                        }
                    }
                }
                context.SaveChanges();
            }
            return inspectionReportItem.InspectionReportItemId;
        }
 /// <summary>
 /// Create a new InspectionReportItem object.
 /// </summary>
 /// <param name="inspectionReportItemId">Initial value of the InspectionReportItemId 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 InspectionReportItem CreateInspectionReportItem(global::System.Guid inspectionReportItemId, global::System.String creationBy, global::System.DateTime creationDate, global::System.Boolean isDeleted)
 {
     InspectionReportItem inspectionReportItem = new InspectionReportItem();
     inspectionReportItem.InspectionReportItemId = inspectionReportItemId;
     inspectionReportItem.CreationBy = creationBy;
     inspectionReportItem.CreationDate = creationDate;
     inspectionReportItem.IsDeleted = isDeleted;
     return inspectionReportItem;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the InspectionReportItems EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToInspectionReportItems(InspectionReportItem inspectionReportItem)
 {
     base.AddObject("InspectionReportItems", inspectionReportItem);
 }
        /// <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);
            }
        }