Ejemplo n.º 1
0
        public async Task PopulateFormModelFromDbAsync(DynamicForm formModel, string formSubmissionId, string sectionUrlSlug, CancellationToken cancellationToken = default(CancellationToken))
        {
            FormSectionSubmissionDto sectionSubmission = null;

            if (!string.IsNullOrEmpty(formSubmissionId) && !string.IsNullOrEmpty(sectionUrlSlug))
            {
                var formSubmissionGuid = new Guid(formSubmissionId);
                sectionSubmission = await _dynamicFormsApplicationServices.FormSectionSubmissionApplicationService.GetOneAsync(cancellationToken, fss => fss.FormSubmissionId == formSubmissionGuid && fss.UrlSlug == sectionUrlSlug, true);
            }

            if (sectionSubmission != null)
            {
                var properties = formModel.GetProperties();

                foreach (var propertyName in formModel.GetDynamicMemberNames())
                {
                    var persistedValue = sectionSubmission.QuestionAnswers.Where(qa => qa.FieldName == propertyName).FirstOrDefault();
                    if (persistedValue != null)
                    {
                        bool isCollection = formModel.IsCollectionProperty(propertyName);
                        var  property     = properties.Find(propertyName, true);

                        if (isCollection)
                        {
                            var genericCollectionType = typeof(List <>).MakeGenericType(property.PropertyType.GetGenericArguments()[0]);
                            var newCollection         = Activator.CreateInstance(genericCollectionType);

                            formModel[propertyName] = newCollection;

                            var addMethod = genericCollectionType.GetMethod("Add");

                            foreach (var csvSplit in persistedValue.Answer.Split(','))
                            {
                                var convertedValue = Convert.ChangeType(csvSplit.Trim(), property.PropertyType.GetGenericArguments()[0]);
                                addMethod.Invoke(newCollection, new object[] { convertedValue });
                            }
                        }
                        else if (property.PropertyType == typeof(DateTime))
                        {
                            if (!String.IsNullOrWhiteSpace(persistedValue.Answer))
                            {
                                //ISO 8601
                                var parsedValue = DateTime.Parse(persistedValue.Answer, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
                                formModel[propertyName] = parsedValue;
                            }
                            else
                            {
                                formModel[propertyName] = new DateTime();
                            }
                        }
                        else if (property.PropertyType == typeof(bool))
                        {
                            formModel[propertyName] = BoolParser.GetValue(persistedValue.Answer);
                        }
                        else if (property.PropertyType == typeof(decimal))
                        {
                            var convertedValue = decimal.Parse(persistedValue.Answer, NumberStyles.Currency);
                            formModel[propertyName] = convertedValue;
                        }
                        else
                        {
                            var convertedValue = Convert.ChangeType(persistedValue.Answer, property.PropertyType);
                            formModel[propertyName] = convertedValue;
                        }
                    }
                }
            }
        }