/// <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>
 /// Create a new ServiceOrder object.
 /// </summary>
 /// <param name="serviceOrderId">Initial value of the ServiceOrderId property.</param>
 /// <param name="businessApplicationId">Initial value of the BusinessApplicationId property.</param>
 /// <param name="xmlFormDefinitionInstance">Initial value of the XmlFormDefinitionInstance 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 ServiceOrder CreateServiceOrder(global::System.Guid serviceOrderId, global::System.Guid businessApplicationId, global::System.String xmlFormDefinitionInstance, global::System.String creationBy, global::System.DateTime creationDate, global::System.Boolean isDeleted)
 {
     ServiceOrder serviceOrder = new ServiceOrder();
     serviceOrder.ServiceOrderId = serviceOrderId;
     serviceOrder.BusinessApplicationId = businessApplicationId;
     serviceOrder.XmlFormDefinitionInstance = xmlFormDefinitionInstance;
     serviceOrder.CreationBy = creationBy;
     serviceOrder.CreationDate = creationDate;
     serviceOrder.IsDeleted = isDeleted;
     return serviceOrder;
 }
        /// <summary>
        /// Set the service order data
        /// </summary>
        /// <param name="businessApplicationId">Business application identifier</param>
        /// <param name="serviceOrder">Service order to be saved in the database</param>
        /// <param name="serviceOrderId">Service order identifier</param>
        /// <param name="userName">User name for auditing</param>
        /// <param name="businessFormDefinition">Object that contains the xml form definition to create the service order dynamic form</param>
        private static void SetServiceOrderToSave(Guid businessApplicationId, ServiceOrder serviceOrder, Guid serviceOrderId, string userName, IList<FormDefinition> businessFormDefinition)
        {
            FormDefinition serviceOrderFormDefinition =
                businessFormDefinition.FirstOrDefault(
                    item => item.CatalogueValue.CatalogueValueData == FormType.ServiceOrder.ToString());

            serviceOrder.ServiceOrderId = serviceOrderId;
            serviceOrder.BusinessApplicationId = businessApplicationId;
            serviceOrder.StatusCode = ConstantApplication.ServicePendingPublish;
            serviceOrder.XmlFormDefinitionInstance = serviceOrderFormDefinition.XmlFormDefinition;
            serviceOrder.CreationBy = userName;
            serviceOrder.CreationDate = DateTime.UtcNow;
            serviceOrder.ModificationDate = DateTime.UtcNow;
            serviceOrder.ModificationBy = userName;
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the ServiceOrders EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToServiceOrders(ServiceOrder serviceOrder)
 {
     base.AddObject("ServiceOrders", serviceOrder);
 }