/// <summary>
 /// Save the values entered in the service order
 /// </summary>
 /// <param name="ctx">EF context</param>
 /// <param name="serviceOrderId">Service order identifier</param>
 /// <param name="userName">User creator</param>
 /// <param name="formCollection"></param>
 /// <param name="formCollectionValue"></param>
 /// <param name="fieldBusinessApplication"></param>
 private static void FillFormValue(VestalisEntities ctx, Guid serviceOrderId, string userName, FormCollection formCollection, object formCollectionValue, Fields fieldBusinessApplication)
 {
     string fieldName;
     string valData;
     fieldName = formCollectionValue.ToString();
     ValueProviderResult val = 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 = SetFormValueToSaveOrder(serviceOrderId, userName, fieldName, valData,
                                                           fieldBusinessApplication);
             //Add the form value to the context
             ctx.FormValues.AddObject(formValue);
         }
     }
 }
        /// <summary>
        /// Update service order values
        /// </summary>
        /// <param name="formCollection">Service order values</param>
        /// <param name="ctx">Context</param>
        /// <param name="serviceOrderId">Service order identifier</param>
        /// <param name="userName">User that modifies the information</param>
        /// <param name="formCollectionValue"></param>
        /// <param name="fieldBusinessApplication"></param>
        private static void FillFormValuesEdit(FormCollection formCollection, VestalisEntities ctx, Guid serviceOrderId, string userName, object formCollectionValue, Fields fieldBusinessApplication)
        {
            string fieldName;
            string valData;
            fieldName = formCollectionValue.ToString();
            ValueProviderResult val = formCollection.GetValue(fieldName);
            //Get the form value of the service order according the field name
            FormValue formValueToEdit = (from formValues in ctx.FormValues
                                         where
                                             formValues.ServiceOrderId == serviceOrderId &&
                                             formValues.IsDeleted == false &&
                                             formValues.FieldName == fieldName
                                         select formValues).FirstOrDefault();

            if (val != null)
            {
                //Get the form value for the specific field
                valData = val.AttemptedValue.ToString();

                if (!String.IsNullOrEmpty(valData))
                {
                    if (formValueToEdit != null)
                    {
                        SetFormValueToEditOrder(formValueToEdit, valData, userName);
                    }
                    else
                    {
                        //Set the form value
                        FormValue formValue = SetFormValueToSaveOrder(serviceOrderId, userName, fieldName,
                                                                      valData,
                                                                      fieldBusinessApplication);
                        //Add the form value to the context
                        ctx.FormValues.AddObject(formValue);
                    }
                }
                //Case this field had a value in the past, but the user has updated now without value
                else if (formValueToEdit != null)
                {
                    formValueToEdit.IsDeleted = true;
                    formValueToEdit.ModificationBy = userName;
                    formValueToEdit.ModificationDate = DateTime.UtcNow;
                }
            }
        }
        /// <summary>
        /// Create a FormValue according each value entered in the form by the user. It is convert to the appropiated data type according the field type
        /// </summary>
        /// <param name="serviceOrderId">Service Order identifier</param>
        /// <param name="userName">User name for auditing</param>
        /// <param name="fieldName">Field name</param>
        /// <param name="valData">Form value</param>
        /// <param name="fieldBusinessApplication">Fields obtained from the XML </param>
        /// <returns>Form value to be inserted in the database</returns>
        private static FormValue SetFormValueToSaveOrder(Guid serviceOrderId, string userName, string fieldName, string valData, Fields fieldBusinessApplication)
        {
            FormValue formValue = new FormValue();
            formValue.ServiceOrderId = serviceOrderId;
            formValue.FieldName = fieldName;
            formValue.FormValueId = Guid.NewGuid();

            Field field = fieldBusinessApplication.Items.Single(x => x.FieldName == fieldName);

            if (field is FieldsSingleTextLineField)
            {
                formValue.FieldType = (int)FieldType.SingleTextLine;
                formValue.TextValue = valData;
            }
            else if (field is FieldsDatepickerField)
            {
                formValue.FieldType = (int)FieldType.Datepicker;
                string[] dateValue = valData.Split(new[] { '-', '/' });
                if (dateValue.Length > 1)
                    formValue.DateValue = new DateTime(int.Parse(dateValue[2]), int.Parse(dateValue[1]), int.Parse(dateValue[0]));
            }
            else if (field is FieldsDecimalField)
            {
                formValue.FieldType = (int)FieldType.Decimal;
                formValue.DecimalValue = Convert.ToDecimal(valData);
            }
            else if (field is FieldsIntegerField)
            {
                formValue.FieldType = (int)FieldType.Integer;
                formValue.IntegerValue = Convert.ToInt32(valData);
            }
            else if (field is FieldsMultipleTextLineField)
            {
                formValue.FieldType = (int)FieldType.MultipleTextLine;
                formValue.TextValue = valData;
            }
            else if (field is FieldsRegularExpressionTextField)
            {
                formValue.FieldType = (int)FieldType.RegularExpressionText;
                formValue.TextValue = valData;
            }
            else if (field is FieldsTimeField)
            {
                formValue.FieldType = (int)FieldType.Time;
                formValue.TextValue = valData;
            }
            else if (field is FieldsUserField)
            {
                formValue.FieldType = (int)FieldType.User;
                formValue.UserName = valData;
            }
            else if (field is FieldsBooleanField)
            {
                formValue.FieldType = (int)FieldType.Boolean;
                if (valData == "on")
                {
                    formValue.CheckValue = true;
                }
            }
            else if (field is FieldsCatalogueField)
            {
                formValue.FieldType = (int)FieldType.Catalogue;
                formValue.CatalogueValueId = new Guid(valData);
            }
            formValue.CreationBy = userName;
            formValue.CreationDate = DateTime.UtcNow;
            formValue.ModificationBy = userName;
            formValue.ModificationDate = DateTime.UtcNow;
            return formValue;
        }