/// <summary>
        /// Save the service order in the database
        /// </summary>
        /// <param name="formCollection">Values entered in the form</param>
        /// <param name="businessApplicationId">Business application identifier</param>
        /// <param name="userName">User name for auditing</param>
        public static void AddServiceOrder(FormCollection formCollection, Guid businessApplicationId, string userName)
        {
            //Get the fields definition for the business application
            Fields fieldBusinessApplication = CacheHandler.Get(String.Format("Field{0}", businessApplicationId),
                                            () => DynamicFormEngine.GetFields(businessApplicationId));

            Guid serviceOrderId = Guid.NewGuid();
            using (VestalisEntities ctx = new VestalisEntities())
            {
                IList<FormDefinition> businessFormDefinition =
                    (from formDefinition in ctx.FormDefinitions
                     where
                         formDefinition.BusinessApplicationId == businessApplicationId &&
                         formDefinition.IsDeleted == false
                     orderby formDefinition.FormOrder
                     select formDefinition).ToList();

                //Set the service Order information
                ServiceOrder serviceOrder = new ServiceOrder();
                SetServiceOrderToSave(businessApplicationId, serviceOrder, serviceOrderId, userName, businessFormDefinition);

                //Add the service order to the context
                ctx.ServiceOrders.AddObject(serviceOrder);

                foreach (var formDefinition in businessFormDefinition.Where(item => item.CatalogueValue.CatalogueValueData != FormType.ServiceOrder.ToString()).OrderBy(data => data.FormOrder))
                {
                    //Set the inspection report information
                    InspectionReport inspectionReport = new InspectionReport();
                    SetInspectionReportToSaveOrder(serviceOrderId, userName, inspectionReport, formDefinition);
                    //Add the inspection report to the context
                    ctx.InspectionReports.AddObject(inspectionReport);
                }

                //For each field form
                foreach (var formCollectionValue in formCollection.Keys)
                {
                    FillFormValue(ctx, serviceOrderId, userName, formCollection, formCollectionValue, fieldBusinessApplication);
                }
                ctx.SaveChanges();
            }
        }
 /// <summary>
 /// Set the new Inspection report to be saved
 /// </summary>
 /// <param name="serviceOrderId">Service order identifier</param>
 /// <param name="userName">User name for auditing</param>
 /// <param name="inspectionReport">Inspection report to be inserted in the database</param>
 /// <param name="formDefinition">Form definition data to be inserted in the Inspection report</param>
 private static void SetInspectionReportToSaveOrder(Guid serviceOrderId, string userName, InspectionReport inspectionReport, FormDefinition formDefinition)
 {
     inspectionReport.InspectionReportId = Guid.NewGuid();
     inspectionReport.ServiceOrderId = serviceOrderId;
     inspectionReport.StatusCode = ConstantApplication.InspectionReportPendingPublish;
     inspectionReport.XmlFormDefinitionInstance = formDefinition.XmlFormDefinition;
     inspectionReport.IsClientVisible = formDefinition.IsClientVisible;
     inspectionReport.FormName = formDefinition.FormName;
     inspectionReport.CreationBy = userName;
     inspectionReport.CreationDate = DateTime.UtcNow;
     inspectionReport.ModificationBy = userName;
     inspectionReport.ModificationDate = DateTime.UtcNow;
     inspectionReport.FormOrder = formDefinition.FormOrder;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the InspectionReports EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToInspectionReports(InspectionReport inspectionReport)
 {
     base.AddObject("InspectionReports", inspectionReport);
 }
 /// <summary>
 /// Create a new InspectionReport object.
 /// </summary>
 /// <param name="inspectionReportId">Initial value of the InspectionReportId property.</param>
 /// <param name="serviceOrderId">Initial value of the ServiceOrderId property.</param>
 /// <param name="xmlFormDefinitionInstance">Initial value of the XmlFormDefinitionInstance property.</param>
 /// <param name="isClientVisible">Initial value of the IsClientVisible 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>
 /// <param name="formOrder">Initial value of the FormOrder property.</param>
 public static InspectionReport CreateInspectionReport(global::System.Guid inspectionReportId, global::System.Guid serviceOrderId, global::System.String xmlFormDefinitionInstance, global::System.Boolean isClientVisible, global::System.String creationBy, global::System.DateTime creationDate, global::System.Boolean isDeleted, global::System.Int32 formOrder)
 {
     InspectionReport inspectionReport = new InspectionReport();
     inspectionReport.InspectionReportId = inspectionReportId;
     inspectionReport.ServiceOrderId = serviceOrderId;
     inspectionReport.XmlFormDefinitionInstance = xmlFormDefinitionInstance;
     inspectionReport.IsClientVisible = isClientVisible;
     inspectionReport.CreationBy = creationBy;
     inspectionReport.CreationDate = creationDate;
     inspectionReport.IsDeleted = isDeleted;
     inspectionReport.FormOrder = formOrder;
     return inspectionReport;
 }
 /// <summary>
 /// Get an inspection report object by name
 /// </summary>
 /// <param name="inspectionReportName">Inspection report name</param>
 /// <param name="serviceOrderId">Id of service order</param>
 /// <returns>InspectionReport</returns>
 public static InspectionReport GetInspectionReportByName(string inspectionReportName, Guid serviceOrderId)
 {
     InspectionReport inspectionReport = new InspectionReport();
     using (VestalisEntities context = new VestalisEntities())
     {
         inspectionReport = context.InspectionReports
             .Where(data => data.ServiceOrderId == serviceOrderId
                 && data.FormName == inspectionReportName
                 && data.IsDeleted == false)
             .FirstOrDefault();
     }
     return inspectionReport;
 }