/// <summary>
 /// Get the value of a field
 /// </summary>
 /// <param name="data">data</param>
 /// <param name="fieldType">field type</param>
 /// <param name="fieldValue">field value</param>
 /// <returns></returns>
 private static string GetFieldValue(FormValue data, int fieldType, string fieldValue)
 {
     switch (fieldType)
     {
         case (int)FieldType.Boolean:
             fieldValue = data.CheckValue.HasValue ? data.CheckValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Catalogue:
             fieldValue = data.CatalogueValueId.HasValue ? data.CatalogueValueId.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Datepicker:
             fieldValue = data.DateValue.HasValue ? data.DateValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Decimal:
             fieldValue = data.DecimalValue.HasValue ? data.DecimalValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Integer:
             fieldValue = data.IntegerValue.HasValue ? data.IntegerValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.MultipleTextLine:
         case (int)FieldType.RegularExpressionText:
         case (int)FieldType.SingleTextLine:
         case (int)FieldType.Time:
         case (int)FieldType.User:
             fieldValue = data.TextValue;
             break;
         case (int)FieldType.None:
             break;
     }
     return fieldValue;
 }
 /// <summary>
 /// Get the service form values
 /// </summary>
 /// <param name="formValue">Service order field</param>
 /// <returns>Field value</returns>
 private static string GetFormValue(FormValue formValue)
 {
     string valueToReturn = String.Empty;
     switch (formValue.FieldType)
     {
         case (int)FieldType.Boolean:
             valueToReturn = formValue.CheckValue.HasValue ? formValue.CheckValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Catalogue:
             valueToReturn = formValue.CatalogueValueId.HasValue ? formValue.CatalogueValueId.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Datepicker:
             valueToReturn = formValue.DateValue.HasValue ? formValue.DateValue.GetValueOrDefault().ToString("dd/MM/yyyy") : string.Empty;
             break;
         case (int)FieldType.Decimal:
             valueToReturn = formValue.DecimalValue.HasValue ? formValue.DecimalValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Integer:
             valueToReturn = formValue.IntegerValue.HasValue ? formValue.IntegerValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.MultipleTextLine:
         case (int)FieldType.RegularExpressionText:
         case (int)FieldType.SingleTextLine:
         case (int)FieldType.Time:
             valueToReturn = formValue.TextValue;
             break;
         case (int)FieldType.User:
             valueToReturn = formValue.UserName;
             break;
         //TODO
         case (int)FieldType.None:
             break;
     }
     return valueToReturn;
 }
        /// <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;
        }
        /// <summary>
        /// Set form values when a service order is editing
        /// </summary>
        /// <param name="formValue">FormValue to be saved in the database</param>
        /// <param name="valData">Form value</param>
        /// <param name="userName">User name for auditing</param>
        private static void SetFormValueToEditOrder(FormValue formValue, string valData, string userName)
        {
            switch (formValue.FieldType)
            {
                case (int)FieldType.SingleTextLine:
                case (int)FieldType.MultipleTextLine:
                case (int)FieldType.RegularExpressionText:
                case (int)FieldType.Time:
                    formValue.TextValue = valData;
                    break;
                case (int)FieldType.Datepicker:
                    formValue.DateValue = Convert.ToDateTime(valData, new System.Globalization.CultureInfo("fr-FR"));
                    break;
                case (int)FieldType.Decimal:
                    formValue.DecimalValue = Convert.ToDecimal(valData);
                    break;
                case (int)FieldType.Integer:
                    formValue.IntegerValue = Convert.ToInt32(valData);
                    break;
                case (int)FieldType.Boolean:
                    if (valData == "on")
                    {
                        formValue.CheckValue = true;
                    }
                    break;
                case (int)FieldType.Catalogue:
                    formValue.CatalogueValueId = new Guid(valData);
                    break;
                case (int)FieldType.User:
                    formValue.UserName = valData;
                    break;
            }

            formValue.ModificationBy = userName;
            formValue.ModificationDate = DateTime.UtcNow;
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the FormValues EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToFormValues(FormValue formValue)
 {
     base.AddObject("FormValues", formValue);
 }
 /// <summary>
 /// Create a new FormValue object.
 /// </summary>
 /// <param name="formValueId">Initial value of the FormValueId property.</param>
 /// <param name="fieldName">Initial value of the FieldName property.</param>
 /// <param name="fieldType">Initial value of the FieldType 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 FormValue CreateFormValue(global::System.Guid formValueId, global::System.String fieldName, global::System.Int32 fieldType, global::System.String creationBy, global::System.DateTime creationDate, global::System.Boolean isDeleted)
 {
     FormValue formValue = new FormValue();
     formValue.FormValueId = formValueId;
     formValue.FieldName = fieldName;
     formValue.FieldType = fieldType;
     formValue.CreationBy = creationBy;
     formValue.CreationDate = creationDate;
     formValue.IsDeleted = isDeleted;
     return formValue;
 }
 /// <summary>
 /// Get the value of a field
 /// </summary>
 /// <param name="data">data</param>
 /// <param name="fieldType">field type</param>
 /// <param name="fieldValue">field value</param>
 /// <returns></returns>
 private static string GetFieldValue(FormValue data, int fieldType, string fieldValue)
 {
     switch (fieldType)
     {
         case (int)FieldType.Boolean:
             fieldValue = data.CheckValue.HasValue ? data.CheckValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Catalogue:
             if (data.CatalogueValueId.HasValue)
             {
                 Guid catalogValueId = new Guid(data.CatalogueValueId.Value.ToString());
                 fieldValue = CatalogueBusiness.GetCatalogueValue(catalogValueId).CatalogueValueData;
             }
             else
             {
                 fieldValue = string.Empty;
             }
             break;
         case (int)FieldType.Datepicker:
             if (data.DateValue.HasValue)
             {
                 DateTime temDateValue = DateTime.Parse(data.DateValue.Value.ToString());
                 fieldValue = string.Format("{0:dd/MM/yyyy}", temDateValue);
             }
             else
             {
                 fieldValue = string.Empty;
             }
             break;
         case (int)FieldType.Decimal:
             fieldValue = data.DecimalValue.HasValue ? data.DecimalValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.Integer:
             fieldValue = data.IntegerValue.HasValue ? data.IntegerValue.GetValueOrDefault().ToString() : string.Empty;
             break;
         case (int)FieldType.MultipleTextLine:
         case (int)FieldType.RegularExpressionText:
         case (int)FieldType.SingleTextLine:
         case (int)FieldType.Time:
         case (int)FieldType.User:
         case (int)FieldType.AutoComplete:
             fieldValue = data.TextValue;
             break;
         case (int)FieldType.None:
             break;
     }
     return fieldValue;
 }